diff --git a/docs/2-handbook/swarm/swarmctl-cli.md b/docs/2-handbook/swarm/swarmctl-cli.md index dc9b2d461..374bacbe8 100644 --- a/docs/2-handbook/swarm/swarmctl-cli.md +++ b/docs/2-handbook/swarm/swarmctl-cli.md @@ -756,13 +756,13 @@ Move the swarm's services from this host onto its k3s cluster. ###### **Subcommands:** -* `status` — Report where each service stands: its runtime, its host units, its pod and the start gate's answer, and which step comes next +* `status` — Report where each service stands: its runtime, its host units and its pod, and which step comes next ## `swarmctl migrate status` -Report where each service stands: its runtime, its host units, its pod and the start gate's answer, and which step comes next. +Report where each service stands: its runtime, its host units and its pod, and which step comes next. Read-only: it creates no pod and changes nothing. Run it as root; it reads `/etc/hyperhive/migrate.json`, asks systemd about the host units and lists the pods in the swarm's namespace with the k3s admin kubeconfig. diff --git a/docs/4-implementation/k8s/migrate.md b/docs/4-implementation/k8s/migrate.md index cdccb6feb..53ba44195 100644 --- a/docs/4-implementation/k8s/migrate.md +++ b/docs/4-implementation/k8s/migrate.md @@ -1,15 +1,15 @@ # Moving an all-local swarm onto k8s: `swarmctl migrate` -`swarmctl migrate` moves a running all-local swarm -(`services.hyperhive.deploy.singleHostSwarm`) onto the k3s cluster on the -same host. It copies each service's state into a persistent volume, checks the -copy, and leaves the host copy in place until the operator finishes. It moves -services in dependency order, resumes after a crash, and rolls a service back -to the host on request. +A running all-local swarm (`services.hyperhive.deploy.singleHostSwarm`) moves +onto the k3s cluster on the same host one service at a time. The operator sets +`deploy..runtime = "k8s"` and rebuilds; `swarmctl migrate status` reports +where each service stands. Nothing copies a service's state: each one's pod +re-derives what it keeps, keeps it in the service's own store, or uses the host +unit's directory in place. ## Scope and guarantees -The command moves the swarm's own daemons and the queue into the cluster: +The swarm's own daemons and the queue move into the cluster: `swarm-notify-router`, `swarm-review-classifier`, `swarm-claude-login`, `swarm-stt`, `swarm-ci-metrics`, NATS with `swarm-nats-auth`, and `swarm-controller`. bao, the forge, authelia, the gateway, `swarm-builder`, @@ -18,59 +18,39 @@ The command moves the swarm's own daemons and the queue into the cluster: - **The hive contract stays the same.** Hives and agents keep the NATS URL, its port, its certificate name and its auth callout. No hive needs a rebuild for any move. -- **The host copy of the state stays on disk** until the operator runs - `swarmctl migrate finish`. A forward move only reads it, so a rollback - always has it to return to. - **One nix option decides where a service runs.** `deploy..runtime` is `"host"` (the default) or `"k8s"`. The cut-over is an operator rebuild, never a runtime toggle that `swarmctl` flips on its own. +- **A move leaves the host's data alone.** The host unit's directories stay + where they're, so going back is `runtime = "host"` and a rebuild. ### Names -| name | what it names | -| --------------------------------------------------- | --------------------------------------------------------------- | -| `services.hyperhive.deploy.k8s.enable` | brings up single-node k3s on the swarm host | -| `services.hyperhive.deploy..runtime` | `"host"` or `"k8s"`, per service | -| `/etc/hyperhive/migrate.json` | the module's render of each service's runtime, state paths, PVC | -| `/var/lib/hyperhive-migrate/` | host record: pre-move snapshots and copy digests, root `0700` | -| `SWARMCTL_KUBECTL`, `SWARMCTL_KUBECONFIG` | the `swarmctl` wrapper's `kubectl` and the k3s admin kubeconfig | -| `.hyperhive-migrated` in a PVC | the copy marker the pod's start gate checks for | -| `swarmctl migrate` `status` / `rollback` / `finish` | the verbs | +| name | what it names | +| ----------------------------------------- | --------------------------------------------------------------- | +| `services.hyperhive.deploy.k8s.enable` | brings up single-node k3s on the swarm host | +| `services.hyperhive.deploy..runtime` | `"host"` or `"k8s"`, per service | +| `/etc/hyperhive/migrate.json` | the module's render of each service's runtime and host units | +| `SWARMCTL_KUBECTL`, `SWARMCTL_KUBECONFIG` | the `swarmctl` wrapper's `kubectl` and the k3s admin kubeconfig | +| `swarmctl migrate status` | the one verb: a read-only report | -## The move of one stateful service +## `swarmctl migrate status` -A service with state moves in four steps. A service without state skips steps -2 and 3. +It reads `/etc/hyperhive/migrate.json`, asks systemd whether each service's +host units run, and lists the pods of the swarm's namespace with one +`kubectl get`. It creates and changes nothing. For each service it reports: -1. **Snapshot** (`swarmctl migrate`, before the rebuild). Records what the - 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 - unit and renders it no more, and applies the workload and its PVC. The - pod's first init container is the **start gate**. It exits 0 only when the - PVC holds `.hyperhive-migrated`, or when the PVC is empty and the - service's `deploy..k8s.freshState` is on. Any other volume fails the - gate: the pod stays in `Init:Error` with one line saying to run - `swarmctl migrate ` or set the option, and the kubelet runs the gate - again with a growing delay. A pod therefore never starts on an empty - volume, whatever re-applies the manifest. -3. **Copy** (`swarmctl migrate`). Refuses while the host unit runs. Streams - the host state directory as a tar archive into a copy pod that mounts the - PVC, into `.incoming/`. Checks the digest of the unpacked tree against the - source, moves `.incoming/` into place, sets ownership to the image's user, - and writes `.hyperhive-migrated` last, with the source path and digest. - The gate opens. -4. **Verify** (`swarmctl migrate`). Waits for the pod's readiness, then - compares what the service serves against the snapshot from step 1. +- **runtime**: whether the host units have stopped when the configuration says + `k8s`, and whether the cluster still holds a pod of a service the + configuration puts on the host; +- **order**: whether the service moved before one that comes earlier in the + [order of moves](#order-of-moves); +- **ready**: whether the newest pod's readiness probe passes (a `CronJob`'s pod + finishes when its Job completed). -The copy pod runs a small `swarmctl` image with the hidden subcommand -`migrate receive`. The start gate is a script in an image of its own that holds -coreutils and nothing else. The service images carry no shell and no `tar`, and -this keeps them that way. The copy streams through the -k8s API instead of writing into the volume's directory on the host, so it works -for every storage class. +It names the next step that isn't done, exits 0 when every check ran, and +exits 1 when a check failed to run. `--json` prints the services as the module +renders them, every step with its status, and the next step. ## Services @@ -78,15 +58,15 @@ for every storage class. endpoints the [services page](../../2-handbook/swarm/services.md) lists; the [readiness page](../swarm/readiness.md) has their mechanics. -| service | state | what moves | readiness signal | cut-over and rollback | -| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `swarm-notify-router` | in the queue: a state bucket and a durable consumer on the forge events stream | nothing | `/readyz`: queue connected | `runtime = "k8s"`, rebuild; back with `runtime = "host"` | -| `swarm-review-classifier` | none on disk; its runs and settings live in buckets the controller owns | nothing | `/readyz`: queue connected | same | -| `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 | 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 | +| service | state | what the pod starts with | readiness signal | cut-over and rollback | +| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `swarm-notify-router` | in the queue: a state bucket and a durable consumer on the forge events stream | the queue's | `/readyz`: queue connected | `runtime = "k8s"`, rebuild; back with `runtime = "host"` | +| `swarm-review-classifier` | none on disk; its runs and settings live in buckets the controller owns | the queue's | `/readyz`: queue connected | same | +| `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` | an empty cache | `/readyz`: queue connected | same | +| `swarm-ci-metrics` | `cursor.json` in the state directory; losing it re-pushes every run | an empty claim: the 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 host unit's own `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 holds the legacy `webhook-secret` (the store has it too), the forge avatar markers and, once, a legacy operator-admin file; the grant record is in the queue's key-value store | an `emptyDir` | `/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. Back with `runtime = "host"` and a rebuild | Two facts the table rests on: @@ -96,11 +76,12 @@ Two facts the table rests on: 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 controller keeps no state of its own that a move could lose.** The + record of the Matrix admin grants is in the queue's key-value store, the + webhook secret is in the secret store, and the controller sets the avatar + markers again at start. The first start of the controller on the host after + an upgrade imports a legacy grant file into the store, so run the host + controller once before the switch. ## The queue's store @@ -130,17 +111,17 @@ The claim `swarm-nats-store` binds it by name, so nothing else binds either. ## Order of moves -`swarmctl migrate` moves the services in this order: +`swarmctl migrate status` reports the services in this order, and flags a +service that moved ahead of an earlier one: 1. **`swarm-notify-router` alone.** The first cut-over proves the runtime switch, the image, the store identity and reaching the host-run NATS by - name, with nothing to copy. + name. 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: 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-ci-metrics`**, in one rebuild or one at a time, the same pattern as + step 1. +3. **NATS with `swarm-nats-auth`.** The riskiest move: every hive and agent + disconnects, and the data is the swarm's desired state. Nothing copies it. `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. @@ -150,101 +131,14 @@ The claim `swarm-nats-store` binds it by name, so nothing else binds either. the NATS move, and `swarmctl` keeps its local socket for the checks. - Its cut-over also changes the gateway's upstream and ends the local socket for `swarmctl`, the widest change to the operator's tools. - - Its state is small, so its rollback is cheap. -Each step needs the previous one Ready and verified. `swarmctl migrate` says -which service comes next, and refuses to copy a service out of order unless -the operator passes `--out-of-order`. - -## Preflight - -`swarmctl migrate` runs these before its first snapshot, and again before each -copy. A failed check names what's missing and stops; it changes nothing. - -| check | how | fails when | -| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------- | -| k3s node Ready | `kubectl get nodes` with the k3s admin kubeconfig | the node is NotReady, or `deploy.k8s.enable` is off | -| images present | every image the next move needs shows in the node's image list | an image is missing | -| store login from a pod | a short-lived probe pod with the next service's identity logs in to bao and reads one path that service may read | the login or the read fails | -| host services reachable | the probe pod resolves the bao, forge, authelia and collector names to the node and completes TLS to each; while NATS runs on the host, NATS too | a name doesn't resolve, or a handshake fails | -| disk space | free space on the filesystem behind the PVCs is at least twice the sum of the state still to copy, plus 1 GiB | not enough room for the copy and a later rollback copy | -| host unit state | the service's host unit is active before the snapshot, and inactive before the copy | the order of steps doesn't hold | -| webhook secret in the store (controller only) | the controller's store path for it holds a value | the store has none; the operator restarts the host controller first, which seeds it | -| controller listener (controller only) | the controller's TCP listener answers a client holding the gateway leaf, and refuses one without | the listener is off or refuses the gateway | - -## Resume and rollback - -`swarmctl migrate` keeps no progress file of its own. Like `swarmctl setup`, it -observes the system, judges each step as done, not done or unknown from what it -sees, and plans again after every step. A rerun resumes where the last run -stopped, and the loop offers retry, skip or quit on a failed step. - -### What's observed and what's recorded - -| fact | observed from | -| ---------------------------------- | ---------------------------------------------------------- | -| where the config wants the service | `/etc/hyperhive/migrate.json`, which the rebuild renders | -| host unit active or not | `systemctl is-active` | -| workload, pod phase, Ready | `kubectl` | -| copy done | the pod's `state-gate` init container, in the pod's status | - -`swarmctl migrate status` reads the gate's answer from the pod's status: the -gate exited 0 when the marker is in the volume, and a refusal puts its reason -in the container's termination message. Status never starts a pod, so it -creates nothing and reads only. - -Two facts vanish once the host unit stops, so `/var/lib/hyperhive-migrate/` -records them: the step 1 snapshot, and the digest of the copied tree. Without -a snapshot, step 4 falls back to the stream names on disk and says so. - -### Idempotence - -- The snapshot overwrites its record while the host unit runs, and refuses to - once it stops. -- The copy starts by emptying `.incoming/`, and runs only while the marker is - absent. Rerunning it after an interrupted copy starts over. -- A rerun skips a copy whose marker exists. -- Verification only reads. -- `rollback` and `finish` check their own preconditions and skip what's done. - -### Half-done migrations - -| what the operator sees | resume | roll back | -| ------------------------------------------------- | --------------------------------------------------------- | ------------------------------------------------------------------- | -| rebuilt, the pod waits in its gate, no marker | `swarmctl migrate` copies | `runtime = "host"`, rebuild; the host state never changed | -| `.incoming/` in the PVC, no marker (copy cut off) | `swarmctl migrate` empties it and copies again | same as above | -| marker written, pod not Ready | read the pod's log; fix, then `swarmctl migrate` verifies | `swarmctl migrate rollback `, then `runtime = "host"`, rebuild | -| Ready, verification failed | the mismatch says what differs; the operator decides | same as above | -| verified | nothing to do | same as above | - -`swarmctl migrate rollback ` works in this order: - -1. Removes the marker and deletes the pod. The replacement waits in the gate, - so nothing writes to the PVC from here on, whatever re-applies the - manifest. -2. Copies the PVC into `.from-k8s` and checks its digest. -3. Renames the host directory to `.pre-rollback` and - `.from-k8s` to the host directory, owned by the host unit's - user. Both renames stay on one filesystem. -4. Tells the operator to set `runtime = "host"` and rebuild, then verifies - the host unit's readiness after that rebuild. - -The rollback copies back because every write after the cut-over exists only -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 -every service passed verification where the config wants it. A rebuild that -stops a workload leaves its PVC in place, so the data survives until `finish`. - -A fresh install straight onto k8s has no host state to copy: with -`deploy..k8s.freshState` on, the gate marks the empty volume itself -(`{"fresh": true, "at":