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