From cf5d9db5bbaf71cbf9aaa5b17b949f4e9b83fcb2 Mon Sep 17 00:00:00 2001 From: Alex Collins Date: Fri, 18 Oct 2019 15:30:46 -0700 Subject: [PATCH] Allows Helm parameters that contains arrays or maps. (#2525) --- util/helm/helm.go | 20 ++++++++++++-------- util/helm/helm_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/util/helm/helm.go b/util/helm/helm.go index 2ec9787a10..da5f6746ff 100644 --- a/util/helm/helm.go +++ b/util/helm/helm.go @@ -10,9 +10,8 @@ import ( "regexp" "strings" - "github.com/ghodss/yaml" - argoexec "github.com/argoproj/pkg/exec" + "github.com/ghodss/yaml" "github.com/argoproj/argo-cd/util/config" ) @@ -138,12 +137,17 @@ func (h *helm) GetParameters(valuesFiles []string) (map[string]string, error) { return output, nil } -func flatVals(input map[string]interface{}, output map[string]string, prefixes ...string) { - for key, val := range input { - if subMap, ok := val.(map[string]interface{}); ok { - flatVals(subMap, output, append(prefixes, fmt.Sprintf("%v", key))...) - } else { - output[strings.Join(append(prefixes, fmt.Sprintf("%v", key)), ".")] = fmt.Sprintf("%v", val) +func flatVals(input interface{}, output map[string]string, prefixes ...string) { + switch i := input.(type) { + case map[string]interface{}: + for k, v := range i { + flatVals(v, output, append(prefixes, k)...) } + case []interface{}: + for j, v := range i { + flatVals(v, output, append(prefixes[0:len(prefixes)-1], fmt.Sprintf("%s[%v]", prefixes[len(prefixes)-1], j))...) + } + default: + output[strings.Join(prefixes, ".")] = fmt.Sprintf("%v", i) } } diff --git a/util/helm/helm_test.go b/util/helm/helm_test.go index 5b53b12c38..f09ea21c6d 100644 --- a/util/helm/helm_test.go +++ b/util/helm/helm_test.go @@ -164,3 +164,27 @@ func TestVersion(t *testing.T) { re := regexp.MustCompile(SemverRegexValidation) assert.True(t, re.MatchString(ver)) } + +func Test_flatVals(t *testing.T) { + t.Run("Map", func(t *testing.T) { + output := map[string]string{} + + flatVals(map[string]interface{}{"foo": map[string]interface{}{"bar": "baz"}}, output) + + assert.Equal(t, map[string]string{"foo.bar": "baz"}, output) + }) + t.Run("Array", func(t *testing.T) { + output := map[string]string{} + + flatVals(map[string]interface{}{"foo": []interface{}{"bar"}}, output) + + assert.Equal(t, map[string]string{"foo[0]": "bar"}, output) + }) + t.Run("Val", func(t *testing.T) { + output := map[string]string{} + + flatVals(map[string]interface{}{"foo": 1}, output) + + assert.Equal(t, map[string]string{"foo": "1"}, output) + }) +}