Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(template): allow overriding chart enabled field through templating #444

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/scaffold/template/gotpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,7 +27,10 @@ func FromGoTemplate(vals map[string]interface{}, globals map[string]interface{},
if err := yaml.Unmarshal(buf.Bytes(), &subVals); err != nil {
return err
}
subVals["enabled"] = true

if _, exists := subVals["enabled"]; !exists {
subVals["enabled"] = true
}

// need to handle globals in a dedicated way
if glob, ok := subVals["global"]; ok {
Expand Down
11 changes: 8 additions & 3 deletions pkg/scaffold/template/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -41,11 +42,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, err := ExecuteLua(vals, tplate)
if err != nil {
return err
}
subVals["enabled"] = true

if _, exists := subVals["enabled"]; !exists {
subVals["enabled"] = true
}

// need to handle globals in a dedicated way
if glob, ok := subVals["global"]; ok {
Expand Down
40 changes: 38 additions & 2 deletions pkg/scaffold/template/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -417,6 +418,41 @@ test:
CYPRESS_BASE_URL: https://console.onplural.sh/
CYPRESS_EMAIL: [email protected]
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
`,
},
}
Expand Down