Skip to content

Commit fc91252

Browse files
tnozickasoltysh
authored andcommitted
UPSTREAM: <carry>: Release lock on KCM and KS termination
UPSTREAM: <carry>: Force releasing the lock on exit for KS squash with UPSTREAM: <carry>: 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
1 parent 5af594b commit fc91252

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

Diff for: cmd/kube-controller-manager/app/controllermanager.go

+34-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/apimachinery/pkg/util/uuid"
3636
"k8s.io/apimachinery/pkg/util/wait"
3737
genericfeatures "k8s.io/apiserver/pkg/features"
38+
"k8s.io/apiserver/pkg/server"
3839
"k8s.io/apiserver/pkg/server/healthz"
3940
"k8s.io/apiserver/pkg/server/mux"
4041
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -138,7 +139,9 @@ controller, and serviceaccounts controller.`,
138139
fmt.Fprintf(os.Stderr, "%v\n", err)
139140
return err
140141
}
141-
return Run(c.Complete(), wait.NeverStop)
142+
143+
stopCh := server.SetupSignalHandler()
144+
return Run(c.Complete(), stopCh)
142145
},
143146
Args: func(cmd *cobra.Command, args []string) error {
144147
for _, arg := range args {
@@ -287,10 +290,18 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
287290
run(ctx, startSATokenController, initializersFunc)
288291
},
289292
OnStoppedLeading: func() {
290-
klog.ErrorS(nil, "leaderelection lost")
291-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
293+
select {
294+
case <-stopCh:
295+
// We were asked to terminate. Exit 0.
296+
klog.Info("Requested to terminate. Exiting.")
297+
os.Exit(0)
298+
default:
299+
// We lost the lock.
300+
klog.ErrorS(nil, "leaderelection lost")
301+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
302+
}
292303
},
293-
})
304+
}, stopCh)
294305

295306
// If Leader Migration is enabled, proceed to attempt the migration lock.
296307
if leaderMigrator != nil {
@@ -311,10 +322,18 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
311322
run(ctx, nil, createInitializersFunc(leaderMigrator.FilterFunc, leadermigration.ControllerMigrated))
312323
},
313324
OnStoppedLeading: func() {
314-
klog.ErrorS(nil, "migration leaderelection lost")
315-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
325+
select {
326+
case <-stopCh:
327+
// We were asked to terminate. Exit 0.
328+
klog.Info("Requested to terminate. Exiting.")
329+
os.Exit(0)
330+
default:
331+
// We lost the lock.
332+
klog.ErrorS(nil, "migration leaderelection lost")
333+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
334+
}
316335
},
317-
})
336+
}, stopCh)
318337
}
319338

320339
<-stopCh
@@ -709,7 +728,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde
709728

710729
// leaderElectAndRun runs the leader election, and runs the callbacks once the leader lease is acquired.
711730
// TODO: extract this function into staging/controller-manager
712-
func leaderElectAndRun(c *config.CompletedConfig, lockIdentity string, electionChecker *leaderelection.HealthzAdaptor, resourceLock string, leaseName string, callbacks leaderelection.LeaderCallbacks) {
731+
func leaderElectAndRun(c *config.CompletedConfig, lockIdentity string, electionChecker *leaderelection.HealthzAdaptor, resourceLock string, leaseName string, callbacks leaderelection.LeaderCallbacks, stopCh <-chan struct{}) {
713732
rl, err := resourcelock.NewFromKubeconfig(resourceLock,
714733
c.ComponentConfig.Generic.LeaderElection.ResourceNamespace,
715734
leaseName,
@@ -723,7 +742,13 @@ func leaderElectAndRun(c *config.CompletedConfig, lockIdentity string, electionC
723742
klog.Fatalf("error creating lock: %v", err)
724743
}
725744

726-
leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
745+
leCtx, cancel := context.WithCancel(context.Background())
746+
defer cancel()
747+
go func() {
748+
<-stopCh
749+
cancel()
750+
}()
751+
leaderelection.RunOrDie(leCtx, leaderelection.LeaderElectionConfig{
727752
Lock: rl,
728753
LeaseDuration: c.ComponentConfig.Generic.LeaderElection.LeaseDuration.Duration,
729754
RenewDeadline: c.ComponentConfig.Generic.LeaderElection.RenewDeadline.Duration,

0 commit comments

Comments
 (0)