@@ -38,6 +38,7 @@ import (
38
38
kscheme "k8s.io/client-go/kubernetes/scheme"
39
39
"k8s.io/client-go/rest"
40
40
kcache "k8s.io/client-go/tools/cache"
41
+ "k8s.io/utils/pointer"
41
42
42
43
"sigs.k8s.io/controller-runtime/pkg/cache"
43
44
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -119,13 +120,10 @@ var _ = Describe("Informer Cache", func() {
119
120
var _ = Describe ("Multi-Namespace Informer Cache" , func () {
120
121
CacheTest (cache .MultiNamespacedCacheBuilder ([]string {testNamespaceOne , testNamespaceTwo , "default" }), cache.Options {})
121
122
})
122
- var _ = Describe ("Informer Cache without DeepCopy" , func () {
123
+
124
+ var _ = Describe ("Informer Cache without global DeepCopy" , func () {
123
125
CacheTest (cache .New , cache.Options {
124
- View : cache.ViewOptions {
125
- ByObject : cache.ViewByObject {
126
- UnsafeDisableDeepCopy : cache.DisableDeepCopyByObject {cache.ObjectAll {}: true },
127
- },
128
- },
126
+ UnsafeDisableDeepCopy : pointer .Bool (true ),
129
127
})
130
128
})
131
129
@@ -190,48 +188,46 @@ var _ = Describe("Cache with transformers", func() {
190
188
191
189
By ("creating the informer cache" )
192
190
informerCache , err = cache .New (cfg , cache.Options {
193
- View : cache.ViewOptions {
194
- DefaultTransform : func (i interface {}) (interface {}, error ) {
195
- obj := i .(runtime.Object )
196
- Expect (obj ).NotTo (BeNil ())
191
+ DefaultTransform : func (i interface {}) (interface {}, error ) {
192
+ obj := i .(runtime.Object )
193
+ Expect (obj ).NotTo (BeNil ())
197
194
198
- accessor , err := meta .Accessor (obj )
199
- Expect (err ).To (BeNil ())
200
- annotations := accessor .GetAnnotations ()
201
-
202
- if _ , exists := annotations ["transformed" ]; exists {
203
- // Avoid performing transformation multiple times.
204
- return i , nil
205
- }
195
+ accessor , err := meta .Accessor (obj )
196
+ Expect (err ).To (BeNil ())
197
+ annotations := accessor .GetAnnotations ()
206
198
207
- if annotations == nil {
208
- annotations = make (map [string ]string )
209
- }
210
- annotations ["transformed" ] = "default"
211
- accessor .SetAnnotations (annotations )
199
+ if _ , exists := annotations ["transformed" ]; exists {
200
+ // Avoid performing transformation multiple times.
212
201
return i , nil
213
- },
214
- ByObject : cache. ViewByObject {
215
- Transform : cache. TransformByObject {
216
- & corev1. Pod {}: func ( i interface {}) ( interface {}, error ) {
217
- obj := i .(runtime. Object )
218
- Expect ( obj ). NotTo ( BeNil ())
219
- accessor , err := meta . Accessor ( obj )
220
- Expect ( err ). To ( BeNil ())
221
-
222
- annotations := accessor . GetAnnotations ()
223
- if _ , exists := annotations [ "transformed" ]; exists {
224
- // Avoid performing transformation multiple times.
225
- return i , nil
226
- }
227
-
228
- if annotations == nil {
229
- annotations = make ( map [ string ] string )
230
- }
231
- annotations ["transformed" ] = "explicit"
232
- accessor . SetAnnotations ( annotations )
202
+ }
203
+
204
+ if annotations == nil {
205
+ annotations = make ( map [ string ] string )
206
+ }
207
+ annotations [ "transformed" ] = "default"
208
+ accessor . SetAnnotations ( annotations )
209
+ return i , nil
210
+ },
211
+ ByObject : map [client. Object ]cache. ByObject {
212
+ & corev1. Pod {}: {
213
+ Transform : func ( i interface {}) ( interface {}, error ) {
214
+ obj := i .(runtime. Object )
215
+ Expect ( obj ). NotTo ( BeNil ())
216
+ accessor , err := meta . Accessor ( obj )
217
+ Expect ( err ). To ( BeNil ())
218
+
219
+ annotations := accessor . GetAnnotations ()
220
+ if _ , exists := annotations ["transformed" ]; exists {
221
+ // Avoid performing transformation multiple times.
233
222
return i , nil
234
- },
223
+ }
224
+
225
+ if annotations == nil {
226
+ annotations = make (map [string ]string )
227
+ }
228
+ annotations ["transformed" ] = "explicit"
229
+ accessor .SetAnnotations (annotations )
230
+ return i , nil
235
231
},
236
232
},
237
233
},
@@ -370,13 +366,9 @@ var _ = Describe("Cache with selectors", func() {
370
366
}
371
367
372
368
opts := cache.Options {
373
- View : cache.ViewOptions {
374
- DefaultSelector : cache.ObjectSelector {Field : fields .OneTermEqualSelector ("metadata.namespace" , testNamespaceTwo )},
375
- ByObject : cache.ViewByObject {
376
- Selectors : cache.SelectorsByObject {
377
- & corev1.ServiceAccount {}: {Field : fields .OneTermEqualSelector ("metadata.namespace" , testNamespaceOne )},
378
- },
379
- },
369
+ DefaultSelector : cache.ObjectSelector {Field : fields .OneTermEqualSelector ("metadata.namespace" , testNamespaceTwo )},
370
+ ByObject : map [client.Object ]cache.ByObject {
371
+ & corev1.ServiceAccount {}: {Field : fields .OneTermEqualSelector ("metadata.namespace" , testNamespaceOne )},
380
372
},
381
373
}
382
374
@@ -798,7 +790,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
798
790
799
791
It ("should be able to restrict cache to a namespace" , func () {
800
792
By ("creating a namespaced cache" )
801
- namespacedCache , err := cache .New (cfg , cache.Options {View : cache. ViewOptions { Namespaces : []string {testNamespaceOne } }})
793
+ namespacedCache , err := cache .New (cfg , cache.Options {Namespaces : []string {testNamespaceOne }})
802
794
Expect (err ).NotTo (HaveOccurred ())
803
795
804
796
By ("running the cache and waiting for it to sync" )
@@ -1079,7 +1071,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
1079
1071
1080
1072
It ("should be able to restrict cache to a namespace" , func () {
1081
1073
By ("creating a namespaced cache" )
1082
- namespacedCache , err := cache .New (cfg , cache.Options {View : cache. ViewOptions { Namespaces : []string {testNamespaceOne } }})
1074
+ namespacedCache , err := cache .New (cfg , cache.Options {Namespaces : []string {testNamespaceOne }})
1083
1075
Expect (err ).NotTo (HaveOccurred ())
1084
1076
1085
1077
By ("running the cache and waiting for it to sync" )
@@ -1233,14 +1225,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
1233
1225
By ("creating the cache" )
1234
1226
builder := cache .BuilderWithOptions (
1235
1227
cache.Options {
1236
- View : cache.ViewOptions {
1237
- ByObject : cache.ViewByObject {
1238
- Selectors : cache.SelectorsByObject {
1239
- & corev1.Pod {}: {
1240
- Label : labels .Set (tc .labelSelectors ).AsSelector (),
1241
- Field : fields .Set (tc .fieldSelectors ).AsSelector (),
1242
- },
1243
- },
1228
+ ByObject : map [client.Object ]cache.ByObject {
1229
+ & corev1.Pod {}: {
1230
+ Label : labels .Set (tc .labelSelectors ).AsSelector (),
1231
+ Field : fields .Set (tc .fieldSelectors ).AsSelector (),
1244
1232
},
1245
1233
},
1246
1234
},
@@ -1822,12 +1810,5 @@ func isKubeService(svc metav1.Object) bool {
1822
1810
}
1823
1811
1824
1812
func isPodDisableDeepCopy (opts cache.Options ) bool {
1825
- if d , ok := opts .View .ByObject .UnsafeDisableDeepCopy [& corev1.Pod {}]; ok {
1826
- return d
1827
- } else if d , ok = opts .View .ByObject .UnsafeDisableDeepCopy [cache.ObjectAll {}]; ok {
1828
- return d
1829
- } else if d , ok = opts .View .ByObject .UnsafeDisableDeepCopy [& cache.ObjectAll {}]; ok {
1830
- return d
1831
- }
1832
- return false
1813
+ return opts .ByObject [& corev1.Pod {}].UnsafeDisableDeepCopy || pointer .BoolDeref (opts .UnsafeDisableDeepCopy , false )
1833
1814
}
0 commit comments