Skip to content

Commit

Permalink
UPSTREAM: <carry>: Release lock on KCM and KS termination
Browse files Browse the repository at this point in the history
UPSTREAM: <carry>: Force releasing the lock on exit for KS
  • Loading branch information
tnozicka authored and soltysh committed Sep 8, 2021
1 parent 02f6407 commit 7fddf48
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
26 changes: 21 additions & 5 deletions cmd/kube-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ controller, and serviceaccounts controller.`,
os.Exit(1)
}

if err := Run(c.Complete(), wait.NeverStop); err != nil {
stopCh := server.SetupSignalHandler()
if err := Run(c.Complete(), stopCh); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand Down Expand Up @@ -300,19 +301,34 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
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,
RetryPeriod: c.ComponentConfig.Generic.LeaderElection.RetryPeriod.Duration,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: run,
OnStoppedLeading: func() {
klog.Fatalf("leaderelection lost")
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.Exitf("leaderelection lost")
}
},
},
WatchDog: electionChecker,
Name: "kube-controller-manager",
WatchDog: electionChecker,
Name: "kube-controller-manager",
ReleaseOnCancel: true,
})
panic("unreachable")
}
Expand Down
18 changes: 16 additions & 2 deletions cmd/kube-scheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
goruntime "runtime"

"github.com/spf13/cobra"

utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authorization/authorizer"
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/server"
genericfilters "k8s.io/apiserver/pkg/server/filters"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/apiserver/pkg/server/mux"
Expand Down Expand Up @@ -123,6 +123,11 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
stopCh := server.SetupSignalHandler()
<-stopCh
cancel()
}()

cc, sched, err := Setup(ctx, opts, registryOptions...)
if err != nil {
Expand Down Expand Up @@ -202,9 +207,18 @@ func Run(ctx context.Context, cc *schedulerserverconfig.CompletedConfig, sched *
sched.Run(ctx)
},
OnStoppedLeading: func() {
klog.Fatalf("leaderelection lost")
select {
case <-ctx.Done():
// We were asked to terminate. Exit 0.
klog.Info("Requested to terminate. Exiting.")
os.Exit(0)
default:
// We lost the lock.
klog.Exitf("leaderelection lost")
}
},
}
cc.LeaderElection.ReleaseOnCancel = true
leaderElector, err := leaderelection.NewLeaderElector(*cc.LeaderElection)
if err != nil {
return fmt.Errorf("couldn't create leader elector: %v", err)
Expand Down

0 comments on commit 7fddf48

Please sign in to comment.