Skip to content

Commit

Permalink
fix: #896 App of apps being overwritten by image-updater [release-0.1…
Browse files Browse the repository at this point in the history
…5] (#920)

Signed-off-by: Pasha Kostohrys <[email protected]>
Signed-off-by: Cheng Fang <[email protected]>
Co-authored-by: pasha-codefresh <[email protected]>
  • Loading branch information
chengfang and pasha-codefresh authored Nov 8, 2024
1 parent e9c78e0 commit c7183f0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create-release-draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
run: |
set -ex
docker login --username "${DOCKER_USERNAME}" --password "${DOCKER_PASSWORD}" quay.io
IMAGE_PUSH=yes make multiarch-image
IMAGE_PUSH=yes IMAGE_TAG=v${{ steps.version.outputs.version }} make multiarch-image
env:
DOCKER_USERNAME: ${{ secrets.QUAY_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.QUAY_TOKEN }}
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ release-binaries:
BINNAME=argocd-image-updater-darwin_amd64 OUTDIR=dist/release OS=darwin ARCH=amd64 make controller
BINNAME=argocd-image-updater-darwin_arm64 OUTDIR=dist/release OS=darwin ARCH=arm64 make controller
BINNAME=argocd-image-updater-win64.exe OUTDIR=dist/release OS=windows ARCH=amd64 make controller
rm -f dist/release/release-v${VERSION}.sha256 dist/release/release-v${VERSION}.sha256.asc
for bin in dist/release/argocd-image-updater-*; do sha256sum "$$bin" >> dist/release/release-v${VERSION}.sha256; done
gpg -a --detach-sign dist/release/release-v${VERSION}.sha256
gpg -a --verify dist/release/release-v${VERSION}.sha256.asc

.PHONY: extract-binary
extract-binary:
Expand Down
2 changes: 1 addition & 1 deletion docs/contributing/releasing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Then create a release branch:

```
git clone [email protected]:argoproj-labs/argocd-image-updater.git
git branch -b release-0.13
git checkout -b release-0.13
git push origin release-0.13
```

Expand Down
41 changes: 14 additions & 27 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,20 @@ type k8sClient struct {

// GetApplication retrieves an application by name across all namespaces.
func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v1alpha1.Application, error) {
log.Debugf("Getting application %s across all namespaces", appName)

// List all applications across all namespaces (using empty labelSelector)
appList, err := client.ListApplications(v1.NamespaceAll)
if err != nil {
return nil, fmt.Errorf("error listing applications: %w", err)
}

// Filter applications by name using nameMatchesPattern
app, err := findApplicationByName(appList, appName)
if err != nil {
log.Errorf("error getting application: %v", err)
return nil, fmt.Errorf("error getting application: %w", err)
}

// Retrieve the application in the specified namespace
return app, nil
}

// ListApplications lists all applications across all namespaces.
func (client *k8sClient) ListApplications(labelSelector string) ([]v1alpha1.Application, error) {
list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, fmt.Errorf("error listing applications: %w", err)
}
log.Debugf("Applications listed: %d", len(list.Items))
return list.Items, nil
}

// findApplicationByName filters the list of applications by name using nameMatchesPattern.
func findApplicationByName(appList []v1alpha1.Application, appName string) (*v1alpha1.Application, error) {
var matchedApps []*v1alpha1.Application
var matchedApps []v1alpha1.Application

for _, app := range appList {
log.Debugf("Found application: %s in namespace %s", app.Name, app.Namespace)
if nameMatchesPattern(app.Name, []string{appName}) {
log.Debugf("Application %s matches the pattern", app.Name)
matchedApps = append(matchedApps, &app)
matchedApps = append(matchedApps, app)
}
}

Expand All @@ -78,7 +54,18 @@ func findApplicationByName(appList []v1alpha1.Application, appName string) (*v1a
return nil, fmt.Errorf("multiple applications found matching %s", appName)
}

return matchedApps[0], nil
// Retrieve the application in the specified namespace
return &matchedApps[0], nil
}

