@@ -34,6 +34,7 @@ import (
34
34
"k8s.io/apimachinery/pkg/types"
35
35
kscheme "k8s.io/client-go/kubernetes/scheme"
36
36
37
+ "sigs.k8s.io/controller-runtime/examples/crd/pkg"
37
38
"sigs.k8s.io/controller-runtime/pkg/client"
38
39
)
39
40
@@ -1397,6 +1398,33 @@ var _ = Describe("Client", func() {
1397
1398
PIt ("should fail if the GVK cannot be mapped to a Resource" , func () {
1398
1399
1399
1400
})
1401
+
1402
+ // Test this with an integrated type and a CRD to make sure it covers both proto
1403
+ // and json deserialization.
1404
+ for idx , object := range []client.Object {& corev1.ConfigMap {}, & pkg.ChaosPod {}} {
1405
+ idx , object := idx , object
1406
+ It (fmt .Sprintf ("should not retain any data in the obj variable that is not on the server for %T" , object ), func () {
1407
+ cl , err := client .New (cfg , client.Options {})
1408
+ Expect (err ).NotTo (HaveOccurred ())
1409
+ Expect (cl ).NotTo (BeNil ())
1410
+
1411
+ object .SetName (fmt .Sprintf ("retain-test-%d" , idx ))
1412
+ object .SetNamespace (ns )
1413
+
1414
+ By ("First creating the object" )
1415
+ toCreate := object .DeepCopyObject ().(client.Object )
1416
+ Expect (cl .Create (ctx , toCreate )).NotTo (HaveOccurred ())
1417
+
1418
+ By ("Fetching it into a variable that has finalizers set" )
1419
+ toGetInto := object .DeepCopyObject ().(client.Object )
1420
+ toGetInto .SetFinalizers ([]string {"some-finalizer" })
1421
+ Expect (cl .Get (ctx , client .ObjectKeyFromObject (object ), toGetInto )).NotTo (HaveOccurred ())
1422
+
1423
+ By ("Ensuring the created and the received object are equal" )
1424
+ Expect (toCreate ).Should (Equal (toGetInto ))
1425
+ })
1426
+ }
1427
+
1400
1428
})
1401
1429
1402
1430
Context ("with unstructured objects" , func () {
@@ -1469,6 +1497,30 @@ var _ = Describe("Client", func() {
1469
1497
err = cl .Get (context .TODO (), key , u )
1470
1498
Expect (err ).To (HaveOccurred ())
1471
1499
})
1500
+
1501
+ It ("should not retain any data in the obj variable that is not on the server" , func () {
1502
+ object := & unstructured.Unstructured {}
1503
+ cl , err := client .New (cfg , client.Options {})
1504
+ Expect (err ).NotTo (HaveOccurred ())
1505
+ Expect (cl ).NotTo (BeNil ())
1506
+
1507
+ object .SetName ("retain-unstructured" )
1508
+ object .SetNamespace (ns )
1509
+ object .SetAPIVersion ("chaosapps.metamagical.io/v1" )
1510
+ object .SetKind ("ChaosPod" )
1511
+
1512
+ By ("First creating the object" )
1513
+ toCreate := object .DeepCopyObject ().(client.Object )
1514
+ Expect (cl .Create (ctx , toCreate )).NotTo (HaveOccurred ())
1515
+
1516
+ By ("Fetching it into a variable that has finalizers set" )
1517
+ toGetInto := object .DeepCopyObject ().(client.Object )
1518
+ toGetInto .SetFinalizers ([]string {"some-finalizer" })
1519
+ Expect (cl .Get (ctx , client .ObjectKeyFromObject (object ), toGetInto )).NotTo (HaveOccurred ())
1520
+
1521
+ By ("Ensuring the created and the received object are equal" )
1522
+ Expect (toCreate ).Should (Equal (toGetInto ))
1523
+ })
1472
1524
})
1473
1525
Context ("with metadata objects" , func () {
1474
1526
It ("should fetch an existing object for a go struct" , func () {
@@ -1547,6 +1599,27 @@ var _ = Describe("Client", func() {
1547
1599
PIt ("should fail if the GVK cannot be mapped to a Resource" , func () {
1548
1600
1549
1601
})
1602
+
1603
+ It ("should not retain any data in the obj variable that is not on the server" , func () {
1604
+ cl , err := client .New (cfg , client.Options {})
1605
+ Expect (err ).NotTo (HaveOccurred ())
1606
+ Expect (cl ).NotTo (BeNil ())
1607
+
1608
+ By ("First creating the object" )
1609
+ toCreate := & pkg.ChaosPod {ObjectMeta : metav1.ObjectMeta {Name : "retain-metadata" , Namespace : ns }}
1610
+ Expect (cl .Create (ctx , toCreate )).NotTo (HaveOccurred ())
1611
+
1612
+ By ("Fetching it into a variable that has finalizers set" )
1613
+ toGetInto := & metav1.PartialObjectMetadata {
1614
+ TypeMeta : metav1.TypeMeta {APIVersion : "chaosapps.metamagical.io/v1" , Kind : "ChaosPod" },
1615
+ ObjectMeta : metav1.ObjectMeta {Namespace : ns , Name : "retain-metadata" },
1616
+ }
1617
+ toGetInto .SetFinalizers ([]string {"some-finalizer" })
1618
+ Expect (cl .Get (ctx , client .ObjectKeyFromObject (toGetInto ), toGetInto )).NotTo (HaveOccurred ())
1619
+
1620
+ By ("Ensuring the created and the received objects metadata are equal" )
1621
+ Expect (toCreate .ObjectMeta ).Should (Equal (toGetInto .ObjectMeta ))
1622
+ })
1550
1623
})
1551
1624
})
1552
1625
0 commit comments