@@ -24,6 +24,7 @@ import (
24
24
"sort"
25
25
"strconv"
26
26
"strings"
27
+ "time"
27
28
28
29
. "github.com/onsi/ginkgo/v2"
29
30
. "github.com/onsi/gomega"
@@ -43,6 +44,7 @@ import (
43
44
44
45
"sigs.k8s.io/controller-runtime/pkg/cache"
45
46
"sigs.k8s.io/controller-runtime/pkg/client"
47
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
46
48
)
47
49
48
50
const testNodeOne = "test-node-1"
@@ -117,6 +119,7 @@ func deletePod(pod client.Object) {
117
119
118
120
var _ = Describe ("Informer Cache" , func () {
119
121
CacheTest (cache .New , cache.Options {})
122
+ NonBlockingGetTest (cache .New , cache.Options {})
120
123
})
121
124
122
125
var _ = Describe ("Informer Cache with ReaderFailOnMissingInformer" , func () {
@@ -131,12 +134,22 @@ var _ = Describe("Multi-Namespace Informer Cache", func() {
131
134
"default" : {},
132
135
},
133
136
})
137
+ NonBlockingGetTest (cache .New , cache.Options {
138
+ DefaultNamespaces : map [string ]cache.Config {
139
+ testNamespaceOne : {},
140
+ testNamespaceTwo : {},
141
+ "default" : {},
142
+ },
143
+ })
134
144
})
135
145
136
146
var _ = Describe ("Informer Cache without global DeepCopy" , func () {
137
147
CacheTest (cache .New , cache.Options {
138
148
DefaultUnsafeDisableDeepCopy : pointer .Bool (true ),
139
149
})
150
+ NonBlockingGetTest (cache .New , cache.Options {
151
+ DefaultUnsafeDisableDeepCopy : pointer .Bool (true ),
152
+ })
140
153
})
141
154
142
155
var _ = Describe ("Cache with transformers" , func () {
@@ -440,7 +453,6 @@ func CacheTestReaderFailOnMissingInformer(createCacheFunc func(config *rest.Conf
440
453
BeforeEach (func () {
441
454
informerCacheCtx , informerCacheCancel = context .WithCancel (context .Background ())
442
455
Expect (cfg ).NotTo (BeNil ())
443
-
444
456
By ("creating the informer cache" )
445
457
var err error
446
458
informerCache , err = createCacheFunc (cfg , opts )
@@ -507,6 +519,83 @@ func CacheTestReaderFailOnMissingInformer(createCacheFunc func(config *rest.Conf
507
519
})
508
520
}
509
521
522
+ func NonBlockingGetTest (createCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error ), opts cache.Options ) {
523
+ Describe ("non-blocking get test" , func () {
524
+ var (
525
+ informerCache cache.Cache
526
+ informerCacheCtx context.Context
527
+ informerCacheCancel context.CancelFunc
528
+ )
529
+ BeforeEach (func () {
530
+ informerCacheCtx , informerCacheCancel = context .WithCancel (context .Background ())
531
+ Expect (cfg ).NotTo (BeNil ())
532
+
533
+ By ("creating expected namespaces" )
534
+ cl , err := client .New (cfg , client.Options {})
535
+ Expect (err ).NotTo (HaveOccurred ())
536
+ err = ensureNode (testNodeOne , cl )
537
+ Expect (err ).NotTo (HaveOccurred ())
538
+ err = ensureNamespace (testNamespaceOne , cl )
539
+ Expect (err ).NotTo (HaveOccurred ())
540
+ err = ensureNamespace (testNamespaceTwo , cl )
541
+ Expect (err ).NotTo (HaveOccurred ())
542
+ err = ensureNamespace (testNamespaceThree , cl )
543
+ Expect (err ).NotTo (HaveOccurred ())
544
+
545
+ By ("creating the informer cache" )
546
+ v := reflect .ValueOf (& opts ).Elem ()
547
+ newInformerField := v .FieldByName ("newInformer" )
548
+ newFakeInformer := func (_ kcache.ListerWatcher , _ runtime.Object , _ time.Duration , _ kcache.Indexers ) kcache.SharedIndexInformer {
549
+ return & controllertest.FakeInformer {Synced : false }
550
+ }
551
+ reflect .NewAt (newInformerField .Type (), newInformerField .Addr ().UnsafePointer ()).
552
+ Elem ().
553
+ Set (reflect .ValueOf (& newFakeInformer ))
554
+ informerCache , err = createCacheFunc (cfg , opts )
555
+ Expect (err ).NotTo (HaveOccurred ())
556
+ By ("running the cache and waiting for it to sync" )
557
+ // pass as an arg so that we don't race between close and re-assign
558
+ go func (ctx context.Context ) {
559
+ defer GinkgoRecover ()
560
+ Expect (informerCache .Start (ctx )).To (Succeed ())
561
+ }(informerCacheCtx )
562
+ Expect (informerCache .WaitForCacheSync (informerCacheCtx )).To (BeTrue ())
563
+ })
564
+
565
+ AfterEach (func () {
566
+ By ("cleaning up created pods" )
567
+ informerCacheCancel ()
568
+ })
569
+
570
+ Describe ("as an Informer" , func () {
571
+ It ("should be able to get informer for the object without blocking" , func () {
572
+ By ("getting a shared index informer for a pod" )
573
+ pod := & corev1.Pod {
574
+ ObjectMeta : metav1.ObjectMeta {
575
+ Name : "informer-obj" ,
576
+ Namespace : "default" ,
577
+ },
578
+ Spec : corev1.PodSpec {
579
+ Containers : []corev1.Container {
580
+ {
581
+ Name : "nginx" ,
582
+ Image : "nginx" ,
583
+ },
584
+ },
585
+ },
586
+ }
587
+
588
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
589
+ defer cancel ()
590
+ sii , err := informerCache .GetInformer (ctx , pod , cache .BlockUntilSynced (false ))
591
+ Expect (err ).NotTo (HaveOccurred ())
592
+ Expect (sii ).NotTo (BeNil ())
593
+ Expect (sii .HasSynced ()).To (BeFalse ())
594
+ })
595
+ })
596
+ })
597
+ }
598
+
510
599
func CacheTest (createCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error ), opts cache.Options ) {
511
600
Describe ("Cache test" , func () {
512
601
var (
0 commit comments