Skip to content

fix(leaderelection): survive malformed API error bodies and lock annotations - #2707

Open
nkbeast wants to merge 1 commit into
kubernetes-client:masterfrom
nkbeast:leaderelection-survive-malformed-payloads
Open

fix(leaderelection): survive malformed API error bodies and lock annotations#2707
nkbeast wants to merge 1 commit into
kubernetes-client:masterfrom
nkbeast:leaderelection-survive-malformed-payloads

Conversation

@nkbeast

@nkbeast nkbeast commented Sep 3, 2026

Copy link
Copy Markdown

What this fixes

try_acquire_or_renew() parsed the raw API error body with json.loads and indexed straight into it:

if json.loads(old_election_record.body)['code'] != HTTPStatus.NOT_FOUND:

That assumes whatever came back is a Kubernetes Status object. Anything sitting in front of the API server — an ingress, load balancer or proxy — answers with an HTML error page, an empty payload or some other non-JSON body on a bad day, and ApiException.body can also be None. Each of those raises out of the election loop (JSONDecodeError/TypeError), and in the aio elector an empty body dies even earlier on an assert.

Leader election dying like this is the one failure mode the code exists to prevent: the controller stops renewing its lease, never calls onstopped_leading, and the workload sits in limbo instead of failing over. Reproduced locally with a lock returning a 502 HTML body — JSONDecodeError before this change, clean False retry after.

The fix

  • An unparsable or missing error body is treated as "not a 404": the elector logs and retries on the next period, exactly like any other API hiccup. A clean 404 still allows lock creation, unchanged.
  • The same crash existed on the read path of the ConfigMap lock: a corrupted leader-election annotation raised out of get(). It is now treated like a missing annotation so the next update rewrites a clean record.
  • Both the sync and the aio implementations are fixed.

Tests

Four regression tests added: HTML error body, None body, the clean-404 create path (to prove that behaviour is untouched), and the aio elector. Full suite green locally: 436 passed, 28 skipped with the same command CI uses.

Fixed leader election crashing when the API server or an intermediary returns a malformed or non-JSON error body; the elector now treats it like any other API error and retries on the next period instead of raising. A corrupted leader-election annotation on the ConfigMap lock no longer breaks reads either.

@kubernetes-prow kubernetes-prow Bot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Sep 3, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: nkbeast
Once this PR has been reviewed and has the lgtm label, please assign roycaihw 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 roycaihw and yliaog September 3, 2026 19:35
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 3, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: nkbeast / name: NK (ce274dc)

@kubernetes-prow kubernetes-prow Bot added the needs-kind Indicates a PR lacks a `kind/foo` label and requires one. label Sep 3, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @nkbeast!

It looks like this is your first PR to kubernetes-client/python 🎉. 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-client/python 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 kubernetes-prow Bot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 3, 2026
…tations

try_acquire_or_renew() parsed the raw error body with json.loads and
indexing straight into it, on the assumption that whatever came back is
a Kubernetes Status object. Anything sitting in front of the API server
(in an ingress, load balancer or proxy) happily answers with an HTML
error page, an empty payload or some other non-JSON body, and
ApiException.body can also be None. Each of those raised out of the
election loop and took the whole leader election down, which is the one
failure mode this code exists to prevent - a controller that stops
renewing its lease without ever calling onstopped_leading leaves the
workload in limbo until an operator notices.

Treat an unparsable or missing error body as 'not a 404' and retry on
the next period, in both the sync and the aio elector (the aio one also
crashed on an empty body through an assert).

The same class of problem existed on the read path of the ConfigMap
lock: a corrupted leader-election annotation raised out of get() and
killed the elector. Treat a non-JSON annotation like a missing one so
the next update rewrites a clean record.

Signed-off-by: NK <nk@localhost.localdomain>
Signed-off-by: NK <92711184+nkbeast@users.noreply.github.com>
@nkbeast
nkbeast force-pushed the leaderelection-survive-malformed-payloads branch from d301654 to ce274dc Compare September 3, 2026 19:39
@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Sep 3, 2026
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. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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.

1 participant