1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-28 10:28:36 +00:00

Introduced the DeletionPropagationPolicy field in CleanupPolicy and C… (#11368)

* Introduced the DeletionPropagationPolicy field in CleanupPolicy and ClusterCleanupPolicy

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

* Add DeletionPropagation field to API versions and implement handling in controller.go for improved cleanup functionality

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

* removed the type for DeletionPrpagationPolicy

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

* Add DeletionPropagationPolicy to CleanupPolicy

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

* Updated DeletionPropagationPolicy in different api versions

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

* Removed the string type declaration from the DeletionPropagationPolicy

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

* Reverted the changes

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

* Added the DeletionPropagation to the cleanup/controller.go

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

* Fixed minor bugs

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

* Fixed the verify-codegen error

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

* Revert "Fixed the verify-codegen error"

This reverts commit 05428bd99b187b93086dc403e674f06f8eeb5a40.

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

* Changed the field's type string to pointer

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

* Fixing the linter issue

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

* changing the v1 to metav1 in the controller.go

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

* Resolved linter problem

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

* fix: codegen

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* Refactor DeletionPropagationPolicy to use a pointer type

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

* Fixed linter

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

* Refactor String type to pointer in controller.go

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

* fixing Linter

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

* Make DeletionPropagationPolicy optional in validation

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

* fix: Update validation for DeletionPropagationPolicy in CleanupPolicySpec

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

* Refactored deletion policy handling

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

* Resolved linter

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

* Updated logic to set deletion options based on user-provided deletion policy.

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

* Removed default deletion policy assignment in the deletion logic

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

* Fix: removed default deletion policy in the deletion logic

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

* Fixing minor issues

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

* Fix: Linter

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

* fix: propagation policy

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* codegen

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: ShivamJha2436 <shivamkumar87148@gmail.com>
Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Shivam Kumar 2024-10-22 16:34:01 +05:30 committed by GitHub
parent 988c04f696
commit 1bc76f6d7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 400 additions and 14 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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