@@ -489,7 +489,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
489
489
},
490
490
},
491
491
}
492
- sii , err := informerCache .GetInformer (pod )
492
+ sii , err := informerCache .GetInformer (context . TODO (), pod )
493
493
Expect (err ).NotTo (HaveOccurred ())
494
494
Expect (sii ).NotTo (BeNil ())
495
495
Expect (sii .HasSynced ()).To (BeTrue ())
@@ -515,7 +515,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
515
515
It ("should be able to get an informer by group/version/kind" , func (done Done ) {
516
516
By ("getting an shared index informer for gvk = core/v1/pod" )
517
517
gvk := schema.GroupVersionKind {Group : "" , Version : "v1" , Kind : "Pod" }
518
- sii , err := informerCache .GetInformerForKind (gvk )
518
+ sii , err := informerCache .GetInformerForKind (context . TODO (), gvk )
519
519
Expect (err ).NotTo (HaveOccurred ())
520
520
Expect (sii ).NotTo (BeNil ())
521
521
Expect (sii .HasSynced ()).To (BeTrue ())
@@ -562,7 +562,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
562
562
indexFunc := func (obj runtime.Object ) []string {
563
563
return []string {string (obj .(* kcorev1.Pod ).Spec .RestartPolicy )}
564
564
}
565
- Expect (informer .IndexField (pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
565
+ Expect (informer .IndexField (context . TODO (), pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
566
566
567
567
By ("running the cache and waiting for it to sync" )
568
568
go func () {
@@ -581,6 +581,45 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
581
581
actual := listObj .Items [0 ]
582
582
Expect (actual .Name ).To (Equal ("test-pod-3" ))
583
583
})
584
+
585
+ It ("should allow for get informer to be cancelled" , func () {
586
+ By ("creating a context and cancelling it" )
587
+ ctx , cancel := context .WithCancel (context .Background ())
588
+ cancel ()
589
+
590
+ By ("getting a shared index informer for a pod with a cancelled context" )
591
+ pod := & kcorev1.Pod {
592
+ ObjectMeta : kmetav1.ObjectMeta {
593
+ Name : "informer-obj" ,
594
+ Namespace : "default" ,
595
+ },
596
+ Spec : kcorev1.PodSpec {
597
+ Containers : []kcorev1.Container {
598
+ {
599
+ Name : "nginx" ,
600
+ Image : "nginx" ,
601
+ },
602
+ },
603
+ },
604
+ }
605
+ sii , err := informerCache .GetInformer (ctx , pod )
606
+ Expect (err ).To (HaveOccurred ())
607
+ Expect (sii ).To (BeNil ())
608
+ Expect (errors .IsTimeout (err )).To (BeTrue ())
609
+ })
610
+
611
+ It ("should allow getting an informer by group/version/kind to be cancelled" , func () {
612
+ By ("creating a context and cancelling it" )
613
+ ctx , cancel := context .WithCancel (context .Background ())
614
+ cancel ()
615
+
616
+ By ("getting an shared index informer for gvk = core/v1/pod with a cancelled context" )
617
+ gvk := schema.GroupVersionKind {Group : "" , Version : "v1" , Kind : "Pod" }
618
+ sii , err := informerCache .GetInformerForKind (ctx , gvk )
619
+ Expect (err ).To (HaveOccurred ())
620
+ Expect (sii ).To (BeNil ())
621
+ Expect (errors .IsTimeout (err )).To (BeTrue ())
622
+ })
584
623
})
585
624
Context ("with unstructured objects" , func () {
586
625
It ("should be able to get informer for the object" , func (done Done ) {
@@ -605,7 +644,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
605
644
Version : "v1" ,
606
645
Kind : "Pod" ,
607
646
})
608
- sii , err := informerCache .GetInformer (pod )
647
+ sii , err := informerCache .GetInformer (context . TODO (), pod )
609
648
Expect (err ).NotTo (HaveOccurred ())
610
649
Expect (sii ).NotTo (BeNil ())
611
650
Expect (sii .HasSynced ()).To (BeTrue ())
@@ -651,7 +690,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
651
690
}
652
691
return []string {fmt .Sprintf ("%v" , m ["restartPolicy" ])}
653
692
}
654
- Expect (informer .IndexField (pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
693
+ Expect (informer .IndexField (context . TODO (), pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
655
694
656
695
By ("running the cache and waiting for it to sync" )
657
696
go func () {
@@ -677,6 +716,26 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
677
716
actual := listObj .Items [0 ]
678
717
Expect (actual .GetName ()).To (Equal ("test-pod-3" ))
679
718
}, 3 )
719
+
720
+ It ("should allow for get informer to be cancelled" , func () {
721
+ By ("creating a context and cancelling it" )
722
+ ctx , cancel := context .WithCancel (context .Background ())
723
+ cancel ()
724
+
725
+ By ("getting a shared index informer for a pod with a cancelled context" )
726
+ pod := & unstructured.Unstructured {}
727
+ pod .SetName ("informer-obj2" )
728
+ pod .SetNamespace ("default" )
729
+ pod .SetGroupVersionKind (schema.GroupVersionKind {
730
+ Group : "" ,
731
+ Version : "v1" ,
732
+ Kind : "Pod" ,
733
+ })
734
+ sii , err := informerCache .GetInformer (ctx , pod )
735
+ Expect (err ).To (HaveOccurred ())
736
+ Expect (sii ).To (BeNil ())
737
+ Expect (errors .IsTimeout (err )).To (BeTrue ())
738
+ })
680
739
})
681
740
})
682
741
})
0 commit comments