Skip to content

Commit

Permalink
Delete resource when finalizers are empty in fake reactor
Browse files Browse the repository at this point in the history
When a resource is deleting and the finalizers become empty on update, delete
the resource. This mimics the real K8s behavior.

Also don't update the DeletionTimestamp on delete if already set.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Jan 24, 2024
1 parent 65bb17f commit fe55f45
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
23 changes: 23 additions & 0 deletions pkg/fake/basic_reactors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ var _ = Describe("Update", func() {
Expect(apierrors.IsConflict(err)).To(BeTrue())
})
})

When("the resource is deleting and the finalizers are empty", func() {
BeforeEach(func() {
t.pod.Finalizers = []string{"some-finalizer"}
})

It("should delete the resource", func() {
t.pod.SetDeletionTimestamp(ptr.To(metav1.Now()))
t.pod = t.doUpdateSuccess()

t.pod.Finalizers = nil
_, err := t.doUpdate()
Expect(err).To(Succeed())

_, err = t.doGet(t.pod.Name)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})
})
})

var _ = Describe("Delete", func() {
Expand Down Expand Up @@ -163,6 +181,11 @@ var _ = Describe("Delete", func() {
actual, err := t.doGet(t.pod.Name)
Expect(err).To(Succeed())
Expect(actual.GetDeletionTimestamp()).ToNot(BeNil())

Expect(t.doDelete(metav1.DeleteOptions{})).To(Succeed())
unchanged, err := t.doGet(t.pod.Name)
Expect(err).To(Succeed())
Expect(unchanged.GetResourceVersion()).To(Equal(actual.GetResourceVersion()))
})
})
})
Expand Down
8 changes: 6 additions & 2 deletions pkg/fake/delete_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/testing"
"k8s.io/utils/ptr"
)

type deleteReactor struct {
Expand Down Expand Up @@ -62,8 +63,11 @@ func (r *deleteReactor) react(a testing.Action) (bool, runtime.Object, error) {
}

if len(existing.GetFinalizers()) > 0 {
now := metav1.Now()
existing.SetDeletionTimestamp(&now)
if !existing.GetDeletionTimestamp().IsZero() {
return true, existingObj, nil
}

existing.SetDeletionTimestamp(ptr.To(metav1.Now()))

obj, err := invokeReactors(testing.NewUpdateAction(action.GetResource(), action.GetNamespace(),
existingObj), r.reactors)
Expand Down
4 changes: 4 additions & 0 deletions pkg/fake/update_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,9 @@ func (r *updateReactor) react(a testing.Action) (bool, runtime.Object, error) {

obj, err := invokeReactors(action, r.reactors)

if err == nil && !target.GetDeletionTimestamp().IsZero() && len(target.GetFinalizers()) == 0 {
_, err = invokeReactors(testing.NewDeleteAction(action.GetResource(), action.GetNamespace(), target.GetName()), r.reactors)
}

return true, obj, err
}

0 comments on commit fe55f45

Please sign in to comment.