Skip to content

Commit

Permalink
Allow setting new value in Helm values file
Browse files Browse the repository at this point in the history
* This was previously supported, but broke when the function was
  refactored to preserve order of yaml file
* Behavior does not change if overriding an existing value
* If the values file is empty or does not have a previous value, add the
  value instead of erroring
  • Loading branch information
jnovick committed Aug 22, 2024
1 parent 43dbd63 commit f19683e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
34 changes: 22 additions & 12 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,11 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
}

err = setHelmValue(helmNewValues, helmAnnotationParamName, helmParamName.Value)
err = setHelmValue(&helmNewValues, helmAnnotationParamName, helmParamName.Value)
if err != nil {
return nil, fmt.Errorf("failed to set image parameter name value: %v", err)
}
err = setHelmValue(helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
err = setHelmValue(&helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
if err != nil {
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
}
Expand Down Expand Up @@ -542,33 +542,43 @@ func findHelmValuesKey(m yaml.MapSlice, key string) (int, bool) {
}

// set value of the parameter passed from the annotations.
func setHelmValue(m yaml.MapSlice, key string, value interface{}) error {
func setHelmValue(currentValues *yaml.MapSlice, key string, value interface{}) error {
// Check if the full key exists
if idx, found := findHelmValuesKey(m, key); found {
m[idx].Value = value
if idx, found := findHelmValuesKey(*currentValues, key); found {
(*currentValues)[idx].Value = value
return nil
}

var err error
keys := strings.Split(key, ".")
current := m
current := currentValues
var parent *yaml.MapSlice
var parentIdx int

for i, k := range keys {
if idx, found := findHelmValuesKey(current, k); found {
if idx, found := findHelmValuesKey(*current, k); found {
if i == len(keys)-1 {
// If we're at the final key, set the value and return
current[idx].Value = value
(*current)[idx].Value = value
return nil
} else {
// Navigate deeper into the map
if nestedMap, ok := current[idx].Value.(yaml.MapSlice); ok {
current = nestedMap
if nestedMap, ok := (*current)[idx].Value.(yaml.MapSlice); ok {
parent = current
parentIdx = idx
current = &nestedMap
} else {
return fmt.Errorf("unexpected type %T for key %s", current[idx].Value, k)
return fmt.Errorf("unexpected type %T for key %s", (*current)[idx].Value, k)
}
}
} else {
err = fmt.Errorf("key %s not found in the map", k)
new := append(*current, yaml.MapItem{Key: k, Value: value})

if parent == nil {
*currentValues = new
} else {
(*parent)[parentIdx].Value = new
}
break
}
}
Expand Down
44 changes: 37 additions & 7 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ func Test_SetHelmValue(t *testing.T) {
key := "image.attributes.tag"
value := "v2.0.0"

err := setHelmValue(input, key, value)
err := setHelmValue(&input, key, value)
require.NoError(t, err)
assert.Equal(t, expected, input)
})
Expand All @@ -1881,23 +1881,53 @@ func Test_SetHelmValue(t *testing.T) {
key := "image.attributes.tag"
value := "v2.0.0"

err := setHelmValue(input, key, value)
err := setHelmValue(&input, key, value)
require.NoError(t, err)
assert.Equal(t, expected, input)
})

t.Run("Key not found", func(t *testing.T) {
expected := yaml.MapSlice{
{Key: "image", Value: yaml.MapSlice{
{Key: "attributes", Value: yaml.MapSlice{
{Key: "name", Value: "repo-name"},
{Key: "tag", Value: "v2.0.0"},
}},
}},
}

input := yaml.MapSlice{
{Key: "image", Value: yaml.MapSlice{
{Key: "tag", Value: "v1.0.0"},
{Key: "attributes", Value: yaml.MapSlice{
{Key: "name", Value: "repo-name"},
}},
}},
}

key := "image.attributes.tag"
value := "v2.0.0"

err := setHelmValue(input, key, value)
assert.Error(t, err)
assert.Equal(t, "key attributes not found in the map", err.Error())
err := setHelmValue(&input, key, value)
require.NoError(t, err)
assert.Equal(t, expected, input)
})

t.Run("Root key not found", func(t *testing.T) {
expected := yaml.MapSlice{
{Key: "name", Value: "repo-name"},
{Key: "tag", Value: "v2.0.0"},
}

input := yaml.MapSlice{
{Key: "name", Value: "repo-name"},
}

key := "tag"
value := "v2.0.0"

err := setHelmValue(&input, key, value)
require.NoError(t, err)
assert.Equal(t, expected, input)
})

t.Run("Unexpected type for key", func(t *testing.T) {
Expand All @@ -1909,7 +1939,7 @@ func Test_SetHelmValue(t *testing.T) {
key := "image.attributes.tag"
value := "v2.0.0"

err := setHelmValue(input, key, value)
err := setHelmValue(&input, key, value)
assert.Error(t, err)
assert.Equal(t, "unexpected type string for key attributes", err.Error())
})
Expand Down

0 comments on commit f19683e

Please sign in to comment.