exit with exit code 0 on cancel & release leader-election on cancel

Signed-off-by: Inteon <42113979+inteon@users.noreply.github.com>
This commit is contained in:
Inteon 2021-07-27 19:43:08 +02:00
parent 632459c6d9
commit 48e9c2bd16
No known key found for this signature in database
GPG Key ID: BD5DCF7303C7C1A7
2 changed files with 23 additions and 15 deletions

View File

@ -117,15 +117,16 @@ servers and webhook servers.`,
func (o InjectorControllerOptions) RunInjectorController(ctx context.Context) error {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: api.Scheme,
Namespace: o.Namespace,
LeaderElection: o.LeaderElect,
LeaderElectionNamespace: o.LeaderElectionNamespace,
LeaderElectionID: "cert-manager-cainjector-leader-election",
LeaseDuration: &o.LeaseDuration,
RenewDeadline: &o.RenewDeadline,
RetryPeriod: &o.RetryPeriod,
MetricsBindAddress: "0",
Scheme: api.Scheme,
Namespace: o.Namespace,
LeaderElection: o.LeaderElect,
LeaderElectionNamespace: o.LeaderElectionNamespace,
LeaderElectionID: "cert-manager-cainjector-leader-election",
LeaderElectionReleaseOnCancel: true,
LeaseDuration: &o.LeaseDuration,
RenewDeadline: &o.RenewDeadline,
RetryPeriod: &o.RetryPeriod,
MetricsBindAddress: "0",
})
if err != nil {
return fmt.Errorf("error creating manager: %v", err)

View File

@ -337,15 +337,22 @@ func startLeaderElection(ctx context.Context, opts *options.ControllerOptions, l
// Try and become the leader and start controller manager loops
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: ml,
LeaseDuration: opts.LeaderElectionLeaseDuration,
RenewDeadline: opts.LeaderElectionRenewDeadline,
RetryPeriod: opts.LeaderElectionRetryPeriod,
Lock: ml,
LeaseDuration: opts.LeaderElectionLeaseDuration,
RenewDeadline: opts.LeaderElectionRenewDeadline,
RetryPeriod: opts.LeaderElectionRetryPeriod,
ReleaseOnCancel: true,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: run,
OnStoppedLeading: func() {
log.V(logf.ErrorLevel).Info("leader election lost")
os.Exit(1)
select {
case <-ctx.Done():
// context was canceled, just return
return
default:
log.V(logf.ErrorLevel).Info("leader election lost")
os.Exit(1)
}
},
},
})