Skip to content

Commit

Permalink
Use new polly implementation for liquid/tpl templates (#179)
Browse files Browse the repository at this point in the history
Also auto-infer kustomize services

Fixes ENG-2044
  • Loading branch information
michaeljguarino authored May 6, 2024
1 parent 6879f5f commit dc055f3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 138 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ require (
github.com/open-policy-agent/frameworks/constraint v0.0.0-20240110234408-18fa1fc7dc06
github.com/open-policy-agent/gatekeeper/v3 v3.15.1
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/osteele/liquid v1.3.2
github.com/pkg/errors v0.9.1
github.com/pluralsh/console-client-go v0.5.2
github.com/pluralsh/controller-reconcile-helper v0.0.4
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
github.com/pluralsh/polly v0.1.7
github.com/pluralsh/polly v0.1.8
github.com/samber/lo v1.39.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -165,6 +164,7 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/osteele/liquid v1.4.0 // indirect
github.com/osteele/tuesday v1.0.3 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
Expand Down
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/
github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM=
github.com/osteele/liquid v1.3.2 h1:G+MvVYt1HX2xuv99JgdrhV7zRVdlvFnNi8M5rN8gQmI=
github.com/osteele/liquid v1.3.2/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
github.com/osteele/liquid v1.4.0 h1:WS6lT3MFWUAxNbveF22tMLluOWNghGnKCZHLn7NbJGs=
github.com/osteele/liquid v1.4.0/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
github.com/osteele/tuesday v1.0.3 h1:SrCmo6sWwSgnvs1bivmXLvD7Ko9+aJvvkmDjB5G4FTU=
github.com/osteele/tuesday v1.0.3/go.mod h1:pREKpE+L03UFuR+hiznj3q7j3qB1rUZ4XfKejwWFF2M=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
Expand All @@ -526,16 +526,14 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pluralsh/console-client-go v0.1.15 h1:1N8FxSkHbu29P7AtzF/tQbXlztM43CMATSNTxCulLhw=
github.com/pluralsh/console-client-go v0.1.15/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.5.2 h1:vDiKzZ/vPFivr9TIXSSi/6Q1nOrH4y1huE5XkrCJ3D0=
github.com/pluralsh/console-client-go v0.5.2/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34/go.mod h1:IagWXKFYu6NTHzcJx2dJyrIlZ1Sv2PH3fhOtplA9qOs=
github.com/pluralsh/polly v0.1.7 h1:MUuTb6rCUV1doisaFGC+iz+33ZPU4FZHOb/kFwSDkjw=
github.com/pluralsh/polly v0.1.7/go.mod h1:Yo1/jcW+4xwhWG+ZJikZy4J4HJkMNPZ7sq5auL2c/tY=
github.com/pluralsh/polly v0.1.8 h1:fkF5fLNofN4CyOs89lQfKeZaSXgRe8MnXz9VK5MzvRU=
github.com/pluralsh/polly v0.1.8/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
Expand Down Expand Up @@ -786,8 +784,6 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down
7 changes: 6 additions & 1 deletion pkg/manifests/template/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ func (k *kustomize) Render(svc *console.GetServiceDeploymentForAgent_ServiceDepl
Example: templates.Examples(i18n.T(h.Example)),
}

subdir := ""
if svc.Kustomize != nil {
subdir = svc.Kustomize.Path
}

command := build.NewCmdBuild(filesys.MakeFsOnDisk(), help, out)
path := filepath.Join(k.dir, svc.Kustomize.Path)
path := filepath.Join(k.dir, subdir)
command.SetArgs([]string{path})
if err := command.Execute(); err != nil {
return nil, err
Expand Down
62 changes: 3 additions & 59 deletions pkg/manifests/template/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,18 @@ import (
"path/filepath"
"strings"

"github.com/Masterminds/sprig/v3"
"github.com/osteele/liquid"
console "github.com/pluralsh/console-client-go"
"github.com/pluralsh/polly/containers"
"github.com/pluralsh/polly/template"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/kubectl/pkg/cmd/util"
)

var (
extensions = []string{".json", ".yaml", ".yml", ".yaml.liquid", ".yml.liquid", ".json.liquid"}
liquidEngine = liquid.NewEngine()
sprigFunctions = map[string]string{
"toJson": "to_json",
"fromJson": "from_json",
"b64enc": "b64enc",
"b64dec": "b64dec",
"semverCompare": "semver_compare",
"sha256sum": "sha26sum",
"quote": "quote",
"squote": "squote",
"replace": "replace",
"coalesce": "coalesce",
}
extensions = []string{".json", ".yaml", ".yml", ".yaml.liquid", ".yml.liquid", ".json.liquid"}
)

func init() {
fncs := sprig.TxtFuncMap()
for key, name := range sprigFunctions {
liquidEngine.RegisterFilter(name, fncs[key])
}
liquidEngine.RegisterFilter("indent", indent)
liquidEngine.RegisterFilter("nindent", nindent)
liquidEngine.RegisterFilter("replace", strings.ReplaceAll)

liquidEngine.RegisterFilter("default", dfault)
liquidEngine.RegisterFilter("ternary", ternary)
}

type raw struct {
dir string
}
Expand All @@ -54,21 +27,13 @@ func NewRaw(dir string) *raw {
return &raw{dir}
}

func isTemplated(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) bool {
if svc.Templated != nil {
return *svc.Templated
}
// default true
return true
}

func renderLiquid(input []byte, svc *console.GetServiceDeploymentForAgent_ServiceDeployment) ([]byte, error) {
bindings := map[string]interface{}{
"configuration": configMap(svc),
"cluster": clusterConfiguration(svc.Cluster),
"contexts": contexts(svc),
}
return liquidEngine.ParseAndRender(input, bindings)
return template.RenderLiquid(input, bindings)
}

func (r *raw) Render(svc *console.GetServiceDeploymentForAgent_ServiceDeployment, utilFactory util.Factory) ([]*unstructured.Unstructured, error) {
Expand Down Expand Up @@ -132,24 +97,3 @@ func (r *raw) Render(svc *console.GetServiceDeploymentForAgent_ServiceDeployment
res = newSet.List()
return res, nil
}

func clusterConfiguration(cluster *console.GetServiceDeploymentForAgent_ServiceDeployment_Cluster) map[string]interface{} {
res := map[string]interface{}{
"ID": cluster.ID,
"Self": cluster.Self,
"Handle": cluster.Handle,
"Name": cluster.Name,
"Version": cluster.Version,
"CurrentVersion": cluster.CurrentVersion,
"KasUrl": cluster.KasURL,
"Metadata": cluster.Metadata,
}

for k, v := range res {
res[strings.ToLower(k)] = v
}
res["kasUrl"] = cluster.KasURL
res["currentVersion"] = cluster.CurrentVersion

return res
}
11 changes: 8 additions & 3 deletions pkg/manifests/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
type Renderer string

const (
RendererHelm Renderer = "helm"
RendererRaw Renderer = "raw"
RendererHelm Renderer = "helm"
RendererRaw Renderer = "raw"
RendererKustomize Renderer = "kustomize"

ChartFileName = "Chart.yaml"
)
Expand All @@ -33,10 +34,14 @@ func Render(dir string, svc *console.GetServiceDeploymentForAgent_ServiceDeploym
}
}

if info.Name() == "kustomization.yaml" {
renderer = RendererKustomize
}

return nil
})

if svc.Kustomize != nil {
if svc.Kustomize != nil || renderer == RendererKustomize {
return NewKustomize(dir).Render(svc, utilFactory)
}

Expand Down
14 changes: 2 additions & 12 deletions pkg/manifests/template/tpl.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package template

import (
"bytes"
"text/template"

"github.com/Masterminds/sprig/v3"
console "github.com/pluralsh/console-client-go"
"github.com/pluralsh/polly/template"
)

func renderTpl(input []byte, svc *console.GetServiceDeploymentForAgent_ServiceDeployment) ([]byte, error) {
Expand All @@ -15,12 +12,5 @@ func renderTpl(input []byte, svc *console.GetServiceDeploymentForAgent_ServiceDe
"Contexts": contexts(svc),
}

tpl, err := template.New("gotpl").Funcs(sprig.TxtFuncMap()).Parse(string(input))
if err != nil {
return nil, err
}

var buffer bytes.Buffer
err = tpl.Execute(&buffer, bindings)
return buffer.Bytes(), err
return template.RenderTpl(input, bindings)
}
82 changes: 29 additions & 53 deletions pkg/manifests/template/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
package template

import (
"reflect"
"strings"

console "github.com/pluralsh/console-client-go"
)

func isTemplated(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) bool {
if svc.Templated != nil {
return *svc.Templated
}
// default true
return true
}

func clusterConfiguration(cluster *console.GetServiceDeploymentForAgent_ServiceDeployment_Cluster) map[string]interface{} {
res := map[string]interface{}{
"ID": cluster.ID,
"Self": cluster.Self,
"Handle": cluster.Handle,
"Name": cluster.Name,
"Version": cluster.Version,
"CurrentVersion": cluster.CurrentVersion,
"KasUrl": cluster.KasURL,
"Metadata": cluster.Metadata,
}

for k, v := range res {
res[strings.ToLower(k)] = v
}
res["kasUrl"] = cluster.KasURL
res["currentVersion"] = cluster.CurrentVersion

return res
}

func configMap(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) map[string]string {
res := map[string]string{}
for _, config := range svc.Configuration {
Expand All @@ -23,55 +51,3 @@ func contexts(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) map[s
}
return res
}

func indent(v string, spaces int) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.ReplaceAll(v, "\n", "\n"+pad)
}

func nindent(v string, spaces int) string {
return "\n" + indent(v, spaces)
}

func ternary(v bool, vt interface{}, vf interface{}) interface{} {
if v {
return vt
}

return vf
}

func dfault(v1, v2 interface{}) interface{} {
if empty(v1) {
return v2
}

return v1
}

func empty(given interface{}) bool {
g := reflect.ValueOf(given)
if !g.IsValid() {
return true
}

// Basically adapted from text/template.isTrue
switch g.Kind() {
default:
return g.IsNil()
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return g.Len() == 0
case reflect.Bool:
return !g.Bool()
case reflect.Complex64, reflect.Complex128:
return g.Complex() == 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return g.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return g.Uint() == 0
case reflect.Float32, reflect.Float64:
return g.Float() == 0
case reflect.Struct:
return false
}
}

0 comments on commit dc055f3

Please sign in to comment.