@@ -496,7 +496,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
496
496
},
497
497
},
498
498
}
499
- sii , err := informerCache .GetInformer (pod )
499
+ sii , err := informerCache .GetInformer (context . TODO (), pod )
500
500
Expect (err ).NotTo (HaveOccurred ())
501
501
Expect (sii ).NotTo (BeNil ())
502
502
Expect (sii .HasSynced ()).To (BeTrue ())
@@ -522,7 +522,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
522
522
It ("should be able to get an informer by group/version/kind" , func (done Done ) {
523
523
By ("getting an shared index informer for gvk = core/v1/pod" )
524
524
gvk := schema.GroupVersionKind {Group : "" , Version : "v1" , Kind : "Pod" }
525
- sii , err := informerCache .GetInformerForKind (gvk )
525
+ sii , err := informerCache .GetInformerForKind (context . TODO (), gvk )
526
526
Expect (err ).NotTo (HaveOccurred ())
527
527
Expect (sii ).NotTo (BeNil ())
528
528
Expect (sii .HasSynced ()).To (BeTrue ())
@@ -569,7 +569,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
569
569
indexFunc := func (obj runtime.Object ) []string {
570
570
return []string {string (obj .(* kcorev1.Pod ).Spec .RestartPolicy )}
571
571
}
572
- Expect (informer .IndexField (pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
572
+ Expect (informer .IndexField (context . TODO (), pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
573
573
574
574
By ("running the cache and waiting for it to sync" )
575
575
go func () {
@@ -588,6 +588,45 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
588
588
actual := listObj .Items [0 ]
589
589
Expect (actual .Name ).To (Equal ("test-pod-3" ))
590
590
})
591
+
592
+ It ("should allow for get informer to be cancelled" , func () {
593
+ By ("creating a context and cancelling it" )
594
+ ctx , cancel := context .WithCancel (context .Background ())
595
+ cancel ()
596
+
597
+ By ("getting a shared index informer for a pod with a cancelled context" )
598
+ pod := & kcorev1.Pod {
599
+ ObjectMeta : kmetav1.ObjectMeta {
600
+ Name : "informer-obj" ,
601
+ Namespace : "default" ,
602
+ },
603
+ Spec : kcorev1.PodSpec {
604
+ Containers : []kcorev1.Container {
605
+ {
606
+ Name : "nginx" ,
607
+ Image : "nginx" ,
608
+ },
609
+ },
610
+ },
611
+ }
612
+ sii , err := informerCache .GetInformer (ctx , pod )
613
+ Expect (err ).To (HaveOccurred ())
614
+ Expect (sii ).To (BeNil ())
615
+ Expect (errors .IsTimeout (err )).To (BeTrue ())
616
+ })
617
+
618
+ It ("should allow getting an informer by group/version/kind to be cancelled" , func () {
619
+ By ("creating a context and cancelling it" )
620
+ ctx , cancel := context .WithCancel (context .Background ())
621
+ cancel ()
622
+
623
+ By ("getting an shared index informer for gvk = core/v1/pod with a cancelled context" )
624
+ gvk := schema.GroupVersionKind {Group : "" , Version : "v1" , Kind : "Pod" }
625
+ sii , err := informerCache .GetInformerForKind (ctx , gvk )
626
+ Expect (err ).To (HaveOccurred ())
627
+ Expect (sii ).To (BeNil ())
628
+ Expect (errors .IsTimeout (err )).To (BeTrue ())
629
+ })
591
630
})
592
631
Context ("with unstructured objects" , func () {
593
632
It ("should be able to get informer for the object" , func (done Done ) {
@@ -612,7 +651,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
612
651
Version : "v1" ,
613
652
Kind : "Pod" ,
614
653
})
615
- sii , err := informerCache .GetInformer (pod )
654
+ sii , err := informerCache .GetInformer (context . TODO (), pod )
616
655
Expect (err ).NotTo (HaveOccurred ())
617
656
Expect (sii ).NotTo (BeNil ())
618
657
Expect (sii .HasSynced ()).To (BeTrue ())
@@ -658,7 +697,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
658
697
}
659
698
return []string {fmt .Sprintf ("%v" , m ["restartPolicy" ])}
660
699
}
661
- Expect (informer .IndexField (pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
700
+ Expect (informer .IndexField (context . TODO (), pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
662
701
663
702
By ("running the cache and waiting for it to sync" )
664
703
go func () {
@@ -684,6 +723,26 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
684
723
actual := listObj .Items [0 ]
685
724
Expect (actual .GetName ()).To (Equal ("test-pod-3" ))
686
725
}, 3 )
726
+
727
+ It ("should allow for get informer to be cancelled" , func () {
728
+ By ("creating a context and cancelling it" )
729
+ ctx , cancel := context .WithCancel (context .Background ())
730
+ cancel ()
731
+
732
+ By ("getting a shared index informer for a pod with a cancelled context" )
733
+ pod := & unstructured.Unstructured {}
734
+ pod .SetName ("informer-obj2" )
735
+ pod .SetNamespace ("default" )
736
+ pod .SetGroupVersionKind (schema.GroupVersionKind {
737
+ Group : "" ,
738
+ Version : "v1" ,
739
+ Kind : "Pod" ,
740
+ })
741
+ sii , err := informerCache .GetInformer (ctx , pod )
742
+ Expect (err ).To (HaveOccurred ())
743
+ Expect (sii ).To (BeNil ())
744
+ Expect (errors .IsTimeout (err )).To (BeTrue ())
745
+ })
687
746
})
688
747
})
689
748
})
0 commit comments