@@ -25,20 +25,25 @@ import (
25
25
"sigs.k8s.io/controller-runtime/pkg/client/fake"
26
26
)
27
27
28
- // NewFakeClientWithScheme creates a new fake client with the given scheme
29
- // for testing.
28
+ // NewFakeClientWithScheme creates a new fake client with the given scheme for testing.
30
29
// You can choose to initialize it with a slice of runtime.Object; all the objects with be given
31
- // a fake ResourceVersion="1" so it will be possible to use optimistic lock when patching conditions .
30
+ // a fake ResourceVersion="1" so it will be possible to use optimistic lock.
32
31
func NewFakeClientWithScheme (clientScheme * runtime.Scheme , initObjs ... runtime.Object ) client.Client {
33
- for _ , obj := range initObjs {
34
- accessor , err := meta .Accessor (obj )
32
+ // NOTE: for consistency with the NewFakeClientWithScheme func in controller runtime, this func
33
+ // should not have side effects on initObjs. So it creates a copy of each object and
34
+ // set the resourceVersion on the copy only.
35
+ initObjsWithResourceVersion := make ([]runtime.Object , len (initObjs ))
36
+ for i := range initObjs {
37
+ objsWithResourceVersion := initObjs [i ].DeepCopyObject ()
38
+ accessor , err := meta .Accessor (objsWithResourceVersion )
35
39
if err != nil {
36
40
panic (fmt .Errorf ("failed to get accessor for object: %v" , err ))
37
41
}
38
42
39
43
if accessor .GetResourceVersion () == "" {
40
44
accessor .SetResourceVersion ("1" )
41
45
}
46
+ initObjsWithResourceVersion [i ] = objsWithResourceVersion
42
47
}
43
- return fake .NewFakeClientWithScheme (clientScheme , initObjs ... )
48
+ return fake .NewFakeClientWithScheme (clientScheme , initObjsWithResourceVersion ... )
44
49
}
0 commit comments