Skip to content

Commit 54cd904

Browse files
authored
fix: Get tags from correct registry/image with a different kustomize image (#253)
* fix: Get tags from correct registry/image with a different kustomize image * Fix logs and update image * Fix tags * More tests * More tests
1 parent 0dbd6cd commit 54cd904

File tree

2 files changed

+130
-4
lines changed

2 files changed

+130
-4
lines changed

pkg/argocd/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
177177

178178
imgCtx.Debugf("Considering this image for update")
179179

180-
rep, err := registry.GetRegistryEndpoint(updateableImage.RegistryURL)
180+
rep, err := registry.GetRegistryEndpoint(applicationImage.RegistryURL)
181181
if err != nil {
182182
imgCtx.Errorf("Could not get registry endpoint from configuration: %v", err)
183183
result.NumErrors += 1
@@ -263,7 +263,7 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
263263

264264
if needsUpdate(updateableImage, applicationImage, latest) {
265265

266-
imgCtx.Infof("Setting new image to %s", updateableImage.WithTag(latest).String())
266+
imgCtx.Infof("Setting new image to %s", applicationImage.WithTag(latest).GetFullNameWithTag())
267267
needUpdate = true
268268

269269
err = setAppImage(&updateConf.UpdateApp.Application, applicationImage.WithTag(latest))
@@ -273,7 +273,7 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
273273
result.NumErrors += 1
274274
continue
275275
} else {
276-
containerImageNew := updateableImage.WithTag(latest)
276+
containerImageNew := applicationImage.WithTag(latest)
277277
imgCtx.Infof("Successfully updated image '%s' to '%s', but pending spec update (dry run=%v)", updateableImage.GetFullNameWithTag(), containerImageNew.GetFullNameWithTag(), updateConf.DryRun)
278278
changeList = append(changeList, ChangeEntry{containerImageNew, updateableImage.ImageTag, containerImageNew.ImageTag})
279279
}

pkg/argocd/update_test.go

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func Test_UpdateApplication(t *testing.T) {
3333
t.Run("Test successful update", func(t *testing.T) {
3434
mockClientFn := func(endpoint *registry.RegistryEndpoint, username, password string) (registry.RegistryClient, error) {
3535
regMock := regmock.RegistryClient{}
36-
regMock.On("Tags", mock.Anything).Return([]string{"1.0.1"}, nil)
36+
regMock.On("Tags", mock.MatchedBy(func(s string) bool {
37+
return s == "jannfis/foobar"
38+
})).Return([]string{"1.0.1"}, nil)
3739
return &regMock, nil
3840
}
3941

@@ -85,6 +87,130 @@ func Test_UpdateApplication(t *testing.T) {
8587
assert.Equal(t, 1, res.NumImagesUpdated)
8688
})
8789

90+
t.Run("Test kustomize w/ different registry", func(t *testing.T) {
91+
mockClientFn := func(endpoint *registry.RegistryEndpoint, username, password string) (registry.RegistryClient, error) {
92+
regMock := regmock.RegistryClient{}
93+
assert.Equal(t, endpoint.RegistryPrefix, "quay.io")
94+
regMock.On("Tags", mock.Anything).Return([]string{"1.0.1"}, nil)
95+
return &regMock, nil
96+
}
97+
98+
argoClient := argomock.ArgoCD{}
99+
argoClient.On("UpdateSpec", mock.Anything, mock.Anything).Return(nil, nil)
100+
101+
kubeClient := kube.KubernetesClient{
102+
Clientset: fake.NewFakeKubeClient(),
103+
}
104+
appImages := &ApplicationImages{
105+
Application: v1alpha1.Application{
106+
ObjectMeta: v1.ObjectMeta{
107+
Name: "guestbook",
108+
Namespace: "guestbook",
109+
Annotations: map[string]string{
110+
"argocd-image-updater.argoproj.io/image-list": "foobar=quay.io/jannfis/foobar:~1.0.0",
111+
"argocd-image-updater.argoproj.io/foobar.kustomize.image-name": "jannfis/foobar",
112+
"argocd-image-updater.argoproj.io/foobar.force-update": "true",
113+
},
114+
},
115+
Spec: v1alpha1.ApplicationSpec{
116+
Source: v1alpha1.ApplicationSource{
117+
Kustomize: &v1alpha1.ApplicationSourceKustomize{
118+
Images: v1alpha1.KustomizeImages{
119+
"jannfis/foobar:1.0.0",
120+
},
121+
},
122+
},
123+
},
124+
Status: v1alpha1.ApplicationStatus{
125+
SourceType: v1alpha1.ApplicationSourceTypeKustomize,
126+
Summary: v1alpha1.ApplicationSummary{
127+
Images: []string{
128+
"jannfis/foobar:1.0.0",
129+
},
130+
},
131+
},
132+
},
133+
Images: image.ContainerImageList{
134+
image.NewFromIdentifier("quay.io/jannfis/foobar"),
135+
},
136+
}
137+
res := UpdateApplication(&UpdateConfiguration{
138+
NewRegFN: mockClientFn,
139+
ArgoClient: &argoClient,
140+
KubeClient: &kubeClient,
141+
UpdateApp: appImages,
142+
DryRun: false,
143+
}, NewSyncIterationState())
144+
assert.Equal(t, 0, res.NumErrors)
145+
assert.Equal(t, 0, res.NumSkipped)
146+
assert.Equal(t, 1, res.NumApplicationsProcessed)
147+
assert.Equal(t, 1, res.NumImagesConsidered)
148+
assert.Equal(t, 1, res.NumImagesUpdated)
149+
})
150+
151+
t.Run("Test kustomize w/ different registry and org", func(t *testing.T) {
152+
mockClientFn := func(endpoint *registry.RegistryEndpoint, username, password string) (registry.RegistryClient, error) {
153+
regMock := regmock.RegistryClient{}
154+
assert.Equal(t, endpoint.RegistryPrefix, "quay.io")
155+
regMock.On("Tags", mock.MatchedBy(func(s string) bool {
156+
return s == "someorg/foobar"
157+
})).Return([]string{"1.0.1"}, nil)
158+
return &regMock, nil
159+
}
160+
161+
argoClient := argomock.ArgoCD{}
162+
argoClient.On("UpdateSpec", mock.Anything, mock.Anything).Return(nil, nil)
163+
164+
kubeClient := kube.KubernetesClient{
165+
Clientset: fake.NewFakeKubeClient(),
166+
}
167+
appImages := &ApplicationImages{
168+
Application: v1alpha1.Application{
169+
ObjectMeta: v1.ObjectMeta{
170+
Name: "guestbook",
171+
Namespace: "guestbook",
172+
Annotations: map[string]string{
173+
"argocd-image-updater.argoproj.io/image-list": "foobar=quay.io/someorg/foobar:~1.0.0",
174+
"argocd-image-updater.argoproj.io/foobar.kustomize.image-name": "jannfis/foobar",
175+
"argocd-image-updater.argoproj.io/foobar.force-update": "true",
176+
},
177+
},
178+
Spec: v1alpha1.ApplicationSpec{
179+
Source: v1alpha1.ApplicationSource{
180+
Kustomize: &v1alpha1.ApplicationSourceKustomize{
181+
Images: v1alpha1.KustomizeImages{
182+
"jannfis/foobar:1.0.0",
183+
},
184+
},
185+
},
186+
},
187+
Status: v1alpha1.ApplicationStatus{
188+
SourceType: v1alpha1.ApplicationSourceTypeKustomize,
189+
Summary: v1alpha1.ApplicationSummary{
190+
Images: []string{
191+
"jannfis/foobar:1.0.0",
192+
},
193+
},
194+
},
195+
},
196+
Images: image.ContainerImageList{
197+
image.NewFromIdentifier("quay.io/someorg/foobar"),
198+
},
199+
}
200+
res := UpdateApplication(&UpdateConfiguration{
201+
NewRegFN: mockClientFn,
202+
ArgoClient: &argoClient,
203+
KubeClient: &kubeClient,
204+
UpdateApp: appImages,
205+
DryRun: false,
206+
}, NewSyncIterationState())
207+
assert.Equal(t, 0, res.NumErrors)
208+
assert.Equal(t, 0, res.NumSkipped)
209+
assert.Equal(t, 1, res.NumApplicationsProcessed)
210+
assert.Equal(t, 1, res.NumImagesConsidered)
211+
assert.Equal(t, 1, res.NumImagesUpdated)
212+
})
213+
88214
t.Run("Test successful update when no tag is set in running workload", func(t *testing.T) {
89215
mockClientFn := func(endpoint *registry.RegistryEndpoint, username, password string) (registry.RegistryClient, error) {
90216
regMock := regmock.RegistryClient{}

0 commit comments

Comments
 (0)