Skip to content

feat: add Prune as application level sync option #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
20 changes: 18 additions & 2 deletions pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,27 @@ func WithPrune(prune bool) SyncOpt {
}
}

// WithRequiresPruneConfirmation specifies if pruning resources requires a confirmation
func WithRequiresPruneConfirmation(requiresConfirmation bool) SyncOpt {
return func(ctx *syncContext) {
ctx.requiresPruneConfirmation = requiresConfirmation
}
}

// WithPruneConfirmed specifies if prune is confirmed for resources that require confirmation
func WithPruneConfirmed(confirmed bool) SyncOpt {
return func(ctx *syncContext) {
ctx.pruneConfirmed = confirmed
}
}

// WithPruneDisabled specifies if prune is globally disabled for this application
func WithPruneDisabled(disabled bool) SyncOpt {
return func(ctx *syncContext) {
ctx.pruneDisabled = disabled
}
}

// WithOperationSettings allows to set sync operation settings
func WithOperationSettings(dryRun bool, prune bool, force bool, skipHooks bool) SyncOpt {
return func(ctx *syncContext) {
Expand Down Expand Up @@ -367,6 +381,8 @@ type syncContext struct {
pruneLast bool
prunePropagationPolicy *metav1.DeletionPropagation
pruneConfirmed bool
requiresPruneConfirmation bool
pruneDisabled bool
clientSideApplyMigrationManager string
enableClientSideApplyMigration bool

Expand Down Expand Up @@ -1212,7 +1228,7 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
func (sc *syncContext) pruneObject(liveObj *unstructured.Unstructured, prune, dryRun bool) (common.ResultCode, string) {
if !prune {
return common.ResultCodePruneSkipped, "ignored (requires pruning)"
} else if resourceutil.HasAnnotationOption(liveObj, common.AnnotationSyncOptions, common.SyncOptionDisablePrune) {
} else if resourceutil.HasAnnotationOption(liveObj, common.AnnotationSyncOptions, common.SyncOptionDisablePrune) || sc.pruneDisabled {
return common.ResultCodePruneSkipped, "ignored (no prune)"
}
if dryRun {
Expand Down Expand Up @@ -1372,7 +1388,7 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
if !sc.pruneConfirmed {
var resources []string
for _, task := range pruneTasks {
if resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
if sc.requiresPruneConfirmation || resourceutil.HasAnnotationOption(task.liveObj, common.AnnotationSyncOptions, common.SyncOptionPruneRequireConfirm) {
resources = append(resources, fmt.Sprintf("%s/%s/%s", task.obj().GetAPIVersion(), task.obj().GetKind(), task.name()))
}
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/sync/sync_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -767,6 +768,60 @@ func TestDoNotPrunePruneFalse(t *testing.T) {

phase, _, _ = syncCtx.GetState()
assert.Equal(t, synccommon.OperationSucceeded, phase)

// test that we can still not prune if prune is disabled on the app level
syncCtx.pruneDisabled = true
pod.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Prune=true"})
syncCtx.Sync()

phase, _, resources = syncCtx.GetState()

assert.Equal(t, synccommon.OperationSucceeded, phase)
assert.Len(t, resources, 1)
assert.Equal(t, synccommon.ResultCodePruneSkipped, resources[0].Status)
assert.Equal(t, "ignored (no prune)", resources[0].Message)

syncCtx.Sync()

phase, _, _ = syncCtx.GetState()
assert.Equal(t, synccommon.OperationSucceeded, phase)
}

// make sure that we need confirmation to prune with Prune=confirm
func TestPruneConfirm(t *testing.T) {
for _, appLevelConfirmation := range []bool{true, false} {
t.Run("appLevelConfirmation="+strconv.FormatBool(appLevelConfirmation), func(t *testing.T) {
syncCtx := newTestSyncCtx(nil, WithOperationSettings(false, true, false, false))
syncCtx.requiresPruneConfirmation = appLevelConfirmation
pod := testingutils.NewPod()
if appLevelConfirmation {
pod.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Prune=true"})
} else {
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
Expand Down