allthingssecurity opened a new pull request, #30907: URL: https://github.com/apache/camel/pull/30907
# Description [CAMEL-27585](https://issues.apache.org/jira/browse/CAMEL-27585) CAMEL-27296 made a stopped `KubernetesClusterView` give up the lease, but `KubernetesLeadershipController.releaseLeadership()` only does so when the leadership of the pod has taken effect (`LEADER`, `LOSING_LEADERSHIP`, `LEADERSHIP_LOST`). Two cases are left out where the pod is `NOT_LEADER` and the lease still names it: - The pod finds the lease empty (or absent) and sends the request that takes it with immediate effect. The API server applies it, and the view is stopped before the answer arrives. The stop interrupts the refresh, the request fails on the client side, and the controller stays `NOT_LEADER`, so the release is skipped. The lease names the stopped view. - The leader is not ready for a moment, so its next refresh moves it to `NOT_LEADER` without touching the lease. The view is stopped in that window, and the release is skipped again. Once the pod is ready again (before another pod took over), the lease names the stopped view. Every other pod then sees a valid holder (a ready pod) and only reports it: the group has no leader for as long as that pod stays ready. That has no bound when only the view stops (the last clustered route or master consumer of the group goes away) and the rest of the pod keeps running. An empty lease is common right after another pod released it at its stop, so the first case fits rolling restarts. The second case needs a readiness flap shorter than the retry period of the other pods, so it is rare; it is folded in because it is one condition in the same place. In both cases the pod is not acting as leader, and no other pod has to wait for it to give up the leadership, so another pod may take over the lease at once. Change (`KubernetesLeadershipController` only): - A new volatile flag `emptyLeaseAcquisitionUnconfirmed` is set just before the request that takes an empty or absent lease (create, or update of a lease without holder). It is cleared by the next successful lookup, which shows the real outcome. It is not set when the pod takes over the lease of a holder that is not ready (that path goes to `BECOMING_LEADER`, whose lease is kept on purpose, as before). - `releaseLeadership()` also goes on from `NOT_LEADER` when that flag is set or when the last lookup found the lease naming this pod. The rest is unchanged: no release when the last lookup or renewal failed or when no time is left, and the release reads the lease and removes the pod only if the lease names it (otherwise it does nothing, at the cost of one read during the stop). Not changed: a stop in `BECOMING_LEADER` still keeps the lease, as documented. A stop whose wait for the running refresh runs out of time (the request is still on the wire) is the documented "no time left" case. If the API server applies the interrupted request only after the release has read the lease, the lease can still end up naming the stopped pod; closing that needs a write that fences the older request, or a holder that is only valid while it renews, which is a larger change. No upgrade guide entry: the only visible difference is that the lease is released in these two cases, as it already is for a leader. This change touches `releaseLeadership()` and `tryAcquireLeadership()` only through a few lines, so it can be combined with other work on the release path of this class. Tests: new `KubernetesClusterViewStopNotLeaderTest` (package-private, the module's `LockTestServer`, both lease resource types, Awaitility waits): - `testStopWhileCreatingTheLeaseReleasesIt` and `testStopWhileTakingAnEmptyLeaseReleasesIt`: the server applies the request of the first pod at once but answers it after 1 s (shorter than the 3 s renew deadline that the stop may use). The view is stopped as soon as the lease names the pod. The view never reported the leadership, the lease is empty after the stop, and the second pod (which could not reach the API server until then) becomes leader. - `testStopWhileNotReadyReleasesTheLease`: the leader is taken out of the ready pods while the second pod cannot reach the API server. Once it is no longer leader, its view is stopped: the lease is empty, and when the pod is ready again the second pod becomes leader. Results: with main's `KubernetesLeadershipController`, all 6 new tests fail (the lease still names the stopped pod after the stop). With this change they pass, and the new class passed 20 runs out of 20 (120 of 120 tests). The camel-kubernetes tests pass (237 run, 0 failures, 6 skipped), and the existing cluster test classes (`KubernetesClusterViewStopTest`, `KubernetesNotReadyLeaseHolderTest`, `KubernetesLeadershipRefreshTest`, `KubernetesClusterServiceTest`) passed 3 runs out of 3 (147 of 147 tests). Found with a TLA+ model of the leadership controller and its stop (liveness: some ready pod eventually leads), and reproduced against the real controller. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-27585](https://issues.apache.org/jira/browse/CAMEL-27585). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built camel-kubernetes offline with the formatter and import-sort plugins and ran the tests listed above. I did not run the full root build. No option or generated file changed.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
