From 7a7491a85a1f51dc75e048ccf51cc34a67ce3782 Mon Sep 17 00:00:00 2001 From: atlas Date: Sun, 22 Nov 2026 17:31:52 +0100 Subject: [PATCH] docs: the queue in the cluster --- docs/4-implementation/k8s/images.md | 5 +- docs/4-implementation/k8s/manifests.md | 24 +++-- docs/4-implementation/k8s/migrate.md | 4 +- docs/4-implementation/k8s/runtime.md | 97 ++++++++++++++++++- docs/4-implementation/k8s/telemetry.md | 6 +- .../swarm/credentials-reference.md | 5 +- 6 files changed, 126 insertions(+), 15 deletions(-) diff --git a/docs/4-implementation/k8s/images.md b/docs/4-implementation/k8s/images.md index 33bddb57e..52ccaeb38 100644 --- a/docs/4-implementation/k8s/images.md +++ b/docs/4-implementation/k8s/images.md @@ -39,7 +39,10 @@ Each of these is a flake package named `image-`, and the image name is compile stays out of CI. `swarm-claude-login`, `swarm-review-classifier` and `swarm-stt` aren't in the set: [the manifest renderer](manifests.md#pods) builds their images, with the paths their units' environments name (claude-code and -opencode, ffmpeg, whisper-cli and the model). +opencode, ffmpeg, whisper-cli and the model). It builds three more for the +queue's pod ([runtime](runtime.md#the-queue)): the NATS server, which also +holds `wget`, the one tool of busybox its readiness probe needs; the +Prometheus exporter; and an image of the queue's init and renewal scripts. ## Tags diff --git a/docs/4-implementation/k8s/manifests.md b/docs/4-implementation/k8s/manifests.md index 47f11449c..aa9d46eb5 100644 --- a/docs/4-implementation/k8s/manifests.md +++ b/docs/4-implementation/k8s/manifests.md @@ -123,6 +123,15 @@ credential directory. renewal container that issue its leaf into an `emptyDir` ([runtime](runtime.md#the-controller)). The image of those containers is part of `system.build.k8sImages`. +- **More than the daemon:** a service whose pod holds more than its daemon + (the queue, whose server and exporter run beside the responder) names a + function in its entry that adds containers, init containers, volumes, host + aliases, pod settings, objects and images. A service named differently from + its daemon (`swarm-nats`, whose daemon is `swarm-nats-auth`) names the + daemon's binary, and the credential directory its unit loads from follows + the daemon's name. The pod runs under one ServiceAccount, so a queue that + holds two store roles has both bound to it + ([runtime](runtime.md#the-queue)). - **Config checksum:** a Deployment whose service has a ConfigMap (`etcFiles`) carries `checksum/etc` on its pod template, the hash of that ConfigMap's content, so a changed file rolls the pod. @@ -149,19 +158,20 @@ environment, so the pod differs from the unit in these places: account and this audience only. - **Environment:** the pod sets neither `BAO_CLIENT_CERT` nor `BAO_CLIENT_KEY`, and the credentials they named aren't in the pod's - Secret. `BAO_AUTH=jwt`, `BAO_JWT_FILE`, `BAO_JWT_ROLE` (the service's name) - and `BAO_JWT_MOUNT` (`jwt-k8s`) take their place, and `BAO_ADDR` names the - store's own name at the port of the listener that asks for no client - certificate (`deploy.bao.podListenerPort`). `BAO_CACERT` stays. + Secret. `BAO_AUTH=jwt`, `BAO_JWT_FILE`, `BAO_JWT_ROLE` (the service's name, + or the role its entry names) and `BAO_JWT_MOUNT` (`jwt-k8s`) take their + place, and `BAO_ADDR` names the store's own name at the port of the listener + that asks for no client certificate (`deploy.bao.podListenerPort`). + `BAO_CACERT` stays. [The cluster node](node.md#logging-in-to-the-secret-store) describes the store's side. ## Not covered yet -- The pods reach the swarm's host-run services (store, queue, identity - provider) by the same names as the units do. The node's CoreDNS answers those - names with the hive bridge's address. +- The pods reach the swarm's host-run services (store, identity provider, and + the queue where it runs on the host) by the same names as the units do. The + node's CoreDNS answers those names with the hive bridge's address. - `swarm-controller`'s socket lives in an `emptyDir`, so only the pod can reach it. The gateway dials the TCP listener instead. diff --git a/docs/4-implementation/k8s/migrate.md b/docs/4-implementation/k8s/migrate.md index 58d108f60..68d090da6 100644 --- a/docs/4-implementation/k8s/migrate.md +++ b/docs/4-implementation/k8s/migrate.md @@ -110,8 +110,8 @@ Two facts the table rests on: 4. **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. The copy path has run once by then. `swarm-nats-auth` moves in the same - pod, because it answers the auth callout on every connect and dials NATS - on loopback. + 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. 5. **`swarm-controller` last.** - It's the operator's view of the swarm. Kept on the host through step 4, it reports from an unchanged place whether the hives reconnected after diff --git a/docs/4-implementation/k8s/runtime.md b/docs/4-implementation/k8s/runtime.md index 22e94cd88..fbd16aa45 100644 --- a/docs/4-implementation/k8s/runtime.md +++ b/docs/4-implementation/k8s/runtime.md @@ -7,8 +7,9 @@ option moves it and never copies it: the host unit and the pod never run together, because they would share one queue consumer and one set of buckets, or two cursors that push every CI run twice. `swarm-notify-router`, `swarm-stt`, `swarm-ci-metrics`, `swarm-review-classifier`, -`swarm-claude-login` and `swarm-controller` have the option today, and the -other services that run in the cluster follow the same pattern. +`swarm-claude-login`, `swarm-controller` and the queue with its auth callout +(`swarm-nats`) have the option today, and the other services that run in the +cluster follow the same pattern. ## Options @@ -26,6 +27,11 @@ other services that run in the cluster follow the same pattern. `deploy.swarm-controller.tcp.listenAddress` at its default `0.0.0.0`: the evaluation fails without either. [The controller](#the-controller) has its own section. +- `services.hyperhive.deploy.nats.runtime` (default `"host"`), with the same + rule. One switch covers the queue's server and its auth callout. It needs + the iptables firewall (`networking.nftables.enable` off), and + `deploy.nats.k8s.freshStore` says whether a fresh store may start the + server. [The queue](#the-queue) has its own section. With `"host"`, a host without [the cluster node](node.md) renders the same configuration as before the option existed. With the node on and every service @@ -179,6 +185,80 @@ controller may move there. re-reads the file with no restart, so this restart, which drops the job graph, is new. +## The queue + +`swarm-nats` is the swarm's message queue: the NATS server and the +auth-callout responder that admits its clients. Both belong to one pod, +because a server whose responder is down refuses every connect. A switch +moves the two together, and two servers or two responders never run: the +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. `store-guard` + 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. +- **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 + queue by the same name as before: the node's CoreDNS and the bridge's + resolver answer it with the bridge's address, and the published port forwards + it to the pod. +- **The client port.** The server's port is a `hostPort` on `0.0.0.0`, the + addresses the container's server listens on, so hives on other hosts keep + their URL. A `hostPort` is a DNAT rule, and the firewall's INPUT rules, the + ones `deploy.nats.openFirewall` and the bridge's port list write, never see + what it rewrites. The module therefore adds rules to the raw table, which is + read before the DNAT: a packet for the node's address and the client port is + dropped unless it arrives on the host's loopback, the hive bridge or a pod + interface, or unless `deploy.nats.openFirewall` is on. The container's port + has that reach. The exporter's port is a `hostPort` on the bridge's address, + for the gateway's `/metrics` location on the host. The rules drop it on every + interface but the host's loopback: the exporter answers with the name of + every client, and only the host's own processes may read it. +- **The leaf.** `tls-issue` logs in to the store with the pod's token as the + role `swarm-nats` and issues the server's leaf from the same script as the + host's `swarm-bao-nats-tls`. The key lands in a volume in memory. The + callout's keys and the responder's own reads go through the role + `swarm-nats-auth`. A pod has one ServiceAccount, `swarm-nats`, so both roles + bind it. A fourth container, `tls-renew`, runs the same script every five + minutes beside the server. It leaves a leaf in the first half of its 30 days + alone, and issues a new one after that. Then it sends `SIGHUP` to the server, + which reloads its TLS files for new connections, so the pod shares its + processes among its containers. A connection already open keeps the old leaf. +- **The host's units.** `swarm-nats-auth-secrets` and + `swarm-bao-nats-callout-xkey` deliver into the container, so the configuration + turns them off. `swarm-bao-nats-tls` stays: it issues the host's own copy of + 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. `store-guard` therefore stops the + pod, with a message naming `swarmctl migrate`, when the volume holds no + JetStream data, unless `deploy.nats.k8s.freshStore` 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. +- **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 + stays on the pod's loopback; the server's image holds `wget` and nothing else + of busybox. +- **Stopping.** The container's unit drains the server's clients, giving them + up to 150 seconds to move. The pod's server gets `SIGTERM` and the pod's + 15-second grace period, so every client's connection drops at once and the + clients reconnect when the next pod is up. +- **Limits.** None, as on the host. The pod has no memory or CPU limit, and its + descriptors are the container runtime's, not the 800000 the container's unit + sets. The VM test checks the runtime's is at least that. + ## Resources and sandbox The pod takes the unit's `MemoryMax` and `CPUQuota` as its container's limits @@ -264,6 +344,19 @@ that `claude` with `HOME` in the volume writes its config there, as the minter's real login step does, while `claude` with `HOME` on the root leaves nothing there. +The VM test `k8s-nats-switch` moves the queue against the real secret store. A +sampler counts servers and responders every 200 ms and sees at most one of each, +and the container and the pod never together. The pod is Ready by its exec probe +and has the init containers, the `hostPort`s and the volumes the 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 store guard's image refuses an empty volume, passes a fresh one and +a volume with data. 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 on `/readyz` with the `hostPort` and the volumes the module renders and no diff --git a/docs/4-implementation/k8s/telemetry.md b/docs/4-implementation/k8s/telemetry.md index 9102990ad..870fa2d3e 100644 --- a/docs/4-implementation/k8s/telemetry.md +++ b/docs/4-implementation/k8s/telemetry.md @@ -29,7 +29,11 @@ route checks itself on each pass. The renderer sets nothing for such a service (`otlp = "own"` in its entry of `render.nix`), because a series that went through the hive collector would carry the `hive` label, and a swarm-level series must not. The render refuses a service marked `"own"` whose unit names -no endpoint. +no endpoint. A daemon that pushes no telemetry of its own, the queue's +responder, has `"none"` in its entry and gets no endpoint. The queue's logs are +the pod's, read like any pod's ([logs](#logs)): the container's journal +forwarder has no counterpart in the pod. Its metrics are the exporter's, which +the swarm collector scrapes through the gateway, as it does for the container. ## Logs diff --git a/docs/4-implementation/swarm/credentials-reference.md b/docs/4-implementation/swarm/credentials-reference.md index c651b51cb..4a3a0b16e 100644 --- a/docs/4-implementation/swarm/credentials-reference.md +++ b/docs/4-implementation/swarm/credentials-reference.md @@ -151,9 +151,10 @@ pieces fit, and the reasoning behind them, is in [`credentials.md`](../../2-hand | the CI run poller's copy of its OIDC secret | authelia's mint, which creates it in the swarm secret store | `swarm-ci-metrics` reads `swarm/services//oidc/client` once per pass under its own leaf, and again when authelia refuses it; memory only. While it differs from authelia's, the token request fails, nothing gets pushed and no cursor moves | same leaf, nothing else | | authelia's container's journal forwarder's OIDC secret | authelia's mint, in its container, like every client's secret (`swarm.authelia.otel.clientId`). It creates it at `swarm/services//oidc/client`, and `swarm-journal-forwarder-oidc.service` in the same container reads it back under the secret publisher's leaf, at start and every 10 minutes | `/run/swarm-journal-forwarder-oidc/client-secret` on the container's tmpfs, root `0400`; `LoadCredential` hands it to the collector. | none: the forwarder runs only in authelia's container, and is off there without the publisher's leaf | | authelia subject store | `swarmctl` and `swarm-authelia-bridge` | `users.yml` — one file, read and written by both | `swarmctl`, on the host that runs authelia | -| queue auth-callout nkeys (user seed + account seed) | `swarm-nats-auth ensure-callout-keys`, run by `swarm-bao-nats-callout-xkey` on the queue's host when the store holds no seed; the verb stores a seed it finds at `/var/lib/swarm-nats-callout/{callout-user,issuer}.seed` as is, then removes the file | the swarm secret store: the seeds at `swarm/services/swarm-nats/callout/{user,issuer}-seed`, read by the `swarm-nats-auth` role alone; the public halves at `swarm/public/swarm-nats/callout-{user,issuer}` | nothing to place beyond the responder's store leaf (`deploy.nats.authBaoClientCertFile`) | -| queue auth-callout xkey (curve seed) | `swarm-nats-auth ensure-callout-keys`, run by `swarm-bao-nats-callout-xkey` on the queue's host when the store holds no seed | the swarm secret store: the seed at `swarm/services/swarm-nats/callout/xkey-seed`, read by the `swarm-nats-auth` role alone; the public half at `swarm/public/swarm-nats/callout-xkey` | nothing to place beyond the responder's store leaf (`deploy.nats.authBaoClientCertFile`) | +| queue auth-callout nkeys (user seed + account seed) | `swarm-nats-auth ensure-callout-keys`, run by `swarm-bao-nats-callout-xkey` on the queue's host when the store holds no seed (with `deploy.nats.runtime = "k8s"`, by the pod's `callout-config` init container under the `swarm-nats-auth` jwt role); the verb stores a seed it finds at `/var/lib/swarm-nats-callout/{callout-user,issuer}.seed` as is, then removes the file | the swarm secret store: the seeds at `swarm/services/swarm-nats/callout/{user,issuer}-seed`, read by the `swarm-nats-auth` role alone; the public halves at `swarm/public/swarm-nats/callout-{user,issuer}` | nothing to place beyond the responder's store leaf (`deploy.nats.authBaoClientCertFile`) | +| queue auth-callout xkey (curve seed) | `swarm-nats-auth ensure-callout-keys`, run by `swarm-bao-nats-callout-xkey` on the queue's host when the store holds no seed (with `deploy.nats.runtime = "k8s"`, by the pod's `callout-config` init container under the `swarm-nats-auth` jwt role) | the swarm secret store: the seed at `swarm/services/swarm-nats/callout/xkey-seed`, read by the `swarm-nats-auth` role alone; the public half at `swarm/public/swarm-nats/callout-xkey` | nothing to place beyond the responder's store leaf (`deploy.nats.authBaoClientCertFile`) | | the queue's TLS key, copied for the gateway's `/metrics` vhost | `swarm-nats-gateway-cert.service` on the queue's host, where the gateway uses self-signed TLS: a copy of the queue's bao-PKI TLS leaf (`/var/lib/swarm-nats-tls/`) once `swarm-bao-nats-tls` has issued it, else a throwaway 30-day self-signed pair for the queue's name until then | `/var/lib/swarm-nats-gateway-tls/{cert,key}.pem`; the key is `0640` group `nginx`, read by nginx for the queue's `/metrics` vhost. The queue's own key stays `0600` root | the copy is redone on each leaf renewal (`swarm-bao-nats-tls` starts the unit, which queues a non-blocking nginx reload when nginx is running); a restart before any leaf exists keeps the pair already there. Until `swarm-bao-nats-tls` issues the leaf the vhost serves the throwaway cert, so the collector's scrape fails TLS. Nothing for an operator to place | +| the queue pod's TLS leaf and key | the pod's `tls-issue` init container, only with `deploy.nats.runtime = "k8s"`: it logs in with the pod's ServiceAccount token as the store's `swarm-nats` jwt role, which has the policy of the `swarm-nats` cert role, and issues one name, `swarm.nats.domain`. The host keeps `swarm-bao-nats-tls`, which issues its own copy for the gateway's `/metrics` vhost and restarts no server | `/var/lib/swarm-nats-tls/{cert,key}.pem` in an in-memory `emptyDir` (`medium: Memory`), gone with the pod. The server reads both as it starts and again on `SIGHUP`; the key never reaches the node's disk | the pod's `tls-renew` container repeats the check every five minutes, issues a new leaf once the old one is past half its 30 days and signals the server, so a long-running pod needs nothing. Nothing to place: the pod's account is the credential | | the controller's TCP listener certificate and key | `swarm-controller-tls.service` on the controller's host, only where you set `swarm.controller.tcp.enable`. It logs in under the controller's own store leaf and issues from the store's `swarm-controller-tls` role: one name, `swarm.controller.tcp.domain`, `serverAuth` only. On the host, a daily timer renews it past half its life. With `deploy.swarm-controller.runtime = "k8s"` the host has no unit: the pod's init container issues the leaf into an in-memory `emptyDir` under its ServiceAccount token, and its `tls-renew` container repeats the same check daily, because nothing rolls the pod before the leaf expires | On the host, `/{cert,key}.pem` (`/var/lib/swarm-controller-tls`), the key `0640` group `swarm-controller`. In the pod, the same two files in an in-memory `emptyDir` (`medium: Memory`), gone with the pod. The daemon re-reads both when they change, so a rotation restarts nothing. A store host that leaves the option off has no role to issue from, and the unit fails with the store's refusal naming it | Nothing to place: the controller's own store leaf is the one it already holds | | the gateway's client certificate for the controller's TCP listener | `swarm-controller-gateway-client.service` on the host that runs the controller, only where you set `swarm.controller.tcp.enable`. It logs in under the services-issuer identity and issues from the store's `swarm-controller-gateway-client` role: one name, `gateway-client.`, `clientAuth` only, so the services leaf can't stand in for it. A daily timer renews it | `/controller-client.pem` and `controller-client-key.pem`, the key `0640` group `nginx`; the gateway's controller locations name them in `proxy_ssl_certificate`. Until the store answers, a throwaway self-signed pair keeps nginx's config loadable. The listener refuses that pair, so those locations answer 502. Every holder of the services-issuer identity can obtain this leaf: the listener trusts "a gateway of this swarm" | Nothing to place: the services-issuer identity is the one every gateway host already holds | | the secret store's own contents | openbao, on first `bao operator init` — **an operator action, not a unit** | inside the `swarm-bao` container, at its own `/var/lib/openbao`, kept across rebuilds by `ephemeral = false`. ⚠️ **Not a host path**: `nixos-container destroy swarm-bao` takes the raft data with it, so back up the container's tree, not `/var/lib/`. Only the store's TLS material (`/var/lib/swarm-bao-tls`) and its PKCS11 token (`/var/lib/swarm-bao-token`) are host-level | n/a — there is one store; a hive elsewhere is a _client_ of it and holds none of this |