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

Test/ttl cleanup deletion policy (#11277)

* Added propagationPolicy to TTL controller for resource deletion

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

* Added per-resource deletion policy with optional global default

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

* Fix: Added nil check for annotations to prevent runtime errors

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

* Fix: Moved logic for retrieving propagation policy from annotations to a dedicated function

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

* Fix: Resolve merge conflict in controller.go and update propagation policy handling
- Added missing gvr field from main branch
- Improved annotation naming as per team discussion
- Moved propagation policy logic closer to usage

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

* Simplified propagation policy logic

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

* Add unit tests for controller.go

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

* Removed the test file for controller.go and fixed the lint error

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

* test: add unit tests for deletion propagation policy in TTL-based cleanup

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

* Resolved conflicts

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

* Resolved conflicts

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

* Resolved merge conflict

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

* Fix:Updated the unit tests

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

* Fix: Removed the method receiver from determinePropagationPolicy as it does not require access to the controller struct

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

* Fix: Fixed the formatting issue

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

* Fix: removed the switch statement inside the test case

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

* Updated the controller.go

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

* Resolved the formatting issue

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

* Fixing the deleted resource label in controller.go

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

* Fixed the linter issue

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-10-07 19:14:55 +05:30 committed by GitHub
parent 53922941d6
commit 9f5e75a2b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 90 additions and 4 deletions

View file

@ -119,13 +119,13 @@ func (c *controller) deregisterEventHandlers() {
}
// Function to determine the deletion propagation policy
func (c *controller) determinePropagationPolicy(metaObj metav1.Object, logger logr.Logger) *metav1.DeletionPropagation {
func determinePropagationPolicy(metaObj metav1.Object, logger logr.Logger) *metav1.DeletionPropagation {
annotations := metaObj.GetAnnotations()
var policy *metav1.DeletionPropagation
if annotations != nil {
annotationPolicy := annotations["kyverno.AnnotationCleanupPropagationPolicy"]
if annotationPolicy == "" {
annotationPolicy := annotations[kyverno.AnnotationCleanupPropagationPolicy]
if annotationPolicy != "" {
switch annotationPolicy {
case "Foreground":
fg := metav1.DeletePropagationForeground
@ -193,7 +193,7 @@ func (c *controller) reconcile(ctx context.Context, logger logr.Logger, itemKey
}
if time.Now().After(deletionTime) {
deleteOptions := metav1.DeleteOptions{
PropagationPolicy: c.determinePropagationPolicy(metaObj, logger),
PropagationPolicy: determinePropagationPolicy(metaObj, logger),
}
err = c.client.Namespace(namespace).Delete(context.Background(), metaObj.GetName(), deleteOptions)
if err != nil {

View file

@ -0,0 +1,86 @@
package ttl
import (
"testing"
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/api/kyverno"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// TestDeterminePropagationPolicy tests the determinePropagationPolicy function
func TestDeterminePropagationPolicy(t *testing.T) {
logger := logr.Discard() // Use a no-op logger
testCases := []struct {
name string
annotations map[string]string
expectedPolicy *metav1.DeletionPropagation
}{
{
name: "No annotations",
annotations: nil,
expectedPolicy: nil,
},
{
name: "Foreground policy",
annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "Foreground",
},
expectedPolicy: func() *metav1.DeletionPropagation {
fg := metav1.DeletePropagationForeground
return &fg
}(),
},
{
name: "Background policy",
annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "Background",
},
expectedPolicy: func() *metav1.DeletionPropagation {
bg := metav1.DeletePropagationBackground
return &bg
}(),
},
{
name: "Orphan policy",
annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "Orphan",
},
expectedPolicy: func() *metav1.DeletionPropagation {
orphan := metav1.DeletePropagationOrphan
return &orphan
}(),
},
{
name: "Empty annotation",
annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "",
},
expectedPolicy: nil,
},
{
name: "Unknown policy",
annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "Unknown",
},
expectedPolicy: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Mock metadata object with annotations
metaObj := &metav1.ObjectMeta{
Annotations: tc.annotations,
}
// Call the function
policy := determinePropagationPolicy(metaObj, logger)
// Assert the result
assert.Equal(t, tc.expectedPolicy, policy)
})
}
}