Skip to content

Commit

Permalink
Fakeclient: Allow Update with empty ResourceVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
detiber committed Apr 24, 2020
1 parent 1c83ff6 commit a9b2401
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
if err != nil {
return err
}

// If the new object does not have the resource version set, default it to the resource version of the existing resource
if accessor.GetResourceVersion() == "" {
accessor.SetResourceVersion(oldAccessor.GetResourceVersion())
}
if accessor.GetResourceVersion() != oldAccessor.GetResourceVersion() {
return apierrors.NewConflict(gvr.GroupResource(), accessor.GetName(), errors.New("object was modified"))
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,36 @@ var _ = Describe("Fake client", func() {
Expect(obj.ObjectMeta.ResourceVersion).To(Equal("1"))
})

It("should allow updates with non-set ResourceVersion", func() {
By("Updating a new configmap")
newcm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-cm",
Namespace: "ns2",
},
Data: map[string]string{
"test-key": "new-value",
},
}
err := cl.Update(context.Background(), newcm)
Expect(err).To(BeNil())

By("Getting the configmap")
namespacedName := types.NamespacedName{
Name: "test-cm",
Namespace: "ns2",
}
obj := &corev1.ConfigMap{}
err = cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(BeNil())
Expect(obj).To(Equal(newcm))
Expect(obj.ObjectMeta.ResourceVersion).To(Equal("1"))
})

It("should reject updates with non-matching ResourceVersion", func() {
By("Updating a new configmap")
newcm := &corev1.ConfigMap{
Expand Down

0 comments on commit a9b2401

Please sign in to comment.