Watch
0
0
Fork
You've already forked hyperhive
0

k8s: render the in-cluster services' manifests from their units

services.hyperhive.k8s.enable renders a Deployment (swarm-controller,
swarm-notify-router) or CronJob (swarm-ci-metrics) per service this host
deploys into system.build.k8sManifests. The renderer reads each evaluated
unit: its environment, LoadCredential ids, state and runtime directories,
ExecStart and timer. Files the unit names are mounted at the same paths, so
the environment carries over as it is. Secrets are referenced by name only.

No unit changes. checks: k8s-manifests-all-local (kubeconform -strict,
offline schemas) and module-eval-k8s; the allLocal fixture turns the
rendering on.
This commit is contained in:
atlas 2026-11-19 10:08:27 +01:00 • committed by mara
commit 40c5b3f5da

View file

@ -0,0 +1,114 @@
# Service manifests
`services.hyperhive.k8s` renders Kubernetes manifests for the swarm services
that run in the cluster. `nix/host-modules/k8s/render.nix` builds them from
the systemd unit each service runs today. A host with
`services.hyperhive.k8s.enable` gets them as `system.build.k8sManifests`, a
directory with one YAML file per object. Rendering changes no unit, and
nothing in this module applies the manifests to a cluster.
## Options
- `services.hyperhive.k8s.enable` (default `false`): render the manifests.
- `services.hyperhive.k8s.namespace` (default `hyperhive`): the namespace of
every rendered object.
## What gets rendered
A service gets manifests on the host whose configuration deploys it:
| service | workload | other objects |
| --------------------- | ---------- | -------------------------------------------------------------------------------- |
| `swarm-controller` | Deployment | PersistentVolumeClaim `swarm-controller-state`, ConfigMap `swarm-controller-etc` |
| `swarm-notify-router` | Deployment | none |
| `swarm-ci-metrics` | CronJob | PersistentVolumeClaim `swarm-ci-metrics-state` |
`namespace.yaml` holds the Namespace. NATS with its auth responder,
`swarm-review-classifier`, `swarm-claude-login` and `swarm-stt` have no
manifests yet: their images don't exist yet.
## One source: the unit
The renderer reads the service's evaluated unit, not a copy of its settings:
- **Environment:** the unit's `environment`, including what other modules
merge into it, minus `PATH`. A systemd `%d` becomes the credential
directory it stands for. The renderer fails if the unit's `path` holds
anything beyond the NixOS default tools, because the image doesn't carry
them.
- **Files:** the pod mounts each file the unit names at the same path,
so the environment carries over unchanged:
- credentials at `/run/credentials/<service>.service/<id>`, from the Secret
`<service>-credentials`, one key per `LoadCredential` id;
- trust bundles (any `…/trust-bundle.pem` the environment names), from the
ConfigMap `hyperhive-ca-trust`, key `trust-bundle.pem`;
- `RuntimeDirectory` as an `emptyDir`, and `StateDirectory` on the
service's PersistentVolumeClaim, with `RUNTIME_DIRECTORY` and
`STATE_DIRECTORY` set the way systemd sets them;
- `/etc` files the unit reads, from the service's own ConfigMap.
The renderer fails evaluation if an environment value names an absolute
path the pod doesn't mount, or a credential the unit doesn't load. A new
path in a unit then shows up as an error here, not as a pod that can't
find a file.
- **Command:** `ExecStart` must be the service's own binary. Its arguments
become the container's `args`.
- **Schedule:** the CronJob runs every `OnUnitInactiveSec` minutes of the
unit's timer, never overlapping, with `TimeoutStartSec` as its deadline.
A change to a unit's environment therefore changes its manifest in the same
commit.
## Secrets
The YAML lands in the world-readable nix store, so it holds no credential.
It names the Secret `<service>-credentials` and the credential ids as keys.
It never names the host path the unit loads a credential from, and never its
contents. The `hyperhive-ca-trust` ConfigMap is only referenced as well,
because the hive CA exists only at runtime. Nothing here creates either
object.
## Pods
- **Image:** `mkServiceImage` from [Service images](images.md), built from the
same `deploy.<service>.package` the unit runs. Store paths the environment
names (the controller's branding assets, for example) go into the image
too. Where the environment names none, the image is the flake's
`image-<service>`. The manifest carries the image name and tag but doesn't
depend on the image: building the manifests builds no daemon.
`imagePullPolicy` is `Never`, because the node imports the image tarball.
- **Security:** the `serviceUser` uid and gid from [Service images](images.md),
with `fsGroup` its gid so the pod reads its credentials (mode `0440`),
read-only root filesystem with an `emptyDir` on `/tmp`, no privilege
escalation, every capability dropped, the runtime's default seccomp profile,
and no service account token.
- **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` serves
`/readyz` on its unix socket only, so it has no probes yet.
- **Shutdown:** `terminationGracePeriodSeconds` is 15, the 10-second drain a
daemon allows itself after SIGTERM (`DRAIN` in
`swarm-secret-client/src/shutdown.rs`) plus headroom.
- **Rollout:** each Deployment has one replica and the `Recreate` strategy,
because each service runs once per swarm.
## Not covered yet
- The pods reach the swarm's host-run services (store, queue, identity
provider) by the same names as the units do. Those names don't resolve to
the host from inside a cluster yet.
- `OTEL_EXPORTER_OTLP_ENDPOINT` keeps the unit's value. For
`swarm-notify-router` that's its container's local log and metrics
forwarder, which a pod doesn't have.
- `swarm-controller`'s socket lives in an `emptyDir`, so only the pod can
reach it.
## Validation
The flake check `k8s-manifests-all-local` renders the manifests for the
single-host swarm fixture and runs `kubeconform -strict` over them, offline,
against JSON schemas for Kubernetes 1.35 pinned by hash. A deliberately
malformed Deployment in the same check must fail, so a validator that loads no
schema can't pass. `module-eval-k8s` checks that each workload carries its
unit's environment and that no credential source path reaches the YAML.