@@ -1209,6 +1209,42 @@ var _ = Describe("Client", func() {
1209
1209
close (done )
1210
1210
})
1211
1211
1212
+ It ("should patch an existing object from a go struct, using optimistic locking" , func (done Done ) {
1213
+ cl , err := client .New (cfg , client.Options {})
1214
+ Expect (err ).NotTo (HaveOccurred ())
1215
+ Expect (cl ).NotTo (BeNil ())
1216
+
1217
+ By ("initially creating a Deployment" )
1218
+ dep , err := clientset .AppsV1 ().Deployments (ns ).Create (dep )
1219
+ Expect (err ).NotTo (HaveOccurred ())
1220
+
1221
+ By ("creating a patch from with optimistic lock" )
1222
+ patch := client .MergeFromWithOptions (dep .DeepCopy (), client.MergeFromWithOptimisticLock {})
1223
+
1224
+ By ("adding a new annotation" )
1225
+ dep .Annotations = map [string ]string {
1226
+ "foo" : "bar" ,
1227
+ }
1228
+
1229
+ By ("patching the Deployment" )
1230
+ err = cl .Patch (context .TODO (), dep , patch )
1231
+ Expect (err ).NotTo (HaveOccurred ())
1232
+
1233
+ By ("validating patched Deployment has new annotation" )
1234
+ actual , err := clientset .AppsV1 ().Deployments (ns ).Get (dep .Name , metav1.GetOptions {})
1235
+ Expect (err ).NotTo (HaveOccurred ())
1236
+ Expect (actual ).NotTo (BeNil ())
1237
+ Expect (actual .Annotations ["foo" ]).To (Equal ("bar" ))
1238
+
1239
+ By ("validating that a patch should fail with conflict, when it has an outdated resource version" )
1240
+ dep .Annotations ["should" ] = "conflict"
1241
+ err = cl .Patch (context .TODO (), dep , patch )
1242
+ Expect (err ).To (HaveOccurred ())
1243
+ Expect (apierrors .IsConflict (err )).To (BeTrue ())
1244
+
1245
+ close (done )
1246
+ })
1247
+
1212
1248
It ("should patch and preserve type information" , func (done Done ) {
1213
1249
cl , err := client .New (cfg , client.Options {})
1214
1250
Expect (err ).NotTo (HaveOccurred ())
@@ -2655,8 +2691,9 @@ var _ = Describe("Patch", func() {
2655
2691
BeforeEach (func () {
2656
2692
cm = & corev1.ConfigMap {
2657
2693
ObjectMeta : metav1.ObjectMeta {
2658
- Namespace : metav1 .NamespaceDefault ,
2659
- Name : "cm" ,
2694
+ Namespace : metav1 .NamespaceDefault ,
2695
+ Name : "cm" ,
2696
+ ResourceVersion : "10" ,
2660
2697
},
2661
2698
}
2662
2699
})
@@ -2685,6 +2722,31 @@ var _ = Describe("Patch", func() {
2685
2722
By ("returning a patch with data only containing the annotation change" )
2686
2723
Expect (data ).To (Equal ([]byte (fmt .Sprintf (`{"metadata":{"annotations":{"%s":"%s"}}}` , annotationKey , annotationValue ))))
2687
2724
})
2725
+
2726
+ It ("creates a merge patch with the modifications applied during the mutation, using optimistic locking" , func () {
2727
+ const (
2728
+ annotationKey = "test"
2729
+ annotationValue = "foo"
2730
+ )
2731
+
2732
+ By ("creating a merge patch" )
2733
+ patch := client .MergeFromWithOptions (cm .DeepCopy (), client.MergeFromWithOptimisticLock {})
2734
+
2735
+ By ("returning a patch with type MergePatch" )
2736
+ Expect (patch .Type ()).To (Equal (types .MergePatchType ))
2737
+
2738
+ By ("retrieving modifying the config map" )
2739
+ metav1 .SetMetaDataAnnotation (& cm .ObjectMeta , annotationKey , annotationValue )
2740
+
2741
+ By ("computing the patch data" )
2742
+ data , err := patch .Data (cm )
2743
+
2744
+ By ("returning no error" )
2745
+ Expect (err ).NotTo (HaveOccurred ())
2746
+
2747
+ By ("returning a patch with data containing the annotation change and the resourceVersion change" )
2748
+ Expect (data ).To (Equal ([]byte (fmt .Sprintf (`{"metadata":{"annotations":{"%s":"%s"},"resourceVersion":"%s"}}` , annotationKey , annotationValue , cm .ResourceVersion ))))
2749
+ })
2688
2750
})
2689
2751
})
2690
2752
0 commit comments