@@ -35,6 +35,7 @@ import (
35
35
"sigs.k8s.io/controller-runtime/pkg/client"
36
36
)
37
37
38
+ const testNodeOne = "test-node-1"
38
39
const testNamespaceOne = "test-namespace-1"
39
40
const testNamespaceTwo = "test-namespace-2"
40
41
const testNamespaceThree = "test-namespace-3"
@@ -98,6 +99,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
98
99
By ("creating three pods" )
99
100
cl , err := client .New (cfg , client.Options {})
100
101
Expect (err ).NotTo (HaveOccurred ())
102
+ err = ensureNode (testNodeOne , cl )
103
+ Expect (err ).NotTo (HaveOccurred ())
101
104
err = ensureNamespace (testNamespaceOne , cl )
102
105
Expect (err ).NotTo (HaveOccurred ())
103
106
err = ensureNamespace (testNamespaceTwo , cl )
@@ -413,17 +416,33 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
413
416
Expect (out .Items ).Should (HaveLen (1 ))
414
417
Expect (out .Items [0 ].GetNamespace ()).To (Equal (testNamespaceOne ))
415
418
416
- By ("listing all namespaces - should still be able to get a cluster-scoped resource" )
417
- namespaceList := & unstructured.UnstructuredList {}
418
- namespaceList .SetGroupVersionKind (schema.GroupVersionKind {
419
+ By ("listing all nodes - should still be able to list a cluster-scoped resource" )
420
+ nodeList := & unstructured.UnstructuredList {}
421
+ nodeList .SetGroupVersionKind (schema.GroupVersionKind {
419
422
Group : "" ,
420
423
Version : "v1" ,
421
- Kind : "NamespaceList " ,
424
+ Kind : "NodeList " ,
422
425
})
423
- Expect (namespacedCache .List (context .Background (), namespaceList )).To (Succeed ())
426
+ Expect (namespacedCache .List (context .Background (), nodeList )).To (Succeed ())
427
+
428
+ By ("verifying the node list is not empty" )
429
+ Expect (nodeList .Items ).NotTo (BeEmpty ())
430
+
431
+ By ("getting a node - should still be able to get a cluster-scoped resource" )
432
+ node := & unstructured.Unstructured {}
433
+ node .SetGroupVersionKind (schema.GroupVersionKind {
434
+ Group : "" ,
435
+ Version : "v1" ,
436
+ Kind : "Node" ,
437
+ })
438
+
439
+ By ("verifying that getting the node works with an empty namespace" )
440
+ key1 := client.ObjectKey {Namespace : "" , Name : testNodeOne }
441
+ Expect (namespacedCache .Get (context .Background (), key1 , node )).To (Succeed ())
424
442
425
- By ("verifying the namespace list is not empty" )
426
- Expect (namespaceList .Items ).NotTo (BeEmpty ())
443
+ By ("verifying that the namespace is ignored when getting a cluster-scoped resource" )
444
+ key2 := client.ObjectKey {Namespace : "random" , Name : testNodeOne }
445
+ Expect (namespacedCache .Get (context .Background (), key2 , node )).To (Succeed ())
427
446
})
428
447
429
448
It ("should deep copy the object unless told otherwise" , func () {
@@ -607,17 +626,33 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
607
626
Expect (out .Items ).Should (HaveLen (1 ))
608
627
Expect (out .Items [0 ].GetNamespace ()).To (Equal (testNamespaceOne ))
609
628
610
- By ("listing all namespaces - should still be able to get a cluster-scoped resource" )
611
- namespaceList := & kmetav1.PartialObjectMetadataList {}
612
- namespaceList .SetGroupVersionKind (schema.GroupVersionKind {
629
+ By ("listing all nodes - should still be able to list a cluster-scoped resource" )
630
+ nodeList := & kmetav1.PartialObjectMetadataList {}
631
+ nodeList .SetGroupVersionKind (schema.GroupVersionKind {
613
632
Group : "" ,
614
633
Version : "v1" ,
615
- Kind : "NamespaceList " ,
634
+ Kind : "NodeList " ,
616
635
})
617
- Expect (namespacedCache .List (context .Background (), namespaceList )).To (Succeed ())
636
+ Expect (namespacedCache .List (context .Background (), nodeList )).To (Succeed ())
637
+
638
+ By ("verifying the node list is not empty" )
639
+ Expect (nodeList .Items ).NotTo (BeEmpty ())
640
+
641
+ By ("getting a node - should still be able to get a cluster-scoped resource" )
642
+ node := & kmetav1.PartialObjectMetadata {}
643
+ node .SetGroupVersionKind (schema.GroupVersionKind {
644
+ Group : "" ,
645
+ Version : "v1" ,
646
+ Kind : "Node" ,
647
+ })
648
+
649
+ By ("verifying that getting the node works with an empty namespace" )
650
+ key1 := client.ObjectKey {Namespace : "" , Name : testNodeOne }
651
+ Expect (namespacedCache .Get (context .Background (), key1 , node )).To (Succeed ())
618
652
619
- By ("verifying the namespace list is not empty" )
620
- Expect (namespaceList .Items ).NotTo (BeEmpty ())
653
+ By ("verifying that the namespace is ignored when getting a cluster-scoped resource" )
654
+ key2 := client.ObjectKey {Namespace : "random" , Name : testNodeOne }
655
+ Expect (namespacedCache .Get (context .Background (), key2 , node )).To (Succeed ())
621
656
})
622
657
623
658
It ("should deep copy the object unless told otherwise" , func () {
@@ -1079,6 +1114,23 @@ func ensureNamespace(namespace string, client client.Client) error {
1079
1114
return err
1080
1115
}
1081
1116
1117
+ func ensureNode (name string , client client.Client ) error {
1118
+ node := kcorev1.Node {
1119
+ ObjectMeta : kmetav1.ObjectMeta {
1120
+ Name : name ,
1121
+ },
1122
+ TypeMeta : kmetav1.TypeMeta {
1123
+ Kind : "Node" ,
1124
+ APIVersion : "v1" ,
1125
+ },
1126
+ }
1127
+ err := client .Create (context .TODO (), & node )
1128
+ if errors .IsAlreadyExists (err ) {
1129
+ return nil
1130
+ }
1131
+ return err
1132
+ }
1133
+
1082
1134
//nolint:interfacer
1083
1135
func isKubeService (svc kmetav1.Object ) bool {
1084
1136
// grumble grumble linters grumble grumble
0 commit comments