Skip to content

fix: Git write back to helm values is incorrect during the first pass and corrupts existing data #885

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

Merged
Merged
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
10 changes: 9 additions & 1 deletion pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func setHelmValue(currentValues *yaml.MapSlice, key string, value interface{}) e
keys := strings.Split(key, ".")
current := currentValues
var parent *yaml.MapSlice
var parentIdx int
parentIdx := -1

for i, k := range keys {
if idx, found := findHelmValuesKey(*current, k); found {
Expand Down Expand Up @@ -590,11 +590,19 @@ func setHelmValue(currentValues *yaml.MapSlice, key string, value interface{}) e
if parent == nil {
*currentValues = newParent
} else {
// if parentIdx has not been set (parent element is also new), set it to the last element
if parentIdx == -1 {
parentIdx = len(*parent) - 1
if parentIdx < 0 {
parentIdx = 0
}
}
(*parent)[parentIdx].Value = newParent
}

parent = &newParent
current = &newCurrent
parentIdx = -1
}
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,20 @@ replicas: 1
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))

// when image.spec.foo fields are missing in the target helm value file,
// they should be auto created without corrupting any other pre-existing elements.
originalData = []byte("test-value1: one")
expected = `
test-value1: one
image:
spec:
foo: nginx:v1.0.0
`
yaml, err = marshalParamsOverride(&app, originalData)
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Valid Helm source with Helm values file with multiple images", func(t *testing.T) {
Expand Down Expand Up @@ -1588,6 +1602,25 @@ replicas: 1
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))

// when nginx.* and redis.* fields are missing in the target helm value file,
// they should be auto created without corrupting any other pre-existing elements.
originalData = []byte("test-value1: one")
expected = `
test-value1: one
nginx:
image:
tag: v1.0.0
name: nginx
redis:
image:
tag: v1.0.0
name: redis
`
yaml, err = marshalParamsOverride(&app, originalData)
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Valid Helm source with Helm values file with multiple aliases", func(t *testing.T) {
Expand Down Expand Up @@ -1695,6 +1728,7 @@ replicas: 1

t.Run("Failed to setValue image parameter name", func(t *testing.T) {
expected := `
test-value1: one
image:
name: nginx
tag: v1.0.0
Expand Down Expand Up @@ -1743,6 +1777,7 @@ replicas: 1
}

originalData := []byte(`
test-value1: one
image:
name: nginx
replicas: 1
Expand Down