From a1eca26c2e4440b839c572d7915e1baa7886269c Mon Sep 17 00:00:00 2001 From: Shamil Ganiev Date: Mon, 22 May 2023 13:32:05 +0400 Subject: [PATCH] add --set-json flag support for the helm module Signed-off-by: Shamil Ganiev --- modules/helm/cmd.go | 1 + modules/helm/format.go | 2 +- modules/helm/format_test.go | 25 ++++++++++++++++++++----- modules/helm/options.go | 1 + 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/modules/helm/cmd.go b/modules/helm/cmd.go index 2b827dd5c..d4cd41b11 100644 --- a/modules/helm/cmd.go +++ b/modules/helm/cmd.go @@ -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 { diff --git a/modules/helm/format.go b/modules/helm/format.go index becc66b79..1ee467d4d 100644 --- a/modules/helm/format.go +++ b/modules/helm/format.go @@ -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{} diff --git a/modules/helm/format_test.go b/modules/helm/format_test.go index db54ff11e..ce03e6454 100644 --- a/modules/helm/format_test.go +++ b/modules/helm/format_test.go @@ -15,16 +15,20 @@ 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{}, }, @@ -32,8 +36,10 @@ func TestFormatSetValuesAsArgs(t *testing.T) { "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", @@ -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", @@ -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={}", + }, }, } @@ -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) }) } } diff --git a/modules/helm/options.go b/modules/helm/options.go index 9e2495db3..7f6f1270e 100644 --- a/modules/helm/options.go +++ b/modules/helm/options.go @@ -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).