Skip to content

Commit

Permalink
feat: abort ongoing operation when a new revision is available
Browse files Browse the repository at this point in the history
Signed-off-by: Zadkiel AHARONIAN <[email protected]>
  • Loading branch information
aslafy-z committed May 25, 2024
1 parent d871b7f commit eb138ae
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions controller/appcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,14 @@ func (ctrl *ApplicationController) processRequestedAppOperation(app *appv1.Appli
ctrl.requestAppRefresh(app.QualifiedName(), CompareWithLatest.Pointer(), &retryAfter)
return
} else {
if state.Phase == synccommon.OperationRunning && app.Spec.SyncPolicy.Retry.Refresh && app.Status.Sync.Revision != state.Operation.Sync.Revision {
logCtx.Infof("A new revision is available, refreshing and terminating app, was phase: %s, message: %s", state.Phase, state.Message)
ctrl.requestAppRefresh(app.QualifiedName(), CompareWithLatest.Pointer(), nil)
state.Phase = synccommon.OperationTerminating
state.Message = "Operation forced to terminate (new revision available)"
ctrl.setOperationState(app, state)
return
}
// retrying operation. remove previous failure time in app since it is used as a trigger
// that previous failed and operation should be retried
state.FinishedAt = nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,8 @@ type RetryStrategy struct {
Limit int64 `json:"limit,omitempty" protobuf:"bytes,1,opt,name=limit"`
// Backoff controls how to backoff on subsequent retries of failed syncs
Backoff *Backoff `json:"backoff,omitempty" protobuf:"bytes,2,opt,name=backoff,casttype=Backoff"`
// Refresh indicates if a new revision should trigger a new sync (default: false)
Refresh bool `json:"refresh,omitempty" protobuf:"bytes,3,opt,name=refresh"`
}

func parseStringToDuration(durationString string) (time.Duration, error) {
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/app_autosync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,49 @@ func TestAutoSyncSelfHealEnabled(t *testing.T) {
And(func(app *Application) {
assert.Len(t, app.Status.Conditions, 0)
})
}

func TestAutoSyncSelfHealRetryAndRefreshEnabled(t *testing.T) {
Given(t).
Path(guestbookPath).
When().
// app should be auto-synced once created
CreateFromFile(func(app *Application) {
app.Spec.SyncPolicy = &SyncPolicy{
Automated: &SyncPolicyAutomated{SelfHeal: true},
Retry: &RetryStrategy{
Limit: -1,
Refresh: true,
},
}
}).
Then().
Expect(OperationPhaseIs(OperationSucceeded)).
Expect(SyncStatusIs(SyncStatusCodeSynced)).
When().
// app should be attempted to auto-synced once and marked with error after failed attempt detected
PatchFile("guestbook-ui-deployment.yaml", `[{"op": "add", "path": "/spec/selector/matchLabels/foo", "value": "bar"}, {"op": "add", "path": "/spec/template/metadata/labels/foo", "value": "bar"}]`).
Refresh(RefreshTypeNormal).
Then().
Expect(OperationPhaseIs(OperationFailed)).
When().
// Trigger refresh again to make sure controller notices previously failed sync attempt before expectation timeout expires
Refresh(RefreshTypeNormal).
Then().
Expect(SyncStatusIs(SyncStatusCodeOutOfSync)).
Expect(Condition(ApplicationConditionSyncError, "Failed sync attempt")).
When().
// SyncError condition should be removed after successful sync
PatchFile("guestbook-ui-deployment.yaml", `[{"op": "remove", "path": "/spec/selector/matchLabels/foo"}, {"op": "remove", "path": "/spec/template/metadata/labels/foo"}]`).
Refresh(RefreshTypeNormal).
Then().
Expect(OperationPhaseIs(OperationSucceeded)).
When().
// Trigger refresh twice to make sure controller notices successful attempt and removes condition
Refresh(RefreshTypeNormal).
Then().
Expect(SyncStatusIs(SyncStatusCodeSynced)).
And(func(app *Application) {
assert.Len(t, app.Status.Conditions, 0)
})
}

0 comments on commit eb138ae

Please sign in to comment.