Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 prevent leader election when shutting down a non-elected manager #2724

Conversation

alexandremahdhaoui
Copy link
Contributor

@alexandremahdhaoui alexandremahdhaoui commented Mar 23, 2024

Context

This PR should address a bug where the runnable group for Leader Election would start while shutting down the manager: #2719

What's happening?

However when leader election is enabled, a non-leader manager will never start the LeaderElection runnable group. Thus, the sync.Once allow starting a new election during shutdown. The change in this PR ensures Start is ineffective during shutdown.

Changes

  • On shutdown, perform a noop call to the sync.Once func associated with the LeaderElection runnableGroup to prevent it from starting a new leader election.

Copy link

linux-foundation-easycla bot commented Mar 23, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: alexandremahdhaoui / name: Alexandre Mahdhaoui (d0c20b4)

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Mar 23, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @alexandremahdhaoui!

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. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @alexandremahdhaoui. 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. Regular contributors should join the org to skip this step.

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.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Mar 23, 2024
@alexandremahdhaoui alexandremahdhaoui changed the title 🐛 prevent new leader election when a non-elected manager is shutting down #2719 🐛 prevent new leader election when a non-elected manager is shutting down Mar 23, 2024
@alexandremahdhaoui alexandremahdhaoui changed the title 🐛 prevent new leader election when a non-elected manager is shutting down 🐛 prevent leader election when shutting down a non-elected manager Mar 23, 2024
@alexandremahdhaoui alexandremahdhaoui force-pushed the fix-non-elected-shutdown branch 2 times, most recently from 4ef9863 to 49681ff Compare March 23, 2024 17:32
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ok-to-test

@@ -518,6 +518,8 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e

// Stop all the leader election runnables, which includes reconcilers.
cm.logger.Info("Stopping and waiting for leader election runnables")
// Prevent leader election when shutting down a non-elected manager
cm.runnables.LeaderElection.startOnce.Do(func() {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test in 06d575d.

@k8s-ci-robot k8s-ci-robot 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 Mar 24, 2024
@alexandremahdhaoui alexandremahdhaoui force-pushed the fix-non-elected-shutdown branch from 49681ff to 06d575d Compare March 24, 2024 17:13
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 24, 2024

Expect(m1.Add(RunnableFunc(func(ctx context.Context) error {
defer GinkgoRecover()
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you simulate a Runnable that blocks until the context is canceled?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

& make sure it is stopped before manager.Start returns?

Copy link
Contributor Author

@alexandremahdhaoui alexandremahdhaoui Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Edit] Done

@alexandremahdhaoui alexandremahdhaoui force-pushed the fix-non-elected-shutdown branch from aa2bbaa to 66e510a Compare March 26, 2024 15:07
When leader election is enabled, a non-leader manager would never start the LeaderElection runnable group.
Thus, as the shutdown process calls the sync.Once Start func of the runnableGroup; it will start a new election.
This change ensures `Start` is ineffective during shutdown.

The test ensures the LeaderElection runnableGroup is not started during shutdown.

Signed-off-by: Alexandre Mahdhaoui <alexandre.mahdhaoui@gmail.com>
@vincepri
Copy link
Member

vincepri commented Apr 2, 2024

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 2, 2024
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cherrypick release-0.17

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexandremahdhaoui, alvaroaleman

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

The pull request process is described here

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 2, 2024
@k8s-ci-robot k8s-ci-robot merged commit a2a90a8 into kubernetes-sigs:main Apr 2, 2024
7 checks passed
@alvaroaleman
Copy link
Member

/cherrypick release-0.17

@k8s-infra-cherrypick-robot

@alvaroaleman: new pull request created: #2752

In response to this:

/cherrypick release-0.17

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/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants