Skip to content
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 @@ -51,6 +51,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 @@ -1733,6 +1734,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
nodeName := node.Name
err = cl.Delete(ctx, 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 @@ -1749,6 +1751,36 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
Expect(err).To(HaveOccurred())
})

It("should update the resource when deleting if it receives a response", func(ctx SpecContext) {
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 @@ -1908,7 +1940,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(ctx SpecContext) {
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(ctx SpecContext) {
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 @@ -180,7 +180,8 @@ var _ = Describe("DryRunClient", func() {
})

It("should not delete objects", func(ctx SpecContext) {
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 @@ -191,7 +192,8 @@ var _ = Describe("DryRunClient", func() {
It("should not delete objects with opts", func(ctx SpecContext) {
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 @@ -87,7 +87,7 @@ func (c *typedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOpti
Name(o.name).
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 @@ -112,7 +112,7 @@ func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...De
Name(o.name).
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

Copy link
Member

Choose a reason for hiding this comment

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

Should we maybe just check if the returned body is a status and only if its not update the original object?

Copy link
Member

Choose a reason for hiding this comment

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

As far as I can tell there's no way to tell apart you got the object back or a metav1.Status unless you actually read the request body.

Copy link
Member

Choose a reason for hiding this comment

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

Which I think might be okay, the main overhead will be that we have to deserialize it twice if it is not a status

Copy link
Member

@sbueringer sbueringer Sep 15, 2025

Choose a reason for hiding this comment

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

I think it would be a bad UX if after calling client.Delete() you only sometimes see the deletionTimestamp set :/

Copy link
Member

Choose a reason for hiding this comment

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

In general, it appears if it's gone from storage you'll get Status object in the response body. (You can try Pod deletion with grace period.) But I don't think there's a guarantee around any of this in the API machinery.

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 it would be a bad UX if after calling client.Delete() you only sometimes see the deletionTimestamp set :/

I mean what can we realistically put in there if the object is gone from storage? I guess we could put a non-nil fake deletiontimestamp in there but not sure if that is a good idea.

What is IMHO annoying about the current state is that you can not do a check of Did I already delete this

Copy link
Member

Choose a reason for hiding this comment

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

Since Delete() already doesn't do what we expect it to do, what if we changed its return signature to (deletedFromStorage bool, err error)?

Copy link
Member

Choose a reason for hiding this comment

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

I mean what can we realistically put in there if the object is gone from storage? I guess we could put a non-nil fake deletiontimestamp in there but not sure if that is a good idea.

Yup agree. I mostly just meant that if deletionTimestamp is only sometimes set (aka in some cases) this would be tricky to rely on

}

// DeleteAllOf implements client.Client.
Expand Down