@@ -35,6 +35,7 @@ import (
35
35
"k8s.io/apimachinery/pkg/util/uuid"
36
36
"k8s.io/apimachinery/pkg/util/wait"
37
37
genericfeatures "k8s.io/apiserver/pkg/features"
38
+ "k8s.io/apiserver/pkg/server"
38
39
"k8s.io/apiserver/pkg/server/healthz"
39
40
"k8s.io/apiserver/pkg/server/mux"
40
41
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -138,7 +139,9 @@ controller, and serviceaccounts controller.`,
138
139
fmt .Fprintf (os .Stderr , "%v\n " , err )
139
140
return err
140
141
}
141
- return Run (c .Complete (), wait .NeverStop )
142
+
143
+ stopCh := server .SetupSignalHandler ()
144
+ return Run (c .Complete (), stopCh )
142
145
},
143
146
Args : func (cmd * cobra.Command , args []string ) error {
144
147
for _ , arg := range args {
@@ -287,10 +290,18 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
287
290
run (ctx , startSATokenController , initializersFunc )
288
291
},
289
292
OnStoppedLeading : func () {
290
- klog .ErrorS (nil , "leaderelection lost" )
291
- klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
293
+ select {
294
+ case <- stopCh :
295
+ // We were asked to terminate. Exit 0.
296
+ klog .Info ("Requested to terminate. Exiting." )
297
+ os .Exit (0 )
298
+ default :
299
+ // We lost the lock.
300
+ klog .ErrorS (nil , "leaderelection lost" )
301
+ klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
302
+ }
292
303
},
293
- })
304
+ }, stopCh )
294
305
295
306
// If Leader Migration is enabled, proceed to attempt the migration lock.
296
307
if leaderMigrator != nil {
@@ -311,10 +322,18 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
311
322
run (ctx , nil , createInitializersFunc (leaderMigrator .FilterFunc , leadermigration .ControllerMigrated ))
312
323
},
313
324
OnStoppedLeading : func () {
314
- klog .ErrorS (nil , "migration leaderelection lost" )
315
- klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
325
+ select {
326
+ case <- stopCh :
327
+ // We were asked to terminate. Exit 0.
328
+ klog .Info ("Requested to terminate. Exiting." )
329
+ os .Exit (0 )
330
+ default :
331
+ // We lost the lock.
332
+ klog .ErrorS (nil , "migration leaderelection lost" )
333
+ klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
334
+ }
316
335
},
317
- })
336
+ }, stopCh )
318
337
}
319
338
320
339
<- stopCh
@@ -709,7 +728,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde
709
728
710
729
// leaderElectAndRun runs the leader election, and runs the callbacks once the leader lease is acquired.
711
730
// TODO: extract this function into staging/controller-manager
712
- func leaderElectAndRun (c * config.CompletedConfig , lockIdentity string , electionChecker * leaderelection.HealthzAdaptor , resourceLock string , leaseName string , callbacks leaderelection.LeaderCallbacks ) {
731
+ func leaderElectAndRun (c * config.CompletedConfig , lockIdentity string , electionChecker * leaderelection.HealthzAdaptor , resourceLock string , leaseName string , callbacks leaderelection.LeaderCallbacks , stopCh <- chan struct {} ) {
713
732
rl , err := resourcelock .NewFromKubeconfig (resourceLock ,
714
733
c .ComponentConfig .Generic .LeaderElection .ResourceNamespace ,
715
734
leaseName ,
@@ -723,7 +742,13 @@ func leaderElectAndRun(c *config.CompletedConfig, lockIdentity string, electionC
723
742
klog .Fatalf ("error creating lock: %v" , err )
724
743
}
725
744
726
- leaderelection .RunOrDie (context .TODO (), leaderelection.LeaderElectionConfig {
745
+ leCtx , cancel := context .WithCancel (context .Background ())
746
+ defer cancel ()
747
+ go func () {
748
+ <- stopCh
749
+ cancel ()
750
+ }()
751
+ leaderelection .RunOrDie (leCtx , leaderelection.LeaderElectionConfig {
727
752
Lock : rl ,
728
753
LeaseDuration : c .ComponentConfig .Generic .LeaderElection .LeaseDuration .Duration ,
729
754
RenewDeadline : c .ComponentConfig .Generic .LeaderElection .RenewDeadline .Duration ,
0 commit comments