Skip to content

Commit

Permalink
Merge pull request #1291 from ganievs/feature/helm-set-json
Browse files Browse the repository at this point in the history
add --set-json flag support for the helm module
  • Loading branch information
denis256 committed May 22, 2023
2 parents ee5aa0e + a1eca26 commit 5389a85
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions modules/helm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func getNamespaceArgs(options *Options) []string {
func getValuesArgsE(t testing.TestingT, options *Options, args ...string) ([]string, error) {
args = append(args, formatSetValuesAsArgs(options.SetValues, "--set")...)
args = append(args, formatSetValuesAsArgs(options.SetStrValues, "--set-string")...)
args = append(args, formatSetValuesAsArgs(options.SetJsonValues, "--set-json")...)

valuesFilesArgs, err := formatValuesFilesAsArgsE(t, options.ValuesFiles)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion modules/helm/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// formatSetValuesAsArgs formats the given values as command line args for helm using the given flag (e.g flags of
// the format "--set"/"--set-string" resulting in args like --set/set-string key=value...)
// the format "--set"/"--set-string"/"--set-json" resulting in args like --set/set-string/set-json key=value...)
func formatSetValuesAsArgs(setValues map[string]string, flag string) []string {
args := []string{}

Expand Down
25 changes: 20 additions & 5 deletions modules/helm/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ func TestFormatSetValuesAsArgs(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
setValues map[string]string
setStrValues map[string]string
expected []string
expectedStr []string
name string
setValues map[string]string
setStrValues map[string]string
setJsonValues map[string]string
expected []string
expectedStr []string
expectedJson []string
}{
{
"EmptyValue",
map[string]string{},
map[string]string{},
map[string]string{},
[]string{},
[]string{},
[]string{},
},
{
"SingleValue",
map[string]string{"containerImage": "null"},
map[string]string{"numericString": "123123123123"},
map[string]string{"limits": `{"cpu": 1}`},
[]string{"--set", "containerImage=null"},
[]string{"--set-string", "numericString=123123123123"},
[]string{"--set-json", fmt.Sprintf("limits=%s", `{"cpu": 1}`)},
},
{
"MultipleValues",
Expand All @@ -45,6 +51,10 @@ func TestFormatSetValuesAsArgs(t *testing.T) {
"numericString": "123123123123",
"otherString": "null",
},
map[string]string{
"containerImage": `{"repository": "nginx", "tag": "v1.15.4"}`,
"otherString": "{}",
},
[]string{
"--set", "containerImage.repository=nginx",
"--set", "containerImage.tag=v1.15.4",
Expand All @@ -53,6 +63,10 @@ func TestFormatSetValuesAsArgs(t *testing.T) {
"--set-string", "numericString=123123123123",
"--set-string", "otherString=null",
},
[]string{
"--set-json", fmt.Sprintf("containerImage=%s", `{"repository": "nginx", "tag": "v1.15.4"}`),
"--set-json", "otherString={}",
},
},
}

Expand All @@ -65,6 +79,7 @@ func TestFormatSetValuesAsArgs(t *testing.T) {
t.Parallel()
assert.Equal(t, formatSetValuesAsArgs(testCase.setValues, "--set"), testCase.expected)
assert.Equal(t, formatSetValuesAsArgs(testCase.setStrValues, "--set-string"), testCase.expectedStr)
assert.Equal(t, formatSetValuesAsArgs(testCase.setJsonValues, "--set-json"), testCase.expectedJson)
})
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/helm/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Options struct {
ValuesFiles []string // List of values files to render.
SetValues map[string]string // Values that should be set via the command line.
SetStrValues map[string]string // Values that should be set via the command line explicitly as `string` types.
SetJsonValues map[string]string // Values that should be set via the command line in JSON format.
SetFiles map[string]string // Values that should be set from a file. These should be file paths. Use to avoid logging secrets.
KubectlOptions *k8s.KubectlOptions // KubectlOptions to control how to authenticate to kubernetes cluster. `nil` => use defaults.
HomePath string // The path to the helm home to use when calling out to helm. Empty string means use default ($HOME/.helm).
Expand Down

0 comments on commit 5389a85

Please sign in to comment.