@@ -436,8 +436,14 @@ func (cm *controllerManager) serveHealthProbes(stop <-chan struct{}) {
436
436
}
437
437
438
438
func (cm * controllerManager ) Start (stop <- chan struct {}) error {
439
- // join the passed-in stop channel as an upstream feeding into cm.internalStopper
440
- defer close (cm .internalStopper )
439
+ // This will block until all runnables started via
440
+ // startNonLeaderElectionRunnables finish.
441
+ doneCh := make (chan struct {})
442
+ defer func () {
443
+ // join the passed-in stop channel as an upstream feeding into cm.internalStopper
444
+ close (cm .internalStopper )
445
+ <- doneCh
446
+ }()
441
447
442
448
// initialize this here so that we reset the signal channel state on every start
443
449
cm .errSignal = & errSignaler {errSignal : make (chan struct {})}
@@ -454,7 +460,7 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
454
460
go cm .serveHealthProbes (cm .internalStop )
455
461
}
456
462
457
- go cm .startNonLeaderElectionRunnables ()
463
+ go cm .startNonLeaderElectionRunnables (doneCh )
458
464
459
465
if cm .resourceLock != nil {
460
466
err := cm .startLeaderElection ()
@@ -477,12 +483,13 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
477
483
}
478
484
}
479
485
480
- func (cm * controllerManager ) startNonLeaderElectionRunnables () {
486
+ func (cm * controllerManager ) startNonLeaderElectionRunnables (doneCh chan <- struct {} ) {
481
487
cm .mu .Lock ()
482
488
defer cm .mu .Unlock ()
483
489
484
490
cm .waitForCache ()
485
491
492
+ returnCh := make (chan struct {})
486
493
// Start the non-leaderelection Runnables after the cache has synced
487
494
for _ , c := range cm .nonLeaderElectionRunnables {
488
495
// Controllers block, but we want to return an error if any have an error starting.
@@ -495,8 +502,22 @@ func (cm *controllerManager) startNonLeaderElectionRunnables() {
495
502
// we use %T here because we don't have a good stand-in for "name",
496
503
// and the full runnable might not serialize (mutexes, etc)
497
504
log .V (1 ).Info ("non-leader-election runnable finished" , "runnable type" , fmt .Sprintf ("%T" , ctrl ))
505
+ returnCh <- struct {}{}
498
506
}()
499
507
}
508
+
509
+ doneCount := 0
510
+
511
+ numRunners := len (cm .nonLeaderElectionRunnables )
512
+ for doneCount < numRunners {
513
+ select {
514
+ case <- returnCh :
515
+ doneCount ++
516
+ default :
517
+ }
518
+ }
519
+ close (returnCh )
520
+ close (doneCh )
500
521
}
501
522
502
523
func (cm * controllerManager ) startLeaderElectionRunnables () {
0 commit comments