diff --git a/api/kyverno/v2/cleanup_policy_types.go b/api/kyverno/v2/cleanup_policy_types.go index 280fd811bb..dc09561d80 100644 --- a/api/kyverno/v2/cleanup_policy_types.go +++ b/api/kyverno/v2/cleanup_policy_types.go @@ -224,6 +224,11 @@ type CleanupPolicySpec struct { // Conditions defines the conditions used to select the resources which will be cleaned up. // +optional Conditions *AnyAllConditions `json:"conditions,omitempty"` + + // DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan). + // +optional + // +kubebuilder:validation:Enum=Foreground;Background;Orphan + DeletionPropagationPolicy *metav1.DeletionPropagation `json:"deletionPropagationPolicy,omitempty"` } // CleanupPolicyStatus stores the status of the policy. @@ -288,6 +293,7 @@ func (spec *CleanupPolicySpec) ValidateMatchExcludeConflict(path *field.Path) (e } return errs } + // If the ExcludeResources is empty, no need to validate further if datautils.DeepEqual(spec.ExcludeResources, &MatchResources{}) { return errs } diff --git a/api/kyverno/v2/zz_generated.deepcopy.go b/api/kyverno/v2/zz_generated.deepcopy.go index afa073fb3f..df5409c757 100644 --- a/api/kyverno/v2/zz_generated.deepcopy.go +++ b/api/kyverno/v2/zz_generated.deepcopy.go @@ -162,6 +162,11 @@ func (in *CleanupPolicySpec) DeepCopyInto(out *CleanupPolicySpec) { *out = new(AnyAllConditions) (*in).DeepCopyInto(*out) } + if in.DeletionPropagationPolicy != nil { + in, out := &in.DeletionPropagationPolicy, &out.DeletionPropagationPolicy + *out = new(metav1.DeletionPropagation) + **out = **in + } return } diff --git a/api/kyverno/v2beta1/cleanup_policy_types.go b/api/kyverno/v2beta1/cleanup_policy_types.go index b2c7af1aa7..f2bcb538cf 100644 --- a/api/kyverno/v2beta1/cleanup_policy_types.go +++ b/api/kyverno/v2beta1/cleanup_policy_types.go @@ -224,6 +224,11 @@ type CleanupPolicySpec struct { // Conditions defines the conditions used to select the resources which will be cleaned up. // +optional Conditions *AnyAllConditions `json:"conditions,omitempty"` + + // DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan). + // +optional + // +kubebuilder:validation:Enum=Foreground;Background;Orphan + DeletionPropagationPolicy *metav1.DeletionPropagation `json:"deletionPropagationPolicy,omitempty"` } // CleanupPolicyStatus stores the status of the policy. diff --git a/api/kyverno/v2beta1/zz_generated.deepcopy.go b/api/kyverno/v2beta1/zz_generated.deepcopy.go index cd452aed40..6887ae9fa3 100755 --- a/api/kyverno/v2beta1/zz_generated.deepcopy.go +++ b/api/kyverno/v2beta1/zz_generated.deepcopy.go @@ -140,6 +140,11 @@ func (in *CleanupPolicySpec) DeepCopyInto(out *CleanupPolicySpec) { *out = new(AnyAllConditions) (*in).DeepCopyInto(*out) } + if in.DeletionPropagationPolicy != nil { + in, out := &in.DeletionPropagationPolicy, &out.DeletionPropagationPolicy + *out = new(metav1.DeletionPropagation) + **out = **in + } return } diff --git a/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_cleanuppolicies.yaml b/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_cleanuppolicies.yaml index a82f846fdc..fc6b1afdcc 100644 --- a/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_cleanuppolicies.yaml +++ b/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_cleanuppolicies.yaml @@ -364,6 +364,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude @@ -1649,6 +1657,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude diff --git a/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_clustercleanuppolicies.yaml b/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_clustercleanuppolicies.yaml index 5d12728830..a05a1c6c46 100644 --- a/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_clustercleanuppolicies.yaml +++ b/charts/kyverno/charts/crds/templates/kyverno.io/kyverno.io_clustercleanuppolicies.yaml @@ -364,6 +364,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude @@ -1649,6 +1657,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude diff --git a/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml b/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml index 1a15acc71f..0903d72b3b 100644 --- a/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml +++ b/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml @@ -358,6 +358,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude @@ -1643,6 +1651,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude diff --git a/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml b/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml index 3692662963..08bae959b3 100644 --- a/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml +++ b/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml @@ -358,6 +358,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude @@ -1643,6 +1651,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude diff --git a/config/install-latest-testing.yaml b/config/install-latest-testing.yaml index d2357e175d..7337e1d414 100644 --- a/config/install-latest-testing.yaml +++ b/config/install-latest-testing.yaml @@ -559,6 +559,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude @@ -1844,6 +1852,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude @@ -3155,6 +3171,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude @@ -4440,6 +4464,14 @@ spec: - name type: object type: array + deletionPropagationPolicy: + description: DeletionPropagationPolicy defines how resources will + be deleted (Foreground, Background, Orphan). + enum: + - Foreground + - Background + - Orphan + type: string exclude: description: |- ExcludeResources defines when cleanuppolicy should not be applied. The exclude diff --git a/docs/user/crd/index.html b/docs/user/crd/index.html index 54a7043b96..c976faf580 100644 --- a/docs/user/crd/index.html +++ b/docs/user/crd/index.html @@ -5898,6 +5898,20 @@ AnyAllConditions <p>Conditions defines the conditions used to select the resources which will be cleaned up.</p> </td> </tr> +<tr> +<td> +<code>deletionPropagationPolicy</code><br/> +<em> +<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#deletionpropagation-v1-meta"> +Kubernetes meta/v1.DeletionPropagation +</a> +</em> +</td> +<td> +<em>(Optional)</em> +<p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> +</td> +</tr> </table> </td> </tr> @@ -6047,6 +6061,20 @@ AnyAllConditions <p>Conditions defines the conditions used to select the resources which will be cleaned up.</p> </td> </tr> +<tr> +<td> +<code>deletionPropagationPolicy</code><br/> +<em> +<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#deletionpropagation-v1-meta"> +Kubernetes meta/v1.DeletionPropagation +</a> +</em> +</td> +<td> +<em>(Optional)</em> +<p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> +</td> +</tr> </table> </td> </tr> @@ -6574,6 +6602,20 @@ AnyAllConditions <p>Conditions defines the conditions used to select the resources which will be cleaned up.</p> </td> </tr> +<tr> +<td> +<code>deletionPropagationPolicy</code><br/> +<em> +<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#deletionpropagation-v1-meta"> +Kubernetes meta/v1.DeletionPropagation +</a> +</em> +</td> +<td> +<em>(Optional)</em> +<p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> +</td> +</tr> </tbody> </table> <hr /> @@ -7708,6 +7750,20 @@ AnyAllConditions <p>Conditions defines the conditions used to select the resources which will be cleaned up.</p> </td> </tr> +<tr> +<td> +<code>deletionPropagationPolicy</code><br/> +<em> +<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#deletionpropagation-v1-meta"> +Kubernetes meta/v1.DeletionPropagation +</a> +</em> +</td> +<td> +<em>(Optional)</em> +<p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> +</td> +</tr> </table> </td> </tr> @@ -7857,6 +7913,20 @@ AnyAllConditions <p>Conditions defines the conditions used to select the resources which will be cleaned up.</p> </td> </tr> +<tr> +<td> +<code>deletionPropagationPolicy</code><br/> +<em> +<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#deletionpropagation-v1-meta"> +Kubernetes meta/v1.DeletionPropagation +</a> +</em> +</td> +<td> +<em>(Optional)</em> +<p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> +</td> +</tr> </table> </td> </tr> @@ -8677,6 +8747,20 @@ AnyAllConditions <p>Conditions defines the conditions used to select the resources which will be cleaned up.</p> </td> </tr> +<tr> +<td> +<code>deletionPropagationPolicy</code><br/> +<em> +<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#deletionpropagation-v1-meta"> +Kubernetes meta/v1.DeletionPropagation +</a> +</em> +</td> +<td> +<em>(Optional)</em> +<p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> +</td> +</tr> </tbody> </table> <hr /> diff --git a/docs/user/crd/kyverno.v2.html b/docs/user/crd/kyverno.v2.html index 969e4df015..39d577f8d0 100644 --- a/docs/user/crd/kyverno.v2.html +++ b/docs/user/crd/kyverno.v2.html @@ -283,6 +283,33 @@ and admission review request information like the name or role.</p> + </td> + </tr> + + + + + <tr> + <td><code>deletionPropagationPolicy</code> + + </br> + + + + + <span style="font-family: monospace">meta/v1.DeletionPropagation</span> + + + </td> + <td> + + + <p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> + + + + + </td> </tr> @@ -576,6 +603,33 @@ and admission review request information like the name or role.</p> + </td> + </tr> + + + + + <tr> + <td><code>deletionPropagationPolicy</code> + + </br> + + + + + <span style="font-family: monospace">meta/v1.DeletionPropagation</span> + + + </td> + <td> + + + <p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> + + + + + </td> </tr> @@ -1647,6 +1701,33 @@ and admission review request information like the name or role.</p> </tr> + + + <tr> + <td><code>deletionPropagationPolicy</code> + + </br> + + + + + <span style="font-family: monospace">meta/v1.DeletionPropagation</span> + + + </td> + <td> + + + <p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> + + + + + + </td> + </tr> + + </tbody> diff --git a/docs/user/crd/kyverno.v2beta1.html b/docs/user/crd/kyverno.v2beta1.html index b4f3e25105..df1ae00ae2 100644 --- a/docs/user/crd/kyverno.v2beta1.html +++ b/docs/user/crd/kyverno.v2beta1.html @@ -289,6 +289,33 @@ and admission review request information like the name or role.</p> + </td> + </tr> + + + + + <tr> + <td><code>deletionPropagationPolicy</code> + + </br> + + + + + <span style="font-family: monospace">meta/v1.DeletionPropagation</span> + + + </td> + <td> + + + <p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> + + + + + </td> </tr> @@ -586,6 +613,33 @@ and admission review request information like the name or role.</p> + </td> + </tr> + + + + + <tr> + <td><code>deletionPropagationPolicy</code> + + </br> + + + + + <span style="font-family: monospace">meta/v1.DeletionPropagation</span> + + + </td> + <td> + + + <p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> + + + + + </td> </tr> @@ -2304,6 +2358,33 @@ and admission review request information like the name or role.</p> </tr> + + + <tr> + <td><code>deletionPropagationPolicy</code> + + </br> + + + + + <span style="font-family: monospace">meta/v1.DeletionPropagation</span> + + + </td> + <td> + + + <p>DeletionPropagationPolicy defines how resources will be deleted (Foreground, Background, Orphan).</p> + + + + + + </td> + </tr> + + </tbody> diff --git a/pkg/client/applyconfigurations/kyverno/v2/cleanuppolicyspec.go b/pkg/client/applyconfigurations/kyverno/v2/cleanuppolicyspec.go index b7976ce3d4..cd014f99d0 100644 --- a/pkg/client/applyconfigurations/kyverno/v2/cleanuppolicyspec.go +++ b/pkg/client/applyconfigurations/kyverno/v2/cleanuppolicyspec.go @@ -21,16 +21,18 @@ package v2 import ( v1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v1" v2beta1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v2beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // CleanupPolicySpecApplyConfiguration represents an declarative configuration of the CleanupPolicySpec type for use // with apply. type CleanupPolicySpecApplyConfiguration struct { - Context []v1.ContextEntryApplyConfiguration `json:"context,omitempty"` - MatchResources *v2beta1.MatchResourcesApplyConfiguration `json:"match,omitempty"` - ExcludeResources *v2beta1.MatchResourcesApplyConfiguration `json:"exclude,omitempty"` - Schedule *string `json:"schedule,omitempty"` - Conditions *AnyAllConditionsApplyConfiguration `json:"conditions,omitempty"` + Context []v1.ContextEntryApplyConfiguration `json:"context,omitempty"` + MatchResources *v2beta1.MatchResourcesApplyConfiguration `json:"match,omitempty"` + ExcludeResources *v2beta1.MatchResourcesApplyConfiguration `json:"exclude,omitempty"` + Schedule *string `json:"schedule,omitempty"` + Conditions *AnyAllConditionsApplyConfiguration `json:"conditions,omitempty"` + DeletionPropagationPolicy *metav1.DeletionPropagation `json:"deletionPropagationPolicy,omitempty"` } // CleanupPolicySpecApplyConfiguration constructs an declarative configuration of the CleanupPolicySpec type for use with @@ -83,3 +85,11 @@ func (b *CleanupPolicySpecApplyConfiguration) WithConditions(value *AnyAllCondit b.Conditions = value return b } + +// WithDeletionPropagationPolicy sets the DeletionPropagationPolicy field in the declarative configuration to the given value +// and returns the receiver, so that objects can be built by chaining "With" function invocations. +// If called multiple times, the DeletionPropagationPolicy field is set to the value of the last call. +func (b *CleanupPolicySpecApplyConfiguration) WithDeletionPropagationPolicy(value metav1.DeletionPropagation) *CleanupPolicySpecApplyConfiguration { + b.DeletionPropagationPolicy = &value + return b +} diff --git a/pkg/client/applyconfigurations/kyverno/v2beta1/cleanuppolicyspec.go b/pkg/client/applyconfigurations/kyverno/v2beta1/cleanuppolicyspec.go index 151b07421e..972f70b504 100644 --- a/pkg/client/applyconfigurations/kyverno/v2beta1/cleanuppolicyspec.go +++ b/pkg/client/applyconfigurations/kyverno/v2beta1/cleanuppolicyspec.go @@ -20,16 +20,18 @@ package v2beta1 import ( v1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // CleanupPolicySpecApplyConfiguration represents an declarative configuration of the CleanupPolicySpec type for use // with apply. type CleanupPolicySpecApplyConfiguration struct { - Context []v1.ContextEntryApplyConfiguration `json:"context,omitempty"` - MatchResources *MatchResourcesApplyConfiguration `json:"match,omitempty"` - ExcludeResources *MatchResourcesApplyConfiguration `json:"exclude,omitempty"` - Schedule *string `json:"schedule,omitempty"` - Conditions *AnyAllConditionsApplyConfiguration `json:"conditions,omitempty"` + Context []v1.ContextEntryApplyConfiguration `json:"context,omitempty"` + MatchResources *MatchResourcesApplyConfiguration `json:"match,omitempty"` + ExcludeResources *MatchResourcesApplyConfiguration `json:"exclude,omitempty"` + Schedule *string `json:"schedule,omitempty"` + Conditions *AnyAllConditionsApplyConfiguration `json:"conditions,omitempty"` + DeletionPropagationPolicy *metav1.DeletionPropagation `json:"deletionPropagationPolicy,omitempty"` } // CleanupPolicySpecApplyConfiguration constructs an declarative configuration of the CleanupPolicySpec type for use with @@ -82,3 +84,11 @@ func (b *CleanupPolicySpecApplyConfiguration) WithConditions(value *AnyAllCondit b.Conditions = value return b } + +// WithDeletionPropagationPolicy sets the DeletionPropagationPolicy field in the declarative configuration to the given value +// and returns the receiver, so that objects can be built by chaining "With" function invocations. +// If called multiple times, the DeletionPropagationPolicy field is set to the value of the last call. +func (b *CleanupPolicySpecApplyConfiguration) WithDeletionPropagationPolicy(value metav1.DeletionPropagation) *CleanupPolicySpecApplyConfiguration { + b.DeletionPropagationPolicy = &value + return b +} diff --git a/pkg/controllers/cleanup/controller.go b/pkg/controllers/cleanup/controller.go index c0c28fa688..28c0b41880 100644 --- a/pkg/controllers/cleanup/controller.go +++ b/pkg/controllers/cleanup/controller.go @@ -181,10 +181,11 @@ func (c *controller) cleanup(ctx context.Context, logger logr.Logger, policy kyv kinds := sets.New(spec.MatchResources.GetKinds()...) debug := logger.V(4) var errs []error - + deleteOptions := metav1.DeleteOptions{ + PropagationPolicy: spec.DeletionPropagationPolicy, + } enginectx := enginecontext.NewContext(c.jp) ctxFactory := factories.DefaultContextLoaderFactory(c.cmResolver, factories.WithGlobalContextStore(c.gctxStore)) - loader := ctxFactory(nil, kyvernov1.Rule{}) if err := loader.Load( ctx, @@ -196,7 +197,6 @@ func (c *controller) cleanup(ctx context.Context, logger logr.Logger, policy kyv ); err != nil { return err } - for kind := range kinds { commonLabels := []attribute.KeyValue{ attribute.String("policy_type", policy.GetKind()), @@ -302,6 +302,9 @@ func (c *controller) cleanup(ctx context.Context, logger logr.Logger, policy kyv var labels []attribute.KeyValue labels = append(labels, commonLabels...) labels = append(labels, attribute.String("resource_namespace", namespace)) + if deleteOptions.PropagationPolicy != nil { + 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 c.metrics.cleanupFailuresTotal != nil { @@ -315,7 +318,7 @@ func (c *controller) cleanup(ctx context.Context, logger logr.Logger, policy kyv if c.metrics.deletedObjectsTotal != nil { c.metrics.deletedObjectsTotal.Add(ctx, 1, metric.WithAttributes(labels...)) } - debug.Info("deleted") + debug.Info("resource deleted") e := event.NewCleanupPolicyEvent(policy, resource, nil) c.eventGen.Add(e) }