Skip to content

Commit

Permalink
⚠️ Fakeclient: Reject Delete with mismatched ResourceVersion
Browse files Browse the repository at this point in the history
`Delete` takes optional varargs comprising `DeleteOption`s. Previously
fakeclient was validating that these could be run through
`ApplyOptions`, but otherwise ignoring them.

With this commit, fakeclient will observe a `DeleteOption` specifying a
`Precondition.ResourceVersion`, rejecting the deletion if the value does
not match what's on the "server", bringing fakeclient's behavior more in
line with a real client.

Note that, while technically a breaking change, any test that was
relying on the previous behavior was already broken.

xref #832 / 25ef372 which did something similar for `Create` and
`Update`. Major difference being that those methods do `ResourceVersion`
matching by default, whereas `Delete` only does it if opted in via the
`Precondition`.
  • Loading branch information
2uasimojo committed Jul 7, 2021
1 parent ef5c8a3 commit 49338fb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,22 @@ func (c *fakeClient) Delete(ctx context.Context, obj client.Object, opts ...clie
delOptions := client.DeleteOptions{}
delOptions.ApplyOptions(opts)

// Check the ResourceVersion if that Precondition was specified.
if delOptions.Preconditions != nil && delOptions.Preconditions.ResourceVersion != nil {
name := accessor.GetName()
dbObj, err := c.tracker.Get(gvr, accessor.GetNamespace(), name)
if err != nil {
return err
}
oldAccessor, err := meta.Accessor(dbObj)
if err != nil {
return err
}
if oldAccessor.GetResourceVersion() != *delOptions.Preconditions.ResourceVersion {
return apierrors.NewConflict(gvr.GroupResource(), name, errors.New("object was modified"))
}
}

return c.deleteObject(gvr, accessor)
}

Expand Down
43 changes: 42 additions & 1 deletion pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,33 @@ var _ = Describe("Fake client", func() {
Expect(obj.ObjectMeta.ResourceVersion).To(Equal(trackerAddResourceVersion))
})

It("should be able to Delete", func() {
It("should reject Delete with a mismatched ResourceVersion", func() {
bogusRV := "bogus"
By("Deleting with a mismatched ResourceVersion Precondition")
err := cl.Delete(context.Background(), dep, client.Preconditions{ResourceVersion: &bogusRV})
Expect(apierrors.IsConflict(err)).To(BeTrue())

list := &appsv1.DeploymentList{}
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(HaveLen(2))
Expect(list.Items).To(ConsistOf(*dep, *dep2))
})

It("should successfully Delete with a matching ResourceVersion", func() {
goodRV := trackerAddResourceVersion
By("Deleting with a mismatched ResourceVersion Precondition")
err := cl.Delete(context.Background(), dep, client.Preconditions{ResourceVersion: &goodRV})
Expect(err).To(BeNil())

list := &appsv1.DeploymentList{}
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(HaveLen(1))
Expect(list.Items).To(ConsistOf(*dep2))
})

It("should be able to Delete with no ResourceVersion Precondition", func() {
By("Deleting a deployment")
err := cl.Delete(context.Background(), dep)
Expect(err).To(BeNil())
Expand All @@ -581,6 +607,21 @@ var _ = Describe("Fake client", func() {
Expect(list.Items).To(ConsistOf(*dep2))
})

It("should be able to Delete with no opts even if object's ResourceVersion doesn't match server", func() {
By("Deleting a deployment")
depCopy := dep.DeepCopy()
depCopy.ResourceVersion = "bogus"
err := cl.Delete(context.Background(), depCopy)
Expect(err).To(BeNil())

By("Listing all deployments in the namespace")
list := &appsv1.DeploymentList{}
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(HaveLen(1))
Expect(list.Items).To(ConsistOf(*dep2))
})

It("should handle finalizers on Update", func() {
namespacedName := types.NamespacedName{
Name: "test-cm",
Expand Down

0 comments on commit 49338fb

Please sign in to comment.