Skip to content

Commit a9b2401

Browse files
committed
Fakeclient: Allow Update with empty ResourceVersion
1 parent 1c83ff6 commit a9b2401

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

pkg/client/fake/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
118118
if err != nil {
119119
return err
120120
}
121+
122+
// If the new object does not have the resource version set, default it to the resource version of the existing resource
123+
if accessor.GetResourceVersion() == "" {
124+
accessor.SetResourceVersion(oldAccessor.GetResourceVersion())
125+
}
121126
if accessor.GetResourceVersion() != oldAccessor.GetResourceVersion() {
122127
return apierrors.NewConflict(gvr.GroupResource(), accessor.GetName(), errors.New("object was modified"))
123128
}

pkg/client/fake/client_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,36 @@ var _ = Describe("Fake client", func() {
276276
Expect(obj.ObjectMeta.ResourceVersion).To(Equal("1"))
277277
})
278278

279+
It("should allow updates with non-set ResourceVersion", func() {
280+
By("Updating a new configmap")
281+
newcm := &corev1.ConfigMap{
282+
TypeMeta: metav1.TypeMeta{
283+
APIVersion: "v1",
284+
Kind: "ConfigMap",
285+
},
286+
ObjectMeta: metav1.ObjectMeta{
287+
Name: "test-cm",
288+
Namespace: "ns2",
289+
},
290+
Data: map[string]string{
291+
"test-key": "new-value",
292+
},
293+
}
294+
err := cl.Update(context.Background(), newcm)
295+
Expect(err).To(BeNil())
296+
297+
By("Getting the configmap")
298+
namespacedName := types.NamespacedName{
299+
Name: "test-cm",
300+
Namespace: "ns2",
301+
}
302+
obj := &corev1.ConfigMap{}
303+
err = cl.Get(context.Background(), namespacedName, obj)
304+
Expect(err).To(BeNil())
305+
Expect(obj).To(Equal(newcm))
306+
Expect(obj.ObjectMeta.ResourceVersion).To(Equal("1"))
307+
})
308+
279309
It("should reject updates with non-matching ResourceVersion", func() {
280310
By("Updating a new configmap")
281311
newcm := &corev1.ConfigMap{

0 commit comments

Comments
 (0)