controller-runtime icon indicating copy to clipboard operation
controller-runtime copied to clipboard

🐛 fix bug when set renewDeadline greater than leaseDuration the control…

Open yuzp1996 opened this issue 3 years ago • 18 comments

fix bug when set renewDeadline greater than leaseDuration the controller will hang and no error will be return

Issue: https://github.com/kubernetes-sigs/controller-runtime/issues/1760

yuzp1996 avatar Dec 30 '21 16:12 yuzp1996

CLA Signed

The committers are authorized under a signed CLA.

  • :white_check_mark: 于志鹏 (e009b832e22b14406b9ee606d4e3a8f8a7e64f4f)

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please sign in with your organization's credentials at https://identity.linuxfoundation.org/projects/cncf to be authorized.
  • If you have done the above and are still having issues with the CLA being reported as unsigned, please log a ticket with the Linux Foundation Helpdesk: https://support.linuxfoundation.org/
  • Should you encounter any issues with the Linux Foundation Helpdesk, send a message to the backup e-mail support address at: [email protected]

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. I understand the commands that are listed here.

k8s-ci-robot avatar Dec 30 '21 16:12 k8s-ci-robot

Welcome @yuzp1996!

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. :smiley:

k8s-ci-robot avatar Dec 30 '21 16:12 k8s-ci-robot

Hi @yuzp1996. 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 avatar Dec 30 '21 16:12 k8s-ci-robot

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yuzp1996 To complete the pull request process, please assign droot after the PR has been reviewed. You can assign the PR to them by writing /assign @droot in a comment when ready.

The full list of commands accepted by this bot can be found 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 avatar Dec 30 '21 16:12 k8s-ci-robot

@yuzp1996 Yeah, it is a bug here, but not only for the NewLeaderElector error.

The cause of the manager hang during stopping, is the cm.leaderElectionStopped might not be closed. Sometimes the cm.leaderElectionCancel might be nil so it could also panic here. For example it fails to cm.add(cm.cluster) or cm.runnables.Webhooks.Start(cm.internalCtx) in the Start().

        // Cancel leader election only after we waited. It will os.Exit() the app for safety.
        if cm.resourceLock != nil {
	        // After asking the context to be cancelled, make sure
	        // we wait for the leader stopped channel to be closed, otherwise
	        // we might encounter race conditions between this code
	        // and the event recorder, which is used within leader election code.
	        cm.leaderElectionCancel()
	        <-cm.leaderElectionStopped
        }

So it might need to change like this, IMHO

  1. Init leaderElectionStopped only when NewLeaderElector succeeds.
  2. Should check if cm.leaderElectionCancel is nil before cm.leaderElectionCancel() and check if cm.leaderElectionStopped is nil before <-cm.leaderElectionStopped. If so, maybe there is no need to check if cm.resourceLock != nil here any more.

FillZpp avatar Dec 31 '21 03:12 FillZpp

@yuzp1996 And you have to fix the failed job, which you can test locally with make lint.

FillZpp avatar Jan 04 '22 03:01 FillZpp

Thanks for fixing this. /lgtm /cc @vincepri @alvaroaleman PTAL

FillZpp avatar Jan 04 '22 06:01 FillZpp

@FillZpp: GitHub didn't allow me to request PR reviews from the following users: PTAL.

Note that only kubernetes-sigs members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

Thanks for fixing this. /lgtm /cc @vincepri @alvaroaleman PTAL

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 avatar Jan 04 '22 06:01 k8s-ci-robot

Yes, I think we have had the validation but when someone set renewDeadline greater than lease duration the whole process will block and does not throw an error.

I don't think this is the correct way to do that so I make some changes to make sure errors can be thrown

yuzp1996 avatar Jan 19 '22 23:01 yuzp1996

@vincepri As I said above, it is not only caused by renewDeadline greater than lease duration.

Any error occurred between defer engageStopProcedure in controllerManager.Start() and // Start the leader elector process in controllerManager.startLeaderElection(), such as:

  1. Error of cm.add(cm.cluster)
  2. Error of cm.runnables.Webhooks.Start(cm.internalCtx)
  3. Error of cm.runnables.Caches.Start(cm.internalCtx)
  4. Error of cm.runnables.Others.Start(cm.internalCtx)
  5. Error of leaderelection.NewLeaderElector

They will all return error, trigger engageStopProcedure in defer, and finally hang on <-cm.leaderElectionStopped, because the channel will not be closed forever.

FillZpp avatar Jan 20 '22 03:01 FillZpp

WDYT @vincepri

FillZpp avatar Jan 25 '22 06:01 FillZpp

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Apr 25 '22 07:04 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar May 25 '22 07:05 k8s-triage-robot

/remove-lifecycle rotten

FillZpp avatar May 25 '22 07:05 FillZpp

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Aug 23 '22 08:08 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Sep 22 '22 08:09 k8s-triage-robot

Can we add a failing test that covers the above bug first which can help explain and prevent the deadlock above? I cannot reproduce locally, so it'd be good to have a test for this

vincepri avatar Sep 26 '22 16:09 vincepri

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

k8s-triage-robot avatar Oct 26 '22 17:10 k8s-triage-robot

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

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 avatar Oct 26 '22 17:10 k8s-ci-robot