From cce0e2ea69283b0a88b6054ea12f8ced31929a99 Mon Sep 17 00:00:00 2001 From: pashakostohrys Date: Wed, 16 Oct 2024 18:50:10 +0300 Subject: [PATCH] feat: acr controller crashes when here no history --- acr_controller/service/acr_service.go | 7 ++-- acr_controller/service/acr_service_test.go | 40 ++++++++++++++++++++++ changelog/CHANGELOG.md | 4 --- util/git/client.go | 5 +++ 4 files changed, 50 insertions(+), 6 deletions(-) delete mode 100644 changelog/CHANGELOG.md diff --git a/acr_controller/service/acr_service.go b/acr_controller/service/acr_service.go index 0eeac9255fb85..211d6492f189f 100644 --- a/acr_controller/service/acr_service.go +++ b/acr_controller/service/acr_service.go @@ -102,7 +102,7 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat func (c *acrService) calculateRevision(ctx context.Context, a *application.Application) (*string, error) { currentRevision, previousRevision := c.getRevisions(ctx, a) - log.Infof("Calculate revision for application %s, current revision %s, previous revision %s", a.Name, currentRevision, previousRevision) + log.Infof("Calculate revision for application '%s', current revision '%s', previous revision '%s'", a.Name, currentRevision, previousRevision) changeRevisionResult, err := c.applicationServiceClient.GetChangeRevision(ctx, &appclient.ChangeRevisionRequest{ AppName: pointer.String(a.GetName()), Namespace: pointer.String(a.GetNamespace()), @@ -173,7 +173,10 @@ func (c *acrService) patchOperationSyncResultWithChangeRevision(ctx context.Cont func (c *acrService) getRevisions(ctx context.Context, a *application.Application) (string, string) { if a.Status.History == nil || len(a.Status.History) == 0 { - // it is first sync operation, and we dont need detect change revision in such case + // it is first sync operation, and we have only current revision + if a.Operation != nil && a.Operation.Sync != nil { + return a.Operation.Sync.Revision, "" + } return "", "" } diff --git a/acr_controller/service/acr_service_test.go b/acr_controller/service/acr_service_test.go index 660e6fbe51672..cac465f51871a 100644 --- a/acr_controller/service/acr_service_test.go +++ b/acr_controller/service/acr_service_test.go @@ -36,6 +36,39 @@ spec: server: https://cluster-api.example.com ` +const fakeAppWithOperation = ` +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + annotations: + argocd.argoproj.io/manifest-generate-paths: . + finalizers: + - resources-finalizer.argocd.argoproj.io + labels: + app.kubernetes.io/instance: guestbook + name: guestbook + namespace: codefresh +operation: + initiatedBy: + automated: true + retry: + limit: 5 + sync: + prune: true + revision: c732f4d2ef24c7eeb900e9211ff98f90bb646505 + syncOptions: + - CreateNamespace=true +spec: + destination: + namespace: guestbook + server: https://kubernetes.default.svc + project: default + source: + path: apps/guestbook + repoURL: https://github.com/pasha-codefresh/precisely-gitsource.git + targetRevision: HEAD +` + const syncedAppWithHistory = ` apiVersion: argoproj.io/v1alpha1 kind: Application @@ -135,6 +168,13 @@ func Test_getRevisions(r *testing.T) { assert.Equal(t, "", previous) }) + r.Run("history list is empty, but operation happens right now", func(t *testing.T) { + acrService := newTestACRService(&mocks.ApplicationClient{}) + current, previous := acrService.getRevisions(context.TODO(), createTestApp(fakeAppWithOperation)) + assert.Equal(t, "c732f4d2ef24c7eeb900e9211ff98f90bb646505", current) + assert.Equal(t, "", previous) + }) + r.Run("application is synced", func(t *testing.T) { acrService := newTestACRService(&mocks.ApplicationClient{}) app := createTestApp(syncedAppWithHistory) diff --git a/changelog/CHANGELOG.md b/changelog/CHANGELOG.md deleted file mode 100644 index ec837707a338b..0000000000000 --- a/changelog/CHANGELOG.md +++ /dev/null @@ -1,4 +0,0 @@ -### Features -- feat: argocd-repo-server: support for arrays in promotion versionSource -- feat: event-reporter: getting version based on synced revision -- feat: argocd-repo-server: suppress 'version not found' error message when version configuration is not provided \ No newline at end of file diff --git a/util/git/client.go b/util/git/client.go index 44a30993f3c75..4122886d894fd 100644 --- a/util/git/client.go +++ b/util/git/client.go @@ -907,6 +907,11 @@ func (m *nativeGitClient) Diff(targetRevision string) ([]string, error) { } func (m *nativeGitClient) ListRevisions(revision string, targetRevision string) ([]string, error) { + // it happens when app just created and there is no revision yet + if revision == "" { + return []string{targetRevision}, nil + } + if !IsCommitSHA(revision) || !IsCommitSHA(targetRevision) { return nil, fmt.Errorf("invalid revision provided, must be SHA") }