Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from flanksource/integrate-kommons-ktemplate
Browse files Browse the repository at this point in the history
Integrate kommons template
  • Loading branch information
moshloop authored Jan 18, 2021
2 parents 0eddef4 + 71591d5 commit 8fa866e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 66 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/flanksource/commons v1.4.3
github.com/flanksource/kommons v0.1.6
github.com/flanksource/kommons v0.1.9
github.com/go-logr/logr v0.3.0
github.com/go-logr/zapr v0.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.3
Expand All @@ -15,7 +15,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.7.0
github.com/sykesm/zap-logfmt v0.0.4
github.com/tidwall/gjson v1.6.1
github.com/tidwall/gjson v1.6.7
github.com/zalando/postgres-operator v1.6.0
go.uber.org/zap v1.15.0
gopkg.in/flanksource/yaml.v3 v3.1.1
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ github.com/flanksource/commons v1.4.3 h1:arEom5vkh5RrEWNy+kj32PmCbZ8d3OdZ0K7X7wQ
github.com/flanksource/commons v1.4.3/go.mod h1:cGrz4qFi4NrP04vjMxv6DQk0SD0u6nnYppKpgFShj0c=
github.com/flanksource/kommons v0.1.6 h1:VmYd/XjuqzQb3ICisTqOUpjzU4Gc08RZafJzFLXtFdA=
github.com/flanksource/kommons v0.1.6/go.mod h1:Dc/oeDDXgybBm1LFr1iSAgARLCja8ep9K42Qouu+yPM=
github.com/flanksource/kommons v0.1.8 h1:TMw1eyi7j2xTOFdT1Jmfins70z2aPXJJ0lT1yevRUBI=
github.com/flanksource/kommons v0.1.8/go.mod h1:oaYRidwjSC8KULM861DMPFc8P64JkcA+GNBZkqpSsUY=
github.com/flanksource/kommons v0.1.9 h1:o0jYg5M6yjT02NcjoyDBrDnPYhvfkS7IopNjLZUOLIw=
github.com/flanksource/kommons v0.1.9/go.mod h1:oaYRidwjSC8KULM861DMPFc8P64JkcA+GNBZkqpSsUY=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
Expand Down Expand Up @@ -763,8 +767,12 @@ github.com/sykesm/zap-logfmt v0.0.4/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWR
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/tidwall/gjson v1.6.1 h1:LRbvNuNuvAiISWg6gxLEFuCe72UKy5hDqhxW/8183ws=
github.com/tidwall/gjson v1.6.1/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0=
github.com/tidwall/gjson v1.6.7 h1:Mb1M9HZCRWEcXQ8ieJo7auYyyiSux6w9XN3AdTpxJrE=
github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand Down
58 changes: 3 additions & 55 deletions k8s/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package k8s

import (
"bytes"
"context"
"encoding/json"
"fmt"
"path/filepath"
osruntime "runtime"
"strings"
"text/template"

"github.com/flanksource/kommons/ktemplate"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
fyaml "gopkg.in/flanksource/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/kustomize"
Expand Down Expand Up @@ -47,10 +44,8 @@ func NewPatchApplier(clientset *kubernetes.Clientset, schemaManager *SchemaManag
SchemaManager: schemaManager,
}

p.FuncMap = template.FuncMap{
"kget": p.KGet,
"jsonPath": p.JSONPath,
}
functions := ktemplate.NewFunctions(clientset)
p.FuncMap = functions.FuncMap()
return p, nil
}

Expand Down Expand Up @@ -179,53 +174,6 @@ func (p *PatchApplier) Apply(resource *unstructured.Unstructured, patchStr strin
return resource, nil
}

func (p *PatchApplier) KGet(path, jsonpath string) string {
parts := strings.Split(path, "/")
if len(parts) != 3 {
p.Log.Error(errors.New("expected path to contain kind/namespace/name"), "invalid call to kget")
return ""
}

kind := parts[0]
namespace := parts[1]
name := parts[2]

if kind == "configmap" || kind == "cm" {
cm, err := p.Clientset.CoreV1().ConfigMaps(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
p.Log.Error(err, "failed to read configmap", "name", name, "namespace", namespace)
return ""
}

encodedJSON, err := json.Marshal(cm)
if err != nil {
p.Log.Error(err, "failed to encode json", "name", name, "namespace", namespace)
return ""
}
value := gjson.Get(string(encodedJSON), jsonpath)
return value.String()
} else if kind == "secret" {
secret, err := p.Clientset.CoreV1().Secrets(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
p.Log.Error(err, "failed to read secret", "name", name, "namespace", namespace)
return ""
}
return string(secret.Data[jsonpath])
}

return ""
}

func (p *PatchApplier) JSONPath(object interface{}, jsonpath string) string {
jsonObject, err := json.Marshal(object)
if err != nil {
p.Log.Error(err, "failed to encode json", "object", object)
return ""
}
value := gjson.Get(string(jsonObject), jsonpath)
return value.String()
}

var annotationsBlacklist = []string{
"metadata.annotations.serving.knative.dev/creator",
"metadata.annotations.serving.knative.dev/lastModifier",
Expand Down
15 changes: 6 additions & 9 deletions k8s/template_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"text/template"

"github.com/flanksource/kommons"
"github.com/flanksource/kommons/ktemplate"
templatev1 "github.com/flanksource/template-operator/api/v1"
"github.com/go-logr/logr"
"github.com/hairyhenderson/gomplate/v3"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
extapi "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
Expand All @@ -34,6 +34,7 @@ type TemplateManager struct {
Log logr.Logger
PatchApplier *PatchApplier
SchemaManager *SchemaManager
FuncMap template.FuncMap
}

type ResourcePatch struct {
Expand Down Expand Up @@ -66,12 +67,15 @@ func NewTemplateManager(c *kommons.Client, log logr.Logger, cache *SchemaCache)
return nil, errors.Wrap(err, "faile to create patch applier")
}

functions := ktemplate.NewFunctions(clientset)

tm := &TemplateManager{
Client: c,
Interface: clientset,
Log: log,
PatchApplier: patchApplier,
SchemaManager: schemaManager,
FuncMap: functions.FuncMap(),
}
return tm, nil
}
Expand Down Expand Up @@ -214,20 +218,13 @@ func (tm *TemplateManager) Run(ctx context.Context, template *templatev1.Templat
return nil
}

var funcs template.FuncMap

func (tm *TemplateManager) Template(data []byte, vars interface{}) ([]byte, error) {
convertedYAML, err := yaml.JSONToYAML(data)
if err != nil {
return nil, err
}

if len(funcs) == 0 {
funcs = gomplate.Funcs(nil)
funcs["kget"] = tm.PatchApplier.KGet
}

tpl, err := template.New("").Funcs(funcs).Parse(string(convertedYAML))
tpl, err := template.New("").Funcs(tm.FuncMap).Parse(string(convertedYAML))

if err != nil {
return nil, fmt.Errorf("invalid template %s: %v", strings.Split(string(data), "\n")[0], err)
Expand Down

0 comments on commit 8fa866e

Please sign in to comment.