Repository navigation
Description
User Story
I maintain the OpenShell Kubernetes compute driver and run the HA gateway (k3s-ha) conformance lanes. While fixing the HA restart flake in #6103, I found that the driver cannot tell an outdated lifecycle action from a current one. Fixing each race where it shows up (#6103, #6109) keeps adding special cases, so I want one ordering rule that every lifecycle actor follows.
Problem Statement
In an HA gateway, any replica serves lifecycle RPCs, but only the reconciler-lease holder runs the Kubernetes driver's repair loop. The driver's per-sandbox LifecycleGates lock works only within one process. That leaves two separate races.
-
The driver's repair loop sits outside the gateway's ordering. The gateway orders each sandbox's transitions with a phase compare-and-swap on the Sandbox record. Create and start also take a durable claim (
claim_provisioning_operation,driver_operation_pending). The repair loop instead suspends sandboxes, reaps bootstraps and deletes supervisor Pods based on its own read of the Sandbox CR. test(conformance): port the on-failure restart test from e2e/rust #5781 (repair deleted a restarting supervisor Pod) and bug: HA lifecycle writes can overwrite a suspension started by another gateway replica #6108 (a restart overwrote a suspension) are both this race. The repair loop isn't even single-instance:spawn_sandbox_runtime_periodic_reconcilechecks for a closed watch only when it next sends, so a pass already in progress finishes after the lease is lost, while the new holder reconciles as soon aswatch_sandboxesstarts. -
The gateway's ordering never reaches the driver.
StopSandboxRequestandSuspendSandboxRequestcarry only the sandbox's ID or name. Several gateway paths can send a late or duplicate stop:- retries;
- startup recovery, which runs on every replica;
- the provisioning-timeout reclaim;
- the automatic-restart stop;
- start compensation;
- the shutdown sweep.
A late stop can tear down a newer start.
None of the existing IDs can say which action is newer. The provisioning attempt ID, driver_operation_id and the runtime generation are all UUIDs, and a completed stop clears the generation from the CR. The record's resource_version does increase, but status writes raise it too.
Impact / Why This Matters
In an HA gateway, a restart can fail spuriously, or be torn down right after it starts. A suspension can be silently undone. Running a single gateway replica is the only workaround. The interim fixes (#6103's Preparing deferral and CR reread before cleanup, and #6109's claim compare-and-swap) each protect one actor, and they defer drift checks for up to runtime_bootstrap_grace_seconds while a start is in progress.
Proposed Design
A gateway-issued lifecycle epoch that the driver fences against:
- Gateway: each sandbox gets a
u64lifecycle epoch, incremented inside every transition's compare-and-swap (create, start, stop, suspend, delete, timeout reclaim, automatic restart). Every driver lifecycle request carries the value from the caller's own write, never a re-read. This is an additive proto field; other drivers may ignore it. - Sandbox CR state:
fence_epoch: the highest epoch seen;runtime_epoch: the runtime that has committed, cleared when a stop completes;- the bootstrap phase, together with the fence it was set at.
- Requests:
- An epoch below the fence returns a distinct "superseded" result, which the gateway treats as a no-op.
- An epoch above the fence raises the fence by compare-and-swap.
- An equal epoch is a retry, allowed only while the CR is still in the phase that epoch set. A retry reuses children already labelled with its epoch instead of recreating them.
- Start commit: a compare-and-swap requiring
fence == c && phase == Preparing(c)sets Running andruntime_epoch = c. - Child resources: the supervisor Pod, Secrets and shared-instance records carry an immutable label with the epoch that created them. A delete reads the object, checks its label, then deletes with
Preconditions{uid, resourceVersion}. - Garbage collection: using a single read of the CR, delete child
conly ifc < fence_obs && c != runtime_obs. This is safe from an out-of-date read because the fence only increases. Unlabelled children count as epoch 0, and a running CR with noruntime_epochcounts asruntime_epoch = 0. - Teardown: a CR in Suspending or Stopping at fence F removes every child with an epoch up to F.
- Repair loop: it never raises the fence. Drift enforcement and the stale-bootstrap reap are both a compare-and-swap to Suspending at the fence it read. That races the owner's start commit (point 4) through one compare-and-swap, so exactly one wins.
- Drift checks run in every phase. Checks that need a committed generation (the control-path Service check and the supervisor generation match) wait for the commit. Only the stale-bootstrap reap waits for the grace period.
A per-sandbox Kubernetes Lease is optional and only reduces contention. Correctness doesn't depend on it.
Acceptance Criteria
- Every gateway lifecycle transition increments the sandbox's epoch in its compare-and-swap, and every driver lifecycle request carries the epoch from its own write.
- The Kubernetes driver returns "superseded" for a request below the CR's fence and leaves the CR and child resources unchanged. The gateway treats that result as a no-op.
- Supervisor Pods, Secrets and shared-instance records carry an epoch label. Every delete is pinned by UID and resourceVersion, and checks that label first.
- Garbage collection, teardown and the repair loop follow rules 6–8. Unit tests cover:
- two concurrent stops racing a restart;
- a repair pass from an out-of-date CR read;
- a repair pass from an old lease holder;
- a suspension racing a start commit;
- unlabelled children left from before the upgrade.
- Drift checks no longer pause while a start is
Preparing. The CR reread before stopped-supervisor cleanup and its read-ordering rule are removed. - The HA paragraph in
crates/openshell-driver-kubernetes/README.mdandruntime_bootstrap_grace_secondsindocs/how-it-works/gateways/configuration.mdxdescribe the new behaviour.
Alternatives Considered
- Fence stop and suspend by runtime generation (this issue's original proposal): a generation is a UUID with no order, and the CR clears it when a stop completes. A late stop that reaches a stopped CR, or a request newer than anything the CR has seen, can't be classified.
- Claim compare-and-swap on the Sandbox CR (fix(kubernetes): compare-and-swap lifecycle writes on the bootstrap claim #6109): it checks claim equality on (CR UID, operation, generation). It protects only CR writes, not the supervisor Pod, Secrets or NetworkPolicies, so each side effect needs its own guard.
- A per-sandbox Kubernetes Lease behind
LifecycleGates: a lock without a fencing token isn't a correctness mechanism, because a holder whose lease has expired can keep writing. This matches the project's existing rule (crates/openshell-server/src/compute/lease.rs) and RFC 0018's lease-plus-epoch design. - Forward all lifecycle RPCs to the lease holder: during lease handoff two replicas still act, and the repair-loop race remains.
Related Gaps
- A dead owner's start is never recovered by the gateway. In
compute/, only the owning task's settlement clearsdriver_operation_pending, and the timeout reclaim sends STOP, not DELETE. So the driver's stale-bootstrap reap stays necessary until the gateway can take over a dead owner's claim. This should be a separate gateway issue. - A reaped create disappears. Kubernetes
create_sandboxreturns while the CR is still bootstrapping, and the driver reaps a stale create atruntime_bootstrap_grace_seconds(300 s). The gateway's start deadline (image_preparation_timeout_seconds) defaults to 1800 s. So the gateway removes the record, and the reason appears only in the driver's log. - Shared-supervisor recovery is process-local.
begin_recoverytakes a local timestamp as its baseline, so shared assignments also need the epoch. RFC 0018'sSupervisorAssignmentrecord is where that fits.
Context
- fix(kubernetes): keep HA reconciliation from deleting a restarting supervisor #6103: interim driver-only fix for the test(conformance): port the on-failure restart test from e2e/rust #5781 HA restart flake.
- bug: HA lifecycle writes can overwrite a suspension started by another gateway replica #6108: a restart overwrites a suspension. This design replaces its fix.
- fix(kubernetes): compare-and-swap lifecycle writes on the bootstrap claim #6109: claim compare-and-swap. On hold; this design replaces it.
Activity
[-]feat: fence StopSandbox/SuspendSandbox by runtime generation[/-][+]feat: fence sandbox lifecycle actions with a gateway-issued epoch[/+]