@@ -48,6 +48,7 @@ import (
4848)
4949
5050const testNodeOne = "test-node-1"
51+ const testNodeTwo = "test-node-2"
5152const testNamespaceOne = "test-namespace-1"
5253const testNamespaceTwo = "test-namespace-2"
5354const testNamespaceThree = "test-namespace-3"
@@ -619,6 +620,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
619620 Expect (err ).NotTo (HaveOccurred ())
620621 err = ensureNode (testNodeOne , cl )
621622 Expect (err ).NotTo (HaveOccurred ())
623+ err = ensureNode (testNodeTwo , cl )
624+ Expect (err ).NotTo (HaveOccurred ())
622625 err = ensureNamespace (testNamespaceOne , cl )
623626 Expect (err ).NotTo (HaveOccurred ())
624627 err = ensureNamespace (testNamespaceTwo , cl )
@@ -1182,7 +1185,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
11821185
11831186 By ("verifying the node list is not empty" )
11841187 Expect (nodeList .Items ).NotTo (BeEmpty ())
1185- Expect (len (nodeList .Items )).To (BeEquivalentTo (1 ))
1188+ Expect (len (nodeList .Items )).To (BeEquivalentTo (2 ))
11861189 })
11871190 It ("should return an error if the continue list options is set" , func () {
11881191 podList := & unstructured.Unstructured {}
@@ -1354,6 +1357,75 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
13541357 Expect (namespacedCache .Get (context .Background (), key2 , node )).To (Succeed ())
13551358 })
13561359
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+
13571429 if ! isPodDisableDeepCopy (opts ) {
13581430 It ("should deep copy the object unless told otherwise" , func () {
13591431 By ("retrieving a specific pod from the cache" )
@@ -2184,7 +2256,8 @@ func ensureNamespace(namespace string, client client.Client) error {
21842256func ensureNode (name string , client client.Client ) error {
21852257 node := corev1.Node {
21862258 ObjectMeta : metav1.ObjectMeta {
2187- Name : name ,
2259+ Name : name ,
2260+ Labels : map [string ]string {"name" : name },
21882261 },
21892262 TypeMeta : metav1.TypeMeta {
21902263 Kind : "Node" ,
0 commit comments