@@ -42,6 +42,7 @@ import (
4242 "sigs.k8s.io/controller-runtime/pkg/handler"
4343 ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
4444 "sigs.k8s.io/controller-runtime/pkg/internal/log"
45+ "sigs.k8s.io/controller-runtime/pkg/kontext"
4546 "sigs.k8s.io/controller-runtime/pkg/predicate"
4647 "sigs.k8s.io/controller-runtime/pkg/reconcile"
4748 "sigs.k8s.io/controller-runtime/pkg/source"
@@ -51,13 +52,13 @@ var _ = Describe("controller", func() {
5152 var fakeReconcile * fakeReconciler
5253 var ctrl * Controller
5354 var queue * controllertest.Queue
54- var reconciled chan reconcile. Request
55+ var reconciled chan reconcilerTrio
5556 var request = reconcile.Request {
5657 NamespacedName : types.NamespacedName {Namespace : "foo" , Name : "bar" },
5758 }
5859
5960 BeforeEach (func () {
60- reconciled = make (chan reconcile. Request )
61+ reconciled = make (chan reconcilerTrio )
6162 fakeReconcile = & fakeReconciler {
6263 Requests : reconciled ,
6364 results : make (chan fakeReconcileResultPair , 10 /* chosen by the completely scientific approach of guessing */ ),
@@ -80,7 +81,7 @@ var _ = Describe("controller", func() {
8081 ctx , cancel := context .WithCancel (context .Background ())
8182 defer cancel ()
8283
83- ctrl .Do = reconcile .Func (func (context.Context , reconcile.Request ) (reconcile.Result , error ) {
84+ ctrl .Do = reconcile .Func (func (doCtx context.Context , doReq reconcile.Request ) (reconcile.Result , error ) {
8485 return reconcile.Result {Requeue : true }, nil
8586 })
8687 result , err := ctrl .Reconcile (ctx ,
@@ -325,7 +326,7 @@ var _ = Describe("controller", func() {
325326
326327 By ("Invoking Reconciler" )
327328 fakeReconcile .AddResult (reconcile.Result {}, nil )
328- Expect (<- reconciled ). To ( Equal ( request ) )
329+ checkReconcile (<- reconciled , request )
329330
330331 By ("Removing the item from the queue" )
331332 Eventually (queue .Len ).Should (Equal (0 ))
@@ -370,14 +371,14 @@ var _ = Describe("controller", func() {
370371
371372 By ("Invoking Reconciler which will give an error" )
372373 fakeReconcile .AddResult (reconcile.Result {}, fmt .Errorf ("expected error: reconcile" ))
373- Expect (<- reconciled ). To ( Equal ( request ) )
374+ checkReconcile (<- reconciled , request )
374375 queue .AddedRateLimitedLock .Lock ()
375376 Expect (queue .AddedRatelimited ).To (Equal ([]any {request }))
376377 queue .AddedRateLimitedLock .Unlock ()
377378
378379 By ("Invoking Reconciler a second time without error" )
379380 fakeReconcile .AddResult (reconcile.Result {}, nil )
380- Expect (<- reconciled ). To ( Equal ( request ) )
381+ checkReconcile (<- reconciled , request )
381382
382383 By ("Removing the item from the queue" )
383384 Eventually (queue .Len ).Should (Equal (0 ))
@@ -396,7 +397,7 @@ var _ = Describe("controller", func() {
396397
397398 By ("Invoking Reconciler which will give an error" )
398399 fakeReconcile .AddResult (reconcile.Result {}, reconcile .TerminalError (fmt .Errorf ("expected error: reconcile" )))
399- Expect (<- reconciled ). To ( Equal ( request ) )
400+ checkReconcile (<- reconciled , request )
400401
401402 queue .AddedRateLimitedLock .Lock ()
402403 Expect (queue .AddedRatelimited ).To (BeEmpty ())
@@ -423,18 +424,18 @@ var _ = Describe("controller", func() {
423424
424425 By ("Invoking Reconciler which returns an error" )
425426 fakeReconcile .AddResult (reconcile.Result {}, fmt .Errorf ("something's wrong" ))
426- Expect (<- reconciled ). To ( Equal ( request ) )
427+ checkReconcile (<- reconciled , request )
427428 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : 1 , AddRateLimited : 1 }))
428429
429430 By ("Invoking Reconciler a second time with an error" )
430431 fakeReconcile .AddResult (reconcile.Result {}, fmt .Errorf ("another thing's wrong" ))
431- Expect (<- reconciled ). To ( Equal ( request ) )
432+ checkReconcile (<- reconciled , request )
432433
433434 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : 1 , AddRateLimited : 2 }))
434435
435436 By ("Invoking Reconciler a third time, where it finally does not return an error" )
436437 fakeReconcile .AddResult (reconcile.Result {}, nil )
437- Expect (<- reconciled ). To ( Equal ( request ) )
438+ checkReconcile (<- reconciled , request )
438439
439440 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : 0 , AddRateLimited : 2 }))
440441
@@ -459,12 +460,12 @@ var _ = Describe("controller", func() {
459460
460461 By ("Invoking Reconciler which will ask for requeue" )
461462 fakeReconcile .AddResult (reconcile.Result {Requeue : true }, nil )
462- Expect (<- reconciled ). To ( Equal ( request ) )
463+ checkReconcile (<- reconciled , request )
463464 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : 1 , AddRateLimited : 1 }))
464465
465466 By ("Invoking Reconciler a second time without asking for requeue" )
466467 fakeReconcile .AddResult (reconcile.Result {Requeue : false }, nil )
467- Expect (<- reconciled ). To ( Equal ( request ) )
468+ checkReconcile (<- reconciled , request )
468469
469470 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : 0 , AddRateLimited : 1 }))
470471
@@ -489,12 +490,12 @@ var _ = Describe("controller", func() {
489490
490491 By ("Invoking Reconciler which will ask for requeue & requeueafter" )
491492 fakeReconcile .AddResult (reconcile.Result {RequeueAfter : time .Millisecond * 100 , Requeue : true }, nil )
492- Expect (<- reconciled ). To ( Equal ( request ) )
493+ checkReconcile (<- reconciled , request )
493494 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : 0 , AddAfter : 1 }))
494495
495496 By ("Invoking Reconciler a second time asking for a requeueafter only" )
496497 fakeReconcile .AddResult (reconcile.Result {RequeueAfter : time .Millisecond * 100 }, nil )
497- Expect (<- reconciled ). To ( Equal ( request ) )
498+ checkReconcile (<- reconciled , request )
498499
499500 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : - 1 /* we don't increment the count in addafter */ , AddAfter : 2 }))
500501
@@ -519,12 +520,12 @@ var _ = Describe("controller", func() {
519520
520521 By ("Invoking Reconciler which will ask for requeueafter with an error" )
521522 fakeReconcile .AddResult (reconcile.Result {RequeueAfter : time .Millisecond * 100 }, fmt .Errorf ("expected error: reconcile" ))
522- Expect (<- reconciled ). To ( Equal ( request ) )
523+ checkReconcile (<- reconciled , request )
523524 Eventually (dq .getCounts ).Should (Equal (countInfo {Trying : 1 , AddRateLimited : 1 }))
524525
525526 By ("Invoking Reconciler a second time asking for requeueafter without errors" )
526527 fakeReconcile .AddResult (reconcile.Result {RequeueAfter : time .Millisecond * 100 }, nil )
527- Expect (<- reconciled ). To ( Equal ( request ) )
528+ checkReconcile (<- reconciled , request )
528529 Eventually (dq .getCounts ).Should (Equal (countInfo {AddAfter : 1 , AddRateLimited : 1 }))
529530
530531 By ("Removing the item from the queue" )
@@ -571,7 +572,7 @@ var _ = Describe("controller", func() {
571572 queue .Add (request )
572573
573574 fakeReconcile .AddResult (reconcile.Result {}, nil )
574- Expect (<- reconciled ). To ( Equal ( request ) )
575+ checkReconcile (<- reconciled , request )
575576 Eventually (func () error {
576577 Expect (ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "success" ).Write (& reconcileTotal )).To (Succeed ())
577578 if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
@@ -600,7 +601,7 @@ var _ = Describe("controller", func() {
600601 queue .Add (request )
601602
602603 fakeReconcile .AddResult (reconcile.Result {}, fmt .Errorf ("expected error: reconcile" ))
603- Expect (<- reconciled ). To ( Equal ( request ) )
604+ checkReconcile (<- reconciled , request )
604605 Eventually (func () error {
605606 Expect (ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "error" ).Write (& reconcileTotal )).To (Succeed ())
606607 if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
@@ -630,7 +631,7 @@ var _ = Describe("controller", func() {
630631 queue .Add (request )
631632
632633 fakeReconcile .AddResult (reconcile.Result {Requeue : true }, nil )
633- Expect (<- reconciled ). To ( Equal ( request ) )
634+ checkReconcile (<- reconciled , request )
634635 Eventually (func () error {
635636 Expect (ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "requeue" ).Write (& reconcileTotal )).To (Succeed ())
636637 if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
@@ -659,7 +660,7 @@ var _ = Describe("controller", func() {
659660 queue .Add (request )
660661
661662 fakeReconcile .AddResult (reconcile.Result {RequeueAfter : 5 * time .Hour }, nil )
662- Expect (<- reconciled ). To ( Equal ( request ) )
663+ checkReconcile (<- reconciled , request )
663664 Eventually (func () error {
664665 Expect (ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "requeue_after" ).Write (& reconcileTotal )).To (Succeed ())
665666 if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
@@ -692,7 +693,7 @@ var _ = Describe("controller", func() {
692693
693694 By ("Invoking Reconciler which will give an error" )
694695 fakeReconcile .AddResult (reconcile.Result {}, fmt .Errorf ("expected error: reconcile" ))
695- Expect (<- reconciled ). To ( Equal ( request ) )
696+ checkReconcile (<- reconciled , request )
696697 Eventually (func () error {
697698 Expect (ctrlmetrics .ReconcileErrors .WithLabelValues (ctrl .Name ).Write (& reconcileErrs )).To (Succeed ())
698699 if reconcileErrs .GetCounter ().GetValue () != 1.0 {
@@ -703,7 +704,7 @@ var _ = Describe("controller", func() {
703704
704705 By ("Invoking Reconciler a second time without error" )
705706 fakeReconcile .AddResult (reconcile.Result {}, nil )
706- Expect (<- reconciled ). To ( Equal ( request ) )
707+ checkReconcile (<- reconciled , request )
707708
708709 By ("Removing the item from the queue" )
709710 Eventually (queue .Len ).Should (Equal (0 ))
@@ -734,7 +735,7 @@ var _ = Describe("controller", func() {
734735
735736 By ("Invoking Reconciler" )
736737 fakeReconcile .AddResult (reconcile.Result {}, nil )
737- Expect (<- reconciled ). To ( Equal ( request ) )
738+ checkReconcile (<- reconciled , request )
738739
739740 By ("Removing the item from the queue" )
740741 Eventually (queue .Len ).Should (Equal (0 ))
@@ -832,20 +833,26 @@ type fakeReconcileResultPair struct {
832833 Err error
833834}
834835
836+ type reconcilerTrio struct {
837+ reconcile.Request
838+ ctx context.Context
839+ }
840+
835841type fakeReconciler struct {
836- Requests chan reconcile. Request
842+ Requests chan reconcilerTrio
837843 results chan fakeReconcileResultPair
838844}
839845
840846func (f * fakeReconciler ) AddResult (res reconcile.Result , err error ) {
841847 f .results <- fakeReconcileResultPair {Result : res , Err : err }
842848}
843849
844- func (f * fakeReconciler ) Reconcile (_ context.Context , r reconcile.Request ) (reconcile.Result , error ) {
850+ func (f * fakeReconciler ) Reconcile (ctx context.Context , r reconcile.Request ) (reconcile.Result , error ) {
845851 res := <- f .results
846852 if f .Requests != nil {
847- f .Requests <- r
853+ f .Requests <- reconcilerTrio { ctx : ctx , Request : r }
848854 }
855+
849856 return res .Result , res .Err
850857}
851858
@@ -877,3 +884,10 @@ func (c *cacheWithIndefinitelyBlockingGetInformer) GetInformer(ctx context.Conte
877884 <- ctx .Done ()
878885 return nil , errors .New ("GetInformer timed out" )
879886}
887+
888+ func checkReconcile (rec reconcilerTrio , request reconcile.Request ) {
889+ Expect (rec .Request ).To (Equal (request ))
890+
891+ _ , ok := kontext .ClusterFrom (rec .ctx )
892+ Expect (ok ).To (BeTrue ())
893+ }
0 commit comments