@@ -38,6 +38,7 @@ import (
38
38
"k8s.io/apimachinery/pkg/util/sets"
39
39
"k8s.io/apimachinery/pkg/util/uuid"
40
40
"k8s.io/apimachinery/pkg/util/wait"
41
+ "k8s.io/apiserver/pkg/server"
41
42
"k8s.io/apiserver/pkg/server/healthz"
42
43
"k8s.io/apiserver/pkg/server/mux"
43
44
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -158,7 +159,8 @@ controller, and serviceaccounts controller.`,
158
159
fg .(featuregate.MutableFeatureGate ).AddMetrics ()
159
160
// add component version metrics
160
161
s .ComponentGlobalsRegistry .AddMetrics ()
161
- return Run (ctx , c .Complete ())
162
+ stopCh := server .SetupSignalHandler ()
163
+ return Run (context .Background (), c .Complete (), stopCh )
162
164
},
163
165
Args : func (cmd * cobra.Command , args []string ) error {
164
166
for _ , arg := range args {
@@ -195,9 +197,9 @@ func ResyncPeriod(c *config.CompletedConfig) func() time.Duration {
195
197
}
196
198
197
199
// Run runs the KubeControllerManagerOptions.
198
- func Run (ctx context.Context , c * config.CompletedConfig ) error {
200
+ func Run (ctx context.Context , c * config.CompletedConfig , stopCh2 <- chan struct {} ) error {
199
201
logger := klog .FromContext (ctx )
200
- stopCh := ctx .Done ()
202
+ stopCh := mergeCh ( ctx .Done (), stopCh2 )
201
203
202
204
// To help debugging, immediately log version
203
205
logger .Info ("Starting" , "version" , utilversion .Get ())
@@ -363,10 +365,18 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
363
365
run (ctx , controllerDescriptors )
364
366
},
365
367
OnStoppedLeading : func () {
366
- logger .Error (nil , "leaderelection lost" )
367
- klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
368
+ select {
369
+ case <- stopCh :
370
+ // We were asked to terminate. Exit 0.
371
+ klog .Info ("Requested to terminate. Exiting." )
372
+ os .Exit (0 )
373
+ default :
374
+ // We lost the lock.
375
+ logger .Error (nil , "leaderelection lost" )
376
+ klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
377
+ }
368
378
},
369
- })
379
+ }, stopCh )
370
380
371
381
// If Leader Migration is enabled, proceed to attempt the migration lock.
372
382
if leaderMigrator != nil {
@@ -390,10 +400,18 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
390
400
run (ctx , controllerDescriptors )
391
401
},
392
402
OnStoppedLeading : func () {
393
- logger .Error (nil , "migration leaderelection lost" )
394
- klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
403
+ select {
404
+ case <- stopCh :
405
+ // We were asked to terminate. Exit 0.
406
+ klog .Info ("Requested to terminate. Exiting." )
407
+ os .Exit (0 )
408
+ default :
409
+ // We lost the lock.
410
+ logger .Error (nil , "migration leaderelection lost" )
411
+ klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
412
+ }
395
413
},
396
- })
414
+ }, stopCh )
397
415
}
398
416
399
417
<- stopCh
@@ -903,7 +921,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde
903
921
904
922
// leaderElectAndRun runs the leader election, and runs the callbacks once the leader lease is acquired.
905
923
// TODO: extract this function into staging/controller-manager
906
- func leaderElectAndRun (ctx context.Context , c * config.CompletedConfig , lockIdentity string , electionChecker * leaderelection.HealthzAdaptor , resourceLock string , leaseName string , callbacks leaderelection.LeaderCallbacks ) {
924
+ func leaderElectAndRun (ctx context.Context , c * config.CompletedConfig , lockIdentity string , electionChecker * leaderelection.HealthzAdaptor , resourceLock string , leaseName string , callbacks leaderelection.LeaderCallbacks , stopCh <- chan struct {} ) {
907
925
logger := klog .FromContext (ctx )
908
926
rl , err := resourcelock .NewFromKubeconfig (resourceLock ,
909
927
c .ComponentConfig .Generic .LeaderElection .ResourceNamespace ,
@@ -919,7 +937,13 @@ func leaderElectAndRun(ctx context.Context, c *config.CompletedConfig, lockIdent
919
937
klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
920
938
}
921
939
922
- leaderelection .RunOrDie (ctx , leaderelection.LeaderElectionConfig {
940
+ leCtx , cancel := context .WithCancel (ctx )
941
+ defer cancel ()
942
+ go func () {
943
+ <- stopCh
944
+ cancel ()
945
+ }()
946
+ leaderelection .RunOrDie (leCtx , leaderelection.LeaderElectionConfig {
923
947
Lock : rl ,
924
948
LeaseDuration : c .ComponentConfig .Generic .LeaderElection .LeaseDuration .Duration ,
925
949
RenewDeadline : c .ComponentConfig .Generic .LeaderElection .RenewDeadline .Duration ,
0 commit comments