From 30d475aaae51c439059c9b235e2e6d14049271f2 Mon Sep 17 00:00:00 2001 From: Shivam Kumar <122988410+ShivamJha2436@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:56:17 +0530 Subject: [PATCH] Passed the deleteOptions to the DeleteResource client (#11484) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Passed the deleteOptions to the DeleteResource client Signed-off-by: ShivamJha2436 * fix:minor change in the DeleteResource client Signed-off-by: ShivamJha2436 --------- Signed-off-by: ShivamJha2436 Co-authored-by: Charles-Edouard Brétéché --- pkg/background/generate/cleanup.go | 5 +++-- pkg/clients/dclient/client.go | 5 ++--- pkg/clients/dclient/client_test.go | 2 +- pkg/controllers/cleanup/controller.go | 2 +- pkg/utils/fuzz/fuzz_interface.go | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/background/generate/cleanup.go b/pkg/background/generate/cleanup.go index 489806c761..10bf58d0fc 100644 --- a/pkg/background/generate/cleanup.go +++ b/pkg/background/generate/cleanup.go @@ -11,6 +11,7 @@ import ( kubeutils "github.com/kyverno/kyverno/pkg/utils/kube" "go.uber.org/multierr" apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) @@ -21,7 +22,7 @@ func (c *GenerateController) deleteDownstream(policy kyvernov1.PolicyInterface, var errs []error failedDownstreams := []kyvernov1.ResourceSpec{} for _, e := range ur.Status.GeneratedResources { - if err := c.client.DeleteResource(context.TODO(), e.GetAPIVersion(), e.GetKind(), e.GetNamespace(), e.GetName(), false); err != nil && !apierrors.IsNotFound(err) { + if err := c.client.DeleteResource(context.TODO(), e.GetAPIVersion(), e.GetKind(), e.GetNamespace(), e.GetName(), false, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { failedDownstreams = append(failedDownstreams, e) errs = append(errs, err) } @@ -73,7 +74,7 @@ func (c *GenerateController) handleNonPolicyChanges(policy kyvernov1.PolicyInter failedDownstreams := []kyvernov1.ResourceSpec{} for _, downstream := range downstreams { spec := common.ResourceSpecFromUnstructured(downstream) - if err := c.client.DeleteResource(context.TODO(), downstream.GetAPIVersion(), downstream.GetKind(), downstream.GetNamespace(), downstream.GetName(), false); err != nil && !apierrors.IsNotFound(err) { + if err := c.client.DeleteResource(context.TODO(), downstream.GetAPIVersion(), downstream.GetKind(), downstream.GetNamespace(), downstream.GetName(), false, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { failedDownstreams = append(failedDownstreams, spec) errs = append(errs, err) } else { diff --git a/pkg/clients/dclient/client.go b/pkg/clients/dclient/client.go index 818bb8000d..f823db91ef 100644 --- a/pkg/clients/dclient/client.go +++ b/pkg/clients/dclient/client.go @@ -41,7 +41,7 @@ type Interface interface { // Access items using []Items ListResource(ctx context.Context, apiVersion string, kind string, namespace string, lselector *metav1.LabelSelector) (*unstructured.UnstructuredList, error) // DeleteResource deletes the specified resource - DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool) error + DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool, options metav1.DeleteOptions) error // CreateResource creates object for the specified resource/namespace CreateResource(ctx context.Context, apiVersion string, kind string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) // UpdateResource updates object for the specified resource/namespace @@ -183,8 +183,7 @@ func (c *client) ListResource(ctx context.Context, apiVersion string, kind strin } // DeleteResource deletes the specified resource -func (c *client) DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool) error { - options := metav1.DeleteOptions{} +func (c *client) DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool, options metav1.DeleteOptions) error { if dryRun { options = metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}} } diff --git a/pkg/clients/dclient/client_test.go b/pkg/clients/dclient/client_test.go index e988cfc734..9e320e8d25 100644 --- a/pkg/clients/dclient/client_test.go +++ b/pkg/clients/dclient/client_test.go @@ -103,7 +103,7 @@ func TestCRUDResource(t *testing.T) { t.Errorf("ListResource not working: %s", err) } // DeleteResouce - err = f.client.DeleteResource(context.TODO(), "", "thekind", "ns-foo", "name-bar", false) + err = f.client.DeleteResource(context.TODO(), "", "thekind", "ns-foo", "name-bar", false, metav1.DeleteOptions{}) if err != nil { t.Errorf("DeleteResouce not working: %s", err) } diff --git a/pkg/controllers/cleanup/controller.go b/pkg/controllers/cleanup/controller.go index f0b88a7c2f..165c3273d8 100644 --- a/pkg/controllers/cleanup/controller.go +++ b/pkg/controllers/cleanup/controller.go @@ -309,7 +309,7 @@ func (c *controller) cleanup(ctx context.Context, logger logr.Logger, policy kyv labels = append(labels, attribute.String("deletion_policy", string(*deleteOptions.PropagationPolicy))) } logger.WithValues("name", name, "namespace", namespace).Info("resource matched, it will be deleted...") - if err := c.client.DeleteResource(ctx, resource.GetAPIVersion(), resource.GetKind(), namespace, name, false); err != nil { + if err := c.client.DeleteResource(ctx, resource.GetAPIVersion(), resource.GetKind(), namespace, name, false, metav1.DeleteOptions{}); err != nil { if c.metrics.cleanupFailuresTotal != nil { c.metrics.cleanupFailuresTotal.Add(ctx, 1, metric.WithAttributes(labels...)) } diff --git a/pkg/utils/fuzz/fuzz_interface.go b/pkg/utils/fuzz/fuzz_interface.go index f2abdc84e3..181cbb6bc6 100644 --- a/pkg/utils/fuzz/fuzz_interface.go +++ b/pkg/utils/fuzz/fuzz_interface.go @@ -60,7 +60,7 @@ func (fi FuzzInterface) ListResource(ctx context.Context, apiVersion string, kin return nil, fmt.Errorf("Not implemented") } -func (fi FuzzInterface) DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool) error { +func (fi FuzzInterface) DeleteResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, dryRun bool, options metav1.DeleteOptions) error { return fmt.Errorf("Not implemented") }