Skip to content

Commit 81250b7

Browse files
committed
refactor: move git write-back source logic to git.go && remove e2e tests && enhance unit tests
Signed-off-by: Atif Ali <atali@redhat.com>
1 parent d655237 commit 81250b7

File tree

7 files changed

+113
-228
lines changed

7 files changed

+113
-228
lines changed

pkg/argocd/argocd.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -675,34 +675,17 @@ func getApplicationSourceType(app *v1alpha1.Application) v1alpha1.ApplicationSou
675675
return app.Status.SourceType
676676
}
677677

678-
// getApplicationSource returns the source matching git-repository annotation
679-
// or the Helm/Kustomize source, or the primary source if no match is found
678+
// getApplicationSource returns the main source of a Helm or Kustomize type of the application
680679
func getApplicationSource(app *v1alpha1.Application) *v1alpha1.ApplicationSource {
681-
// Get the git repository from annotations for write-back
682-
gitRepo := app.Annotations["argocd-image-updater.argoproj.io/git-repository"]
683-
logCtx := log.WithContext().AddField("application", app.GetName())
684680

685681
if app.Spec.HasMultipleSources() {
686-
// If we have a specific git repo configured for write-back
687-
if gitRepo != "" {
688-
// Try to find the source matching the write-back git repository
689-
for _, s := range app.Spec.Sources {
690-
if s.RepoURL == gitRepo {
691-
logCtx.Debugf("Found matching source for git repository %s", gitRepo)
692-
return &s
693-
}
694-
}
695-
logCtx.Debugf("No matching source found for git repository %s", gitRepo)
696-
}
697-
698-
// If no git repo match, check for Helm/Kustomize sources
699682
for _, s := range app.Spec.Sources {
700683
if s.Helm != nil || s.Kustomize != nil {
701684
return &s
702685
}
703686
}
704687

705-
logCtx.Tracef("Could not get Source of type Helm or Kustomize from multisource configuration. Returning first source from the list")
688+
log.WithContext().AddField("application", app.GetName()).Tracef("Could not get Source of type Helm or Kustomize from multisource configuration. Returning first source from the list")
706689
return &app.Spec.Sources[0]
707690
}
708691

pkg/argocd/git.go

Lines changed: 32 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,36 @@ 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+
// First check if branch is explicitly set in annotations
137+
annotations := app.GetAnnotations()
138+
if branch, ok := annotations[common.GitBranchAnnotation]; ok {
139+
return branch
140+
}
141+
142+
logCtx := log.WithContext().AddField("application", app.GetName())
143+
144+
// If git repository is specified, find matching source
145+
if gitRepo, ok := annotations[common.GitRepositoryAnnotation]; ok {
146+
if app.Spec.HasMultipleSources() {
147+
for _, s := range app.Spec.Sources {
148+
if s.RepoURL == gitRepo {
149+
logCtx.Debugf("Found matching source for git repository %s", gitRepo)
150+
return s.TargetRevision
151+
}
152+
}
153+
logCtx.Debugf("No matching source found for git repository %s, falling back to getApplicationSource", gitRepo)
154+
}
155+
}
156+
157+
// Fall back to getApplicationSource's targetRevision
158+
// This maintains consistency with how other parts of the code select the source
159+
return getApplicationSource(app).TargetRevision
160+
}
161+
131162
// commitChanges commits any changes required for updating one or more images
132163
// after the UpdateApplication cycle has finished.
133164
func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeList []ChangeEntry, write changeWriter) error {
@@ -164,7 +195,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
164195
// config, or taken from the application spec's targetRevision. If the
165196
// target revision is set to the special value HEAD, or is the empty
166197
// string, we'll try to resolve it to a branch name.
167-
checkOutBranch := getApplicationSource(app).TargetRevision
198+
checkOutBranch := getWriteBackBranch(app)
168199
if wbc.GitBranch != "" {
169200
checkOutBranch = wbc.GitBranch
170201
}

pkg/argocd/git_test.go

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,67 @@ func Test_updateKustomizeFile(t *testing.T) {
333333
}
334334

335335
func Test_getApplicationSource(t *testing.T) {
336-
t.Run("multi-source with matching git repo", func(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("explicit branch in annotations", func(t *testing.T) {
383+
app := &v1alpha1.Application{
384+
ObjectMeta: v1.ObjectMeta{
385+
Name: "test-app",
386+
Annotations: map[string]string{
387+
"argocd-image-updater.argoproj.io/git-branch": "custom-branch",
388+
},
389+
},
390+
}
391+
392+
branch := getWriteBackBranch(app)
393+
assert.Equal(t, "custom-branch", branch)
394+
})
395+
396+
t.Run("matching git-repository annotation", func(t *testing.T) {
337397
app := &v1alpha1.Application{
338398
ObjectMeta: v1.ObjectMeta{
339399
Name: "test-app",
@@ -356,12 +416,11 @@ func Test_getApplicationSource(t *testing.T) {
356416
},
357417
}
358418

359-
source := getApplicationSource(app)
360-
assert.Equal(t, "main", source.TargetRevision)
361-
assert.Equal(t, "https://github.com/chengfang/image-updater-examples.git", source.RepoURL)
419+
branch := getWriteBackBranch(app)
420+
assert.Equal(t, "main", branch)
362421
})
363422

364-
t.Run("multi-source without git repo annotation", func(t *testing.T) {
423+
t.Run("fallback to primary source when no match", func(t *testing.T) {
365424
app := &v1alpha1.Application{
366425
ObjectMeta: v1.ObjectMeta{
367426
Name: "test-app",
@@ -382,26 +441,31 @@ func Test_getApplicationSource(t *testing.T) {
382441
},
383442
}
384443

385-
source := getApplicationSource(app)
386-
assert.Equal(t, "18.2.3", source.TargetRevision)
387-
assert.Equal(t, "https://charts.bitnami.com/bitnami", source.RepoURL)
444+
branch := getWriteBackBranch(app)
445+
assert.Equal(t, "18.2.3", branch)
388446
})
389447

390-
t.Run("single source application", func(t *testing.T) {
448+
t.Run("git-repository annotation with non-matching URL", func(t *testing.T) {
391449
app := &v1alpha1.Application{
392450
ObjectMeta: v1.ObjectMeta{
393451
Name: "test-app",
452+
Annotations: map[string]string{
453+
"argocd-image-updater.argoproj.io/git-repository": "https://github.com/different/repo.git",
454+
},
394455
},
395456
Spec: v1alpha1.ApplicationSpec{
396-
Source: &v1alpha1.ApplicationSource{
397-
RepoURL: "https://github.com/example/repo.git",
398-
TargetRevision: "main",
457+
Sources: v1alpha1.ApplicationSources{
458+
{
459+
RepoURL: "https://charts.bitnami.com/bitnami",
460+
TargetRevision: "18.2.3",
461+
Chart: "nginx",
462+
Helm: &v1alpha1.ApplicationSourceHelm{},
463+
},
399464
},
400465
},
401466
}
402467

403-
source := getApplicationSource(app)
404-
assert.Equal(t, "main", source.TargetRevision)
405-
assert.Equal(t, "https://github.com/example/repo.git", source.RepoURL)
468+
branch := getWriteBackBranch(app)
469+
assert.Equal(t, "18.2.3", branch)
406470
})
407471
}

test/e2e/suite/104-multi-source-git-repository-matching/01-install.yaml

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/e2e/suite/104-multi-source-git-repository-matching/02-verify-branch.yaml

Lines changed: 0 additions & 36 deletions
This file was deleted.

test/e2e/suite/104-multi-source-git-repository-matching/99-delete.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)