Skip to content

Commit

Permalink
fix: Appcontroller respects sync windows (argoproj#16492)
Browse files Browse the repository at this point in the history
* fix: Appcontroller keeps op running when denied by sync window

Signed-off-by: Charles Coupal-Jetté <[email protected]>

* fix: Update test name

Signed-off-by: Charles Coupal-Jetté <[email protected]>

---------

Signed-off-by: Charles Coupal-Jetté <[email protected]>
Co-authored-by: Blake Pettersson <[email protected]>
  • Loading branch information
2 people authored and Hariharasuthan99 committed Jun 16, 2024
1 parent 73f1ff9 commit c969055
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
15 changes: 15 additions & 0 deletions controller/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ func (m *appStateManager) SyncAppState(app *v1alpha1.Application, state *v1alpha
state.Phase = common.OperationError
state.Message = fmt.Sprintf("Failed to load application project: %v", err)
return
} else if syncWindowPreventsSync(app, proj) {
// If the operation is currently running, simply let the user know the sync is blocked by a current sync window
if state.Phase == common.OperationRunning {
state.Message = "Sync operation blocked by sync window"
}
return
}

if app.Spec.HasMultipleSources() {
Expand Down Expand Up @@ -573,3 +579,12 @@ func delayBetweenSyncWaves(phase common.SyncPhase, wave int, finalWave bool) err
}
return nil
}

func syncWindowPreventsSync(app *v1alpha1.Application, proj *v1alpha1.AppProject) bool {
window := proj.Spec.SyncWindows.Matches(app)
isManual := false
if app.Status.OperationState != nil {
isManual = !app.Status.OperationState.Operation.InitiatedBy.Automated
}
return !window.CanSync(isManual)
}
69 changes: 69 additions & 0 deletions controller/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,75 @@ func TestAppStateManager_SyncAppState(t *testing.T) {
})
}

func TestSyncWindowDeniesSync(t *testing.T) {
type fixture struct {
project *v1alpha1.AppProject
application *v1alpha1.Application
controller *ApplicationController
}

setup := func() *fixture {
app := newFakeApp()
app.Status.OperationState = nil
app.Status.History = nil

project := &v1alpha1.AppProject{
ObjectMeta: v1.ObjectMeta{
Namespace: test.FakeArgoCDNamespace,
Name: "default",
},
Spec: v1alpha1.AppProjectSpec{
SyncWindows: v1alpha1.SyncWindows{{
Kind: "deny",
Schedule: "0 0 * * *",
Duration: "24h",
Clusters: []string{"*"},
Namespaces: []string{"*"},
Applications: []string{"*"},
}},
},
}
data := fakeData{
apps: []runtime.Object{app, project},
manifestResponse: &apiclient.ManifestResponse{
Manifests: []string{},
Namespace: test.FakeDestNamespace,
Server: test.FakeClusterURL,
Revision: "abc123",
},
managedLiveObjs: make(map[kube.ResourceKey]*unstructured.Unstructured),
}
ctrl := newFakeController(&data, nil)

return &fixture{
project: project,
application: app,
controller: ctrl,
}
}

t.Run("will keep the sync progressing if a sync window prevents the sync", func(t *testing.T) {
// given a project with an active deny sync window and an operation in progress
t.Parallel()
f := setup()
opMessage := "Sync operation blocked by sync window"

opState := &v1alpha1.OperationState{Operation: v1alpha1.Operation{
Sync: &v1alpha1.SyncOperation{
Source: &v1alpha1.ApplicationSource{},
}},
Phase: common.OperationRunning,
}
// when
f.controller.appStateManager.SyncAppState(f.application, opState)

//then
assert.Equal(t, common.OperationRunning, opState.Phase)
assert.Contains(t, opState.Message, opMessage)
})

}

func TestNormalizeTargetResources(t *testing.T) {
type fixture struct {
comparisonResult *comparisonResult
Expand Down

0 comments on commit c969055

Please sign in to comment.