From 140ee7f38a6bf0db6cbd058a050c907ec7516bda Mon Sep 17 00:00:00 2001 From: atlas Date: Mon, 23 Nov 2026 02:58:52 +0100 Subject: [PATCH] k8s: the queue's pod mounts the host unit's store in place --- docs/4-implementation/k8s/migrate.md | 71 ++++++++++++------ docs/4-implementation/k8s/runtime.md | 68 ++++++++--------- nix/checks.nix | 1 + nix/host-modules/deploy.nix | 4 + nix/host-modules/k8s/migrate.nix | 15 ++-- nix/host-modules/k8s/render.nix | 24 ++---- nix/host-modules/swarm-nats.nix | 99 ++++++++++++++++--------- nix/module-eval/k8s.nix | 54 ++++++++++---- nix/module-eval/nats-authelia.nix | 14 +++- nix/script-tests/nats-render-config.nix | 5 +- nix/vm-tests/k8s-nats-switch.nix | 67 +++++++++++------ 11 files changed, 267 insertions(+), 155 deletions(-) diff --git a/docs/4-implementation/k8s/migrate.md b/docs/4-implementation/k8s/migrate.md index e570efe67..cdccb6feb 100644 --- a/docs/4-implementation/k8s/migrate.md +++ b/docs/4-implementation/k8s/migrate.md @@ -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/.json`. 2. **Cut-over rebuild** (the operator). The operator sets `deploy..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: diff --git a/docs/4-implementation/k8s/runtime.md b/docs/4-implementation/k8s/runtime.md index f456d4bdb..8501c9fda 100644 --- a/docs/4-implementation/k8s/runtime.md +++ b/docs/4-implementation/k8s/runtime.md @@ -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..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":