From 07a004fad11b21fdfd70541d152b176e87351c95 Mon Sep 17 00:00:00 2001 From: atlas Date: Fri, 27 Nov 2026 09:11:27 +0100 Subject: [PATCH] run the metrics store and its auth responders as pods --- AGENTS.md | 2 +- docs/2-handbook/agents/security.md | 25 +- docs/2-handbook/swarm/services.md | 7 +- docs/4-implementation/k8s/manifests.md | 18 +- docs/4-implementation/k8s/node.md | 5 +- docs/4-implementation/k8s/runtime.md | 88 +- docs/4-implementation/k8s/telemetry.md | 4 +- .../swarm/credentials-reference.md | 105 ++- docs/4-implementation/swarm/telemetry.md | 86 +- docs/5-project/process/conventions.md | 2 +- docs/5-project/process/gotchas.md | 3 +- nix/host-modules/default.nix | 1 - nix/host-modules/deploy.nix | 5 +- .../glue-attic-otel-oidc-client.nix | 2 +- .../glue-victoriametrics-otel-oidc-client.nix | 37 - nix/host-modules/k8s/node.nix | 10 +- nix/host-modules/k8s/render.nix | 57 +- nix/host-modules/swarm-bao-readers.nix | 17 +- nix/host-modules/swarm-bao.nix | 5 +- nix/host-modules/swarm-otel-service.nix | 5 - nix/host-modules/swarm-otel.nix | 3 +- nix/host-modules/swarm-secret-publisher.nix | 2 - .../swarm-victoriametrics-service.nix | 30 +- nix/host-modules/swarm-victoriametrics.nix | 858 +++++++----------- nix/module-eval/bao-grants.nix | 13 +- nix/module-eval/core-toggle.nix | 108 ++- nix/module-eval/k8s.nix | 295 +++++- nix/module-eval/nats-authelia.nix | 70 -- nix/module-eval/secret-publisher.nix | 1 - nix/module-eval/swarm-otel-identity.nix | 2 +- nix/module-eval/swarm-services-switch.nix | 93 +- nix/vm-tests/k8s-services.nix | 368 +++++++- swarm-metrics/README.md | 6 +- 33 files changed, 1305 insertions(+), 1028 deletions(-) delete mode 100644 nix/host-modules/glue-victoriametrics-otel-oidc-client.nix diff --git a/AGENTS.md b/AGENTS.md index 9cc3e8ef9..42e662c93 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -154,7 +154,7 @@ Swarm-level: token against the store; shared by `swarm-nats-auth` and `swarm-logs-auth`. - **`swarm-logs-auth/`** — nginx `auth_request` responder for agent access - to the log store, and to the metrics store as `swarm-metrics-agent-auth`. + to the log store, and to the metrics store (`--surface metrics`). - **`swarm-metrics-auth/`** — nginx `auth_request` responder for Grafana's bearer on the metrics store's query API. - **`swarm-logs/`** — agent CLI: a LogsQL query in, log lines out. diff --git a/docs/2-handbook/agents/security.md b/docs/2-handbook/agents/security.md index 7e34649a7..b64258452 100644 --- a/docs/2-handbook/agents/security.md +++ b/docs/2-handbook/agents/security.md @@ -86,24 +86,23 @@ and the store's granter fetches the bao UI's. Three services read **every agent's queue secret** (`swarm/agents//queue`), each under a leaf of its own whose policy reaches that one leaf per agent: the queue's auth callout (`swarm-nats-auth`), -the log store's agent check (`swarm-logs-auth`, a pod on the log store's host), and -the metrics store's (`swarm-metrics-agent-auth`, under the `swarm-metrics-auth` -leaf on the metrics store's host). Any one compromised can present any agent's +the log store's agent check (`swarm-logs-auth`, on the log store's host), and +the metrics store's (`swarm-metrics-auth`, on the metrics store's host). Any one compromised can present any agent's queue secret, at the queue and at both stores' read routes alike. The two store checks also read the agent's `swarm/agents//observability` leaf, which only the controller writes, and let the agent through only with the `logs` or `metrics` permission it names. -### The log store takes writes and reads from the node +### The log and metrics stores take writes and reads from the node -VictoriaLogs checks no credential: the gateway's vhost does. The store -listens on its pod's address behind a ClusterIP Service, and the Service's -NetworkPolicy admits traffic from the node and from nothing else. A process on -the log store's host, whatever its user, can therefore read every log line and -write records into the store without a bearer. An agent can't: its traffic -doesn't come from the node, and the hive's egress rules don't forward it to the -pod network. Treat the host that runs the log store as trusted for logs. The -metrics store still listens on a unix socket that only the gateway's group can -open. +VictoriaLogs and VictoriaMetrics check no credential: the gateway's vhost does. +Each store listens on its pod's address behind a ClusterIP Service, and the +Service's NetworkPolicy admits traffic from the node and from nothing else. A +process on the host that runs a store, whatever its user, can therefore read +every record in it and write records into it without a bearer. The metrics store +also serves its import and admin routes on that listener, so the same process +can delete series from it. An agent can't: its traffic doesn't come from the +node, and the hive's egress rules don't forward it to the pod network. Treat the +host that runs the stores as trusted for logs and metrics. ### Per-hive credentials live under the hive kind diff --git a/docs/2-handbook/swarm/services.md b/docs/2-handbook/swarm/services.md index 53804f60f..ad4401127 100644 --- a/docs/2-handbook/swarm/services.md +++ b/docs/2-handbook/swarm/services.md @@ -134,9 +134,10 @@ container, its sockets, secrets and metrics: ### Metrics (VictoriaMetrics + Grafana) The swarm's telemetry lands in one VictoriaMetrics, and one Grafana reads it, -at `metrics.` and `grafana.`. They sit in two -containers so you can restart or break Grafana without losing the time-series -database. Enabling either enables both; to run one alone, set it directly with +at `metrics.` and `grafana.`. They run apart so you +can restart or break Grafana without losing the time-series database. +`deploy.victoriametrics.enable` turns [the cluster node](../../4-implementation/k8s/node.md) +on with it. Enabling either enables both; to run one alone, set it directly with `deploy.victoriametrics.enable` or `deploy.grafana.enable`. Log in to Grafana with your swarm SSO account. Grafana provisions its diff --git a/docs/4-implementation/k8s/manifests.md b/docs/4-implementation/k8s/manifests.md index f13222a5c..8d041afbf 100644 --- a/docs/4-implementation/k8s/manifests.md +++ b/docs/4-implementation/k8s/manifests.md @@ -30,6 +30,8 @@ A service gets manifests on the host whose configuration deploys it: | `swarm-ci-metrics` | CronJob | ServiceAccount, PersistentVolume and Claim | | `swarm-victorialogs` | Deployment | Service, NetworkPolicy, PersistentVolume and Claim | | `swarm-logs-auth` | Deployment | ServiceAccount, two Services, NetworkPolicy | +| `swarm-victoriametrics` | Deployment | Service, NetworkPolicy, PersistentVolume and Claim | +| `swarm-metrics-auth` | Deployment | ServiceAccount, two Services, NetworkPolicy | `namespace.yaml` holds the Namespace ([Pod Security](#pod-security)), and `default-deny-ingress/networkpolicy.yaml` its default-deny ingress policy @@ -136,14 +138,15 @@ credential directory. every pod and admits nobody. The cluster's policy engine takes the union of the policies that select a pod and always admits the node's own addresses, so the node reaches a pod, and so does whatever its own policy names. A service with - a listener and `egress` (`swarm-logs-auth`) has the one policy: it admits the + a listener and `egress` (`swarm-logs-auth`, `swarm-metrics-auth`) has the one policy: it admits the node to the listener and nothing else, and allows the egress above. - **Probes:** a service with an opt-in health listener gets its variable set to `0.0.0.0:8080`, with liveness on `/healthz` and readiness on `/readyz` (see [Readiness checks](../swarm/readiness.md)). `swarm-controller` is one of them, with `SWARM_CONTROLLER_HEALTH_ADDR`. A third-party daemon with no health variable, - `victoria-logs`, names a `probe` path instead (`/health`), which both probes - request on its listener. The health port takes no credential, because the + `victoria-logs` and `victoria-metrics`, names a `probe` path instead (`/health`), which both probes + request on its listener. A `startupFailureThreshold` in it (`victoria-metrics`, 60) adds a + startup probe on the same path, tried every ten seconds, before the other two begin. The health port takes no credential, because the kubelet's probes carry none and the routes answer a status code with no body. Only the node reaches it, through the namespace's default-deny ingress policy and the pod's own policy, which admit no other pod and no @@ -156,7 +159,7 @@ credential directory. gets an init container and a 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`. A listener with no `tlsDir` - (`swarm-victorialogs`, `swarm-logs-auth`) serves plain HTTP: it gets the + (`swarm-victorialogs`, `swarm-logs-auth`, `swarm-victoriametrics`, `swarm-metrics-auth`) serves plain HTTP: it gets the Service and the policy and no leaf, init container or renewal container. - **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 @@ -166,10 +169,11 @@ credential directory. 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)). The responders' pod of the log store holds a second container the same way - ([runtime](runtime.md#the-log-store)). + ([runtime](runtime.md#the-queue)). The responders' pods of the log and metrics + stores hold a second container the same way + ([runtime](runtime.md#the-log-store), [runtime](runtime.md#the-metrics-store)). - **A unit of the module's own making:** a service that never ran under systemd on the - host (`swarm-victorialogs`, `swarm-logs-auth`) gives the renderer the + host (`swarm-victorialogs`, `swarm-logs-auth`, `swarm-victoriametrics`, `swarm-metrics-auth`) gives the renderer the `serviceConfig`, `environment` and `path` of one as a plain attribute set, with the daemon's flags as a list in `ExecStart`. The renderer reads nothing else of a unit. `fsGroupChangePolicy` in the entry sets the pod's field of that name. diff --git a/docs/4-implementation/k8s/node.md b/docs/4-implementation/k8s/node.md index a9548fb9c..5963e1b98 100644 --- a/docs/4-implementation/k8s/node.md +++ b/docs/4-implementation/k8s/node.md @@ -3,7 +3,8 @@ `services.hyperhive.deploy.k8s.enable` makes the host run the main node of a k3s cluster for the swarm services that run in the cluster. It defaults on where the host enables one of them (`deploy.notify-router`, `stt`, `ci-metrics`, -`review-classifier`, `claude-login`, `nats` or `swarm-controller`), which +`review-classifier`, `claude-login`, `nats`, `swarm-controller`, `victorialogs` or +`victoriametrics`), which `services.hyperhive.deploy.singleHostSwarm` and `services.hyperhive.deploy.allSwarmServices` do, so the all-local host gets the cluster with its services in it and no option set. Setting it to `false` on such @@ -70,7 +71,7 @@ says so with a label. The module sets the labels (`services.hyperhive.deploy.k8s.nodeLabels`, passed to `services.k3s.nodeLabel`): - `hyperhive.darkest.space/state-=true`, one per service whose data - the cluster keeps. Today that's `nats`, `ci-metrics` and `victorialogs`, each when its + the cluster keeps. Today that's `nats`, `ci-metrics`, `victorialogs` and `victoriametrics`, each when its `deploy..enable` is on. The service's static local volume has a `nodeAffinity` on the label, and its pod a `nodeSelector` on it, so a node without the label can't run the pod and shows diff --git a/docs/4-implementation/k8s/runtime.md b/docs/4-implementation/k8s/runtime.md index 2d9b534d9..d0227ef45 100644 --- a/docs/4-implementation/k8s/runtime.md +++ b/docs/4-implementation/k8s/runtime.md @@ -3,8 +3,9 @@ The swarm services that have a pod run in the host's cluster and nowhere else: `swarm-notify-router`, `swarm-stt`, `swarm-ci-metrics`, `swarm-review-classifier`, `swarm-claude-login`, `swarm-controller`, the queue with its auth callout -(`swarm-nats`) and the log store with its responders (`swarm-victorialogs`, -`swarm-logs-auth`). Each runs once per swarm, because two copies would share one +(`swarm-nats`), the log store with its responders (`swarm-victorialogs`, +`swarm-logs-auth`) and the metrics store with its responders (`swarm-victoriametrics`, +`swarm-metrics-auth`). Each runs once per swarm, because two copies would share one queue consumer and one set of buckets, or two cursors that push every CI run twice. A host that enables one gets [the cluster node](node.md) with it: no option moves a service between the host and the cluster, and the host has no @@ -15,7 +16,8 @@ gateway, `swarm-builder`, `swarm-matrix-ctl` and every hive stay on the host. `services.hyperhive.deploy.k8s.enable` defaults on where the host enables any of `deploy.notify-router`, `stt`, `ci-metrics`, `review-classifier`, -`claude-login`, `nats`, `swarm-controller` or `victorialogs`. `deploy.singleHostSwarm` and +`claude-login`, `nats`, `swarm-controller`, `victorialogs` or `victoriametrics`. +`deploy.singleHostSwarm` and `deploy.allSwarmServices` enable them, so an all-local host gets the node and every service in the cluster with no further option. A host that sets `deploy.k8s.enable = false` while it enables one fails at evaluation, and the @@ -58,6 +60,9 @@ The data stays where it was: way. - The log store's pod mounts `/var/lib/nixos-containers/swarm-victorialogs/var/lib/victorialogs`, the directory the container's store used ([The log store](#the-log-store)). +- The metrics store's pod mounts + `/var/lib/nixos-containers/swarm-victoriametrics/var/lib/victoriametrics`, the + directory the container's store used ([The metrics store](#the-metrics-store)). - The other services keep nothing a move has to carry. The pods log in to the store with their ServiceAccount tokens, so the store's @@ -377,6 +382,83 @@ writes to the store and Grafana that reads it. either pod, and no journal forwarder: the host collector reads the pods' output from the node like any pod's. +## The metrics store + +`swarm-victoriametrics` is the swarm's metrics store: one VictoriaMetrics, with +the two `auth_request` responders of its gateway vhost in a second pod, the +shape of [the log store](#the-log-store). The gateway's vhost +`metrics.` stays on the host, and so do the swarm collector that +pushes to the store and Grafana that reads it. + +- **The store's pod.** The module defines the daemon's `ExecStart`, with its flags + (`-storageDataPath`, `-httpListenAddr`, `-retentionPeriod`) as a list, and its + `StateDirectory`, and the renderer reads the pod from those. The image holds + the nixpkgs build of `victoria-metrics` and nothing else. The pod serves plain + HTTP on port 8428, logs in to nothing and pushes no telemetry: its output is + the pod's log, which the host collector reads like any pod's + ([logs](telemetry.md#logs)), and the collector scrapes the store's own metrics + through the gateway. Its Deployment has one replica and the `Recreate` + strategy. +- **Probes.** The store opens its storage before it serves HTTP, so it answers + nothing while a large store opens. The pod's startup probe requests `/health` + every ten seconds for up to ten minutes, and the liveness and readiness probes + begin once it has succeeded. The kubelet restarts a pod that fails the startup probe, + so a store that needs longer than that restarts in a loop. The termination + grace period is the renderer's 15 seconds; the store writes its in-memory data to disk when it + stops. +- **State.** The store's data directory on the host is + `/var/lib/nixos-containers/swarm-victoriametrics/var/lib/victoriametrics`: the + claim `swarm-victoriametrics-store` binds a static local volume on it, so the + history the store holds survives a restart or an upgrade of the pod. Its + `nodeAffinity` and the pod's `nodeSelector` both name the node label + `hyperhive.darkest.space/state-victoriametrics` + ([node labels](node.md#node-labels)). The files in the directory belong to a + user of its own, which the pod's user isn't: the pod's `fsGroup` is `65532` + with the `OnRootMismatch` policy, as for the log store, so the first start gives + the pod's group the whole tree and later starts look at the directory alone. + The module makes the directory when it's missing and leaves the owner and mode + of an existing one alone. The volume is the plain directory: a store that keeps + its history under `private/` (the layout of a `DynamicUser` unit) isn't + covered. The volume's capacity is nominal, and retention + (`deploy.victoriametrics.retentionPeriod`, 5 + years) bounds the store. +- **The gateway's hop.** The store serves TCP and a ClusterIP Service, + `swarm-victoriametrics..svc.`, publishes it. The + vhost's locations dial the Service's name through nginx's `resolver` and a + variable, as the log store's do, so `gateway.dns.enable` has to be on. An exact + location names its path after the variable, so the store receives the path + nginx matched and the request's query string, as it did over the socket. The + two prefix locations (`/api/v1/label/` and `/vmui/`) name none, so the request's + own path reaches the store: a decoded path in a `proxy_pass` could carry a line + break into the request line. The collector's push and scrape, Grafana's + datasource, the controller's and hive-c0re's URLs and the agents' + `swarm-metrics` all use `metrics.`, which the host serves: none + of them changes. The vhost has no location for the store's import and admin + routes, so the gateway refuses them. +- **The responders' pod.** The agents' check (the `swarm-logs-auth` binary on + the metrics surface, which checks an agent's bearer and its `metrics` + permission) and Grafana's (`swarm-metrics-auth`, which checks the bearer + Grafana's datasource sends) run as two containers of one Deployment, + `swarm-metrics-auth`. Both log in as the cert role `swarm-metrics-auth` does, so + the pod has one ServiceAccount and one jwt role of that name, and both read the + store with the pod's token. They hold no state, so the pod has no node label + and runs on any node. Each serves TCP (`--listen`) on 8083 and 8084 behind a + Service of its own. The second container names its ports `grafana-api` and + `grafana-health`: a Service resolves a named port to the first container of the + pod that declares the name, so a shared name would send Grafana's bearer to the + agents' responder. +- **Who reaches what.** Both pods have a NetworkPolicy that selects them for + ingress and allows none, so only the node's own traffic reaches them: the + host's nginx, which dials the Services, and the kubelet's probes. The store + takes writes, deletes and reads with no credential, so a process on the host + can use all three: see [Agent security](../../2-handbook/agents/security.md). + The responders' policy also lets the pod reach cluster DNS and the secret + store's pod listener (`egress = [ "store" ]`). +- **The host's part.** The host keeps the vhost, `gateway.localNames` and the + options (`retentionPeriod`, `agentAuthRate`, `agentAuthBurst`, + `agentAuthVerdictTtl` and the store identity files). The host collector reads + the pods' output from the node like any pod's. + ## Resources and sandbox The pod takes the unit's `MemoryMax` and `CPUQuota` as its container's limits diff --git a/docs/4-implementation/k8s/telemetry.md b/docs/4-implementation/k8s/telemetry.md index 0cf2880c4..ca926a3ba 100644 --- a/docs/4-implementation/k8s/telemetry.md +++ b/docs/4-implementation/k8s/telemetry.md @@ -1,7 +1,7 @@ # Pod logs and metrics -The swarm's metric store is a container on the host and its log store is a pod -([the log store](runtime.md#the-log-store)). A pod's output reaches both +The swarm's log and metrics stores are pods ([the log store](runtime.md#the-log-store), +[the metrics store](runtime.md#the-metrics-store)). A pod's output reaches both through the hive collector that runs on the host (`opentelemetry-collector`, [Telemetry](../swarm/telemetry.md)), which pushes to the stores through the gateway, so diff --git a/docs/4-implementation/swarm/credentials-reference.md b/docs/4-implementation/swarm/credentials-reference.md index c059a6d1a..d3fccbb42 100644 --- a/docs/4-implementation/swarm/credentials-reference.md +++ b/docs/4-implementation/swarm/credentials-reference.md @@ -16,57 +16,57 @@ the rest of the cell says how. -| store path | minter | reader | automatic re-mint | automatic re-pull | -| ---------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `swarm/agents//matrix/main` | `swarm-controller`, at agent creation and every 5 minutes | the agent | ✅ when missing or rejected by the homeserver | ✅ the daemon restarts within 5 minutes of a rejection | -| `swarm/agents//matrix/` | `swarm-controller`, from a credential you hand it | the agent | must be stated | ✅ retried within 2 to 16 minutes after a rejection | -| `swarm/agents//matrix-recovery/` | `swarm-controller`, a random passphrase per account | the agent | ❌ never replaced: the account's key backup opens only with this passphrase | ❌ read when the account comes up | -| `swarm/controller/swarm-controller/matrix/appservice-token` | `swarm-matrix-ctl appservice sync` in `hive-matrix`, from the registration's `as_token` | `swarm-controller` | ❌ follows the registration below | ✅ read every 5 minutes | -| `swarm/controller/swarm-controller/matrix/appservice-registration` | `swarm-matrix-ctl appservice render` in `hive-matrix`, when the store answers and holds none; before that, the tokens an earlier version kept in the container, stored unchanged | `swarm-matrix-ctl` in `hive-matrix`; the controller has no grant to it | ❌ minted once; an operator who replaces it makes `sync` render the new pair and restart the homeserver | ✅ `sync` re-reads it every 10 minutes | -| `swarm/controller/swarm-controller/oidc/client` | authelia, first boot | `swarm-controller` | ❌ minted once, straight into the store; minted again only when the store holds none | ✅ re-read when authelia refuses it | -| `swarm/controller/swarm-controller/forge/token` | the forge, once | `swarm-controller` | ❌ minted once; delete `/var/lib/forgejo/swarm-controller-token-name` in the forge container to mint a new one | ✅ re-read on a 401 | -| `swarm/controller/swarm-controller/grafana/token` | `swarm-bao-grafana-controller-token` on Grafana's host | `swarm-controller` | ✅ every 10 minutes, when Grafana refuses it or none is stored | ✅ re-read on a 401 | -| `swarm/controller/swarm-controller/forge/webhook-secret` | `swarm-controller`, at start, from its old `webhook-secret` file; a new one only when there is no file | `swarm-controller` | ❌ minted once; a rotated secret fails every delivery until the forge hooks are re-registered | ❌ read once; a store down at start is re-read in the background until it answers | -| `swarm/agents//bao-mtls` | the store's agent PKI mount (`deploy.bao.agentPkiMountPath`), at agent creation, and by the renewal pass when none is stored or its cert-auth role is missing | `hive-c0re`, then the agent | ✅ re-issued every 5 minutes once past half its 7-day validity; minted when none is stored or its cert-auth role is missing, for agents not `Destroyed` | ✅ the agent re-reads every 10 minutes; it renews only while its current leaf is valid | -| `swarm/agents//queue` | `swarm-controller`, at agent creation | the agent; the queue, log and metrics auth services | ✅ re-minted at 45 days; skipped for `Destroyed` agents, whose path is deleted | ✅ read on every reconnect; an open connection keeps the old secret for up to an hour; the log and metrics stores honour a replaced secret for up to `deploy.victorialogs.authVerdictTtl` and `deploy.victoriametrics.agentAuthVerdictTtl` (60 s by default) | -| `swarm/agents//observability` | `swarm-controller`, when an operator sets, clears or creates an agent's permissions with `logs` or `metrics` in the old or new list, and when it destroys the agent; the leaf stays absent for an agent that never held either | the agent (read-only, its own prefix); `swarm-logs-auth` and `swarm-metrics-agent-auth`, which deny when it's absent or unreadable | ✅ the controller rewrites it with the permission, and every five minutes makes it equal the record's | ✅ the stores honour a changed grant for up to `deploy.victorialogs.authVerdictTtl` and `deploy.victoriametrics.agentAuthVerdictTtl` (60 s by default) | -| `swarm/agents//forge-token` | `swarm-controller`, at agent creation and every 5 minutes | the agent | ✅ when missing or no longer matching the forge | ✅ every 10 minutes | -| `swarm/agents//forge/