Skip to content

Commit a37aef2

Browse files
committed
feat: add WithPruneDisabled to syncoption
Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@ledger.fr>
1 parent fcbaecc commit a37aef2

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pkg/sync/sync_context.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ func WithPruneConfirmed(confirmed bool) SyncOpt {
129129
}
130130
}
131131

132+
// WithPruneDisabled specifies if prune is globally disabled for this application
133+
func WithPruneDisabled(disabled bool) SyncOpt {
134+
return func(ctx *syncContext) {
135+
ctx.pruneDisabled = disabled
136+
}
137+
}
138+
132139
// WithOperationSettings allows to set sync operation settings
133140
func WithOperationSettings(dryRun bool, prune bool, force bool, skipHooks bool) SyncOpt {
134141
return func(ctx *syncContext) {
@@ -375,6 +382,7 @@ type syncContext struct {
375382
prunePropagationPolicy *metav1.DeletionPropagation
376383
pruneConfirmed bool
377384
requiresPruneConfirmation bool
385+
pruneDisabled bool
378386
clientSideApplyMigrationManager string
379387
enableClientSideApplyMigration bool
380388

@@ -1220,7 +1228,7 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
12201228
func (sc *syncContext) pruneObject(liveObj *unstructured.Unstructured, prune, dryRun bool) (common.ResultCode, string) {
12211229
if !prune {
12221230
return common.ResultCodePruneSkipped, "ignored (requires pruning)"
1223-
} else if resourceutil.HasAnnotationOption(liveObj, common.AnnotationSyncOptions, common.SyncOptionDisablePrune) {
1231+
} else if resourceutil.HasAnnotationOption(liveObj, common.AnnotationSyncOptions, common.SyncOptionDisablePrune) || sc.pruneDisabled {
12241232
return common.ResultCodePruneSkipped, "ignored (no prune)"
12251233
}
12261234
if dryRun {

pkg/sync/sync_context_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,23 @@ func TestDoNotPrunePruneFalse(t *testing.T) {
768768

769769
phase, _, _ = syncCtx.GetState()
770770
assert.Equal(t, synccommon.OperationSucceeded, phase)
771+
772+
// test that we can still not prune if prune is disabled on the app level
773+
syncCtx.pruneDisabled = true
774+
pod.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Prune=true"})
775+
syncCtx.Sync()
776+
777+
phase, _, resources = syncCtx.GetState()
778+
779+
assert.Equal(t, synccommon.OperationSucceeded, phase)
780+
assert.Len(t, resources, 1)
781+
assert.Equal(t, synccommon.ResultCodePruneSkipped, resources[0].Status)
782+
assert.Equal(t, "ignored (no prune)", resources[0].Message)
783+
784+
syncCtx.Sync()
785+
786+
phase, _, _ = syncCtx.GetState()
787+
assert.Equal(t, synccommon.OperationSucceeded, phase)
771788
}
772789

773790
// make sure that we need confirmation to prune with Prune=confirm

0 commit comments

Comments
 (0)