From 2a9b09d72a4d7e9644aec604f198ca60b4f67588 Mon Sep 17 00:00:00 2001 From: jikwan Date: Tue, 21 Oct 2025 20:42:46 +0900 Subject: [PATCH] feat: preserve original tag in annotations when using digest strategy --- pkg/argocd/argocd.go | 9 +++++++++ pkg/argocd/argocd_test.go | 40 +++++++++++++++++++++++++++++++++++++++ pkg/argocd/update.go | 9 +++++++++ 3 files changed, 58 insertions(+) diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index 629d83a7..8318e1c4 100644 --- a/pkg/argocd/argocd.go +++ b/pkg/argocd/argocd.go @@ -522,6 +522,15 @@ func SetHelmImage(app *v1alpha1.Application, newImage *image.ContainerImage) err return nil } +// recordOriginalTag saves the original tag as an annotation (digest strategy only) +func RecordOriginalTag(app *v1alpha1.Application, imageName, originalTag string) { + if app.Annotations == nil { + app.Annotations = make(map[string]string) + } + key := fmt.Sprintf("argocd-image-updater.argoproj.io/original-tag.%s", imageName) + app.Annotations[key] = originalTag +} + // GetKustomizeImage gets the image set in Application source matching new image // or an empty string if match is not found func GetKustomizeImage(app *v1alpha1.Application, newImage *image.ContainerImage) (string, error) { diff --git a/pkg/argocd/argocd_test.go b/pkg/argocd/argocd_test.go index 6caf4298..07b24e26 100644 --- a/pkg/argocd/argocd_test.go +++ b/pkg/argocd/argocd_test.go @@ -1235,3 +1235,43 @@ func Test_parseImageList(t *testing.T) { assert.Equal(t, "baz", imgs[0].KustomizeImage.ImageName) }) } + +func Test_RecordOriginalTag(t *testing.T) { + tests := []struct { + name string + app *v1alpha1.Application + imageName string + originalTag string + }{ + { + name: "when annotations already exist", + app: &v1alpha1.Application{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{}, + }, + }, + imageName: "nginx", + originalTag: "1.14.2", + }, + { + name: "when annotations is nil", + app: &v1alpha1.Application{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: nil, + }, + }, + imageName: "redis", + originalTag: "6.0", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + RecordOriginalTag(tt.app, tt.imageName, tt.originalTag) + + key := fmt.Sprintf("argocd-image-updater.argoproj.io/original-tag.%s", tt.imageName) + require.NotNil(t, tt.app.Annotations) + require.Equal(t, tt.originalTag, tt.app.Annotations[key]) + }) + } +} \ No newline at end of file diff --git a/pkg/argocd/update.go b/pkg/argocd/update.go index 65c79c05..b061310a 100644 --- a/pkg/argocd/update.go +++ b/pkg/argocd/update.go @@ -288,6 +288,15 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat } if needsUpdate(updateableImage, applicationImage, latest, vc.Strategy) { + + if vc.Strategy == image.StrategyDigest && applicationImage.ImageTag != nil { + RecordOriginalTag( + &updateConf.UpdateApp.Application, + applicationImage.ImageName, + applicationImage.ImageTag.TagName, + ) + } + appImageWithTag := applicationImage.WithTag(latest) appImageFullNameWithTag := appImageWithTag.GetFullNameWithTag()