Skip to content

Commit 205fe73

Browse files
authored
fix: use correct default branch in multi-source applications (#1056)
Signed-off-by: Atif Ali <atali@redhat.com>
1 parent c0ccf02 commit 205fe73

File tree

2 files changed

+162
-1
lines changed

2 files changed

+162
-1
lines changed

pkg/argocd/git.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"sigs.k8s.io/kustomize/kyaml/order"
1818
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
1919

20+
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
2021
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/image"
2122

2223
"github.com/argoproj-labs/argocd-image-updater/ext/git"
@@ -128,6 +129,33 @@ func TemplateBranchName(branchName string, changeList []ChangeEntry) string {
128129

129130
type changeWriter func(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Client) (err error, skip bool)
130131

132+
// getWriteBackBranch returns the branch to use for write-back operations.
133+
// It first checks for a branch specified in annotations, then uses the
134+
// targetRevision from the matching git source, falling back to getApplicationSource.
135+
func getWriteBackBranch(app *v1alpha1.Application) string {
136+
if app == nil {
137+
return ""
138+
}
139+
// If git repository is specified, find matching source
140+
if gitRepo, ok := app.GetAnnotations()[common.GitRepositoryAnnotation]; ok {
141+
if app.Spec.HasMultipleSources() {
142+
for _, s := range app.Spec.Sources {
143+
if s.RepoURL == gitRepo {
144+
log.WithContext().AddField("application", app.GetName()).
145+
Debugf("Using target revision '%s' from matching source '%s'", s.TargetRevision, gitRepo)
146+
return s.TargetRevision
147+
}
148+
}
149+
log.WithContext().AddField("application", app.GetName()).
150+
Debugf("No matching source found for git repository %s, falling back to primary source", gitRepo)
151+
}
152+
}
153+
154+
// Fall back to getApplicationSource's targetRevision
155+
// This maintains consistency with how other parts of the code select the source
156+
return getApplicationSource(app).TargetRevision
157+
}
158+
131159
// commitChanges commits any changes required for updating one or more images
132160
// after the UpdateApplication cycle has finished.
133161
func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeList []ChangeEntry, write changeWriter) error {
@@ -164,9 +192,11 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
164192
// config, or taken from the application spec's targetRevision. If the
165193
// target revision is set to the special value HEAD, or is the empty
166194
// string, we'll try to resolve it to a branch name.
167-
checkOutBranch := getApplicationSource(app).TargetRevision
195+
var checkOutBranch string
168196
if wbc.GitBranch != "" {
169197
checkOutBranch = wbc.GitBranch
198+
} else {
199+
checkOutBranch = getWriteBackBranch(app)
170200
}
171201
logCtx.Tracef("targetRevision for update is '%s'", checkOutBranch)
172202
if checkOutBranch == "" || checkOutBranch == "HEAD" {

pkg/argocd/git_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/image"
1111
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag"
1212

13+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
1315
"sigs.k8s.io/kustomize/api/types"
1416
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
1517

@@ -329,3 +331,132 @@ func Test_updateKustomizeFile(t *testing.T) {
329331
})
330332
}
331333
}
334+
335+
func Test_getApplicationSource(t *testing.T) {
336+
t.Run("multi-source without git repo annotation", func(t *testing.T) {
337+
app := &v1alpha1.Application{
338+
ObjectMeta: v1.ObjectMeta{
339+
Name: "test-app",
340+
},
341+
Spec: v1alpha1.ApplicationSpec{
342+
Sources: v1alpha1.ApplicationSources{
343+
{
344+
RepoURL: "https://charts.bitnami.com/bitnami",
345+
TargetRevision: "18.2.3",
346+
Chart: "nginx",
347+
Helm: &v1alpha1.ApplicationSourceHelm{},
348+
},
349+
{
350+
RepoURL: "https://github.com/chengfang/image-updater-examples.git",
351+
TargetRevision: "main",
352+
},
353+
},
354+
},
355+
}
356+
357+
source := getApplicationSource(app)
358+
assert.Equal(t, "18.2.3", source.TargetRevision)
359+
assert.Equal(t, "https://charts.bitnami.com/bitnami", source.RepoURL)
360+
})
361+
362+
t.Run("single source application", func(t *testing.T) {
363+
app := &v1alpha1.Application{
364+
ObjectMeta: v1.ObjectMeta{
365+
Name: "test-app",
366+
},
367+
Spec: v1alpha1.ApplicationSpec{
368+
Source: &v1alpha1.ApplicationSource{
369+
RepoURL: "https://github.com/example/repo.git",
370+
TargetRevision: "main",
371+
},
372+
},
373+
}
374+
375+
source := getApplicationSource(app)
376+
assert.Equal(t, "main", source.TargetRevision)
377+
assert.Equal(t, "https://github.com/example/repo.git", source.RepoURL)
378+
})
379+
}
380+
381+
func Test_getWriteBackBranch(t *testing.T) {
382+
t.Run("nil application", func(t *testing.T) {
383+
branch := getWriteBackBranch(nil)
384+
assert.Equal(t, "", branch)
385+
})
386+
387+
t.Run("matching git-repository annotation", func(t *testing.T) {
388+
app := &v1alpha1.Application{
389+
ObjectMeta: v1.ObjectMeta{
390+
Name: "test-app",
391+
Annotations: map[string]string{
392+
"argocd-image-updater.argoproj.io/git-repository": "https://github.com/chengfang/image-updater-examples.git",
393+
},
394+
},
395+
Spec: v1alpha1.ApplicationSpec{
396+
Sources: v1alpha1.ApplicationSources{
397+
{
398+
RepoURL: "https://charts.bitnami.com/bitnami",
399+
TargetRevision: "18.2.3",
400+
Chart: "nginx",
401+
},
402+
{
403+
RepoURL: "https://github.com/chengfang/image-updater-examples.git",
404+
TargetRevision: "main",
405+
},
406+
},
407+
},
408+
}
409+
410+
branch := getWriteBackBranch(app)
411+
assert.Equal(t, "main", branch)
412+
})
413+
414+
t.Run("fallback to primary source when no match", func(t *testing.T) {
415+
app := &v1alpha1.Application{
416+
ObjectMeta: v1.ObjectMeta{
417+
Name: "test-app",
418+
},
419+
Spec: v1alpha1.ApplicationSpec{
420+
Sources: v1alpha1.ApplicationSources{
421+
{
422+
RepoURL: "https://charts.bitnami.com/bitnami",
423+
TargetRevision: "18.2.3",
424+
Chart: "nginx",
425+
Helm: &v1alpha1.ApplicationSourceHelm{},
426+
},
427+
{
428+
RepoURL: "https://github.com/chengfang/image-updater-examples.git",
429+
TargetRevision: "main",
430+
},
431+
},
432+
},
433+
}
434+
435+
branch := getWriteBackBranch(app)
436+
assert.Equal(t, "18.2.3", branch)
437+
})
438+
439+
t.Run("git-repository annotation with non-matching URL", func(t *testing.T) {
440+
app := &v1alpha1.Application{
441+
ObjectMeta: v1.ObjectMeta{
442+
Name: "test-app",
443+
Annotations: map[string]string{
444+
"argocd-image-updater.argoproj.io/git-repository": "https://github.com/different/repo.git",
445+
},
446+
},
447+
Spec: v1alpha1.ApplicationSpec{
448+
Sources: v1alpha1.ApplicationSources{
449+
{
450+
RepoURL: "https://charts.bitnami.com/bitnami",
451+
TargetRevision: "18.2.3",
452+
Chart: "nginx",
453+
Helm: &v1alpha1.ApplicationSourceHelm{},
454+
},
455+
},
456+
},
457+
}
458+
459+
branch := getWriteBackBranch(app)
460+
assert.Equal(t, "18.2.3", branch)
461+
})
462+
}

0 commit comments

Comments
 (0)