Skip to content

Commit 2a2b321

Browse files
zioufburnb
authored andcommitted
fix: filter images by image-list annotation
Signed-off-by: Cyril MARIN <[email protected]> Signed-off-by: Aleksandr Petrov <[email protected]>
1 parent 50b90c2 commit 2a2b321

File tree

4 files changed

+92
-39
lines changed

4 files changed

+92
-39
lines changed

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ RUN apk update && \
1717
apk add ca-certificates git openssh-client aws-cli tini && \
1818
rm -rf /var/cache/apk/*
1919

20-
RUN mkdir -p /usr/local/bin
21-
RUN mkdir -p /app/config
20+
RUN mkdir -p /usr/local/bin /app/config
2221
RUN adduser --home "/app" --disabled-password --uid 1000 argocd
2322

2423
COPY --from=builder /src/argocd-image-updater/dist/argocd-image-updater /usr/local/bin/

pkg/argocd/argocd.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,32 @@ func SetKustomizeImage(app *v1alpha1.Application, newImage *image.ContainerImage
478478
return nil
479479
}
480480

481+
// ImageIsAllowed checks whether img is declared in image-list annotation
482+
func ImageIsAllowed(img *image.ContainerImage, list *image.ContainerImageList) bool {
483+
for _, i := range *list {
484+
if i.ImageName == img.ImageName {
485+
return true
486+
}
487+
}
488+
return false
489+
}
490+
481491
// GetImagesFromApplication returns the list of known images for the given application
482492
func GetImagesFromApplication(app *v1alpha1.Application) image.ContainerImageList {
483493
images := make(image.ContainerImageList, 0)
494+
annotations := app.Annotations
495+
imagesFromAnnotations := parseImageList(annotations)
484496

485497
for _, imageStr := range app.Status.Summary.Images {
486-
image := image.NewFromIdentifier(imageStr)
487-
images = append(images, image)
498+
img := image.NewFromIdentifier(imageStr)
499+
if ImageIsAllowed(img, imagesFromAnnotations) {
500+
images = append(images, img)
501+
}
488502
}
489503

490504
// The Application may wish to update images that don't create a container we can detect.
491505
// Check the image list for images with a force-update annotation, and add them if they are not already present.
492-
annotations := app.Annotations
493-
for _, img := range *parseImageList(annotations) {
506+
for _, img := range *imagesFromAnnotations {
494507
if img.HasForceUpdateOptionAnnotation(annotations) {
495508
img.ImageTag = nil // the tag from the image list will be a version constraint, which isn't a valid tag
496509
images = append(images, img)

pkg/argocd/argocd_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func Test_GetImagesFromApplication(t *testing.T) {
2727
ObjectMeta: v1.ObjectMeta{
2828
Name: "test-app",
2929
Namespace: "argocd",
30+
Annotations: map[string]string{
31+
common.ImageUpdaterAnnotation: "nginx:1.12.2,that/image,quay.io/dexidp/dex:v1.23.0",
32+
},
3033
},
3134
Spec: v1alpha1.ApplicationSpec{},
3235
Status: v1alpha1.ApplicationStatus{

pkg/argocd/update_test.go

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,15 @@ func Test_UpdateApplication(t *testing.T) {
107107
kubeClient := kube.KubernetesClient{
108108
Clientset: fake.NewFakeKubeClient(),
109109
}
110+
annotations := map[string]string{
111+
common.ImageUpdaterAnnotation: "jannfis/foobar:1.0.0",
112+
}
110113
appImages := &ApplicationImages{
111114
Application: v1alpha1.Application{
112115
ObjectMeta: v1.ObjectMeta{
113-
Name: "guestbook",
114-
Namespace: "guestbook",
116+
Name: "guestbook",
117+
Namespace: "guestbook",
118+
Annotations: annotations,
115119
},
116120
Spec: v1alpha1.ApplicationSpec{
117121
Source: &v1alpha1.ApplicationSource{
@@ -173,11 +177,15 @@ func Test_UpdateApplication(t *testing.T) {
173177
kubeClient := kube.KubernetesClient{
174178
Clientset: fake.NewFakeKubeClient(),
175179
}
180+
annotations := map[string]string{
181+
common.ImageUpdaterAnnotation: "jannfis/foobar:1.0.0,jannfis/barbar:1.0.0",
182+
}
176183
appImages := &ApplicationImages{
177184
Application: v1alpha1.Application{
178185
ObjectMeta: v1.ObjectMeta{
179-
Name: "guestbook",
180-
Namespace: "guestbook",
186+
Name: "guestbook",
187+
Namespace: "guestbook",
188+
Annotations: annotations,
181189
},
182190
Spec: v1alpha1.ApplicationSpec{
183191
Source: &v1alpha1.ApplicationSource{
@@ -360,11 +368,15 @@ func Test_UpdateApplication(t *testing.T) {
360368
kubeClient := kube.KubernetesClient{
361369
Clientset: fake.NewFakeKubeClient(),
362370
}
371+
annotations := map[string]string{
372+
common.ImageUpdaterAnnotation: "jannfis/foobar:1.0.x",
373+
}
363374
appImages := &ApplicationImages{
364375
Application: v1alpha1.Application{
365376
ObjectMeta: v1.ObjectMeta{
366-
Name: "guestbook",
367-
Namespace: "guestbook",
377+
Name: "guestbook",
378+
Namespace: "guestbook",
379+
Annotations: annotations,
368380
},
369381
Spec: v1alpha1.ApplicationSpec{
370382
Source: &v1alpha1.ApplicationSource{
@@ -418,14 +430,16 @@ func Test_UpdateApplication(t *testing.T) {
418430
kubeClient := kube.KubernetesClient{
419431
Clientset: fake.NewFakeClientsetWithResources(fixture.NewSecret("foo", "bar", map[string][]byte{"creds": []byte("myuser:mypass")})),
420432
}
433+
annotations := map[string]string{
434+
common.ImageUpdaterAnnotation: "jannfis/foobar:1.0.0",
435+
fmt.Sprintf(common.PullSecretAnnotation, "dummy"): "secret:foo/bar#creds",
436+
}
421437
appImages := &ApplicationImages{
422438
Application: v1alpha1.Application{
423439
ObjectMeta: v1.ObjectMeta{
424-
Name: "guestbook",
425-
Namespace: "guestbook",
426-
Annotations: map[string]string{
427-
fmt.Sprintf(common.PullSecretAnnotation, "dummy"): "secret:foo/bar#creds",
428-
},
440+
Name: "guestbook",
441+
Namespace: "guestbook",
442+
Annotations: annotations,
429443
},
430444
Spec: v1alpha1.ApplicationSpec{
431445
Source: &v1alpha1.ApplicationSource{
@@ -532,11 +546,15 @@ func Test_UpdateApplication(t *testing.T) {
532546
kubeClient := kube.KubernetesClient{
533547
Clientset: fake.NewFakeKubeClient(),
534548
}
549+
annotations := map[string]string{
550+
common.ImageUpdaterAnnotation: "jannfis/foobar:1.0.1",
551+
}
535552
appImages := &ApplicationImages{
536553
Application: v1alpha1.Application{
537554
ObjectMeta: v1.ObjectMeta{
538-
Name: "guestbook",
539-
Namespace: "guestbook",
555+
Name: "guestbook",
556+
Namespace: "guestbook",
557+
Annotations: annotations,
540558
},
541559
Spec: v1alpha1.ApplicationSpec{
542560
Source: &v1alpha1.ApplicationSource{
@@ -722,15 +740,17 @@ func Test_UpdateApplication(t *testing.T) {
722740
kubeClient := kube.KubernetesClient{
723741
Clientset: fake.NewFakeKubeClient(),
724742
}
743+
annotations := map[string]string{
744+
common.ImageUpdaterAnnotation: "dummy=jannfis/foobar",
745+
fmt.Sprintf(common.AllowTagsOptionAnnotation, "dummy"): "regexp:^foobar$",
746+
fmt.Sprintf(common.UpdateStrategyAnnotation, "dummy"): "name",
747+
}
725748
appImages := &ApplicationImages{
726749
Application: v1alpha1.Application{
727750
ObjectMeta: v1.ObjectMeta{
728-
Name: "guestbook",
729-
Namespace: "guestbook",
730-
Annotations: map[string]string{
731-
fmt.Sprintf(common.AllowTagsOptionAnnotation, "dummy"): "regexp:^foobar$",
732-
fmt.Sprintf(common.UpdateStrategyAnnotation, "dummy"): "name",
733-
},
751+
Name: "guestbook",
752+
Namespace: "guestbook",
753+
Annotations: annotations,
734754
},
735755
Spec: v1alpha1.ApplicationSpec{
736756
Source: &v1alpha1.ApplicationSource{
@@ -798,15 +818,17 @@ func Test_UpdateApplication(t *testing.T) {
798818
kubeClient := kube.KubernetesClient{
799819
Clientset: fake.NewFakeKubeClient(),
800820
}
821+
annotations := map[string]string{
822+
common.ImageUpdaterAnnotation: "dummy=jannfis/foobar",
823+
fmt.Sprintf(common.IgnoreTagsOptionAnnotation, "dummy"): "*",
824+
fmt.Sprintf(common.UpdateStrategyAnnotation, "dummy"): "name",
825+
}
801826
appImages := &ApplicationImages{
802827
Application: v1alpha1.Application{
803828
ObjectMeta: v1.ObjectMeta{
804-
Name: "guestbook",
805-
Namespace: "guestbook",
806-
Annotations: map[string]string{
807-
fmt.Sprintf(common.IgnoreTagsOptionAnnotation, "dummy"): "*",
808-
fmt.Sprintf(common.UpdateStrategyAnnotation, "dummy"): "name",
809-
},
829+
Name: "guestbook",
830+
Namespace: "guestbook",
831+
Annotations: annotations,
810832
},
811833
Spec: v1alpha1.ApplicationSpec{
812834
Source: &v1alpha1.ApplicationSource{
@@ -858,11 +880,15 @@ func Test_UpdateApplication(t *testing.T) {
858880
kubeClient := kube.KubernetesClient{
859881
Clientset: fake.NewFakeKubeClient(),
860882
}
883+
annotations := map[string]string{
884+
common.ImageUpdaterAnnotation: "example.io/jannfis/example:1.0.x",
885+
}
861886
appImages := &ApplicationImages{
862887
Application: v1alpha1.Application{
863888
ObjectMeta: v1.ObjectMeta{
864-
Name: "guestbook",
865-
Namespace: "guestbook",
889+
Name: "guestbook",
890+
Namespace: "guestbook",
891+
Annotations: annotations,
866892
},
867893
Spec: v1alpha1.ApplicationSpec{
868894
Source: &v1alpha1.ApplicationSource{
@@ -911,11 +937,15 @@ func Test_UpdateApplication(t *testing.T) {
911937
kubeClient := kube.KubernetesClient{
912938
Clientset: fake.NewFakeKubeClient(),
913939
}
940+
annotations := map[string]string{
941+
common.ImageUpdaterAnnotation: "jannfis/foobar:1.0.0",
942+
}
914943
appImages := &ApplicationImages{
915944
Application: v1alpha1.Application{
916945
ObjectMeta: v1.ObjectMeta{
917-
Name: "guestbook",
918-
Namespace: "guestbook",
946+
Name: "guestbook",
947+
Namespace: "guestbook",
948+
Annotations: annotations,
919949
},
920950
Spec: v1alpha1.ApplicationSpec{
921951
Source: &v1alpha1.ApplicationSource{
@@ -967,11 +997,15 @@ func Test_UpdateApplication(t *testing.T) {
967997
kubeClient := kube.KubernetesClient{
968998
Clientset: fake.NewFakeKubeClient(),
969999
}
1000+
annotations := map[string]string{
1001+
common.ImageUpdaterAnnotation: "jannfis/foobar:1.0.0",
1002+
}
9701003
appImages := &ApplicationImages{
9711004
Application: v1alpha1.Application{
9721005
ObjectMeta: v1.ObjectMeta{
973-
Name: "guestbook",
974-
Namespace: "guestbook",
1006+
Name: "guestbook",
1007+
Namespace: "guestbook",
1008+
Annotations: annotations,
9751009
},
9761010
Spec: v1alpha1.ApplicationSpec{
9771011
Source: &v1alpha1.ApplicationSource{
@@ -1023,11 +1057,15 @@ func Test_UpdateApplication(t *testing.T) {
10231057
kubeClient := kube.KubernetesClient{
10241058
Clientset: fake.NewFakeKubeClient(),
10251059
}
1060+
annotations := map[string]string{
1061+
common.ImageUpdaterAnnotation: "jannfis/foobar:stable",
1062+
}
10261063
appImages := &ApplicationImages{
10271064
Application: v1alpha1.Application{
10281065
ObjectMeta: v1.ObjectMeta{
1029-
Name: "guestbook",
1030-
Namespace: "guestbook",
1066+
Name: "guestbook",
1067+
Namespace: "guestbook",
1068+
Annotations: annotations,
10311069
},
10321070
Spec: v1alpha1.ApplicationSpec{
10331071
Source: &v1alpha1.ApplicationSource{

0 commit comments

Comments
 (0)