Skip to content

🐛 Update object upon deletion #3098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"sigs.k8s.io/controller-runtime/examples/crd/pkg"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func deleteDeployment(ctx context.Context, dep *appsv1.Deployment, ns string) {
Expand Down Expand Up @@ -1638,6 +1639,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
nodeName := node.Name
err = cl.Delete(context.TODO(), node)
Expect(err).NotTo(HaveOccurred())
Expect(node.ObjectMeta.DeletionTimestamp).To(BeNil())

By("validating the Node no longer exists")
_, err = clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
Expand All @@ -1654,6 +1656,36 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
Expect(err).To(HaveOccurred())
})

It("should update the resource when deleting if it receives a response", func() {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

By("initially creating a Node")
node, err := clientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred())

By("adding a finalizer we prevent the node from being deleted immediately")
controllerutil.AddFinalizer(node, "example.com/test")
node, err = clientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred())

By("deleting the Node")
nodeName := node.Name
err = cl.Delete(ctx, node)
Expect(err).NotTo(HaveOccurred())
Expect(node.ObjectMeta.DeletionTimestamp).NotTo(BeNil())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also test this with unstructured.Unstructured and metav1.MetaObject, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unstructured works... metav1.MetaObject will require a fair bit more work.

return resInt.Delete(ctx, metadata.Name, *deleteOpts.AsDeleteOptions())

Not sure if this is relevant?

// TODO(directxman12): we could rewrite this on top of the low-level REST

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also implement this for the metadata client. It's already tricky that we change a nuance like this, but I think it's a lot worse if our clients behave inconsistently


By("removing the finalizer")
controllerutil.RemoveFinalizer(node, "example.com/test")
_, err = clientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred())

By("validating the Node no longer exists")
_, err = clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
})

PIt("should fail if the object doesn't have meta", func() {

})
Expand Down Expand Up @@ -1813,7 +1845,48 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
_, err = clientset.AppsV1().Deployments(ns).Get(ctx, dep2Name, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
})

It("should update the resource when deleting if it receives a response", func() {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

By("initially creating a Node")
node, err := clientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred())

By("adding a finalizer we prevent the node from being deleted immediately")
controllerutil.AddFinalizer(node, "example.com/test")
node, err = clientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred())

By("deleting the Node")
nodeName := node.Name
u := &unstructured.Unstructured{}
Expect(scheme.Convert(node, u, nil)).To(Succeed())
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Kind: "Node",
Version: "v1",
})
err = cl.Delete(ctx, u)
Expect(err).NotTo(HaveOccurred())

accessor, err := meta.Accessor(u)
Expect(err).NotTo(HaveOccurred())
Expect(accessor.GetDeletionTimestamp()).NotTo(BeNil())

By("removing the finalizer")
controllerutil.RemoveFinalizer(u, "example.com/test")
err = cl.Delete(ctx, u)
Expect(err).NotTo(HaveOccurred())

By("validating the Node no longer exists")
_, err = clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
})
})

Context("with metadata objects", func() {
It("should delete an existing object from a go struct", func() {
cl, err := client.New(cfg, client.Options{})
Expand Down
6 changes: 4 additions & 2 deletions pkg/client/dryrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ var _ = Describe("DryRunClient", func() {
})

It("should not delete objects", func() {
Expect(getClient().Delete(ctx, dep)).NotTo(HaveOccurred())
changedDep := dep.DeepCopy()
Expect(getClient().Delete(ctx, changedDep)).NotTo(HaveOccurred())

actual, err := clientset.AppsV1().Deployments(ns).Get(ctx, dep.Name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -193,7 +194,8 @@ var _ = Describe("DryRunClient", func() {
It("should not delete objects with opts", func() {
opts := &client.DeleteOptions{DryRun: []string{"Bye", "Pippa"}}

Expect(getClient().Delete(ctx, dep, opts)).NotTo(HaveOccurred())
changedDep := dep.DeepCopy()
Expect(getClient().Delete(ctx, changedDep, opts)).NotTo(HaveOccurred())

actual, err := clientset.AppsV1().Deployments(ns).Get(ctx, dep.Name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *typedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOpti
Name(o.GetName()).
Body(deleteOpts.AsDeleteOptions()).
Do(ctx).
Error()
Into(obj)
}

// DeleteAllOf implements client.Client.
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unstructured_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...De
Name(o.GetName()).
Body(deleteOpts.AsDeleteOptions()).
Do(ctx).
Error()
Into(obj)
Copy link
Member

@sbueringer sbueringer May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this through the debugger and this object looks like this afterwards if I'm not mistaken:

image

(I assume this is the case when we don't have a finalizer on the object, but this is still bad?)

Copy link
Member

@sbueringer sbueringer May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need a test case that when we delete via Unstructured without a finalizer the object keeps its data and does not become a Status object

}

// DeleteAllOf implements client.Client.
Expand Down
Loading