Skip to content

Commit f7bc1a7

Browse files
committed
fix: handle existing resources gracefully in create operation #2351
When a test exits unexpectedly after creating resources, those resources aren't cleaned up. When the test is run again, it detects the existing resources but returns an error instead of cleaning them up, leaving them stale indefinitely. This change modifies the tryCreateResource method to not return an error when a resource already exists, but instead continue with the operation. This ensures that pre-existing resources are properly cleaned up at the end of the test. Fixes #2351 Signed-off-by: Ashish Thirunagari <[email protected]>
1 parent dfe952c commit f7bc1a7

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pkg/engine/operations/create/operation.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package create
22

33
import (
44
"context"
5-
"errors"
65

76
"github.com/kyverno/chainsaw/pkg/apis"
87
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
@@ -101,7 +100,11 @@ func (o *operation) tryCreateResource(ctx context.Context, bindings apis.Binding
101100
actual.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())
102101
err := o.client.Get(ctx, client.Key(&obj), &actual)
103102
if err == nil {
104-
return nil, errors.New("the resource already exists in the cluster")
103+
// Add pre-existing resource to cleaner for proper cleanup
104+
if o.cleaner != nil {
105+
o.cleaner.Add(o.client, &actual)
106+
}
107+
return o.handleCheck(ctx, bindings, actual, nil)
105108
}
106109
if kerrors.IsNotFound(err) {
107110
return o.createResource(ctx, bindings, obj)

pkg/engine/operations/create/operation_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/kyverno/chainsaw/pkg/mocks"
1616
"github.com/stretchr/testify/assert"
1717
kerrors "k8s.io/apimachinery/pkg/api/errors"
18+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1920
"k8s.io/utils/ptr"
2021
)
@@ -52,9 +53,13 @@ func Test_create(t *testing.T) {
5253
*obj.(*unstructured.Unstructured) = pod
5354
return nil
5455
},
56+
DeleteFn: func(ctx context.Context, _ int, obj client.Object, opts ...client.DeleteOption) error {
57+
return nil
58+
},
5559
},
5660
expect: nil,
57-
expectedErr: errors.New("the resource already exists in the cluster"),
61+
expectedErr: nil,
62+
cleaner: cleaner.New(0*time.Second, nil, metav1.DeletePropagationBackground),
5863
}, {
5964
name: "Dry Run Resource already exists",
6065
object: pod,
@@ -65,7 +70,8 @@ func Test_create(t *testing.T) {
6570
},
6671
},
6772
expect: nil,
68-
expectedErr: errors.New("the resource already exists in the cluster"),
73+
expectedErr: nil,
74+
cleaner: cleaner.New(0*time.Second, nil, metav1.DeletePropagationBackground),
6975
}, {
7076
name: "Resource does not exist, create it",
7177
object: pod,
@@ -235,6 +241,10 @@ func Test_create(t *testing.T) {
235241
} else {
236242
assert.NoError(t, err)
237243
}
244+
if tt.name == "Resource already exists" && tt.cleaner != nil {
245+
assert.False(t, tt.cleaner.Empty(), "Cleaner should contain the pre-existing resource for cleanup")
246+
assert.NotNil(t, tt.client.DeleteFn, "DeleteFn should be set for this test case")
247+
}
238248
})
239249
}
240250
}

0 commit comments

Comments
 (0)