@@ -492,12 +492,12 @@ func leaderRun(ctx context.Context, c *Controller, threadiness int, stopCh <-cha
492
492
for {
493
493
select {
494
494
case oerr := <- notificationChannel :
495
- if ! errors .Is (oerr .Err , http .ErrServerClosed ) {
495
+ if oerr != nil && ! errors .Is (oerr .Err , http .ErrServerClosed ) {
496
496
klog .Errorf ("STS API Server stopped: %v, going to restart" , oerr .Err )
497
497
go c .startSTSAPIServer (ctx , notificationChannel )
498
498
}
499
499
case err := <- upgradeServerChannel :
500
- if err != http .ErrServerClosed {
500
+ if err != nil && ! errors . Is ( err , http .ErrServerClosed ) {
501
501
klog .Errorf ("Upgrade Server stopped: %v, going to restart" , err )
502
502
upgradeServerChannel = c .startUpgradeServer ()
503
503
}
@@ -584,8 +584,24 @@ func (c *Controller) Start(threadiness int, stopCh <-chan struct{}) error {
584
584
leaderRun (ctx , c , threadiness , stopCh , notificationChannel )
585
585
},
586
586
OnStoppedLeading : func () {
587
- // we can do cleanup here
588
- klog .Infof ("leader lost: %s" , c .podName )
587
+ klog .Infof ("leader lost, removing any leader labels that I '%s' might have" , c .podName )
588
+ p := []patchAnnotation {{
589
+ Op : "remove" ,
590
+ Path : "/metadata/labels/operator" ,
591
+ }}
592
+
593
+ payloadBytes , err := json .Marshal (p )
594
+ if err != nil {
595
+ klog .Errorf ("failed to marshal patch: %#v" , err )
596
+ } else {
597
+ c .kubeClientSet .CoreV1 ().Pods (leaseLockNamespace ).Patch (ctx , c .podName , types .JSONPatchType , payloadBytes , metav1.PatchOptions {})
598
+ }
599
+ // Even if Stop() is called twice, stopping it here ensures the sync handler no longer is handling events,
600
+ // in case SIGTERM fails or the controller takes longer to exit.
601
+ c .Stop ()
602
+ if err := syscall .Kill (os .Getpid (), syscall .SIGTERM ); err != nil {
603
+ klog .Errorf ("error sending SIGTERM: %v" , err )
604
+ }
589
605
},
590
606
OnNewLeader : func (identity string ) {
591
607
// we're notified when new leader elected
0 commit comments