Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the queue's pod mounts the host unit's store in place

This commit is contained in:
atlas 2026-11-23 02:58:52 +01:00 • committed by maia
commit 140ee7f38a
2 changed files with 267 additions and 155 deletions

View file

@ -43,10 +43,8 @@ A service with state moves in four steps. A service without state skips steps
2 and 3.
1. **Snapshot** (`swarmctl migrate`, before the rebuild). Records what the
service holds while its host unit still runs, for the check in step 4. For
NATS that's the stream list with message counts and last sequences, read
from the monitoring endpoint (`/jsz?streams=true`). For a state directory
it's a file list with sizes. The record goes to
service holds while its host unit still runs, for the check in step 4: a file
list with sizes of the state directory. The record goes to
`/var/lib/hyperhive-migrate/<svc>.json`.
2. **Cut-over rebuild** (the operator). The operator sets
`deploy.<svc>.runtime = "k8s"` and rebuilds. The rebuild stops the host
@ -87,20 +85,49 @@ endpoints the [services page](../../2-handbook/swarm/services.md) lists; the
| `swarm-claude-login` | none; credentials it writes go to the store | nothing; a login in progress ends | `/readyz`: queue connected | same |
| `swarm-stt` | the transcript cache, entries live 24 h; the pod keeps it in an `emptyDir` | nothing; the pod starts with an empty cache | `/readyz`: queue connected | same |
| `swarm-ci-metrics` | `cursor.json` in the state directory; losing it re-pushes every run | nothing; the pod starts on an empty PVC with no gate, and its first pass re-pushes every run as the same points | the CronJob's next Job completes | `runtime = "k8s"`, rebuild; the timer goes, the CronJob runs; back with `runtime = "host"` |
| NATS + `swarm-nats-auth` | JetStream's `store_dir`: the swarm's desired state, operator profiles, schedules and run records. `swarm-nats-auth` keeps nothing | the whole `store_dir`, to a PVC; the cut-over rebuild stops NATS, so the copy reads a quiescent tree | nats-server's `/healthz?js-enabled-only=true` and `swarm-nats-auth`'s `/readyz`; Ready when both pass | `runtime = "k8s"`, rebuild; the pod takes the same host port, so hives keep their URL. Rollback copies the PVC back |
| NATS + `swarm-nats-auth` | JetStream's `store_dir`: the swarm's desired state, operator profiles, schedules and run records. `swarm-nats-auth` keeps nothing | nothing; the pod mounts the container's `store_dir` in place | nats-server's `/healthz?js-enabled-only=true` and `swarm-nats-auth`'s `/readyz`; Ready when both pass | `runtime = "k8s"`, rebuild; the pod takes the same host port, so hives keep their URL. Rollback is `runtime = "host"` and a rebuild: the same files |
| `swarm-controller` | its state directory: the operator-admin set, the legacy `webhook-secret` fallback and the forge avatar markers. The rest sits in the queue or the store | the state directory, to a PVC | `/readyz` on the controller's plain health listener | `runtime = "k8s"`, rebuild; the same rebuild points the gateway's `/api/` and `/webhook/forge/` upstreams at the controller's TCP listener. Rollback copies the PVC back and returns the upstreams to the socket |
Two facts the table rests on:
- **The queue's store is state that must survive.** Losing it empties the
hive-status view, and it also loses the buckets that hold desired state.
`swarmctl migrate` treats the whole `store_dir` as data to keep.
It never leaves its directory on the host: the pod and the container use
the same files, so a switch in either direction copies nothing, and the
streams, consumers and acknowledgements are the ones the other side left.
See [the queue's store](#the-queues-store).
- **The webhook secret lives in the store.** The state directory's
`webhook-secret` is a fallback the controller reads only when the store
doesn't answer. The copy moves it anyway, so a pod that starts while the
store is down still verifies forge deliveries. Preflight checks that the
store holds the secret.
## The queue's store
The queue's pod uses the directory its container's `nats` unit already uses:
`/var/lib/nixos-containers/swarm-nats/var/lib/nats` on the host, `/var/lib/nats`
in both. The module renders a static `local` PersistentVolume on that path,
pinned to the node, with a storage class of its own and the `Retain` policy.
The claim `swarm-nats-store` binds it by name, so nothing else binds either.
- **One server at a time.** The prune unit stops a pod before the container
starts, and the Deployment replaces its pod rather than running two. Nothing
reads the directory while the other side writes.
- **Pod to host.** nats-server creates its files with mode `0600`, owned by the
uid it runs as. The pod runs as uid 65532, so the container's `nats` unit
runs `chown -R nats:nats` on the directory before the server starts, as root
and at every start.
- **Host to pod.** The pod's `fsGroup` makes the kubelet set the directory's
group to the pod's and add group read and write on every file at each pod
start, so the pod's uid can use files the host's `nats` user wrote.
- **No lock or identity file.** nats-server keeps none in the store, so
nothing there ties the directory to the side that wrote it.
- **A host that never ran the container.** The module makes the directory,
because the volume needs it before the pod starts.
- **An older claim.** A `swarm-nats-state` claim from an earlier shape isn't
the queue's any more: the pod mounts `swarm-nats-store`, so the old claim
stays unused and needs no step.
## Order of moves
`swarmctl migrate` moves the services in this order:
@ -111,11 +138,12 @@ Two facts the table rests on:
2. **`swarm-review-classifier`, `swarm-claude-login`, `swarm-stt`,
`swarm-ci-metrics`**, in one rebuild or one at a time. Nothing to copy, same
pattern as step 1: the pods start with no gate on an empty volume.
3. **NATS with `swarm-nats-auth`.** The riskiest move: the largest copy, every
hive and agent disconnects, and the data is the swarm's desired state.
It's the first copy `swarmctl migrate` makes. `swarm-nats-auth` moves in the
same pod, because it answers the auth callout on every connect and dials the
server by name, which the pod's host aliases map to its loopback.
3. **NATS with `swarm-nats-auth`.** The riskiest move: every hive and
agent disconnects, and the data is the swarm's desired state. Nothing is
copied, and the controller is the only service `swarmctl migrate` copies.
`swarm-nats-auth` moves in the same pod, because it answers the auth
callout on every connect and dials the server by name, which the pod's
host aliases map to its loopback.
4. **`swarm-controller` last.**
- It's the operator's view of the swarm. Kept on the host through step 3,
it reports from an unchanged place whether the hives reconnected after
@ -202,8 +230,7 @@ a snapshot, step 4 falls back to the stream names on disk and says so.
the host unit's readiness after that rebuild.
The rollback copies back because every write after the cut-over exists only
in the PVC. Rolling NATS back to the untouched host copy would lose every
desired-state change made since then.
in the PVC.
`swarmctl migrate finish` deletes the host copies, the `.pre-rollback`
directories and the PVC of any service back on the host. It runs only when
@ -219,15 +246,15 @@ A fresh install straight onto k8s has no host state to copy: with
The budget is the time from the cut-over rebuild stopping the host unit to the
pod passing step 4.
| service | budget | what hives, agents and the operator see |
| ------------------------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `swarm-notify-router` | 2 min | Forge events wait in the stream behind the durable consumer and arrive late |
| `swarm-review-classifier` | 2 min | A try-out request in the gap gets no answer, and its caller reports a failure |
| `swarm-claude-login` | 2 min | A login in progress fails; the operator starts it again |
| `swarm-stt` | 2 min | Transcription requests fail during the gap; the cache starts empty |
| `swarm-ci-metrics` | one 5-minute period | One pass runs late; the first pass on the pod's empty cursor pushes every run again |
| NATS + `swarm-nats-auth` | 10 min; the copy takes most of it | below |
| `swarm-controller` | 5 min | The swarm UI and API answer with gateway errors. Forge webhook deliveries in the gap fail at the forge. After the cut-over, `swarmctl` on the host needs `--swarm` |
| service | budget | what hives, agents and the operator see |
| ------------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `swarm-notify-router` | 2 min | Forge events wait in the stream behind the durable consumer and arrive late |
| `swarm-review-classifier` | 2 min | A try-out request in the gap gets no answer, and its caller reports a failure |
| `swarm-claude-login` | 2 min | A login in progress fails; the operator starts it again |
| `swarm-stt` | 2 min | Transcription requests fail during the gap; the cache starts empty |
| `swarm-ci-metrics` | one 5-minute period | One pass runs late; the first pass on the pod's empty cursor pushes every run again |
| NATS + `swarm-nats-auth` | 10 min | below |
| `swarm-controller` | 5 min | The swarm UI and API answer with gateway errors. Forge webhook deliveries in the gap fail at the forge. After the cut-over, `swarmctl` on the host needs `--swarm` |
During the NATS gap:

View file

@ -32,8 +32,8 @@ cluster follow the same pattern.
the iptables firewall (`networking.nftables.enable` off).
[The queue](#the-queue) has its own section.
- `services.hyperhive.deploy.<service>.k8s.freshState` (default `false`) for
each service whose pod keeps state worth moving in a volume:
`swarm-controller` and `nats`. It says whether the pod may start on an empty
the service whose pod keeps state worth moving in a volume:
`swarm-controller`. It says whether the pod may start on an empty
volume ([State](#state)).
With `"host"`, a host without [the cluster node](node.md) renders the same
@ -108,13 +108,13 @@ claims stay; `swarmctl migrate finish` deletes them.
## State
A pod with a state volume that holds state worth moving (`swarm-controller` and
NATS) starts only on migrated state. Its first init container, `state-gate`,
mounts the volume and exits 0 when the volume holds `.hyperhive-migrated`. A
volume with no entries besides `lost+found` gets the marker (`{"fresh": true,
"at": <time>}`) and passes where the service's `k8s.freshState` is on. Every
other volume fails the gate, including one that holds files and no marker, since
a copy may have stopped half way. The pod stays in `Init:Error`, and the
A pod with a state volume that holds state worth moving (`swarm-controller`)
starts only on migrated state. Its first init container, `state-gate`, mounts
the volume and exits 0 when the volume holds `.hyperhive-migrated`. A volume
with no entries besides `lost+found` gets the marker (`{"fresh": true, "at":
<time>}`) and passes where the service's `k8s.freshState` is on. Every other
volume fails the gate, including one that holds files and no marker, since a
copy may have stopped half way. The pod stays in `Init:Error`, and the
container's termination message and log carry one line that names the service,
the `swarmctl migrate` command and the option to set for a fresh install.
Nothing of the service runs on the empty volume. The gate doesn't decide which
@ -122,8 +122,9 @@ copy runs: the host unit stops before the pod exists. Switching a stateful
service to `"k8s"` on a live host therefore needs `swarmctl migrate`, which
copies the state and writes the marker, or the fresh flag, which is for a swarm
with no state to lose. `swarm-notify-router`, `swarm-review-classifier` and
`swarm-claude-login` have no volume and no gate, and `swarm-stt` and
`swarm-ci-metrics` have a volume that the gate doesn't check.
`swarm-claude-login` have no volume and no gate, `swarm-stt` and
`swarm-ci-metrics` have a volume that the gate doesn't check, and the queue's
volume is the host unit's own directory.
`swarm-notify-router` keeps nothing on the host. Its durable consumer on the
forge events stream and its `notify-router-state` bucket live in the queue, so a
@ -215,15 +216,14 @@ Deployment has one replica and the `Recreate` strategy, and the prune unit
holds the container back until the pod has gone.
- **Containers.** The responder is the pod's main container, `swarm-nats`,
rendered from the unit the container runs, as the other daemons are. Beside
it run `nats`, the server, `tls-renew` and `exporter`, the Prometheus
exporter. Three init containers run in order before them. `state-gate`
checks the JetStream volume. `tls-issue` issues the server's leaf.
`callout-config` creates the callout's keys if the store holds none, fetches
their public halves and writes the server's configuration. The server's
settings are the ones the container's `services.nats.settings` render, so a
change to them reaches both. Only the path of the server's key differs,
because the pod has no credential directory.
rendered from the unit the container runs, as the other daemons are. Beside it
run `nats`, the server, `tls-renew` and `exporter`, the Prometheus exporter.
Two init containers run in order before them. `tls-issue` issues the server's
leaf. `callout-config` creates the callout's keys if the store holds none,
fetches their public halves and writes the server's configuration. The
server's settings are the ones the container's `services.nats.settings`
render, so a change to them reaches both. Only the path of the server's key
differs, because the pod has no credential directory.
- **Names.** The pod's `hostAliases` map the queue's name to the pod's own
loopback, so the responder dials the server by the name its leaf carries and
verifies it, with no traffic leaving the pod. Every other client reaches the
@ -265,14 +265,15 @@ holds the container back until the pod has gone.
the leaf for the gateway's `/metrics` vhost, and with no container it has
nothing to restart. The gateway's location dials the exporter on the bridge's
address where it dialled loopback.
- **State.** The server's store directory, `/var/lib/nats`, is the
PersistentVolumeClaim `swarm-nats-state`. The claim starts empty, and a switch
copies nothing from the container's store: it holds the swarm's declared
desired state, which nothing regenerates. `state-gate` therefore stops the
pod, with a message naming `swarmctl migrate`, until the volume holds the
migration marker, unless `deploy.nats.k8s.freshState` is on. Turn it on for
a fresh install only. The claim stays after a switch back, as every claim
does, and the container's store stays too.
- **State.** The server's store directory, `/var/lib/nats`, is the container's
own on the host, `/var/lib/nixos-containers/swarm-nats/var/lib/nats`: the
PersistentVolumeClaim `swarm-nats-store` binds a static local volume on it,
so a switch moves the process and not the bytes, and the pod has no state
gate. The container's unit runs `chown -R nats:nats` on the directory before
the server starts, and the pod's `fsGroup` gives its group read and write.
[The queue's store](migrate.md#the-queues-store) has the details. The claim
and its volume stay after a switch back, as every claim does, with the
directory.
- **Readiness.** The pod is Ready when the responder's `/readyz`, on the pod's
port 8080, passes and the server's exec probe does. The probe runs
`wget` against `/healthz?js-enabled-only=true` on the server's monitor, which
@ -379,11 +380,12 @@ module renders, and its leaf's volume is a tmpfs. A client on the bridge
reaches the queue by name, and a second node outside the node reaches the
control port and not the queue's, as with the container; with `openFirewall`
it dials the pod by name and verifies the name, and the responder in the pod
admits it. A stream written before a pod delete survives it: the replacement
pod reads it from the same volume. The state gate's image refuses an empty
volume, passes a fresh one and a volume with the marker. The exporter answers
the host's own processes through the published port, and the switch back
removes the pod before the container starts.
admits it. A key written on the host before the switch is the pod's to read, a
key the pod writes is the host server's after the switch back, and a stream
written before a pod delete survives it: the replacement pod reads it from the
same directory. The exporter answers the host's own processes through the
published port, and the switch back removes the pod before the container
starts.
The VM test `k8s-controller-switch` moves the controller. It asserts one copy at
each step by the switch's own timestamps and by a sampler, that the pod is Ready