Repository navigation
Description
User Story
I maintain the OpenShell Kubernetes driver and run the HA gateway (k3s-ha) conformance lanes. While fixing an HA restart race in #6103, I found that a lifecycle request on one gateway replica can silently overwrite a suspension that the lease holder started, so a sandbox the holder decided to suspend keeps running.
Problem Statement
In an HA gateway, only the lease holder reconciles sandbox runtimes, but any replica serves lifecycle RPCs. Lifecycle writes to the Sandbox CR go through patch_dynamic_object_with_resource_version_retry, which rereads the CR on a resourceVersion conflict and passes the patch builder only the new resourceVersion. Every write therefore retries until it lands, whatever transition the CR is in by then. A restart never checks that its Preparing claim still stands.
Interleaving:
- A restart on replica A claims a stopped sandbox (
Preparing, operationrestart). - The lease holder suspends the sandbox while that bootstrap is fresh, for example because the resource admission re-check fails. The CR becomes Suspended, phase
Suspending, operationstop. - Replica A's Running patch conflicts, rereads, and sets the sandbox back to Running.
- Replica A's
Releasedphase patch leaves phaseReleasedwith operationstop. That state never completes, because completion requires operationcreateorrestart. The workload keeps running until the 300 s bootstrap grace period expires and reconciliation reaps it.
On main before #6103, the restart's Running patch rewrote the whole claim, so it fully undid the suspension instead of leaving a half-applied state. In both cases a suspension decided by the holder, such as an admission or fence failure, can be overridden by another replica.
Impact / Why This Matters
A security-relevant suspension (admission or network-fence drift) can be reversed by an unrelated restart on another replica, leaving the workload running for up to the grace period. There is no operator workaround besides running a single gateway replica.
Acceptance Criteria
- A create or restart write to the Sandbox CR fails instead of overwriting a transition another replica started, and the request takes its existing rollback path.
- A restart claims the Sandbox only when it is stopped and no other transition is in progress.
- A unit test covers the interleaving above: a claimed write that conflicts, rereads a suspension, and fails without writing again.
- The HA paragraph in
crates/openshell-driver-kubernetes/README.mdno longer lists this as a limitation.
Reproduction Steps
There is no deterministic end-to-end reproduction; the suspension has to land between a restart's claim and its Running or Released patch.
- Run an HA gateway with the Kubernetes driver (for example the k3s-ha conformance environment) with at least two replicas.
- Restart a stopped sandbox through a replica that does not hold the reconcile lease.
- While that restart is
Preparing, make the lease holder suspend the sandbox, for example by invalidating its admitted resources. - Observe the Sandbox CR end up Running with phase
Releasedand operationstopuntil the grace period reaps it.
Proposed Direction
Branch fix/ha-lifecycle-claim-cas/matthewgrossman (commit 54af099) has a working draft:
compare_and_patch_dynamic_objectpasses the current Sandbox to the patch builder, which may refuse the write.- A
SandboxRuntimeBootstrapClaim(Sandbox UID, operation, generation) is claimed by restart only on a stopped, unclaimed Sandbox. Create's claim is its initial annotations. - The Running, fence-binding and
Releasedwrites of create and restart require the claim to still hold. - A
SandboxRuntimeBootstrapOperationenum replaces bare"create"/"restart"/"stop"strings.
Open questions to resolve there:
- Stale claims: with the claim check, can a retried start with a new generation be refused after a replica crashed mid-restart, until the 300 s grace period reaps the abandoned claim? Today it proceeds. On a first reading, the start path stops an in-progress restart before claiming, which would clear the abandoned claim, but this needs confirming. Decide whether a request may take over a stale claim, or whether waiting is acceptable.
- Helper shape: the draft layers three write helpers (
compare_and_patch_dynamic_object,patch_dynamic_object_with_resource_version_retry,patch_claimed_sandbox). Consider a flatter shape.
Environment
- OpenShell:
mainafter fix(kubernetes): keep HA reconciliation from deleting a restarting supervisor #6103 - Runtime, deployment, or integration: Kubernetes compute driver, HA gateway with two or more replicas
Activity
docs(kubernetes): document HA reconciliation ordering and Preparing c…