Skip to content

Commit dc055f3

Browse files
Use new polly implementation for liquid/tpl templates (#179)
Also auto-infer kustomize services Fixes ENG-2044
1 parent 6879f5f commit dc055f3

File tree

7 files changed

+54
-138
lines changed

7 files changed

+54
-138
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ require (
1818
github.com/open-policy-agent/frameworks/constraint v0.0.0-20240110234408-18fa1fc7dc06
1919
github.com/open-policy-agent/gatekeeper/v3 v3.15.1
2020
github.com/orcaman/concurrent-map/v2 v2.0.1
21-
github.com/osteele/liquid v1.3.2
2221
github.com/pkg/errors v0.9.1
2322
github.com/pluralsh/console-client-go v0.5.2
2423
github.com/pluralsh/controller-reconcile-helper v0.0.4
2524
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
26-
github.com/pluralsh/polly v0.1.7
25+
github.com/pluralsh/polly v0.1.8
2726
github.com/samber/lo v1.39.0
2827
github.com/spf13/pflag v1.0.5
2928
github.com/stretchr/testify v1.9.0
@@ -165,6 +164,7 @@ require (
165164
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
166165
github.com/opencontainers/go-digest v1.0.0 // indirect
167166
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
167+
github.com/osteele/liquid v1.4.0 // indirect
168168
github.com/osteele/tuesday v1.0.3 // indirect
169169
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
170170
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/
512512
github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
513513
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
514514
github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM=
515-
github.com/osteele/liquid v1.3.2 h1:G+MvVYt1HX2xuv99JgdrhV7zRVdlvFnNi8M5rN8gQmI=
516-
github.com/osteele/liquid v1.3.2/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
515+
github.com/osteele/liquid v1.4.0 h1:WS6lT3MFWUAxNbveF22tMLluOWNghGnKCZHLn7NbJGs=
516+
github.com/osteele/liquid v1.4.0/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
517517
github.com/osteele/tuesday v1.0.3 h1:SrCmo6sWwSgnvs1bivmXLvD7Ko9+aJvvkmDjB5G4FTU=
518518
github.com/osteele/tuesday v1.0.3/go.mod h1:pREKpE+L03UFuR+hiznj3q7j3qB1rUZ4XfKejwWFF2M=
519519
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
@@ -526,16 +526,14 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
526526
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
527527
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
528528
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
529-
github.com/pluralsh/console-client-go v0.1.15 h1:1N8FxSkHbu29P7AtzF/tQbXlztM43CMATSNTxCulLhw=
530-
github.com/pluralsh/console-client-go v0.1.15/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
531529
github.com/pluralsh/console-client-go v0.5.2 h1:vDiKzZ/vPFivr9TIXSSi/6Q1nOrH4y1huE5XkrCJ3D0=
532530
github.com/pluralsh/console-client-go v0.5.2/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
533531
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
534532
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
535533
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=
536534
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34/go.mod h1:IagWXKFYu6NTHzcJx2dJyrIlZ1Sv2PH3fhOtplA9qOs=
537-
github.com/pluralsh/polly v0.1.7 h1:MUuTb6rCUV1doisaFGC+iz+33ZPU4FZHOb/kFwSDkjw=
538-
github.com/pluralsh/polly v0.1.7/go.mod h1:Yo1/jcW+4xwhWG+ZJikZy4J4HJkMNPZ7sq5auL2c/tY=
535+
github.com/pluralsh/polly v0.1.8 h1:fkF5fLNofN4CyOs89lQfKeZaSXgRe8MnXz9VK5MzvRU=
536+
github.com/pluralsh/polly v0.1.8/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
539537
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
540538
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
541539
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
@@ -786,8 +784,6 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
786784
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
787785
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
788786
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
789-
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
790-
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
791787
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
792788
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
793789
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=

pkg/manifests/template/kustomize.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ func (k *kustomize) Render(svc *console.GetServiceDeploymentForAgent_ServiceDepl
3232
Example: templates.Examples(i18n.T(h.Example)),
3333
}
3434

35+
subdir := ""
36+
if svc.Kustomize != nil {
37+
subdir = svc.Kustomize.Path
38+
}
39+
3540
command := build.NewCmdBuild(filesys.MakeFsOnDisk(), help, out)
36-
path := filepath.Join(k.dir, svc.Kustomize.Path)
41+
path := filepath.Join(k.dir, subdir)
3742
command.SetArgs([]string{path})
3843
if err := command.Execute(); err != nil {
3944
return nil, err

pkg/manifests/template/raw.go

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,18 @@ import (
77
"path/filepath"
88
"strings"
99

10-
"github.com/Masterminds/sprig/v3"
11-
"github.com/osteele/liquid"
1210
console "github.com/pluralsh/console-client-go"
1311
"github.com/pluralsh/polly/containers"
12+
"github.com/pluralsh/polly/template"
1413
"github.com/samber/lo"
1514
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1615
"k8s.io/kubectl/pkg/cmd/util"
1716
)
1817

1918
var (
20-
extensions = []string{".json", ".yaml", ".yml", ".yaml.liquid", ".yml.liquid", ".json.liquid"}
21-
liquidEngine = liquid.NewEngine()
22-
sprigFunctions = map[string]string{
23-
"toJson": "to_json",
24-
"fromJson": "from_json",
25-
"b64enc": "b64enc",
26-
"b64dec": "b64dec",
27-
"semverCompare": "semver_compare",
28-
"sha256sum": "sha26sum",
29-
"quote": "quote",
30-
"squote": "squote",
31-
"replace": "replace",
32-
"coalesce": "coalesce",
33-
}
19+
extensions = []string{".json", ".yaml", ".yml", ".yaml.liquid", ".yml.liquid", ".json.liquid"}
3420
)
3521

36-
func init() {
37-
fncs := sprig.TxtFuncMap()
38-
for key, name := range sprigFunctions {
39-
liquidEngine.RegisterFilter(name, fncs[key])
40-
}
41-
liquidEngine.RegisterFilter("indent", indent)
42-
liquidEngine.RegisterFilter("nindent", nindent)
43-
liquidEngine.RegisterFilter("replace", strings.ReplaceAll)
44-
45-
liquidEngine.RegisterFilter("default", dfault)
46-
liquidEngine.RegisterFilter("ternary", ternary)
47-
}
48-
4922
type raw struct {
5023
dir string
5124
}
@@ -54,21 +27,13 @@ func NewRaw(dir string) *raw {
5427
return &raw{dir}
5528
}
5629

57-
func isTemplated(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) bool {
58-
if svc.Templated != nil {
59-
return *svc.Templated
60-
}
61-
// default true
62-
return true
63-
}
64-
6530
func renderLiquid(input []byte, svc *console.GetServiceDeploymentForAgent_ServiceDeployment) ([]byte, error) {
6631
bindings := map[string]interface{}{
6732
"configuration": configMap(svc),
6833
"cluster": clusterConfiguration(svc.Cluster),
6934
"contexts": contexts(svc),
7035
}
71-
return liquidEngine.ParseAndRender(input, bindings)
36+
return template.RenderLiquid(input, bindings)
7237
}
7338

7439
func (r *raw) Render(svc *console.GetServiceDeploymentForAgent_ServiceDeployment, utilFactory util.Factory) ([]*unstructured.Unstructured, error) {
@@ -132,24 +97,3 @@ func (r *raw) Render(svc *console.GetServiceDeploymentForAgent_ServiceDeployment
13297
res = newSet.List()
13398
return res, nil
13499
}
135-
136-
func clusterConfiguration(cluster *console.GetServiceDeploymentForAgent_ServiceDeployment_Cluster) map[string]interface{} {
137-
res := map[string]interface{}{
138-
"ID": cluster.ID,
139-
"Self": cluster.Self,
140-
"Handle": cluster.Handle,
141-
"Name": cluster.Name,
142-
"Version": cluster.Version,
143-
"CurrentVersion": cluster.CurrentVersion,
144-
"KasUrl": cluster.KasURL,
145-
"Metadata": cluster.Metadata,
146-
}
147-
148-
for k, v := range res {
149-
res[strings.ToLower(k)] = v
150-
}
151-
res["kasUrl"] = cluster.KasURL
152-
res["currentVersion"] = cluster.CurrentVersion
153-
154-
return res
155-
}

pkg/manifests/template/template.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
type Renderer string
1313

1414
const (
15-
RendererHelm Renderer = "helm"
16-
RendererRaw Renderer = "raw"
15+
RendererHelm Renderer = "helm"
16+
RendererRaw Renderer = "raw"
17+
RendererKustomize Renderer = "kustomize"
1718

1819
ChartFileName = "Chart.yaml"
1920
)
@@ -33,10 +34,14 @@ func Render(dir string, svc *console.GetServiceDeploymentForAgent_ServiceDeploym
3334
}
3435
}
3536

37+
if info.Name() == "kustomization.yaml" {
38+
renderer = RendererKustomize
39+
}
40+
3641
return nil
3742
})
3843

39-
if svc.Kustomize != nil {
44+
if svc.Kustomize != nil || renderer == RendererKustomize {
4045
return NewKustomize(dir).Render(svc, utilFactory)
4146
}
4247

pkg/manifests/template/tpl.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package template
22

33
import (
4-
"bytes"
5-
"text/template"
6-
7-
"github.com/Masterminds/sprig/v3"
84
console "github.com/pluralsh/console-client-go"
5+
"github.com/pluralsh/polly/template"
96
)
107

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

18-
tpl, err := template.New("gotpl").Funcs(sprig.TxtFuncMap()).Parse(string(input))
19-
if err != nil {
20-
return nil, err
21-
}
22-
23-
var buffer bytes.Buffer
24-
err = tpl.Execute(&buffer, bindings)
25-
return buffer.Bytes(), err
15+
return template.RenderTpl(input, bindings)
2616
}

pkg/manifests/template/utils.go

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
package template
22

33
import (
4-
"reflect"
54
"strings"
65

76
console "github.com/pluralsh/console-client-go"
87
)
98

9+
func isTemplated(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) bool {
10+
if svc.Templated != nil {
11+
return *svc.Templated
12+
}
13+
// default true
14+
return true
15+
}
16+
17+
func clusterConfiguration(cluster *console.GetServiceDeploymentForAgent_ServiceDeployment_Cluster) map[string]interface{} {
18+
res := map[string]interface{}{
19+
"ID": cluster.ID,
20+
"Self": cluster.Self,
21+
"Handle": cluster.Handle,
22+
"Name": cluster.Name,
23+
"Version": cluster.Version,
24+
"CurrentVersion": cluster.CurrentVersion,
25+
"KasUrl": cluster.KasURL,
26+
"Metadata": cluster.Metadata,
27+
}
28+
29+
for k, v := range res {
30+
res[strings.ToLower(k)] = v
31+
}
32+
res["kasUrl"] = cluster.KasURL
33+
res["currentVersion"] = cluster.CurrentVersion
34+
35+
return res
36+
}
37+
1038
func configMap(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) map[string]string {
1139
res := map[string]string{}
1240
for _, config := range svc.Configuration {
@@ -23,55 +51,3 @@ func contexts(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) map[s
2351
}
2452
return res
2553
}
26-
27-
func indent(v string, spaces int) string {
28-
pad := strings.Repeat(" ", spaces)
29-
return pad + strings.ReplaceAll(v, "\n", "\n"+pad)
30-
}
31-
32-
func nindent(v string, spaces int) string {
33-
return "\n" + indent(v, spaces)
34-
}
35-
36-
func ternary(v bool, vt interface{}, vf interface{}) interface{} {
37-
if v {
38-
return vt
39-
}
40-
41-
return vf
42-
}
43-
44-
func dfault(v1, v2 interface{}) interface{} {
45-
if empty(v1) {
46-
return v2
47-
}
48-
49-
return v1
50-
}
51-
52-
func empty(given interface{}) bool {
53-
g := reflect.ValueOf(given)
54-
if !g.IsValid() {
55-
return true
56-
}
57-
58-
// Basically adapted from text/template.isTrue
59-
switch g.Kind() {
60-
default:
61-
return g.IsNil()
62-
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
63-
return g.Len() == 0
64-
case reflect.Bool:
65-
return !g.Bool()
66-
case reflect.Complex64, reflect.Complex128:
67-
return g.Complex() == 0
68-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
69-
return g.Int() == 0
70-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
71-
return g.Uint() == 0
72-
case reflect.Float32, reflect.Float64:
73-
return g.Float() == 0
74-
case reflect.Struct:
75-
return false
76-
}
77-
}

0 commit comments

Comments
 (0)