Skip to content

Commit 8c9e269

Browse files
authored
Allow setting new value in Helm values file (#838)
Signed-off-by: Joshua Novick <[email protected]>
1 parent 0c9c0a9 commit 8c9e269

File tree

2 files changed

+115
-30
lines changed

2 files changed

+115
-30
lines changed

pkg/argocd/update.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,11 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
465465
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
466466
}
467467

468-
err = setHelmValue(helmNewValues, helmAnnotationParamName, helmParamName.Value)
468+
err = setHelmValue(&helmNewValues, helmAnnotationParamName, helmParamName.Value)
469469
if err != nil {
470470
return nil, fmt.Errorf("failed to set image parameter name value: %v", err)
471471
}
472-
err = setHelmValue(helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
472+
err = setHelmValue(&helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
473473
if err != nil {
474474
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
475475
}
@@ -542,34 +542,53 @@ func findHelmValuesKey(m yaml.MapSlice, key string) (int, bool) {
542542
}
543543

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

552552
var err error
553553
keys := strings.Split(key, ".")
554-
current := m
554+
current := currentValues
555+
var parent *yaml.MapSlice
556+
var parentIdx int
555557

556558
for i, k := range keys {
557-
if idx, found := findHelmValuesKey(current, k); found {
559+
if idx, found := findHelmValuesKey(*current, k); found {
558560
if i == len(keys)-1 {
559561
// If we're at the final key, set the value and return
560-
current[idx].Value = value
562+
(*current)[idx].Value = value
561563
return nil
562564
} else {
563565
// Navigate deeper into the map
564-
if nestedMap, ok := current[idx].Value.(yaml.MapSlice); ok {
565-
current = nestedMap
566+
if nestedMap, ok := (*current)[idx].Value.(yaml.MapSlice); ok {
567+
parent = current
568+
parentIdx = idx
569+
current = &nestedMap
566570
} else {
567-
return fmt.Errorf("unexpected type %T for key %s", current[idx].Value, k)
571+
return fmt.Errorf("unexpected type %T for key %s", (*current)[idx].Value, k)
568572
}
569573
}
570574
} else {
571-
err = fmt.Errorf("key %s not found in the map", k)
572-
break
575+
newCurrent := yaml.MapSlice{}
576+
var newParent yaml.MapSlice
577+
578+
if i == len(keys)-1 {
579+
newParent = append(*current, yaml.MapItem{Key: k, Value: value})
580+
} else {
581+
newParent = append(*current, yaml.MapItem{Key: k, Value: newCurrent})
582+
}
583+
584+
if parent == nil {
585+
*currentValues = newParent
586+
} else {
587+
(*parent)[parentIdx].Value = newParent
588+
}
589+
590+
parent = &newParent
591+
current = &newCurrent
573592
}
574593
}
575594

pkg/argocd/update_test.go

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,13 @@ replicas: 1
14721472
})
14731473

14741474
t.Run("Failed to setValue image parameter name", func(t *testing.T) {
1475+
expected := `
1476+
image:
1477+
name: nginx
1478+
tag: v1.0.0
1479+
replicas: 1
1480+
`
1481+
14751482
app := v1alpha1.Application{
14761483
ObjectMeta: v1.ObjectMeta{
14771484
Name: "testapp",
@@ -1514,16 +1521,24 @@ replicas: 1
15141521
}
15151522

15161523
originalData := []byte(`
1517-
image_name: nginx
1518-
image.tag: v0.0.0
1524+
image:
1525+
name: nginx
15191526
replicas: 1
15201527
`)
1521-
_, err := marshalParamsOverride(&app, originalData)
1522-
assert.Error(t, err)
1523-
assert.Equal(t, "failed to set image parameter name value: key image not found in the map", err.Error())
1528+
1529+
yaml, err := marshalParamsOverride(&app, originalData)
1530+
require.NoError(t, err)
1531+
assert.NotEmpty(t, yaml)
1532+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
15241533
})
15251534

15261535
t.Run("Failed to setValue image parameter version", func(t *testing.T) {
1536+
expected := `
1537+
image:
1538+
tag: v1.0.0
1539+
name: nginx
1540+
replicas: 1
1541+
`
15271542
app := v1alpha1.Application{
15281543
ObjectMeta: v1.ObjectMeta{
15291544
Name: "testapp",
@@ -1566,13 +1581,15 @@ replicas: 1
15661581
}
15671582

15681583
originalData := []byte(`
1569-
image.name: nginx
1570-
image_tag: v0.0.0
1584+
image:
1585+
tag: v0.0.0
15711586
replicas: 1
15721587
`)
1573-
_, err := marshalParamsOverride(&app, originalData)
1574-
assert.Error(t, err)
1575-
assert.Equal(t, "failed to set image parameter version value: key image not found in the map", err.Error())
1588+
1589+
yaml, err := marshalParamsOverride(&app, originalData)
1590+
require.NoError(t, err)
1591+
assert.NotEmpty(t, yaml)
1592+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
15761593
})
15771594

15781595
t.Run("Missing annotation image-tag for helmvalues write-back-target", func(t *testing.T) {
@@ -1865,7 +1882,7 @@ func Test_SetHelmValue(t *testing.T) {
18651882
key := "image.attributes.tag"
18661883
value := "v2.0.0"
18671884

1868-
err := setHelmValue(input, key, value)
1885+
err := setHelmValue(&input, key, value)
18691886
require.NoError(t, err)
18701887
assert.Equal(t, expected, input)
18711888
})
@@ -1881,23 +1898,72 @@ func Test_SetHelmValue(t *testing.T) {
18811898
key := "image.attributes.tag"
18821899
value := "v2.0.0"
18831900

1884-
err := setHelmValue(input, key, value)
1901+
err := setHelmValue(&input, key, value)
18851902
require.NoError(t, err)
18861903
assert.Equal(t, expected, input)
18871904
})
18881905

18891906
t.Run("Key not found", func(t *testing.T) {
1907+
expected := yaml.MapSlice{
1908+
{Key: "image", Value: yaml.MapSlice{
1909+
{Key: "attributes", Value: yaml.MapSlice{
1910+
{Key: "name", Value: "repo-name"},
1911+
{Key: "tag", Value: "v2.0.0"},
1912+
}},
1913+
}},
1914+
}
1915+
18901916
input := yaml.MapSlice{
18911917
{Key: "image", Value: yaml.MapSlice{
1892-
{Key: "tag", Value: "v1.0.0"},
1918+
{Key: "attributes", Value: yaml.MapSlice{
1919+
{Key: "name", Value: "repo-name"},
1920+
}},
18931921
}},
18941922
}
1923+
18951924
key := "image.attributes.tag"
18961925
value := "v2.0.0"
18971926

1898-
err := setHelmValue(input, key, value)
1899-
assert.Error(t, err)
1900-
assert.Equal(t, "key attributes not found in the map", err.Error())
1927+
err := setHelmValue(&input, key, value)
1928+
require.NoError(t, err)
1929+
assert.Equal(t, expected, input)
1930+
})
1931+
1932+
t.Run("Root key not found", func(t *testing.T) {
1933+
expected := yaml.MapSlice{
1934+
{Key: "name", Value: "repo-name"},
1935+
{Key: "tag", Value: "v2.0.0"},
1936+
}
1937+
1938+
input := yaml.MapSlice{
1939+
{Key: "name", Value: "repo-name"},
1940+
}
1941+
1942+
key := "tag"
1943+
value := "v2.0.0"
1944+
1945+
err := setHelmValue(&input, key, value)
1946+
require.NoError(t, err)
1947+
assert.Equal(t, expected, input)
1948+
})
1949+
1950+
t.Run("Empty values with deep key", func(t *testing.T) {
1951+
expected := yaml.MapSlice{
1952+
{Key: "image", Value: yaml.MapSlice{
1953+
{Key: "attributes", Value: yaml.MapSlice{
1954+
{Key: "tag", Value: "v2.0.0"},
1955+
}},
1956+
}},
1957+
}
1958+
1959+
input := yaml.MapSlice{}
1960+
1961+
key := "image.attributes.tag"
1962+
value := "v2.0.0"
1963+
1964+
err := setHelmValue(&input, key, value)
1965+
require.NoError(t, err)
1966+
assert.Equal(t, expected, input)
19011967
})
19021968

19031969
t.Run("Unexpected type for key", func(t *testing.T) {
@@ -1909,7 +1975,7 @@ func Test_SetHelmValue(t *testing.T) {
19091975
key := "image.attributes.tag"
19101976
value := "v2.0.0"
19111977

1912-
err := setHelmValue(input, key, value)
1978+
err := setHelmValue(&input, key, value)
19131979
assert.Error(t, err)
19141980
assert.Equal(t, "unexpected type string for key attributes", err.Error())
19151981
})

0 commit comments

Comments
 (0)