Skip to content

Commit f03d2d3

Browse files
chengfangTchoupinax
authored andcommitted
fix: Multiple aliases for the same image only 1 parameter is updated (argoproj-labs#846)
Signed-off-by: Cheng Fang <[email protected]> Signed-off-by: Tchoupinax <[email protected]>
1 parent d8db36a commit f03d2d3

File tree

2 files changed

+118
-3
lines changed

2 files changed

+118
-3
lines changed

pkg/argocd/argocd.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,22 @@ func GetImagesAndAliasesFromApplication(app *v1alpha1.Application) image.Contain
483483
// We update the ImageAlias field of the Images found in the app.Status.Summary.Images list.
484484
for _, img := range *parseImageList(app.Annotations) {
485485
if image := images.ContainsImage(img, false); image != nil {
486-
if img.ImageAlias == "" {
487-
image.ImageAlias = img.ImageName
486+
if image.ImageAlias != "" {
487+
// this image has already been matched to an alias, so create a copy
488+
// and assign this alias to the image copy to avoid overwriting the existing alias association
489+
imageCopy := *image
490+
if img.ImageAlias == "" {
491+
imageCopy.ImageAlias = img.ImageName
492+
} else {
493+
imageCopy.ImageAlias = img.ImageAlias
494+
}
495+
images = append(images, &imageCopy)
488496
} else {
489-
image.ImageAlias = img.ImageAlias
497+
if img.ImageAlias == "" {
498+
image.ImageAlias = img.ImageName
499+
} else {
500+
image.ImageAlias = img.ImageAlias
501+
}
490502
}
491503
}
492504
}

pkg/argocd/update_test.go

+103
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,109 @@ replicas: 1
14711471
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
14721472
})
14731473

1474+
t.Run("Valid Helm source with Helm values file with multiple aliases", func(t *testing.T) {
1475+
expected := `
1476+
foo.image.name: nginx
1477+
foo.image.tag: v1.0.0
1478+
bar.image.name: nginx
1479+
bar.image.tag: v1.0.0
1480+
bbb.image.name: nginx
1481+
bbb.image.tag: v1.0.0
1482+
replicas: 1
1483+
`
1484+
app := v1alpha1.Application{
1485+
ObjectMeta: v1.ObjectMeta{
1486+
Name: "testapp",
1487+
Annotations: map[string]string{
1488+
"argocd-image-updater.argoproj.io/image-list": "foo=nginx, bar=nginx, bbb=nginx",
1489+
"argocd-image-updater.argoproj.io/write-back-method": "git",
1490+
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
1491+
"argocd-image-updater.argoproj.io/foo.helm.image-name": "foo.image.name",
1492+
"argocd-image-updater.argoproj.io/foo.helm.image-tag": "foo.image.tag",
1493+
"argocd-image-updater.argoproj.io/bar.helm.image-name": "bar.image.name",
1494+
"argocd-image-updater.argoproj.io/bar.helm.image-tag": "bar.image.tag",
1495+
"argocd-image-updater.argoproj.io/bbb.helm.image-name": "bbb.image.name",
1496+
"argocd-image-updater.argoproj.io/bbb.helm.image-tag": "bbb.image.tag",
1497+
},
1498+
},
1499+
Spec: v1alpha1.ApplicationSpec{
1500+
Sources: []v1alpha1.ApplicationSource{
1501+
{
1502+
Chart: "my-app",
1503+
Helm: &v1alpha1.ApplicationSourceHelm{
1504+
ReleaseName: "my-app",
1505+
ValueFiles: []string{"$values/some/dir/values.yaml"},
1506+
Parameters: []v1alpha1.HelmParameter{
1507+
{
1508+
Name: "foo.image.name",
1509+
Value: "nginx",
1510+
ForceString: true,
1511+
},
1512+
{
1513+
Name: "foo.image.tag",
1514+
Value: "v1.0.0",
1515+
ForceString: true,
1516+
},
1517+
{
1518+
Name: "bar.image.name",
1519+
Value: "nginx",
1520+
ForceString: true,
1521+
},
1522+
{
1523+
Name: "bar.image.tag",
1524+
Value: "v1.0.0",
1525+
ForceString: true,
1526+
},
1527+
{
1528+
Name: "bbb.image.name",
1529+
Value: "nginx",
1530+
ForceString: true,
1531+
},
1532+
{
1533+
Name: "bbb.image.tag",
1534+
Value: "v1.0.0",
1535+
ForceString: true,
1536+
},
1537+
},
1538+
},
1539+
RepoURL: "https://example.com/example",
1540+
TargetRevision: "main",
1541+
},
1542+
{
1543+
Ref: "values",
1544+
RepoURL: "https://example.com/example2",
1545+
TargetRevision: "main",
1546+
},
1547+
},
1548+
},
1549+
Status: v1alpha1.ApplicationStatus{
1550+
SourceTypes: []v1alpha1.ApplicationSourceType{
1551+
v1alpha1.ApplicationSourceTypeHelm,
1552+
"",
1553+
},
1554+
Summary: v1alpha1.ApplicationSummary{
1555+
Images: []string{
1556+
"nginx:v0.0.0",
1557+
},
1558+
},
1559+
},
1560+
}
1561+
1562+
originalData := []byte(`
1563+
foo.image.name: nginx
1564+
foo.image.tag: v0.0.0
1565+
bar.image.name: nginx
1566+
bar.image.tag: v0.0.0
1567+
bbb.image.name: nginx
1568+
bbb.image.tag: v0.0.0
1569+
replicas: 1
1570+
`)
1571+
yaml, err := marshalParamsOverride(&app, originalData)
1572+
require.NoError(t, err)
1573+
assert.NotEmpty(t, yaml)
1574+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
1575+
})
1576+
14741577
t.Run("Failed to setValue image parameter name", func(t *testing.T) {
14751578
expected := `
14761579
image:

0 commit comments

Comments
 (0)