@@ -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"
@@ -155,7 +156,9 @@ controller, and serviceaccounts controller.`,
155
156
// add feature enablement metrics
156
157
fg := s .ComponentGlobalsRegistry .FeatureGateFor (basecompatibility .DefaultKubeComponent )
157
158
fg .(featuregate.MutableFeatureGate ).AddMetrics ()
158
- return Run (context .Background (), c .Complete ())
159
+
160
+ stopCh := server .SetupSignalHandler ()
161
+ return Run (context .Background (), c .Complete (), stopCh )
159
162
},
160
163
Args : func (cmd * cobra.Command , args []string ) error {
161
164
for _ , arg := range args {
@@ -192,9 +195,9 @@ func ResyncPeriod(c *config.CompletedConfig) func() time.Duration {
192
195
}
193
196
194
197
// Run runs the KubeControllerManagerOptions.
195
- func Run (ctx context.Context , c * config.CompletedConfig ) error {
198
+ func Run (ctx context.Context , c * config.CompletedConfig , stopCh2 <- chan struct {} ) error {
196
199
logger := klog .FromContext (ctx )
197
- stopCh := ctx .Done ()
200
+ stopCh := mergeCh ( ctx .Done (), stopCh2 )
198
201
199
202
// To help debugging, immediately log version
200
203
logger .Info ("Starting" , "version" , utilversion .Get ())
@@ -360,10 +363,18 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
360
363
run (ctx , controllerDescriptors )
361
364
},
362
365
OnStoppedLeading : func () {
363
- logger .Error (nil , "leaderelection lost" )
364
- klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
366
+ select {
367
+ case <- stopCh :
368
+ // We were asked to terminate. Exit 0.
369
+ klog .Info ("Requested to terminate. Exiting." )
370
+ os .Exit (0 )
371
+ default :
372
+ // We lost the lock.
373
+ logger .Error (nil , "leaderelection lost" )
374
+ klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
375
+ }
365
376
},
366
- })
377
+ }, stopCh )
367
378
368
379
// If Leader Migration is enabled, proceed to attempt the migration lock.
369
380
if leaderMigrator != nil {
@@ -387,10 +398,18 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
387
398
run (ctx , controllerDescriptors )
388
399
},
389
400
OnStoppedLeading : func () {
390
- logger .Error (nil , "migration leaderelection lost" )
391
- klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
401
+ select {
402
+ case <- stopCh :
403
+ // We were asked to terminate. Exit 0.
404
+ klog .Info ("Requested to terminate. Exiting." )
405
+ os .Exit (0 )
406
+ default :
407
+ // We lost the lock.
408
+ logger .Error (nil , "migration leaderelection lost" )
409
+ klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
410
+ }
392
411
},
393
- })
412
+ }, stopCh )
394
413
}
395
414
396
415
<- stopCh
@@ -900,7 +919,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde
900
919
901
920
// leaderElectAndRun runs the leader election, and runs the callbacks once the leader lease is acquired.
902
921
// TODO: extract this function into staging/controller-manager
903
- func leaderElectAndRun (ctx context.Context , c * config.CompletedConfig , lockIdentity string , electionChecker * leaderelection.HealthzAdaptor , resourceLock string , leaseName string , callbacks leaderelection.LeaderCallbacks ) {
922
+ func leaderElectAndRun (ctx context.Context , c * config.CompletedConfig , lockIdentity string , electionChecker * leaderelection.HealthzAdaptor , resourceLock string , leaseName string , callbacks leaderelection.LeaderCallbacks , stopCh <- chan struct {} ) {
904
923
logger := klog .FromContext (ctx )
905
924
rl , err := resourcelock .NewFromKubeconfig (resourceLock ,
906
925
c .ComponentConfig .Generic .LeaderElection .ResourceNamespace ,
@@ -916,7 +935,13 @@ func leaderElectAndRun(ctx context.Context, c *config.CompletedConfig, lockIdent
916
935
klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
917
936
}
918
937
919
- leaderelection .RunOrDie (ctx , leaderelection.LeaderElectionConfig {
938
+ leCtx , cancel := context .WithCancel (ctx )
939
+ defer cancel ()
940
+ go func () {
941
+ <- stopCh
942
+ cancel ()
943
+ }()
944
+ leaderelection .RunOrDie (leCtx , leaderelection.LeaderElectionConfig {
920
945
Lock : rl ,
921
946
LeaseDuration : c .ComponentConfig .Generic .LeaderElection .LeaseDuration .Duration ,
922
947
RenewDeadline : c .ComponentConfig .Generic .LeaderElection .RenewDeadline .Duration ,
0 commit comments