Skip to content

Commit 3f47c8b

Browse files
Merge pull request #725 from askhari/fix/set-default-image-alias-with-helmvalues
fix: set default ImageAlias for Helm app type using helmvalues git write-back method
2 parents dc190d2 + c1784a3 commit 3f47c8b

File tree

3 files changed

+108
-16
lines changed

3 files changed

+108
-16
lines changed

pkg/argocd/argocd.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,29 +309,26 @@ func (client *argoCD) UpdateSpec(ctx context.Context, in *application.Applicatio
309309
// getHelmParamNamesFromAnnotation inspects the given annotations for whether
310310
// the annotations for specifying Helm parameter names are being set and
311311
// returns their values.
312-
func getHelmParamNamesFromAnnotation(annotations map[string]string, symbolicName string) (string, string) {
312+
func getHelmParamNamesFromAnnotation(annotations map[string]string, img *image.ContainerImage) (string, string) {
313313
// Return default values without symbolic name given
314-
if symbolicName == "" {
314+
if img.ImageAlias == "" {
315315
return "image.name", "image.tag"
316316
}
317317

318318
var annotationName, helmParamName, helmParamVersion string
319319

320320
// Image spec is a full-qualified specifier, if we have it, we return early
321-
annotationName = fmt.Sprintf(common.HelmParamImageSpecAnnotation, symbolicName)
322-
if param, ok := annotations[annotationName]; ok {
321+
if param := img.GetParameterHelmImageSpec(annotations); param != "" {
323322
log.Tracef("found annotation %s", annotationName)
324323
return strings.TrimSpace(param), ""
325324
}
326325

327-
annotationName = fmt.Sprintf(common.HelmParamImageNameAnnotation, symbolicName)
328-
if param, ok := annotations[annotationName]; ok {
326+
if param := img.GetParameterHelmImageName(annotations); param != "" {
329327
log.Tracef("found annotation %s", annotationName)
330328
helmParamName = param
331329
}
332330

333-
annotationName = fmt.Sprintf(common.HelmParamImageTagAnnotation, symbolicName)
334-
if param, ok := annotations[annotationName]; ok {
331+
if param := img.GetParameterHelmImageTag(annotations); param != "" {
335332
log.Tracef("found annotation %s", annotationName)
336333
helmParamVersion = param
337334
}
@@ -503,6 +500,24 @@ func GetImagesFromApplication(app *v1alpha1.Application) image.ContainerImageLis
503500
return images
504501
}
505502

503+
// GetImagesFromApplicationImagesAnnotation returns the list of known images for the given application from the images annotation
504+
func GetImagesAndAliasesFromApplication(app *v1alpha1.Application) image.ContainerImageList {
505+
images := GetImagesFromApplication(app)
506+
507+
// We update the ImageAlias field of the Images found in the app.Status.Summary.Images list.
508+
for _, img := range *parseImageList(app.Annotations) {
509+
if image := images.ContainsImage(img, false); image != nil {
510+
if img.ImageAlias == "" {
511+
image.ImageAlias = img.ImageName
512+
} else {
513+
image.ImageAlias = img.ImageAlias
514+
}
515+
}
516+
}
517+
518+
return images
519+
}
520+
506521
// GetApplicationTypeByName first retrieves application with given appName and
507522
// returns its application type
508523
func GetApplicationTypeByName(client ArgoCD, appName string) (ApplicationType, error) {

pkg/argocd/argocd_test.go

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,65 @@ func Test_GetImagesFromApplication(t *testing.T) {
7979
})
8080
}
8181

82+
func Test_GetImagesAndAliasesFromApplication(t *testing.T) {
83+
t.Run("Get list of images from application", func(t *testing.T) {
84+
application := &v1alpha1.Application{
85+
ObjectMeta: v1.ObjectMeta{
86+
Name: "test-app",
87+
Namespace: "argocd",
88+
},
89+
Spec: v1alpha1.ApplicationSpec{},
90+
Status: v1alpha1.ApplicationStatus{
91+
Summary: v1alpha1.ApplicationSummary{
92+
Images: []string{"nginx:1.12.2", "that/image", "quay.io/dexidp/dex:v1.23.0"},
93+
},
94+
},
95+
}
96+
imageList := GetImagesAndAliasesFromApplication(application)
97+
require.Len(t, imageList, 3)
98+
assert.Equal(t, "nginx", imageList[0].ImageName)
99+
assert.Equal(t, "that/image", imageList[1].ImageName)
100+
assert.Equal(t, "dexidp/dex", imageList[2].ImageName)
101+
})
102+
103+
t.Run("Get list of images and image aliases from application that has no images", func(t *testing.T) {
104+
application := &v1alpha1.Application{
105+
ObjectMeta: v1.ObjectMeta{
106+
Name: "test-app",
107+
Namespace: "argocd",
108+
},
109+
Spec: v1alpha1.ApplicationSpec{},
110+
Status: v1alpha1.ApplicationStatus{
111+
Summary: v1alpha1.ApplicationSummary{},
112+
},
113+
}
114+
imageList := GetImagesAndAliasesFromApplication(application)
115+
assert.Empty(t, imageList)
116+
})
117+
118+
t.Run("Get list of images and aliases from application annotations", func(t *testing.T) {
119+
application := &v1alpha1.Application{
120+
ObjectMeta: v1.ObjectMeta{
121+
Name: "test-app",
122+
Namespace: "argocd",
123+
Annotations: map[string]string{
124+
common.ImageUpdaterAnnotation: "webserver=nginx",
125+
},
126+
},
127+
Spec: v1alpha1.ApplicationSpec{},
128+
Status: v1alpha1.ApplicationStatus{
129+
Summary: v1alpha1.ApplicationSummary{
130+
Images: []string{"nginx:1.12.2"},
131+
},
132+
},
133+
}
134+
imageList := GetImagesAndAliasesFromApplication(application)
135+
require.Len(t, imageList, 1)
136+
assert.Equal(t, "nginx", imageList[0].ImageName)
137+
assert.Equal(t, "webserver", imageList[0].ImageAlias)
138+
})
139+
}
140+
82141
func Test_GetApplicationType(t *testing.T) {
83142
t.Run("Get application of type Helm", func(t *testing.T) {
84143
application := &v1alpha1.Application{
@@ -541,7 +600,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
541600
fmt.Sprintf(common.HelmParamImageSpecAnnotation, "myimg"): "image.blub",
542601
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.blab",
543602
}
544-
name, tag := getHelmParamNamesFromAnnotation(annotations, "")
603+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
604+
ImageAlias: "",
605+
})
545606
assert.Equal(t, "image.name", name)
546607
assert.Equal(t, "image.tag", tag)
547608
})
@@ -551,7 +612,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
551612
fmt.Sprintf(common.HelmParamImageSpecAnnotation, "myimg"): "image.path",
552613
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.tag",
553614
}
554-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
615+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
616+
ImageAlias: "myimg",
617+
})
555618
assert.Equal(t, "image.path", name)
556619
assert.Empty(t, tag)
557620
})
@@ -561,7 +624,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
561624
fmt.Sprintf(common.HelmParamImageNameAnnotation, "myimg"): "image.name",
562625
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.tag",
563626
}
564-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
627+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
628+
ImageAlias: "myimg",
629+
})
565630
assert.Equal(t, "image.name", name)
566631
assert.Equal(t, "image.tag", tag)
567632
})
@@ -571,7 +636,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
571636
fmt.Sprintf(common.HelmParamImageNameAnnotation, "otherimg"): "image.name",
572637
fmt.Sprintf(common.HelmParamImageTagAnnotation, "otherimg"): "image.tag",
573638
}
574-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
639+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
640+
ImageAlias: "myimg",
641+
})
575642
assert.Empty(t, name)
576643
assert.Empty(t, tag)
577644
})
@@ -580,14 +647,18 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
580647
annotations := map[string]string{
581648
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.tag",
582649
}
583-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
650+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
651+
ImageAlias: "myimg",
652+
})
584653
assert.Empty(t, name)
585654
assert.Equal(t, "image.tag", tag)
586655
})
587656

588657
t.Run("No suitable annotations found", func(t *testing.T) {
589658
annotations := map[string]string{}
590-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
659+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
660+
ImageAlias: "myimg",
661+
})
591662
assert.Empty(t, name)
592663
assert.Empty(t, tag)
593664
})

pkg/argocd/update.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,16 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
416416
}
417417

418418
if strings.HasPrefix(app.Annotations[common.WriteBackTargetAnnotation], common.HelmPrefix) {
419-
images := GetImagesFromApplication(app)
419+
images := GetImagesAndAliasesFromApplication(app)
420420

421421
for _, c := range images {
422-
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c.ImageName)
422+
423+
if c.ImageAlias == "" {
424+
continue
425+
}
426+
427+
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c)
428+
423429
if helmAnnotationParamName == "" {
424430
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
425431
}

0 commit comments

Comments
 (0)