Skip to content

Commit 81309dc

Browse files
authored
Merge pull request #1582 from 2uasimojo/fake-delete-ResourceVersion
⚠️ Fakeclient: Reject Delete with mismatched ResourceVersion
2 parents ef5c8a3 + 5fb1382 commit 81309dc

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

pkg/client/fake/client.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,28 @@ func (c *fakeClient) Delete(ctx context.Context, obj client.Object, opts ...clie
419419
delOptions := client.DeleteOptions{}
420420
delOptions.ApplyOptions(opts)
421421

422+
// Check the ResourceVersion if that Precondition was specified.
423+
if delOptions.Preconditions != nil && delOptions.Preconditions.ResourceVersion != nil {
424+
name := accessor.GetName()
425+
dbObj, err := c.tracker.Get(gvr, accessor.GetNamespace(), name)
426+
if err != nil {
427+
return err
428+
}
429+
oldAccessor, err := meta.Accessor(dbObj)
430+
if err != nil {
431+
return err
432+
}
433+
actualRV := oldAccessor.GetResourceVersion()
434+
expectRV := *delOptions.Preconditions.ResourceVersion
435+
if actualRV != expectRV {
436+
msg := fmt.Sprintf(
437+
"the ResourceVersion in the precondition (%s) does not match the ResourceVersion in record (%s). "+
438+
"The object might have been modified",
439+
expectRV, actualRV)
440+
return apierrors.NewConflict(gvr.GroupResource(), name, errors.New(msg))
441+
}
442+
}
443+
422444
return c.deleteObject(gvr, accessor)
423445
}
424446

pkg/client/fake/client_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,33 @@ var _ = Describe("Fake client", func() {
568568
Expect(obj.ObjectMeta.ResourceVersion).To(Equal(trackerAddResourceVersion))
569569
})
570570

571-
It("should be able to Delete", func() {
571+
It("should reject Delete with a mismatched ResourceVersion", func() {
572+
bogusRV := "bogus"
573+
By("Deleting with a mismatched ResourceVersion Precondition")
574+
err := cl.Delete(context.Background(), dep, client.Preconditions{ResourceVersion: &bogusRV})
575+
Expect(apierrors.IsConflict(err)).To(BeTrue())
576+
577+
list := &appsv1.DeploymentList{}
578+
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
579+
Expect(err).To(BeNil())
580+
Expect(list.Items).To(HaveLen(2))
581+
Expect(list.Items).To(ConsistOf(*dep, *dep2))
582+
})
583+
584+
It("should successfully Delete with a matching ResourceVersion", func() {
585+
goodRV := trackerAddResourceVersion
586+
By("Deleting with a matching ResourceVersion Precondition")
587+
err := cl.Delete(context.Background(), dep, client.Preconditions{ResourceVersion: &goodRV})
588+
Expect(err).To(BeNil())
589+
590+
list := &appsv1.DeploymentList{}
591+
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
592+
Expect(err).To(BeNil())
593+
Expect(list.Items).To(HaveLen(1))
594+
Expect(list.Items).To(ConsistOf(*dep2))
595+
})
596+
597+
It("should be able to Delete with no ResourceVersion Precondition", func() {
572598
By("Deleting a deployment")
573599
err := cl.Delete(context.Background(), dep)
574600
Expect(err).To(BeNil())
@@ -581,6 +607,21 @@ var _ = Describe("Fake client", func() {
581607
Expect(list.Items).To(ConsistOf(*dep2))
582608
})
583609

610+
It("should be able to Delete with no opts even if object's ResourceVersion doesn't match server", func() {
611+
By("Deleting a deployment")
612+
depCopy := dep.DeepCopy()
613+
depCopy.ResourceVersion = "bogus"
614+
err := cl.Delete(context.Background(), depCopy)
615+
Expect(err).To(BeNil())
616+
617+
By("Listing all deployments in the namespace")
618+
list := &appsv1.DeploymentList{}
619+
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
620+
Expect(err).To(BeNil())
621+
Expect(list.Items).To(HaveLen(1))
622+
Expect(list.Items).To(ConsistOf(*dep2))
623+
})
624+
584625
It("should handle finalizers on Update", func() {
585626
namespacedName := types.NamespacedName{
586627
Name: "test-cm",

0 commit comments

Comments
 (0)