Skip to content

Commit 4f21ade

Browse files
authored
fix: Git write back to helm values is incorrect during the first pass and corrupts existing data (#885)
Signed-off-by: Cheng Fang <[email protected]>
1 parent 02eee1d commit 4f21ade

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

pkg/argocd/update.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ func setHelmValue(currentValues *yaml.MapSlice, key string, value interface{}) e
559559
keys := strings.Split(key, ".")
560560
current := currentValues
561561
var parent *yaml.MapSlice
562-
var parentIdx int
562+
parentIdx := -1
563563

564564
for i, k := range keys {
565565
if idx, found := findHelmValuesKey(*current, k); found {
@@ -590,11 +590,19 @@ func setHelmValue(currentValues *yaml.MapSlice, key string, value interface{}) e
590590
if parent == nil {
591591
*currentValues = newParent
592592
} else {
593+
// if parentIdx has not been set (parent element is also new), set it to the last element
594+
if parentIdx == -1 {
595+
parentIdx = len(*parent) - 1
596+
if parentIdx < 0 {
597+
parentIdx = 0
598+
}
599+
}
593600
(*parent)[parentIdx].Value = newParent
594601
}
595602

596603
parent = &newParent
597604
current = &newCurrent
605+
parentIdx = -1
598606
}
599607
}
600608

pkg/argocd/update_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,20 @@ replicas: 1
15001500
require.NoError(t, err)
15011501
assert.NotEmpty(t, yaml)
15021502
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
1503+
1504+
// when image.spec.foo fields are missing in the target helm value file,
1505+
// they should be auto created without corrupting any other pre-existing elements.
1506+
originalData = []byte("test-value1: one")
1507+
expected = `
1508+
test-value1: one
1509+
image:
1510+
spec:
1511+
foo: nginx:v1.0.0
1512+
`
1513+
yaml, err = marshalParamsOverride(&app, originalData)
1514+
require.NoError(t, err)
1515+
assert.NotEmpty(t, yaml)
1516+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
15031517
})
15041518

15051519
t.Run("Valid Helm source with Helm values file with multiple images", func(t *testing.T) {
@@ -1588,6 +1602,25 @@ replicas: 1
15881602
require.NoError(t, err)
15891603
assert.NotEmpty(t, yaml)
15901604
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
1605+
1606+
// when nginx.* and redis.* fields are missing in the target helm value file,
1607+
// they should be auto created without corrupting any other pre-existing elements.
1608+
originalData = []byte("test-value1: one")
1609+
expected = `
1610+
test-value1: one
1611+
nginx:
1612+
image:
1613+
tag: v1.0.0
1614+
name: nginx
1615+
redis:
1616+
image:
1617+
tag: v1.0.0
1618+
name: redis
1619+
`
1620+
yaml, err = marshalParamsOverride(&app, originalData)
1621+
require.NoError(t, err)
1622+
assert.NotEmpty(t, yaml)
1623+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
15911624
})
15921625

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

16961729
t.Run("Failed to setValue image parameter name", func(t *testing.T) {
16971730
expected := `
1731+
test-value1: one
16981732
image:
16991733
name: nginx
17001734
tag: v1.0.0
@@ -1743,6 +1777,7 @@ replicas: 1
17431777
}
17441778

17451779
originalData := []byte(`
1780+
test-value1: one
17461781
image:
17471782
name: nginx
17481783
replicas: 1

0 commit comments

Comments
 (0)