Skip to content

Self-fence Kubernetes Lease leadership using monotonic time#4042

Merged
marcschier merged 4 commits into
OPCFoundation:masterfrom
marcschier:marcschier/review-fix/kubernetes-lease-fencing
Jul 21, 2026
Merged

Self-fence Kubernetes Lease leadership using monotonic time#4042
marcschier merged 4 commits into
OPCFoundation:masterfrom
marcschier:marcschier/review-fix/kubernetes-lease-fencing

Conversation

@marcschier

@marcschier marcschier commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Failure

Lease takeover trusted another replica's wall-clock RenewTime, so clock skew could steal a live Lease. A hung renewal could also leave local leadership asserted, and a stale completion or notification could resurrect leadership after fencing or disposal.

Fix

  • Measure unchanged foreign resourceVersion age with local monotonic time.
  • Serialize and bound API attempts, and self-fence with a lease-duration watchdog.
  • Reject stale completions and suppress out-of-order leadership notifications.

Standard

This is a Kubernetes coordination extension to OPC 10000-4 §6.6 redundancy. Kubernetes Lease ownership is now evaluated from stable resource observations rather than untrusted peer wall clocks.

Tests

  • dotnet test tests\Opc.Ua.Redundancy.Kubernetes.Tests\Opc.Ua.Redundancy.Kubernetes.Tests.csproj -c Release --nologo -m:1 — 97 passed on net8.0 and 97 passed on net10.0

  • Review follow-up: KubernetesLeaseLeaderElectionTests — 22 passed on net8.0.

Split

Split from #4021.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@marcschier
marcschier marked this pull request as ready for review July 19, 2026 10:56
Copilot AI review requested due to automatic review settings July 19, 2026 10:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR hardens Kubernetes Lease leader election against clock skew and stale/late operations by switching takeover logic to local monotonic observations and adding fencing/serialization around API attempts and notifications.

Changes:

  • Evaluate foreign lease expiry using unchanged resourceVersion duration measured with local monotonic time.
  • Serialize and bound Kubernetes API calls; add a watchdog to self-fence on hung renewal attempts.
  • Suppress stale/out-of-order leadership notifications and reject stale completions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
tests/Opc.Ua.Redundancy.Kubernetes.Tests/LeaderElection/KubernetesLeaseLeaderElectionTests.cs Updates/expands tests to validate monotonic takeover window, resourceVersion reset behavior, watchdog fencing, and notification ordering.
src/Opc.Ua.Redundancy.Kubernetes/LeaderElection/KubernetesLeaseLeaderElection.cs Implements monotonic resourceVersion observation, API attempt gating/timeouts, watchdog self-fencing, and notification generation fencing.

Comment thread src/Opc.Ua.Redundancy.Kubernetes/LeaderElection/KubernetesLeaseLeaderElection.cs Outdated
Comment thread src/Opc.Ua.Redundancy.Kubernetes/LeaderElection/KubernetesLeaseLeaderElection.cs Outdated
@marcschier
marcschier marked this pull request as draft July 19, 2026 11:35
@marcschier
marcschier marked this pull request as ready for review July 19, 2026 11:54
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@marcschier
marcschier marked this pull request as draft July 19, 2026 13:38
@marcschier
marcschier marked this pull request as ready for review July 19, 2026 14:13
marcschier and others added 2 commits July 19, 2026 16:35
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@marcschier
marcschier marked this pull request as draft July 19, 2026 17:25
@marcschier
marcschier marked this pull request as ready for review July 19, 2026 17:31
@marcschier

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@marcschier
marcschier requested a review from romanett July 19, 2026 18:37
@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.53%. Comparing base (70c74e7) to head (f85d0b5).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4042      +/-   ##
==========================================
- Coverage   73.55%   73.53%   -0.03%     
==========================================
  Files        1345     1345              
  Lines      180036   180038       +2     
  Branches    31677    31678       +1     
==========================================
- Hits       132427   132390      -37     
- Misses      36911    36944      +33     
- Partials    10698    10704       +6     

see 17 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@marcschier marcschier added the ready Ready to merge once CI Passes label Jul 21, 2026
@marcschier
marcschier merged commit 5206cc4 into OPCFoundation:master Jul 21, 2026
209 of 211 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready Ready to merge once CI Passes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants