diff options
| author | David Vidal Villamide <david@askharilabs.com> | 2024-05-27 15:31:11 +0200 |
|---|---|---|
| committer | David Vidal Villamide <david@askharilabs.com> | 2024-05-27 15:31:11 +0200 |
| commit | 00ce8a7f857308b9e17fdadfb429056b8f1fed8b (patch) | |
| tree | 3faabef7af11f210c66b13985c153c18ffb76dec | |
| parent | b40e984c531c528befb218e2e6a54ffda81e6b4e (diff) | |
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>
| -rw-r--r-- | pkg/argocd/argocd.go | 18 | ||||
| -rw-r--r-- | pkg/argocd/argocd_test.go | 59 | ||||
| -rw-r--r-- | pkg/argocd/update.go | 10 |
3 files changed, 83 insertions, 4 deletions
diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index 4584dbe..e3f7063 100644 --- a/pkg/argocd/argocd.go +++ b/pkg/argocd/argocd.go @@ -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) { diff --git a/pkg/argocd/argocd_test.go b/pkg/argocd/argocd_test.go index 9555357..84df479 100644 --- a/pkg/argocd/argocd_test.go +++ b/pkg/argocd/argocd_test.go @@ -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{ diff --git a/pkg/argocd/update.go b/pkg/argocd/update.go index fd1463e..8447466 100644 --- a/pkg/argocd/update.go +++ b/pkg/argocd/update.go @@ -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) |
