@@ -38,6 +38,7 @@ import (
3838 "k8s.io/apimachinery/pkg/util/sets"
3939 "k8s.io/apimachinery/pkg/util/uuid"
4040 "k8s.io/apimachinery/pkg/util/wait"
41+ "k8s.io/apiserver/pkg/server"
4142 "k8s.io/apiserver/pkg/server/healthz"
4243 "k8s.io/apiserver/pkg/server/mux"
4344 utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -158,7 +159,8 @@ controller, and serviceaccounts controller.`,
158159 fg .(featuregate.MutableFeatureGate ).AddMetrics ()
159160 // add component version metrics
160161 s .ComponentGlobalsRegistry .AddMetrics ()
161- return Run (ctx , c .Complete ())
162+ stopCh := server .SetupSignalHandler ()
163+ return Run (context .Background (), c .Complete (), stopCh )
162164 },
163165 Args : func (cmd * cobra.Command , args []string ) error {
164166 for _ , arg := range args {
@@ -195,9 +197,9 @@ func ResyncPeriod(c *config.CompletedConfig) func() time.Duration {
195197}
196198
197199// 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 {
199201 logger := klog .FromContext (ctx )
200- stopCh := ctx .Done ()
202+ stopCh := mergeCh ( ctx .Done (), stopCh2 )
201203
202204 // To help debugging, immediately log version
203205 logger .Info ("Starting" , "version" , utilversion .Get ())
@@ -363,10 +365,18 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
363365 run (ctx , controllerDescriptors )
364366 },
365367 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+ }
368378 },
369- })
379+ }, stopCh )
370380
371381 // If Leader Migration is enabled, proceed to attempt the migration lock.
372382 if leaderMigrator != nil {
@@ -390,10 +400,18 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
390400 run (ctx , controllerDescriptors )
391401 },
392402 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+ }
395413 },
396- })
414+ }, stopCh )
397415 }
398416
399417 <- stopCh
@@ -903,7 +921,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde
903921
904922// leaderElectAndRun runs the leader election, and runs the callbacks once the leader lease is acquired.
905923// 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 {} ) {
907925 logger := klog .FromContext (ctx )
908926 rl , err := resourcelock .NewFromKubeconfig (resourceLock ,
909927 c .ComponentConfig .Generic .LeaderElection .ResourceNamespace ,
@@ -919,7 +937,13 @@ func leaderElectAndRun(ctx context.Context, c *config.CompletedConfig, lockIdent
919937 klog .FlushAndExit (klog .ExitFlushTimeout , 1 )
920938 }
921939
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 {
923947 Lock : rl ,
924948 LeaseDuration : c .ComponentConfig .Generic .LeaderElection .LeaseDuration .Duration ,
925949 RenewDeadline : c .ComponentConfig .Generic .LeaderElection .RenewDeadline .Duration ,
0 commit comments