// ListApplications lists all applications across all namespaces.
func (client *k8sClient) ListApplications(labelSelector string) ([]v1alpha1.Application, error) {
list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, fmt.Errorf("error listing applications: %w", err)
}
log.Debugf("Applications listed: %d", len(list.Items))
return list.Items, nil
}

func (client *k8sClient) UpdateSpec(ctx context.Context, spec *application.ApplicationUpdateSpecRequest) (*v1alpha1.ApplicationSpec, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ func TestKubernetesClient(t *testing.T) {
// Test GetApplication with multiple matching applications
_, err = client.GetApplication(context.TODO(), "test-app")
assert.Error(t, err)
assert.EqualError(t, err, "error getting application: multiple applications found matching test-app")
assert.EqualError(t, err, "multiple applications found matching test-app")
})
}

Expand Down
42 changes: 27 additions & 15 deletions pkg/argocd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
return err
}

// Set username and e-mail address used to identify the commiter
if wbc.GitCommitUser != "" && wbc.GitCommitEmail != "" {
err = gitC.Config(wbc.GitCommitUser, wbc.GitCommitEmail)
if err != nil {
return err
}
}

// The branch to checkout is either a configured branch in the write-back
// config, or taken from the application spec's targetRevision. If the
// target revision is set to the special value HEAD, or is the empty
Expand All @@ -182,10 +174,6 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
return err
}
}
err = gitC.ShallowFetch(checkOutBranch, 1)
if err != nil {
return err
}

// The push branch is by default the same as the checkout branch, unless
// specified after a : separator git-branch annotation, in which case a
Expand All @@ -196,14 +184,30 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
if wbc.GitWriteBranch != "" {
logCtx.Debugf("Using branch template: %s", wbc.GitWriteBranch)
pushBranch = TemplateBranchName(wbc.GitWriteBranch, changeList)
if pushBranch != "" {
if pushBranch == "" {
return fmt.Errorf("Git branch name could not be created from the template: %s", wbc.GitWriteBranch)
}
}

// If the pushBranch already exists in the remote origin, directly use it.
// Otherwise, create the new pushBranch from checkoutBranch
if checkOutBranch != pushBranch {
fetchErr := gitC.ShallowFetch(pushBranch, 1)
if fetchErr != nil {
err = gitC.ShallowFetch(checkOutBranch, 1)
if err != nil {
return err
}
logCtx.Debugf("Creating branch '%s' and using that for push operations", pushBranch)
err = gitC.Branch(checkOutBranch, pushBranch)
if err != nil {
return err
}
} else {
return fmt.Errorf("Git branch name could not be created from the template: %s", wbc.GitWriteBranch)
}
} else {
err = gitC.ShallowFetch(checkOutBranch, 1)
if err != nil {
return err
}
}

Expand Down Expand Up @@ -235,6 +239,14 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
defer os.Remove(cm.Name())
}

// Set username and e-mail address used to identify the commiter
if wbc.GitCommitUser != "" && wbc.GitCommitEmail != "" {
err = gitC.Config(wbc.GitCommitUser, wbc.GitCommitEmail)
if err != nil {
return err
}
}

if wbc.GitCommitSigningKey != "" {
commitOpts.SigningKey = wbc.GitCommitSigningKey
}
Expand Down
1 change: 1 addition & 0 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,7 @@ replacements: []
app := app.DeepCopy()
gitMock := &gitmock.Client{}
gitMock.On("Init").Return(nil)
gitMock.On("Root").Return(t.TempDir())
gitMock.On("ShallowFetch", mock.Anything, mock.Anything).Return(nil)
gitMock.On("Checkout", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
args.Assert(t, "mydefaultbranch", false)
Expand Down

0 comments on commit c7183f0

Please sign in to comment.