-
Notifications
You must be signed in to change notification settings - Fork 311
fix: use correct default branch in multi-source applications #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chengfang
merged 4 commits into
argoproj-labs:master
from
aali309:Wrong-default-write-branch
Mar 11, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
"sigs.k8s.io/kustomize/kyaml/order" | ||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml" | ||
|
||
"github.com/argoproj-labs/argocd-image-updater/pkg/common" | ||
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/image" | ||
|
||
"github.com/argoproj-labs/argocd-image-updater/ext/git" | ||
|
@@ -128,6 +129,33 @@ func TemplateBranchName(branchName string, changeList []ChangeEntry) string { | |
|
||
type changeWriter func(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Client) (err error, skip bool) | ||
|
||
// getWriteBackBranch returns the branch to use for write-back operations. | ||
// It first checks for a branch specified in annotations, then uses the | ||
// targetRevision from the matching git source, falling back to getApplicationSource. | ||
func getWriteBackBranch(app *v1alpha1.Application) string { | ||
if app == nil { | ||
return "" | ||
} | ||
// If git repository is specified, find matching source | ||
if gitRepo, ok := app.GetAnnotations()[common.GitRepositoryAnnotation]; ok { | ||
if app.Spec.HasMultipleSources() { | ||
for _, s := range app.Spec.Sources { | ||
if s.RepoURL == gitRepo { | ||
log.WithContext().AddField("application", app.GetName()). | ||
Debugf("Using target revision '%s' from matching source '%s'", s.TargetRevision, gitRepo) | ||
return s.TargetRevision | ||
} | ||
} | ||
log.WithContext().AddField("application", app.GetName()). | ||
Debugf("No matching source found for git repository %s, falling back to primary source", gitRepo) | ||
} | ||
} | ||
|
||
// Fall back to getApplicationSource's targetRevision | ||
// This maintains consistency with how other parts of the code select the source | ||
return getApplicationSource(app).TargetRevision | ||
} | ||
|
||
// commitChanges commits any changes required for updating one or more images | ||
// after the UpdateApplication cycle has finished. | ||
func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeList []ChangeEntry, write changeWriter) error { | ||
|
@@ -164,9 +192,11 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis | |
// config, or taken from the application spec's targetRevision. If the | ||
// target revision is set to the special value HEAD, or is the empty | ||
// string, we'll try to resolve it to a branch name. | ||
checkOutBranch := getApplicationSource(app).TargetRevision | ||
var checkOutBranch string | ||
if wbc.GitBranch != "" { | ||
checkOutBranch = wbc.GitBranch | ||
} else { | ||
checkOutBranch = getWriteBackBranch(app) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the gitbranch is set, we always take it. So I think we only need to call the func |
||
logCtx.Tracef("targetRevision for update is '%s'", checkOutBranch) | ||
if checkOutBranch == "" || checkOutBranch == "HEAD" { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we cannot take the annotation value as is, since it can be a composite form like "main:changes". See https://argocd-image-updater.readthedocs.io/en/latest/basics/update-methods/#specifying-a-branch-to-commit-to
I guess we dont' see the above block. Line 200 already handles this case. See next comment.