From 00d16f3a8d3bcfd31fca7bb147884b3ca4babd50 Mon Sep 17 00:00:00 2001 From: Sebastian Florek Date: Wed, 6 Sep 2023 12:40:46 +0200 Subject: [PATCH 1/3] feat(template): allow overriding chart enabled field through templating --- pkg/scaffold/template/gotpl.go | 7 ++++--- pkg/scaffold/template/lua.go | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/scaffold/template/gotpl.go b/pkg/scaffold/template/gotpl.go index 0c01dfd8b..a7db762d7 100644 --- a/pkg/scaffold/template/gotpl.go +++ b/pkg/scaffold/template/gotpl.go @@ -4,9 +4,10 @@ import ( "bytes" "github.com/imdario/mergo" + "gopkg.in/yaml.v2" + "github.com/pluralsh/plural/pkg/template" "github.com/pluralsh/plural/pkg/utils" - "gopkg.in/yaml.v2" ) func FromGoTemplate(vals map[string]interface{}, globals map[string]interface{}, output map[string]map[string]interface{}, chartName, tplate string) error { @@ -22,11 +23,11 @@ func FromGoTemplate(vals map[string]interface{}, globals map[string]interface{}, return err } - var subVals map[string]interface{} + var subVals = map[string]interface{}{} + subVals["enabled"] = true if err := yaml.Unmarshal(buf.Bytes(), &subVals); err != nil { return err } - subVals["enabled"] = true // need to handle globals in a dedicated way if glob, ok := subVals["global"]; ok { diff --git a/pkg/scaffold/template/lua.go b/pkg/scaffold/template/lua.go index 92e495f53..1ee0e08d8 100644 --- a/pkg/scaffold/template/lua.go +++ b/pkg/scaffold/template/lua.go @@ -5,10 +5,11 @@ import ( "github.com/Masterminds/sprig/v3" "github.com/imdario/mergo" - "github.com/pluralsh/plural/pkg/template" - "github.com/pluralsh/plural/pkg/utils" lua "github.com/yuin/gopher-lua" luar "layeh.com/gopher-luar" + + "github.com/pluralsh/plural/pkg/template" + "github.com/pluralsh/plural/pkg/utils" ) func ExecuteLua(vals map[string]interface{}, tplate string) (map[string]interface{}, error) { @@ -41,11 +42,12 @@ func ExecuteLua(vals map[string]interface{}, tplate string) (map[string]interfac } func FromLuaTemplate(vals map[string]interface{}, globals map[string]interface{}, output map[string]map[string]interface{}, chartName, tplate string) error { + var subVals = map[string]interface{}{} + subVals["enabled"] = true subVals, err := ExecuteLua(vals, tplate) if err != nil { return err } - subVals["enabled"] = true // need to handle globals in a dedicated way if glob, ok := subVals["global"]; ok { From 6c9617699bddf512a874aed86faffb30468edafb Mon Sep 17 00:00:00 2001 From: Sebastian Florek Date: Wed, 6 Sep 2023 14:05:57 +0200 Subject: [PATCH 2/3] fix logic and add unit test --- pkg/scaffold/template/gotpl.go | 5 +++- pkg/scaffold/template/lua.go | 5 +++- pkg/scaffold/template/lua_test.go | 40 +++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/pkg/scaffold/template/gotpl.go b/pkg/scaffold/template/gotpl.go index a7db762d7..690097cf9 100644 --- a/pkg/scaffold/template/gotpl.go +++ b/pkg/scaffold/template/gotpl.go @@ -24,11 +24,14 @@ func FromGoTemplate(vals map[string]interface{}, globals map[string]interface{}, } var subVals = map[string]interface{}{} - subVals["enabled"] = true if err := yaml.Unmarshal(buf.Bytes(), &subVals); err != nil { return err } + if _, exists := subVals["enabled"]; !exists { + subVals["enabled"] = true + } + // need to handle globals in a dedicated way if glob, ok := subVals["global"]; ok { globMap := utils.CleanUpInterfaceMap(glob.(map[interface{}]interface{})) diff --git a/pkg/scaffold/template/lua.go b/pkg/scaffold/template/lua.go index 1ee0e08d8..f1ad961d1 100644 --- a/pkg/scaffold/template/lua.go +++ b/pkg/scaffold/template/lua.go @@ -43,12 +43,15 @@ func ExecuteLua(vals map[string]interface{}, tplate string) (map[string]interfac func FromLuaTemplate(vals map[string]interface{}, globals map[string]interface{}, output map[string]map[string]interface{}, chartName, tplate string) error { var subVals = map[string]interface{}{} - subVals["enabled"] = true subVals, err := ExecuteLua(vals, tplate) if err != nil { return err } + if _, exists := subVals["enabled"]; !exists { + subVals["enabled"] = true + } + // need to handle globals in a dedicated way if glob, ok := subVals["global"]; ok { globMap := utils.CleanUpInterfaceMap(glob.(map[interface{}]interface{})) diff --git a/pkg/scaffold/template/lua_test.go b/pkg/scaffold/template/lua_test.go index 3739ff618..95b8ebba8 100644 --- a/pkg/scaffold/template/lua_test.go +++ b/pkg/scaffold/template/lua_test.go @@ -5,12 +5,13 @@ import ( "path" "testing" + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v2" + "github.com/pluralsh/plural/pkg/config" "github.com/pluralsh/plural/pkg/scaffold/template" pluraltest "github.com/pluralsh/plural/pkg/test" "github.com/pluralsh/plural/pkg/utils/git" - "github.com/stretchr/testify/assert" - "gopkg.in/yaml.v2" ) func TestFromLuaTemplateComplex(t *testing.T) { @@ -417,6 +418,41 @@ test: CYPRESS_BASE_URL: https://console.onplural.sh/ CYPRESS_EMAIL: test@plural.sh CYPRESS_PASSWORD: xyz +`, + }, + { + name: `test enabled flag set to false`, + vals: map[string]interface{}{ + "enabled": false, + }, + script: `output={ + enabled=Var.enabled + }`, + expectedResponse: `global: {} +test: + enabled: false +`, + }, + { + name: `test enabled flag defaulting`, + vals: map[string]interface{}{}, + script: `output={}`, + expectedResponse: `global: {} +test: + enabled: true +`, + }, + { + name: `test enabled flag set to true`, + vals: map[string]interface{}{ + "enabled": true, + }, + script: `output={ + enabled=Var.enabled + }`, + expectedResponse: `global: {} +test: + enabled: true `, }, } From c70b3d5a9d5f9e4545ad529348cb3b0a588cbbe1 Mon Sep 17 00:00:00 2001 From: Sebastian Florek Date: Wed, 6 Sep 2023 16:07:02 +0200 Subject: [PATCH 3/3] fix lint --- pkg/scaffold/template/gotpl.go | 2 +- pkg/scaffold/template/lua.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/scaffold/template/gotpl.go b/pkg/scaffold/template/gotpl.go index 690097cf9..3b7ba35ca 100644 --- a/pkg/scaffold/template/gotpl.go +++ b/pkg/scaffold/template/gotpl.go @@ -23,7 +23,7 @@ func FromGoTemplate(vals map[string]interface{}, globals map[string]interface{}, return err } - var subVals = map[string]interface{}{} + var subVals map[string]interface{} if err := yaml.Unmarshal(buf.Bytes(), &subVals); err != nil { return err } diff --git a/pkg/scaffold/template/lua.go b/pkg/scaffold/template/lua.go index f1ad961d1..3e5359c38 100644 --- a/pkg/scaffold/template/lua.go +++ b/pkg/scaffold/template/lua.go @@ -42,7 +42,7 @@ func ExecuteLua(vals map[string]interface{}, tplate string) (map[string]interfac } func FromLuaTemplate(vals map[string]interface{}, globals map[string]interface{}, output map[string]map[string]interface{}, chartName, tplate string) error { - var subVals = map[string]interface{}{} + var subVals map[string]interface{} subVals, err := ExecuteLua(vals, tplate) if err != nil { return err