Skip to content

✨ leader election: Block in OnStartedLeading until the context is cancelled - #3560

Open
tchap wants to merge 2 commits into
kubernetes-sigs:mainfrom
tchap:leader-election-block-on-release
Open

✨ leader election: Block in OnStartedLeading until the context is cancelled#3560
tchap wants to merge 2 commits into
kubernetes-sigs:mainfrom
tchap:leader-election-block-on-release

Conversation

@tchap

@tchap tchap commented Jul 22, 2026

Copy link
Copy Markdown

This change is necessary to push kubernetes/kubernetes#139991 in upstream.

Basically after the upstream change is merged, when ReleaseOnCancel is set for leader election, Run() calls OnStartedLeading synchronously (it tracks the goroutine) and keeps renewing the lease until it returns, then releases the lease. We must block until ctx is cancelled and all runnables have finished.

This patch aligns controller-runtime to be compatible with that change.

The change should be actually backwards compatible from what I can tell. I added a second commit containing a test, but that points to the upstream PR so that I can test the new behavior. So before merging this, the second commit needs to be dropped.

/hold

tchap added 2 commits July 21, 2026 15:30
Align with upstream Kubernetes changes to leaderelection.Run() which,
when ReleaseOnCancel is true, calls OnStartedLeading synchronously and
keeps renewing the lease until it returns before releasing it.

When leaderElectionReleaseOnCancel is set, OnStartedLeading now blocks
on <-ctx.Done() and then calls StopAndWait on the leader election
runnables to ensure all goroutines have terminated before returning.
This guarantees the lease is not released while runnables are still
active — both in the normal shutdown path (where engageStopProcedure
already called StopAndWait, making this a no-op via stopOnce) and in
the renewal failure path (where this is the first StopAndWait call).
Add two tests verifying that leader election runnables are stopped
before the lease is released when ReleaseOnCancel is true:

- Normal shutdown: the runnable asserts the lease is still held while
  it shuts down, confirming the lease is only released afterward.
- Renewal failure: blocking the fake lock simulates renewal failure.
  The test verifies the runnable finishes before Start returns, which
  is only possible if OnStartedLeading called StopAndWait.

Point k8s.io/* dependencies to github.com/tchap/kubernetes at the
fix-leader-election-release-on-cancel branch to pick up the upstream
ReleaseOnCancel behavior changes.
@kubernetes-prow kubernetes-prow Bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 22, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tchap
Once this PR has been reviewed and has the lgtm label, please assign alvaroaleman for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow
kubernetes-prow Bot requested review from inteon and sbueringer July 22, 2026 11:26
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @tchap!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @tchap. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 22, 2026
@Jefftree

Copy link
Copy Markdown
Member

/ok-to-test

@kubernetes-prow kubernetes-prow Bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 22, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

@tchap: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-controller-runtime-test ab4d6b3 link true /test pull-controller-runtime-test

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@kubernetes-prow kubernetes-prow Bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 24, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants