From 995854136fa49642d4d160de137dcf82a58f07ec Mon Sep 17 00:00:00 2001 From: pasha-codefresh Date: Mon, 30 Sep 2024 17:23:51 +0300 Subject: [PATCH 1/2] fix: validate if revision already exist before recalculation (#340) --- acr_controller/service/acr_service.go | 12 +++++++----- acr_controller/service/acr_service_test.go | 20 ++++++++++++++++++++ changelog/CHANGELOG.md | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/acr_controller/service/acr_service.go b/acr_controller/service/acr_service.go index 09e2aa89e8c5a..0eeac9255fb85 100644 --- a/acr_controller/service/acr_service.go +++ b/acr_controller/service/acr_service.go @@ -3,6 +3,7 @@ package service import ( "context" "encoding/json" + "errors" "sync" log "github.com/sirupsen/logrus" @@ -41,11 +42,12 @@ func getChangeRevisionFromRevisions(revisions []string) string { } func getChangeRevision(app *application.Application) string { - if changeRevision := getChangeRevisionFromRevisions(app.Operation.Sync.ChangeRevisions); changeRevision != "" { - return changeRevision - } if app.Status.OperationState != nil && app.Status.OperationState.Operation.Sync != nil { - if changeRevision := getChangeRevisionFromRevisions(app.Status.OperationState.Operation.Sync.ChangeRevisions); changeRevision != "" { + changeRevision := app.Status.OperationState.Operation.Sync.ChangeRevision + if changeRevision != "" { + return changeRevision + } + if changeRevision = getChangeRevisionFromRevisions(app.Status.OperationState.Operation.Sync.ChangeRevisions); changeRevision != "" { return changeRevision } } @@ -67,7 +69,7 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat if getChangeRevision(app) != "" { log.Infof("Change revision already calculated for application %s", app.Name) - return nil + return errors.New("change revision already calculated") } revision, err := c.calculateRevision(ctx, app) diff --git a/acr_controller/service/acr_service_test.go b/acr_controller/service/acr_service_test.go index 310fb2b88800e..660e6fbe51672 100644 --- a/acr_controller/service/acr_service_test.go +++ b/acr_controller/service/acr_service_test.go @@ -170,4 +170,24 @@ func Test_ChangeRevision(r *testing.T) { assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision) }) + + r.Run("Change revision already exists", func(t *testing.T) { + client := &mocks.ApplicationClient{} + client.On("GetChangeRevision", mock.Anything, mock.Anything).Return(&appclient.ChangeRevisionResponse{ + Revision: pointer.String("new-revision"), + }, nil) + acrService := newTestACRService(client) + app := createTestApp(syncedAppWithHistory) + + err := acrService.ChangeRevision(context.TODO(), app) + require.NoError(t, err) + + app, err = acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(context.TODO(), app.Name, metav1.GetOptions{}) + require.NoError(t, err) + + assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision) + + err = acrService.ChangeRevision(context.TODO(), app) + require.Error(t, err, "change revision already calculated") + }) } diff --git a/changelog/CHANGELOG.md b/changelog/CHANGELOG.md index 4d55728e17ee7..db818c0e9351f 100644 --- a/changelog/CHANGELOG.md +++ b/changelog/CHANGELOG.md @@ -1,2 +1,2 @@ ### Features -- feat: monorepo controller v1.0.0 \ No newline at end of file +- fix: change revision controller should verify that revision already exists \ No newline at end of file From 288e7d3e712702f8c229edeeff0e5dc4ed5242db Mon Sep 17 00:00:00 2001 From: pashakostohrys Date: Mon, 30 Sep 2024 17:24:25 +0300 Subject: [PATCH 2/2] chore: update version to 2.12-2024.9.30-995854136 --- VERSION | 2 +- changelog/CHANGELOG-2.12-2024.9.30-995854136.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelog/CHANGELOG-2.12-2024.9.30-995854136.md diff --git a/VERSION b/VERSION index 362d862e73c67..040aa068fb3d2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.12-2024.9.30-a87492b22 \ No newline at end of file +2.12-2024.9.30-995854136 \ No newline at end of file diff --git a/changelog/CHANGELOG-2.12-2024.9.30-995854136.md b/changelog/CHANGELOG-2.12-2024.9.30-995854136.md new file mode 100644 index 0000000000000..db818c0e9351f --- /dev/null +++ b/changelog/CHANGELOG-2.12-2024.9.30-995854136.md @@ -0,0 +1,2 @@ +### Features +- fix: change revision controller should verify that revision already exists \ No newline at end of file