Skip to content

Add LeaseLock for leader election - #2690

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
chala2001:leaselock-leader-election
Aug 30, 2026
Merged

Add LeaseLock for leader election#2690
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
chala2001:leaselock-leader-election

Conversation

@chala2001

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Leader election can currently only use a ConfigMap. client-go moved to the coordination.k8s.io Lease resource and removed ConfigMap support, so this adds a LeaseLock next to ConfigMapLock. The example now uses it, with the ConfigMap line kept commented out.

Which issue(s) this PR fixes:

Fixes #1877

Special notes for your reviewer:

This picks up @rvlane's #2314, which you reviewed with "LGTM overall. Could you add a test?" and which the triage robot later closed as rotten. The original work is credited with a Co-authored-by trailer. The tests you asked for are in leaselock_test.py.

Four things changed from that version while rebasing onto current master:

  • Logger. The original called logging.basicConfig(level=logging.INFO) at import, which reconfigures the root logger of whatever application imports the library. This package moved to logger = logging.getLogger("leaderelection") in the meantime, so it uses that now.
  • Time formats. str(datetime) leaves the microseconds out when they are exactly zero, and the original only accepted the %f spellings. Those times fell through and returned None, so the lease was written with a null acquireTime. Covered by test_record_without_microseconds_survives_a_write_and_read.
  • lease_reference is kept after create, so a following update has something to work from. update also returns False rather than raising AttributeError when it is called before a get, which matches client-go's lease not initialized check.
  • UTC. leaderelection.py builds its times with datetime.fromtimestamp(), which is naive local time. The original appended Z to it, which labels local wall clock as UTC and writes a timestamp that is wrong by the host's offset. Since the point of a Lease is that other clients read it, the times are now converted to the real UTC instant and converted back on read, so the election record still round trips unchanged. test_times_are_written_as_the_real_utc_instant pins that under Asia/Kolkata.

One thing I left out, happy to follow up if you would like it: leaseTransitions is neither read nor written, because LeaderElectionRecord has no field for it. client-go writes it on every update. Adding it would touch the shared record class and ConfigMapLock as well, so it seemed better as a separate change.

Verified: 17 tests pass under kubernetes/base/leaderelection (5 existing, 12 new), and reverting each of the four fixes above makes the matching test fail. pycodestyle --max-line-length=100 is clean on both new files. I have not run the functional or e2e suites.

Add LeaseLock for leader election, using the coordination.k8s.io Lease resource.

@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 28, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from fabianvf and yliaog August 28, 2026 18:57
Comment thread kubernetes/base/leaderelection/example.py
@chala2001
chala2001 force-pushed the leaselock-leader-election branch from ff94d3b to 6073f7f Compare August 28, 2026 19:23
@chala2001

chala2001 commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

@yliaog this one is ready when you get a chance. @Invictus17 reviewed it and approved, and I pushed the change he asked for in 6073f7f.

He isn't in OWNERS though, so Prow still shows it as not approved ,, it needs /lgtm and /approve from you or @fabianvf before it can go in..

Comment thread kubernetes/base/leaderelection/resourcelock/leaselock.py Outdated
Comment thread kubernetes/base/leaderelection/resourcelock/leaselock_test.py Outdated
Comment thread kubernetes/base/leaderelection/resourcelock/leaselock.py Outdated
Leader election could only use a ConfigMap. client-go moved to the
coordination.k8s.io Lease resource and dropped ConfigMap support, so add
a LeaseLock next to ConfigMapLock and use it in the example.

Based on the implementation from kubernetes-client#2314, with four corrections: use the
package logger instead of calling logging.basicConfig at import, accept
the times str(datetime) produces when the microseconds are zero, keep the
created lease so a following update has a reference, and store the times
as the real UTC instant rather than local wall clock labeled as UTC.

Co-authored-by: Lane Richard <rick.lane@nokia.com>
@chala2001
chala2001 force-pushed the leaselock-leader-election branch from 6073f7f to 9b87f18 Compare August 30, 2026 04:22
@chala2001

Copy link
Copy Markdown
Contributor Author

Fixed all three in 9b87f18 - both copyright years and the spelling. Thanks for the review.

@chala2001
chala2001 requested a review from yliaog August 30, 2026 04:25
@yliaog

yliaog commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

thanks for the PR

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 30, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chala2001, Invictus17, yliaog

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

The pull request process is described 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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 30, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit ffd05cd into kubernetes-client:master Aug 30, 2026
10 checks passed
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. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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.

add support Lease-based leader election (rather than ConfigMaps)

3 participants