Skip to content

Commit

Permalink
add: GetImagesAndAliasesFromApplication to retrieve images with aliases
Browse files Browse the repository at this point in the history
fix: now using alias to retrieve annotations when updating Helm type app

Signed-off-by: David Vidal Villamide <david@askharilabs.com>
  • Loading branch information
askhari committed May 27, 2024
1 parent b40e984 commit 00ce8a7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
18 changes: 18 additions & 0 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,24 @@ func GetImagesFromApplication(app *v1alpha1.Application) image.ContainerImageLis
return images
}

// GetImagesFromApplicationImagesAnnotation returns the list of known images for the given application from the images annotation
func GetImagesAndAliasesFromApplication(app *v1alpha1.Application) image.ContainerImageList {
images := GetImagesFromApplication(app)

// We update the ImageAlias field of the Images found in the app.Status.Summary.Images list.
for _, img := range *parseImageList(app.Annotations) {
if image := images.ContainsImage(img, false); image != nil {
if img.ImageAlias == "" {
image.ImageAlias = img.ImageName
} else {
image.ImageAlias = img.ImageAlias
}
}
}

return images
}

// GetApplicationTypeByName first retrieves application with given appName and
// returns its application type
func GetApplicationTypeByName(client ArgoCD, appName string) (ApplicationType, error) {
Expand Down
59 changes: 59 additions & 0 deletions pkg/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,65 @@ func Test_GetImagesFromApplication(t *testing.T) {
})
}

func Test_GetImagesAndAliasesFromApplication(t *testing.T) {
t.Run("Get list of images from application", func(t *testing.T) {
application := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "test-app",
Namespace: "argocd",
},
Spec: v1alpha1.ApplicationSpec{},
Status: v1alpha1.ApplicationStatus{
Summary: v1alpha1.ApplicationSummary{
Images: []string{"nginx:1.12.2", "that/image", "quay.io/dexidp/dex:v1.23.0"},
},
},
}
imageList := GetImagesAndAliasesFromApplication(application)
require.Len(t, imageList, 3)
assert.Equal(t, "nginx", imageList[0].ImageName)
assert.Equal(t, "that/image", imageList[1].ImageName)
assert.Equal(t, "dexidp/dex", imageList[2].ImageName)
})

t.Run("Get list of images and image aliases from application that has no images", func(t *testing.T) {
application := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "test-app",
Namespace: "argocd",
},
Spec: v1alpha1.ApplicationSpec{},
Status: v1alpha1.ApplicationStatus{
Summary: v1alpha1.ApplicationSummary{},
},
}
imageList := GetImagesAndAliasesFromApplication(application)
assert.Empty(t, imageList)
})

t.Run("Get list of images and aliases from application annotations", func(t *testing.T) {
application := &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "test-app",
Namespace: "argocd",
Annotations: map[string]string{
common.ImageUpdaterAnnotation: "webserver=nginx",
},
},
Spec: v1alpha1.ApplicationSpec{},
Status: v1alpha1.ApplicationStatus{
Summary: v1alpha1.ApplicationSummary{
Images: []string{"nginx:1.12.2"},
},
},
}
imageList := GetImagesAndAliasesFromApplication(application)
require.Len(t, imageList, 1)
assert.Equal(t, "nginx", imageList[0].ImageName)
assert.Equal(t, "webserver", imageList[0].ImageAlias)
})
}

func Test_GetApplicationType(t *testing.T) {
t.Run("Get application of type Helm", func(t *testing.T) {
application := &v1alpha1.Application{
Expand Down
10 changes: 6 additions & 4 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,17 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
}

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

for _, c := range images {
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c.ImageName)
helmAnnotationParamName := c.GetParameterHelmImageName(app.Annotations)
helmAnnotationParamVersion := c.GetParameterHelmImageTag(app.Annotations)

if helmAnnotationParamName == "" {
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageAlias)
}
if helmAnnotationParamVersion == "" {
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageAlias)
}

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

0 comments on commit 00ce8a7

Please sign in to comment.