From fc91252212ea7d55de5f31d4e57ce227d80ebb74 Mon Sep 17 00:00:00 2001 From: Tomas Nozicka Date: Thu, 29 Oct 2020 13:56:29 +0100 Subject: [PATCH] UPSTREAM: : Release lock on KCM and KS termination UPSTREAM: : Force releasing the lock on exit for KS squash with UPSTREAM: : Release lock on KCM and KS termination openshift-rebase(v1.24):source=93017a1df89 openshift-rebase(v1.24):source=93017a1df89 openshift-rebase(v1.24):source=93017a1df89 --- .../app/controllermanager.go | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/cmd/kube-controller-manager/app/controllermanager.go b/cmd/kube-controller-manager/app/controllermanager.go index 4115b072a7809..4138a88a9a2ad 100644 --- a/cmd/kube-controller-manager/app/controllermanager.go +++ b/cmd/kube-controller-manager/app/controllermanager.go @@ -35,6 +35,7 @@ import ( "k8s.io/apimachinery/pkg/util/uuid" "k8s.io/apimachinery/pkg/util/wait" genericfeatures "k8s.io/apiserver/pkg/features" + "k8s.io/apiserver/pkg/server" "k8s.io/apiserver/pkg/server/healthz" "k8s.io/apiserver/pkg/server/mux" utilfeature "k8s.io/apiserver/pkg/util/feature" @@ -138,7 +139,9 @@ controller, and serviceaccounts controller.`, fmt.Fprintf(os.Stderr, "%v\n", err) return err } - return Run(c.Complete(), wait.NeverStop) + + stopCh := server.SetupSignalHandler() + return Run(c.Complete(), stopCh) }, Args: func(cmd *cobra.Command, args []string) error { for _, arg := range args { @@ -287,10 +290,18 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error { run(ctx, startSATokenController, initializersFunc) }, OnStoppedLeading: func() { - klog.ErrorS(nil, "leaderelection lost") - klog.FlushAndExit(klog.ExitFlushTimeout, 1) + select { + case <-stopCh: + // We were asked to terminate. Exit 0. + klog.Info("Requested to terminate. Exiting.") + os.Exit(0) + default: + // We lost the lock. + klog.ErrorS(nil, "leaderelection lost") + klog.FlushAndExit(klog.ExitFlushTimeout, 1) + } }, - }) + }, stopCh) // If Leader Migration is enabled, proceed to attempt the migration lock. if leaderMigrator != nil { @@ -311,10 +322,18 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error { run(ctx, nil, createInitializersFunc(leaderMigrator.FilterFunc, leadermigration.ControllerMigrated)) }, OnStoppedLeading: func() { - klog.ErrorS(nil, "migration leaderelection lost") - klog.FlushAndExit(klog.ExitFlushTimeout, 1) + select { + case <-stopCh: + // We were asked to terminate. Exit 0. + klog.Info("Requested to terminate. Exiting.") + os.Exit(0) + default: + // We lost the lock. + klog.ErrorS(nil, "migration leaderelection lost") + klog.FlushAndExit(klog.ExitFlushTimeout, 1) + } }, - }) + }, stopCh) } <-stopCh @@ -709,7 +728,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde // leaderElectAndRun runs the leader election, and runs the callbacks once the leader lease is acquired. // TODO: extract this function into staging/controller-manager -func leaderElectAndRun(c *config.CompletedConfig, lockIdentity string, electionChecker *leaderelection.HealthzAdaptor, resourceLock string, leaseName string, callbacks leaderelection.LeaderCallbacks) { +func leaderElectAndRun(c *config.CompletedConfig, lockIdentity string, electionChecker *leaderelection.HealthzAdaptor, resourceLock string, leaseName string, callbacks leaderelection.LeaderCallbacks, stopCh <-chan struct{}) { rl, err := resourcelock.NewFromKubeconfig(resourceLock, c.ComponentConfig.Generic.LeaderElection.ResourceNamespace, leaseName, @@ -723,7 +742,13 @@ func leaderElectAndRun(c *config.CompletedConfig, lockIdentity string, electionC klog.Fatalf("error creating lock: %v", err) } - leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{ + leCtx, cancel := context.WithCancel(context.Background()) + defer cancel() + go func() { + <-stopCh + cancel() + }() + leaderelection.RunOrDie(leCtx, leaderelection.LeaderElectionConfig{ Lock: rl, LeaseDuration: c.ComponentConfig.Generic.LeaderElection.LeaseDuration.Duration, RenewDeadline: c.ComponentConfig.Generic.LeaderElection.RenewDeadline.Duration,