@@ -48,6 +48,7 @@ import (
48
48
)
49
49
50
50
const testNodeOne = "test-node-1"
51
+ const testNodeTwo = "test-node-2"
51
52
const testNamespaceOne = "test-namespace-1"
52
53
const testNamespaceTwo = "test-namespace-2"
53
54
const testNamespaceThree = "test-namespace-3"
@@ -619,6 +620,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
619
620
Expect (err ).NotTo (HaveOccurred ())
620
621
err = ensureNode (testNodeOne , cl )
621
622
Expect (err ).NotTo (HaveOccurred ())
623
+ err = ensureNode (testNodeTwo , cl )
624
+ Expect (err ).NotTo (HaveOccurred ())
622
625
err = ensureNamespace (testNamespaceOne , cl )
623
626
Expect (err ).NotTo (HaveOccurred ())
624
627
err = ensureNamespace (testNamespaceTwo , cl )
@@ -1182,7 +1185,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
1182
1185
1183
1186
By ("verifying the node list is not empty" )
1184
1187
Expect (nodeList .Items ).NotTo (BeEmpty ())
1185
- Expect (len (nodeList .Items )).To (BeEquivalentTo (1 ))
1188
+ Expect (len (nodeList .Items )).To (BeEquivalentTo (2 ))
1186
1189
})
1187
1190
It ("should return an error if the continue list options is set" , func () {
1188
1191
podList := & unstructured.Unstructured {}
@@ -1354,6 +1357,75 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
1354
1357
Expect (namespacedCache .Get (context .Background (), key2 , node )).To (Succeed ())
1355
1358
})
1356
1359
1360
+ It ("should be able to restrict cache to a namespace for namespaced object and to given selectors for non namespaced object" , func () {
1361
+ By ("creating a namespaced cache" )
1362
+ namespacedCache , err := cache .New (cfg , cache.Options {
1363
+ DefaultNamespaces : map [string ]cache.Config {testNamespaceOne : {}},
1364
+ ByObject : map [client.Object ]cache.ByObject {
1365
+ & corev1.Node {}: {
1366
+ Label : labels .SelectorFromSet (labels.Set {"name" : testNodeTwo }),
1367
+ },
1368
+ },
1369
+ })
1370
+ Expect (err ).NotTo (HaveOccurred ())
1371
+
1372
+ By ("running the cache and waiting for it to sync" )
1373
+ go func () {
1374
+ defer GinkgoRecover ()
1375
+ Expect (namespacedCache .Start (informerCacheCtx )).To (Succeed ())
1376
+ }()
1377
+ Expect (namespacedCache .WaitForCacheSync (informerCacheCtx )).To (BeTrue ())
1378
+
1379
+ By ("listing pods in all namespaces" )
1380
+ out := & metav1.PartialObjectMetadataList {}
1381
+ out .SetGroupVersionKind (schema.GroupVersionKind {
1382
+ Group : "" ,
1383
+ Version : "v1" ,
1384
+ Kind : "PodList" ,
1385
+ })
1386
+ Expect (namespacedCache .List (context .Background (), out )).To (Succeed ())
1387
+
1388
+ By ("verifying the returned pod is from the watched namespace" )
1389
+ Expect (out .Items ).NotTo (BeEmpty ())
1390
+ Expect (out .Items ).Should (HaveLen (2 ))
1391
+ for _ , item := range out .Items {
1392
+ Expect (item .Namespace ).To (Equal (testNamespaceOne ))
1393
+ }
1394
+ By ("listing all nodes - should still be able to list a cluster-scoped resource" )
1395
+ nodeList := & metav1.PartialObjectMetadataList {}
1396
+ nodeList .SetGroupVersionKind (schema.GroupVersionKind {
1397
+ Group : "" ,
1398
+ Version : "v1" ,
1399
+ Kind : "NodeList" ,
1400
+ })
1401
+ Expect (namespacedCache .List (context .Background (), nodeList )).To (Succeed ())
1402
+
1403
+ By ("verifying the node list is not empty" )
1404
+ Expect (nodeList .Items ).NotTo (BeEmpty ())
1405
+
1406
+ By ("getting a node - should still be able to get a cluster-scoped resource" )
1407
+ node := & metav1.PartialObjectMetadata {}
1408
+ node .SetGroupVersionKind (schema.GroupVersionKind {
1409
+ Group : "" ,
1410
+ Version : "v1" ,
1411
+ Kind : "Node" ,
1412
+ })
1413
+
1414
+ By ("verifying that getting the node works with an empty namespace" )
1415
+ key1 := client.ObjectKey {Namespace : "" , Name : testNodeTwo }
1416
+ Expect (namespacedCache .Get (context .Background (), key1 , node )).To (Succeed ())
1417
+
1418
+ By ("verifying that the namespace is ignored when getting a cluster-scoped resource" )
1419
+ key2 := client.ObjectKey {Namespace : "random" , Name : testNodeTwo }
1420
+ Expect (namespacedCache .Get (context .Background (), key2 , node )).To (Succeed ())
1421
+
1422
+ By ("verifying that an error is returned for node with not matching label" )
1423
+ key3 := client.ObjectKey {Namespace : "" , Name : testNodeOne }
1424
+ err = namespacedCache .Get (context .Background (), key3 , node )
1425
+ Expect (err ).To (HaveOccurred ())
1426
+ Expect (apierrors .IsNotFound (err )).To (BeTrue ())
1427
+ })
1428
+
1357
1429
if ! isPodDisableDeepCopy (opts ) {
1358
1430
It ("should deep copy the object unless told otherwise" , func () {
1359
1431
By ("retrieving a specific pod from the cache" )
@@ -2184,7 +2256,8 @@ func ensureNamespace(namespace string, client client.Client) error {
2184
2256
func ensureNode (name string , client client.Client ) error {
2185
2257
node := corev1.Node {
2186
2258
ObjectMeta : metav1.ObjectMeta {
2187
- Name : name ,
2259
+ Name : name ,
2260
+ Labels : map [string ]string {"name" : name },
2188
2261
},
2189
2262
TypeMeta : metav1.TypeMeta {
2190
2263
Kind : "Node" ,
0 commit comments