Skip to content

Commit fa7ae4c

Browse files
authored
Fix updates to multiple images when using helm values file (argoproj-labs#790)
Signed-off-by: Lyupcho Kotev <[email protected]>
1 parent 9611c99 commit fa7ae4c

File tree

2 files changed

+100
-15
lines changed

2 files changed

+100
-15
lines changed

pkg/argocd/update.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,13 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
435435
if strings.HasPrefix(app.Annotations[common.WriteBackTargetAnnotation], common.HelmPrefix) {
436436
images := GetImagesAndAliasesFromApplication(app)
437437

438-
for _, c := range images {
438+
helmNewValues := yaml.MapSlice{}
439+
err = yaml.Unmarshal(originalData, &helmNewValues)
440+
if err != nil {
441+
return nil, err
442+
}
439443

444+
for _, c := range images {
440445
if c.ImageAlias == "" {
441446
continue
442447
}
@@ -460,14 +465,6 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
460465
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
461466
}
462467

463-
// Create new values structure
464-
//var helmNewValues map[string]interface{}
465-
helmNewValues := yaml.MapSlice{}
466-
err = yaml.Unmarshal(originalData, &helmNewValues)
467-
if err != nil {
468-
return nil, err
469-
}
470-
471468
err = setHelmValue(helmNewValues, helmAnnotationParamName, helmParamName.Value)
472469
if err != nil {
473470
return nil, fmt.Errorf("failed to set image parameter name value: %v", err)
@@ -476,9 +473,9 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
476473
if err != nil {
477474
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
478475
}
479-
480-
override, err = yaml.Marshal(helmNewValues)
481476
}
477+
478+
override, err = yaml.Marshal(helmNewValues)
482479
} else {
483480
var params helmOverride
484481
newParams := helmOverride{

pkg/argocd/update_test.go

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,94 @@ replicas: 1
13831383
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
13841384
})
13851385

1386+
t.Run("Valid Helm source with Helm values file with multiple images", func(t *testing.T) {
1387+
expected := `
1388+
nginx.image.name: nginx
1389+
nginx.image.tag: v1.0.0
1390+
redis.image.name: redis
1391+
redis.image.tag: v1.0.0
1392+
replicas: 1
1393+
`
1394+
app := v1alpha1.Application{
1395+
ObjectMeta: v1.ObjectMeta{
1396+
Name: "testapp",
1397+
Annotations: map[string]string{
1398+
"argocd-image-updater.argoproj.io/image-list": "nginx=nginx, redis=redis",
1399+
"argocd-image-updater.argoproj.io/write-back-method": "git",
1400+
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
1401+
"argocd-image-updater.argoproj.io/nginx.helm.image-name": "nginx.image.name",
1402+
"argocd-image-updater.argoproj.io/nginx.helm.image-tag": "nginx.image.tag",
1403+
"argocd-image-updater.argoproj.io/redis.helm.image-name": "redis.image.name",
1404+
"argocd-image-updater.argoproj.io/redis.helm.image-tag": "redis.image.tag",
1405+
},
1406+
},
1407+
Spec: v1alpha1.ApplicationSpec{
1408+
Sources: []v1alpha1.ApplicationSource{
1409+
{
1410+
Chart: "my-app",
1411+
Helm: &v1alpha1.ApplicationSourceHelm{
1412+
ReleaseName: "my-app",
1413+
ValueFiles: []string{"$values/some/dir/values.yaml"},
1414+
Parameters: []v1alpha1.HelmParameter{
1415+
{
1416+
Name: "nginx.image.name",
1417+
Value: "nginx",
1418+
ForceString: true,
1419+
},
1420+
{
1421+
Name: "nginx.image.tag",
1422+
Value: "v1.0.0",
1423+
ForceString: true,
1424+
},
1425+
{
1426+
Name: "redis.image.name",
1427+
Value: "redis",
1428+
ForceString: true,
1429+
},
1430+
{
1431+
Name: "redis.image.tag",
1432+
Value: "v1.0.0",
1433+
ForceString: true,
1434+
},
1435+
},
1436+
},
1437+
RepoURL: "https://example.com/example",
1438+
TargetRevision: "main",
1439+
},
1440+
{
1441+
Ref: "values",
1442+
RepoURL: "https://example.com/example2",
1443+
TargetRevision: "main",
1444+
},
1445+
},
1446+
},
1447+
Status: v1alpha1.ApplicationStatus{
1448+
SourceTypes: []v1alpha1.ApplicationSourceType{
1449+
v1alpha1.ApplicationSourceTypeHelm,
1450+
"",
1451+
},
1452+
Summary: v1alpha1.ApplicationSummary{
1453+
Images: []string{
1454+
"nginx:v0.0.0",
1455+
"redis:v0.0.0",
1456+
},
1457+
},
1458+
},
1459+
}
1460+
1461+
originalData := []byte(`
1462+
nginx.image.name: nginx
1463+
nginx.image.tag: v0.0.0
1464+
redis.image.name: redis
1465+
redis.image.tag: v0.0.0
1466+
replicas: 1
1467+
`)
1468+
yaml, err := marshalParamsOverride(&app, originalData)
1469+
require.NoError(t, err)
1470+
assert.NotEmpty(t, yaml)
1471+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
1472+
})
1473+
13861474
t.Run("Failed to setValue image parameter name", func(t *testing.T) {
13871475
app := v1alpha1.Application{
13881476
ObjectMeta: v1.ObjectMeta{
@@ -1528,7 +1616,7 @@ replicas: 1
15281616
},
15291617
}
15301618

1531-
originalData := []byte(`random content`)
1619+
originalData := []byte(`random: yaml`)
15321620
_, err := marshalParamsOverride(&app, originalData)
15331621
assert.Error(t, err)
15341622
assert.Equal(t, "could not find an image-tag annotation for image nginx", err.Error())
@@ -1575,7 +1663,7 @@ replicas: 1
15751663
},
15761664
}
15771665

1578-
originalData := []byte(`random content`)
1666+
originalData := []byte(`random: yaml`)
15791667
_, err := marshalParamsOverride(&app, originalData)
15801668
assert.Error(t, err)
15811669
assert.Equal(t, "could not find an image-name annotation for image nginx", err.Error())
@@ -1623,7 +1711,7 @@ replicas: 1
16231711
},
16241712
}
16251713

1626-
originalData := []byte(`random content`)
1714+
originalData := []byte(`random: yaml`)
16271715
_, err := marshalParamsOverride(&app, originalData)
16281716
assert.Error(t, err)
16291717
assert.Equal(t, "wrongimage.name parameter not found", err.Error())
@@ -1671,7 +1759,7 @@ replicas: 1
16711759
},
16721760
}
16731761

1674-
originalData := []byte(`random content`)
1762+
originalData := []byte(`random: yaml`)
16751763
_, err := marshalParamsOverride(&app, originalData)
16761764
assert.Error(t, err)
16771765
assert.Equal(t, "wrongimage.tag parameter not found", err.Error())

0 commit comments

Comments
 (0)