Skip to content

Commit 68cdf84

Browse files
committed
Update object upon deletion
If an object is deleted and the API responds with a resource, update the deleted object. This causes the object to be updated with the deletion timestamp.
1 parent bbc9711 commit 68cdf84

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pkg/client/client_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"sigs.k8s.io/controller-runtime/examples/crd/pkg"
5151
"sigs.k8s.io/controller-runtime/pkg/cache"
5252
"sigs.k8s.io/controller-runtime/pkg/client"
53+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
5354
)
5455

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

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

1659+
It("should update the resource when deleting if it receives a response", func() {
1660+
cl, err := client.New(cfg, client.Options{})
1661+
Expect(err).NotTo(HaveOccurred())
1662+
Expect(cl).NotTo(BeNil())
1663+
1664+
By("initially creating a Node")
1665+
node, err := clientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{})
1666+
Expect(err).NotTo(HaveOccurred())
1667+
1668+
By("adding a finalizer we prevent the node from being deleted immediately")
1669+
controllerutil.AddFinalizer(node, "example.com/test")
1670+
node, err = clientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
1671+
Expect(err).NotTo(HaveOccurred())
1672+
1673+
By("deleting the Node")
1674+
nodeName := node.Name
1675+
err = cl.Delete(context.TODO(), node)
1676+
Expect(err).NotTo(HaveOccurred())
1677+
Expect(node.ObjectMeta.DeletionTimestamp).NotTo(BeNil())
1678+
1679+
By("removing the finalizer")
1680+
controllerutil.RemoveFinalizer(node, "example.com/test")
1681+
_, err = clientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
1682+
Expect(err).NotTo(HaveOccurred())
1683+
1684+
By("validating the Node no longer exists")
1685+
_, err = clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
1686+
Expect(err).To(HaveOccurred())
1687+
})
1688+
16571689
PIt("should fail if the object doesn't have meta", func() {
16581690

16591691
})

pkg/client/typed_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (c *typedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOpti
8585
Name(o.GetName()).
8686
Body(deleteOpts.AsDeleteOptions()).
8787
Do(ctx).
88-
Error()
88+
Into(obj)
8989
}
9090

9191
// DeleteAllOf implements client.Client.

0 commit comments

Comments
 (0)