Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions gitops-engine/pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,26 @@ func (sc *syncContext) setRunningPhase(tasks syncTasks, isPendingDeletion bool)
return
}

{
// Check for prune tasks pending confirmation
if !sc.pruneConfirmed {
var resources []string
for _, task := range tasks {
if task.isPrune() && resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
resources = append(resources, fmt.Sprintf("%s/%s/%s", task.obj().GetAPIVersion(), task.obj().GetKind(), task.name()))
}
}
if len(resources) > 0 {
andMessage := ""
if len(resources) > 1 {
andMessage = fmt.Sprintf(" and %d more resources", len(resources)-1)
}
sc.setOperationPhase(common.OperationRunning, fmt.Sprintf("Waiting for pruning confirmation of %s%s", resources[0], andMessage))
return
}
}
}

hooks, resources := tasks.Split(func(task *syncTask) bool { return task.isHook() })

reason := "completion of hook"
Expand Down Expand Up @@ -620,9 +640,7 @@ func (sc *syncContext) Sync() {
sc.setRunningPhase(remainingTasks, false)
}
default:
sc.setRunningPhase(tasks.Filter(func(task *syncTask) bool {
return task.deleteOnPhaseCompletion()
}), true)
sc.setRunningPhase(tasks, true)
}
}

Expand Down Expand Up @@ -1422,20 +1440,11 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
// prune first
{
if !sc.pruneConfirmed {
var resources []string
for _, task := range pruneTasks {
if resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
resources = append(resources, fmt.Sprintf("%s/%s/%s", task.obj().GetAPIVersion(), task.obj().GetKind(), task.name()))
}
}
if len(resources) > 0 {
sc.log.WithValues("resources", resources).Info("Prune requires confirmation")
andMessage := ""
if len(resources) > 1 {
andMessage = fmt.Sprintf(" and %d more resources", len(resources)-1)
sc.log.WithValues("task", task).Info("Prune requires confirmation")
return pending
}
sc.message = fmt.Sprintf("Waiting for pruning confirmation of %s%s", resources[0], andMessage)
return pending
}
}

Expand Down
27 changes: 27 additions & 0 deletions gitops-engine/pkg/sync/sync_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,33 @@ func TestDoNotPrunePruneFalse(t *testing.T) {
assert.Equal(t, synccommon.OperationSucceeded, phase)
}

func TestPruneConfirm(t *testing.T) {
syncCtx := newTestSyncCtx(nil, WithOperationSettings(false, true, false, false))
pod := testingutils.NewPod()
pod.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Prune=confirm"})
pod.SetNamespace(testingutils.FakeArgoCDNamespace)
syncCtx.resources = groupResources(ReconciliationResult{
Live: []*unstructured.Unstructured{pod},
Target: []*unstructured.Unstructured{nil},
})

syncCtx.Sync()
phase, msg, resources := syncCtx.GetState()

assert.Equal(t, synccommon.OperationRunning, phase)
assert.Empty(t, resources)
assert.Equal(t, "Waiting for pruning confirmation of v1/Pod/my-pod", msg)

syncCtx.pruneConfirmed = true
syncCtx.Sync()

phase, _, resources = syncCtx.GetState()
assert.Equal(t, synccommon.OperationSucceeded, phase)
assert.Len(t, resources, 1)
assert.Equal(t, synccommon.ResultCodePruned, resources[0].Status)
assert.Equal(t, "pruned", resources[0].Message)
}

// // make sure Validate=false means we don't validate
func TestSyncOptionValidate(t *testing.T) {
tests := []struct {
Expand Down
Loading