Skip to content

Commit

Permalink
feat: added cascade option to DeleteResource - argo-cd #5368 (#220)
Browse files Browse the repository at this point in the history
* feat: added cascade option to DeleteResource - argo-cd #5368

Signed-off-by: ishitasequeira <[email protected]>

* added a comment re-trigger the unit test

Signed-off-by: ishitasequeira <[email protected]>

* feat: updated delete option logic in DeleteResource

Signed-off-by: ishitasequeira <[email protected]>
  • Loading branch information
ishitasequeira committed Feb 22, 2021
1 parent 354817a commit aae8ded
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 3 additions & 1 deletion pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,9 @@ func (sc *syncContext) pruneObject(liveObj *unstructured.Unstructured, prune, dr
// Skip deletion if object is already marked for deletion, so we don't cause a resource update hotloop
deletionTimestamp := liveObj.GetDeletionTimestamp()
if deletionTimestamp == nil || deletionTimestamp.IsZero() {
err := sc.kubectl.DeleteResource(context.TODO(), sc.config, liveObj.GroupVersionKind(), liveObj.GetName(), liveObj.GetNamespace(), false)
propagationPolicy := metav1.DeletePropagationForeground
deleteOption := metav1.DeleteOptions{PropagationPolicy: &propagationPolicy}
err := sc.kubectl.DeleteResource(context.TODO(), sc.config, liveObj.GroupVersionKind(), liveObj.GetName(), liveObj.GetNamespace(), deleteOption)
if err != nil {
return common.ResultCodeSyncFailed, err.Error()
}
Expand Down
15 changes: 6 additions & 9 deletions pkg/utils/kube/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type OnKubectlRunFunc func(command string) (CleanupFunc, error)
type Kubectl interface {
ApplyResource(ctx context.Context, config *rest.Config, obj *unstructured.Unstructured, namespace string, dryRunStrategy cmdutil.DryRunStrategy, force, validate bool) (string, error)
ConvertToVersion(obj *unstructured.Unstructured, group, version string) (*unstructured.Unstructured, error)
DeleteResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, forceDelete bool) error
DeleteResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, deleteOptions metav1.DeleteOptions) error
GetResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string) (*unstructured.Unstructured, error)
PatchResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, patchType types.PatchType, patchBytes []byte, subresources ...string) (*unstructured.Unstructured, error)
GetAPIResources(config *rest.Config, resourceFilter ResourceFilter) ([]APIResourceInfo, error)
Expand Down Expand Up @@ -187,7 +187,7 @@ func (k *KubectlCmd) PatchResource(ctx context.Context, config *rest.Config, gvk
}

// DeleteResource deletes resource
func (k *KubectlCmd) DeleteResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, forceDelete bool) error {
func (k *KubectlCmd) DeleteResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, deleteOptions metav1.DeleteOptions) error {
span := k.Tracer.StartSpan("DeleteResource")
span.SetBaggageItem("kind", gvk.Kind)
span.SetBaggageItem("name", name)
Expand All @@ -206,14 +206,11 @@ func (k *KubectlCmd) DeleteResource(ctx context.Context, config *rest.Config, gv
}
resource := gvk.GroupVersion().WithResource(apiResource.Name)
resourceIf := ToResourceInterface(dynamicIf, apiResource, resource, namespace)
propagationPolicy := metav1.DeletePropagationForeground
deleteOptions := metav1.DeleteOptions{PropagationPolicy: &propagationPolicy}
if forceDelete {
propagationPolicy = metav1.DeletePropagationBackground
zeroGracePeriod := int64(0)
deleteOptions.GracePeriodSeconds = &zeroGracePeriod
}

if deleteOptions.PropagationPolicy == nil {
propagationPolicy := metav1.DeletePropagationForeground
deleteOptions = metav1.DeleteOptions{PropagationPolicy: &propagationPolicy}
}
return resourceIf.Delete(ctx, name, deleteOptions)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/kube/kubetest/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (k *MockKubectlCmd) PatchResource(ctx context.Context, config *rest.Config,
return nil, nil
}

func (k *MockKubectlCmd) DeleteResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, forceDelete bool) error {
func (k *MockKubectlCmd) DeleteResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, deleteOptions metav1.DeleteOptions) error {
command, ok := k.Commands[name]
if !ok {
return nil
Expand Down

0 comments on commit aae8ded

Please sign in to comment.