1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

Passed the deleteOptions to the DeleteResource client (#11484)

* Passed the deleteOptions to the DeleteResource client

Signed-off-by: ShivamJha2436 <shivamkumar87148@gmail.com>

* fix:minor change in the DeleteResource client

Signed-off-by: ShivamJha2436 <shivamkumar87148@gmail.com>

---------

Signed-off-by: ShivamJha2436 <shivamkumar87148@gmail.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Shivam Kumar 2024-11-19 14:56:17 +05:30 committed by GitHub
parent 2279a7082c
commit 30d475aaae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 8 additions and 8 deletions

View file

@ -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 {

View file

@ -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}}
}

View file

@ -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)
}

View file

@ -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...))
}

View file

@ -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")
}