Skip to content

Commit 7fd46b2

Browse files
committed
feat: add WithRequiresPruneConfirmation to sync option
Add WithRequiresPruneConfirmation so that we can pass this as an Application sync option in ArgoCD Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@ledger.fr>
1 parent 8007df5 commit 7fd46b2

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

pkg/sync/sync_context.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ func WithPrune(prune bool) SyncOpt {
115115
}
116116
}
117117

118+
// WithRequiresPruneConfirmation specifies if pruning resources requires a confirmation
119+
func WithRequiresPruneConfirmation(requiresConfirmation bool) SyncOpt {
120+
return func(ctx *syncContext) {
121+
ctx.requiresPruneConfirmation = requiresConfirmation
122+
}
123+
}
124+
118125
// WithPruneConfirmed specifies if prune is confirmed for resources that require confirmation
119126
func WithPruneConfirmed(confirmed bool) SyncOpt {
120127
return func(ctx *syncContext) {
@@ -367,6 +374,7 @@ type syncContext struct {
367374
pruneLast bool
368375
prunePropagationPolicy *metav1.DeletionPropagation
369376
pruneConfirmed bool
377+
requiresPruneConfirmation bool
370378
clientSideApplyMigrationManager string
371379
enableClientSideApplyMigration bool
372380

@@ -1372,7 +1380,7 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
13721380
if !sc.pruneConfirmed {
13731381
var resources []string
13741382
for _, task := range pruneTasks {
1375-
if resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
1383+
if sc.requiresPruneConfirmation || resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
13761384
resources = append(resources, fmt.Sprintf("%s/%s/%s", task.obj().GetAPIVersion(), task.obj().GetKind(), task.name()))
13771385
}
13781386
}

pkg/sync/sync_context_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,63 @@ func TestDoNotPrunePruneFalse(t *testing.T) {
769769
assert.Equal(t, synccommon.OperationSucceeded, phase)
770770
}
771771

772+
// make sure that we need confirmation to prune resources with Prune=confirm
773+
func TestPruneConfirmResource(t *testing.T) {
774+
syncCtx := newTestSyncCtx(nil, WithOperationSettings(false, true, false, false))
775+
pod := testingutils.NewPod()
776+
pod.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Prune=confirm"})
777+
pod.SetNamespace(testingutils.FakeArgoCDNamespace)
778+
syncCtx.resources = groupResources(ReconciliationResult{
779+
Live: []*unstructured.Unstructured{pod},
780+
Target: []*unstructured.Unstructured{nil},
781+
})
782+
783+
syncCtx.Sync()
784+
phase, msg, resources := syncCtx.GetState()
785+
786+
assert.Equal(t, synccommon.OperationRunning, phase)
787+
assert.Len(t, resources, 0)
788+
assert.Equal(t, "Waiting for pruning confirmation of v1/Pod/my-pod", msg)
789+
790+
syncCtx.pruneConfirmed = true
791+
syncCtx.Sync()
792+
793+
phase, msg, resources = syncCtx.GetState()
794+
assert.Equal(t, synccommon.OperationSucceeded, phase)
795+
assert.Len(t, resources, 1)
796+
assert.Equal(t, synccommon.ResultCodePruned, resources[0].Status)
797+
assert.Equal(t, "pruned", resources[0].Message)
798+
}
799+
800+
// make sure that we need confirmation to prune with Prune=confirm on the app
801+
func TestPruneConfirmApp(t *testing.T) {
802+
syncCtx := newTestSyncCtx(nil, WithOperationSettings(false, true, false, false))
803+
syncCtx.requiresPruneConfirmation = true
804+
pod := testingutils.NewPod()
805+
pod.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Prune=true"})
806+
pod.SetNamespace(testingutils.FakeArgoCDNamespace)
807+
syncCtx.resources = groupResources(ReconciliationResult{
808+
Live: []*unstructured.Unstructured{pod},
809+
Target: []*unstructured.Unstructured{nil},
810+
})
811+
812+
syncCtx.Sync()
813+
phase, msg, resources := syncCtx.GetState()
814+
815+
assert.Equal(t, synccommon.OperationRunning, phase)
816+
assert.Len(t, resources, 0)
817+
assert.Equal(t, "Waiting for pruning confirmation of v1/Pod/my-pod", msg)
818+
819+
syncCtx.pruneConfirmed = true
820+
syncCtx.Sync()
821+
822+
phase, msg, resources = syncCtx.GetState()
823+
assert.Equal(t, synccommon.OperationSucceeded, phase)
824+
assert.Len(t, resources, 1)
825+
assert.Equal(t, synccommon.ResultCodePruned, resources[0].Status)
826+
assert.Equal(t, "pruned", resources[0].Message)
827+
}
828+
772829
// // make sure Validate=false means we don't validate
773830
func TestSyncOptionValidate(t *testing.T) {
774831
tests := []struct {

0 commit comments

Comments
 (0)