Watch
0
0
Fork
You've already forked hyperhive
0

require k8s: swarm services with a pod run in the cluster only

This commit is contained in:
atlas 2026-11-26 00:36:09 +01:00 • committed by mara
commit 052b559196

View file

@ -1,33 +1,23 @@
# The swarm's speech-to-text service, in a `swarm-stt` container on the host
# The swarm's speech-to-text service, a pod in the host's cluster on the host
# that sets `deploy.stt.enable`, which `deploy.allSwarmServices` defaults on
# where the host has the service's store identity. It answers the voice memos
# agents send on `$SWARM.stt.transcribe.<agent>` with whisper.cpp, once per memo
# (the cache is keyed by the audio's hash). Run it where the inference hardware
# is. docs/4-implementation/swarm/stt.md has the design. With
# `deploy.stt.runtime = "k8s"` it runs as a pod instead, rendered from the unit
# below (docs/4-implementation/k8s/runtime.md).
# is. docs/4-implementation/swarm/stt.md has the design. The pod is rendered
# from the unit below (docs/4-implementation/k8s/runtime.md).
#
# The audio comes from rooms with outside people, so the daemon runs as its own
# unprivileged user under a tight systemd sandbox (see `serviceConfig` below),
# with a memory and CPU ceiling, and a state directory as its only writable
# path.
# path. The pod's NetworkPolicy lets it reach cluster DNS, the store, the
# identity provider, the queue and the hive collector, and admits no ingress
# (./k8s/render.nix).
#
# Its own network namespace on the bridge, as ./swarm-claude-login.nix has: it
# reaches the swarm services by name and nothing on the host's loopback. It
# needs only the queue, the store and authelia, so it can run on any host.
#
# ⚠️ Nothing here limits what it may reach: the container's egress is whatever
# the bridge and the host's NAT allow. `ffmpeg` and `whisper-cli` run as
# children of the daemon, in the same network namespace and user, and can read
# the unit's credentials (the store leaf, the queue client secret). A decoder
# exploit on a hostile file could read them and send them out. The sandbox
# below narrows what such an exploit can do on the host; the network side
# waits on the strict-egress series covering service containers, or on running
# the decode in a child with no network and no credentials.
#
# The container holds one credential, its own store leaf, which nspawn hands in
# as a system credential with no copy on disk. The daemon reads its queue
# client secret with it.
# The daemon holds one credential, its own store leaf, and reads its queue
# client secret with it. `ffmpeg` and `whisper-cli` run as children of the
# daemon, under the same user, and can read it: a decoder exploit on a hostile
# file could read it and use it against the hosts the policy allows. Running the
# decode in a child with no network and no credentials would close that.
{
pkgs,
lib,
@ -63,8 +53,6 @@ let
# takes the service's metrics there and pushes them to the swarm collector.
forwarderOtlpPort = 4318;
rootfsCheck = import ./lib/container-rootfs-check.nix { inherit config; };
# The daemon reaches authelia, the store and the queue by name over TLS, and
# in self-signed mode their leaves chain to the hive CA.
caTrust = import ./lib/hive-ca-trust.nix { inherit lib tlsCfg gatewayCfg; };
@ -87,9 +75,6 @@ let
stt-bao-key = baoDeploy.sttClientKeyFile;
stt-bao-server-ca = baoDeploy.serverCaFile;
};
# nixos-container expands `extraFlags` unquoted, so a source path must be
# one word.
oneWord = path: builtins.match ".*[[:space:]].*" path == null;
credential = id: "/run/credentials/${unit}.service/${id}";
forwarderCredential = id: "/run/credentials/${forwarderUnit}.service/${id}";
@ -109,11 +94,8 @@ let
hash = modelHashes.${settings.modelSize};
};
onK8s = cfg.runtime == "k8s";
# The module the container runs: its own NixOS system on the host's bridge,
# or, with the service on the cluster, only the source of the unit the pod is
# rendered from (`deploy.stt.unit`).
# The unit the pod is rendered from (`deploy.stt.unit`) is
# defined in this module.
containerConfig =
{ lib, ... }:
{
@ -195,7 +177,7 @@ let
User = unit;
Group = unit;
UMask = "0077";
# Bare ids: the system credentials nspawn handed in.
# Bare ids, which the pod's login replaces.
LoadCredential = lib.attrNames storeFiles;
Restart = "on-failure";
RestartSec = "10s";
@ -246,8 +228,8 @@ let
};
};
# The container's system, evaluated as `containers.<name>.config` evaluates
# it, for a host that has no such container.
# The service's NixOS system, evaluated on its own: its unit is what the pod is
# rendered from.
podSystem =
(import "${pkgs.path}/nixos/lib/eval-config.nix" {
modules = [
@ -344,58 +326,19 @@ in
};
};
options.services.hyperhive.deploy.stt.runtime = lib.mkOption {
type = lib.types.enum [
"host"
"k8s"
];
default = "host";
description = ''
Where the speech-to-text service runs: `host` in its `swarm-stt`
container, or `k8s` as a pod in this host's cluster
({option}`services.hyperhive.deploy.k8s.enable`), which the host's
configuration must enable. The queue spreads requests over the instances
in its group, and the cache is each instance's own, so a switch moves
the service: the container goes and its objects are applied, or the
objects are deleted and the container returns. The transcript cache is
in the container's state directory on the host, and in an `emptyDir` of the
pod; neither moves with the service, so a transcript is
made again after a switch.
'';
};
options.services.hyperhive.deploy.stt.unit = lib.mkOption {
type = lib.types.raw;
readOnly = true;
internal = true;
default =
if onK8s then
podSystem.systemd.services.${unit}
else
config.containers.${machine}.config.systemd.services.${unit};
default = podSystem.systemd.services.${unit};
description = ''
The service's evaluated systemd unit, which the cluster's manifests are
rendered from (./k8s/render.nix). It is the unit the
container runs, taken from the container's configuration, or from the
same configuration evaluated alone when the service runs in the cluster
and the host has no container.
rendered from (./k8s/render.nix). It is the unit of the
pod's own configuration, evaluated alone: no container runs it.
'';
};
config = lib.mkMerge [
{
assertions = [
{
assertion = !onK8s || hyperhiveCfg.deploy.k8s.enable;
message = ''
services.hyperhive.deploy.stt.runtime = "k8s" needs the cluster: set
services.hyperhive.deploy.k8s.enable on this host, or leave the
service's runtime at "host".
'';
}
];
}
(lib.mkIf cfg.enable {
assertions = [
{
@ -416,48 +359,10 @@ in
two options.
'';
}
{
assertion = lib.all oneWord (lib.attrValues storeFiles);
message = ''
The speech-to-text service's store identity paths must not contain
whitespace: nspawn's --load-credential receives them through
nixos-container's unquoted extra flags.
'';
}
];
# The container hangs off the bridge, and the cluster's pods reach the
# host's services through it.
# The cluster's pods reach the host's services through the bridge.
services.hyperhive.network.enable = lib.mkDefault true;
})
(lib.mkIf (cfg.enable && !onK8s) {
# The container takes its address from the gateway's DHCP and resolves
# through its dnsmasq.
services.hyperhive.gateway.enable = lib.mkDefault true;
services.hyperhive.gateway.dns.enable = lib.mkDefault true;
# nspawn fails to start the container when a credential source is
# missing, so where this host mints the leaf the container waits for it.
systemd.services."container@${machine}" = lib.mkMerge [
caTrust.containerOrdering
(rootfsCheck.warnOnly machine)
{ after = [ "swarm-bao-pki.service" ]; }
];
containers.${machine} = {
autoStart = true;
# The transcript cache lives in the container's /var/lib/swarm-stt.
ephemeral = false;
# No `--link-journal=host`: the journal stays in the guest, where the
# container's own forwarder reads it (../container-modules/swarm-journal-forwarder.nix).
extraFlags = lib.mapAttrsToList (id: source: "--load-credential=${id}:${source}") storeFiles;
inherit privateNetwork;
hostBridge = networkCfg.bridgeName;
bindMounts = caTrust.bindMount;
config = containerConfig;
};
})
];
}