Skip to content

Commit 8a174c5

Browse files
committed
fix(engine): correctly set prune confirmation message in setRunningPhase
setRunningPhase was overwriting the message when it receives an empty list of tasks while we were setting the message before setRunningPhase was called which broke the prune confirmation message since commit 95d19f2. This moves the message logic in setRunningPhase with the rest of the message logic and remove the tasks filtering when the sync is pending to allow retrieving the pruning tasks and construct the message indicating to wait for pruning confirmation. Also add a unit test covering this recently broken behavior Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@ledger.fr>
1 parent e8539be commit 8a174c5

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

gitops-engine/pkg/sync/sync_context.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,26 @@ func (sc *syncContext) setRunningPhase(tasks syncTasks, isPendingDeletion bool)
397397
return
398398
}
399399

400+
{
401+
// Check for prune tasks pending confirmation
402+
if !sc.pruneConfirmed {
403+
var resources []string
404+
for _, task := range tasks {
405+
if task.isPrune() && resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
406+
resources = append(resources, fmt.Sprintf("%s/%s/%s", task.obj().GetAPIVersion(), task.obj().GetKind(), task.name()))
407+
}
408+
}
409+
if len(resources) > 0 {
410+
andMessage := ""
411+
if len(resources) > 1 {
412+
andMessage = fmt.Sprintf(" and %d more resources", len(resources)-1)
413+
}
414+
sc.setOperationPhase(common.OperationRunning, fmt.Sprintf("Waiting for pruning confirmation of %s%s", resources[0], andMessage))
415+
return
416+
}
417+
}
418+
}
419+
400420
hooks, resources := tasks.Split(func(task *syncTask) bool { return task.isHook() })
401421

402422
reason := "completion of hook"
@@ -1487,20 +1507,11 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
14871507
// prune first
14881508
{
14891509
if !sc.pruneConfirmed {
1490-
var resources []string
14911510
for _, task := range pruneTasks {
14921511
if resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
1493-
resources = append(resources, fmt.Sprintf("%s/%s/%s", task.obj().GetAPIVersion(), task.obj().GetKind(), task.name()))
1494-
}
1495-
}
1496-
if len(resources) > 0 {
1497-
sc.log.WithValues("resources", resources).Info("Prune requires confirmation")
1498-
andMessage := ""
1499-
if len(resources) > 1 {
1500-
andMessage = fmt.Sprintf(" and %d more resources", len(resources)-1)
1512+
sc.log.WithValues("task", task).Info("Prune requires confirmation")
1513+
return pending
15011514
}
1502-
sc.message = fmt.Sprintf("Waiting for pruning confirmation of %s%s", resources[0], andMessage)
1503-
return pending
15041515
}
15051516
}
15061517

gitops-engine/pkg/sync/sync_context_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,33 @@ func TestDoNotPrunePruneFalse(t *testing.T) {
850850
assert.Equal(t, synccommon.OperationSucceeded, phase)
851851
}
852852

853+
func TestPruneConfirm(t *testing.T) {
854+
syncCtx := newTestSyncCtx(nil, WithOperationSettings(false, true, false, false))
855+
pod := testingutils.NewPod()
856+
pod.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Prune=confirm"})
857+
pod.SetNamespace(testingutils.FakeArgoCDNamespace)
858+
syncCtx.resources = groupResources(ReconciliationResult{
859+
Live: []*unstructured.Unstructured{pod},
860+
Target: []*unstructured.Unstructured{nil},
861+
})
862+
863+
syncCtx.Sync()
864+
phase, msg, resources := syncCtx.GetState()
865+
866+
assert.Equal(t, synccommon.OperationRunning, phase)
867+
assert.Empty(t, resources)
868+
assert.Equal(t, "Waiting for pruning confirmation of v1/Pod/my-pod", msg)
869+
870+
syncCtx.pruneConfirmed = true
871+
syncCtx.Sync()
872+
873+
phase, _, resources = syncCtx.GetState()
874+
assert.Equal(t, synccommon.OperationSucceeded, phase)
875+
assert.Len(t, resources, 1)
876+
assert.Equal(t, synccommon.ResultCodePruned, resources[0].Status)
877+
assert.Equal(t, "pruned", resources[0].Message)
878+
}
879+
853880
// // make sure Validate=false means we don't validate
854881
func TestSyncOptionValidate(t *testing.T) {
855882
tests := []struct {

0 commit comments

Comments
 (0)