Skip to content

Commit 89cb483

Browse files
author
Alexander Matyushentsev
authored
feat: support resource prune propagation policy (argoproj#235)
Signed-off-by: Alexander Matyushentsev <[email protected]>
1 parent aae8ded commit 89cb483

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

pkg/sync/sync_context.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ type SyncContext interface {
5959
// SyncOpt is a callback that update sync operation settings
6060
type SyncOpt func(ctx *syncContext)
6161

62+
// WithPrunePropagationPolicy sets specified permission validator
63+
func WithPrunePropagationPolicy(policy *metav1.DeletionPropagation) SyncOpt {
64+
return func(ctx *syncContext) {
65+
ctx.prunePropagationPolicy = policy
66+
}
67+
}
68+
6269
// WithPermissionValidator sets specified permission validator
6370
func WithPermissionValidator(validator common.PermissionValidator) SyncOpt {
6471
return func(ctx *syncContext) {
@@ -292,13 +299,14 @@ type syncContext struct {
292299
kubectl kube.Kubectl
293300
namespace string
294301

295-
dryRun bool
296-
force bool
297-
validate bool
298-
skipHooks bool
299-
resourcesFilter func(key kube.ResourceKey, target *unstructured.Unstructured, live *unstructured.Unstructured) bool
300-
prune bool
301-
pruneLast bool
302+
dryRun bool
303+
force bool
304+
validate bool
305+
skipHooks bool
306+
resourcesFilter func(key kube.ResourceKey, target *unstructured.Unstructured, live *unstructured.Unstructured) bool
307+
prune bool
308+
pruneLast bool
309+
prunePropagationPolicy *metav1.DeletionPropagation
302310

303311
syncRes map[string]common.ResourceSyncResult
304312
startedAt time.Time
@@ -820,9 +828,7 @@ func (sc *syncContext) pruneObject(liveObj *unstructured.Unstructured, prune, dr
820828
// Skip deletion if object is already marked for deletion, so we don't cause a resource update hotloop
821829
deletionTimestamp := liveObj.GetDeletionTimestamp()
822830
if deletionTimestamp == nil || deletionTimestamp.IsZero() {
823-
propagationPolicy := metav1.DeletePropagationForeground
824-
deleteOption := metav1.DeleteOptions{PropagationPolicy: &propagationPolicy}
825-
err := sc.kubectl.DeleteResource(context.TODO(), sc.config, liveObj.GroupVersionKind(), liveObj.GetName(), liveObj.GetNamespace(), deleteOption)
831+
err := sc.kubectl.DeleteResource(context.TODO(), sc.config, liveObj.GroupVersionKind(), liveObj.GetName(), liveObj.GetNamespace(), sc.getDeleteOptions())
826832
if err != nil {
827833
return common.ResultCodeSyncFailed, err.Error()
828834
}
@@ -832,6 +838,15 @@ func (sc *syncContext) pruneObject(liveObj *unstructured.Unstructured, prune, dr
832838
}
833839
}
834840

841+
func (sc *syncContext) getDeleteOptions() metav1.DeleteOptions {
842+
propagationPolicy := metav1.DeletePropagationForeground
843+
if sc.prunePropagationPolicy != nil {
844+
propagationPolicy = *sc.prunePropagationPolicy
845+
}
846+
deleteOption := metav1.DeleteOptions{PropagationPolicy: &propagationPolicy}
847+
return deleteOption
848+
}
849+
835850
func (sc *syncContext) targetObjs() []*unstructured.Unstructured {
836851
objs := sc.hooks
837852
for _, r := range sc.resources {
@@ -907,8 +922,7 @@ func (sc *syncContext) deleteResource(task *syncTask) error {
907922
if err != nil {
908923
return err
909924
}
910-
propagationPolicy := metav1.DeletePropagationForeground
911-
return resIf.Delete(context.TODO(), task.name(), metav1.DeleteOptions{PropagationPolicy: &propagationPolicy})
925+
return resIf.Delete(context.TODO(), task.name(), sc.getDeleteOptions())
912926
}
913927

914928
func (sc *syncContext) getResourceIf(task *syncTask) (dynamic.ResourceInterface, error) {

pkg/sync/sync_context_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,3 +1133,19 @@ func TestApplyOutOfSyncOnly(t *testing.T) {
11331133
assert.Len(t, tasks, 3)
11341134
})
11351135
}
1136+
1137+
func TestSyncContext_GetDeleteOptions_Default(t *testing.T) {
1138+
sc := syncContext{}
1139+
opts := sc.getDeleteOptions()
1140+
assert.Equal(t, v1.DeletePropagationForeground, *opts.PropagationPolicy)
1141+
}
1142+
1143+
func TestSyncContext_GetDeleteOptions_WithPrunePropagationPolicy(t *testing.T) {
1144+
sc := syncContext{}
1145+
1146+
policy := v1.DeletePropagationBackground
1147+
WithPrunePropagationPolicy(&policy)(&sc)
1148+
1149+
opts := sc.getDeleteOptions()
1150+
assert.Equal(t, v1.DeletePropagationBackground, *opts.PropagationPolicy)
1151+
}

0 commit comments

Comments
 (0)