Skip to content

Commit 00ce8a7

Browse files
committed
add: GetImagesAndAliasesFromApplication to retrieve images with aliases
fix: now using alias to retrieve annotations when updating Helm type app Signed-off-by: David Vidal Villamide <david@askharilabs.com>
1 parent b40e984 commit 00ce8a7

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

pkg/argocd/argocd.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,24 @@ func GetImagesFromApplication(app *v1alpha1.Application) image.ContainerImageLis
503503
return images
504504
}
505505

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

pkg/argocd/argocd_test.go

Lines changed: 59 additions & 0 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{

pkg/argocd/update.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,17 @@ 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+
helmAnnotationParamName := c.GetParameterHelmImageName(app.Annotations)
423+
helmAnnotationParamVersion := c.GetParameterHelmImageTag(app.Annotations)
424+
423425
if helmAnnotationParamName == "" {
424-
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
426+
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageAlias)
425427
}
426428
if helmAnnotationParamVersion == "" {
427-
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
429+
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageAlias)
428430
}
429431

430432
helmParamName := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamName)

0 commit comments

Comments
 (0)