Watch
0
0
Fork
You've already forked hyperhive
0

k8s: deploy.notify-router.runtime moves the router between its container and the cluster

This commit is contained in:
atlas 2026-11-21 15:16:32 +01:00 • committed by maia
commit a1d44ed046

View file

@ -5,6 +5,11 @@
# each agent's subject of `ROUTER_TODOS`, which the agent's harness applies.
# docs/4-implementation/swarm/notify-router.md has the design.
#
# With `deploy.notify-router.runtime = "k8s"` the host has no container: the
# router runs as a pod in the host's cluster, which ../host-modules/k8s renders
# from the unit this file defines. docs/4-implementation/k8s/runtime.md has
# the switch.
#
# 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
# talks only to the queue, the store and authelia, so it can run on any host.
@ -79,6 +84,121 @@ let
forwarderCredential = id: "/run/credentials/${forwarderUnit}.service/${id}";
privateNetwork = true;
onK8s = cfg.runtime == "k8s";
# The module the container runs: its own NixOS system on the host's bridge,
# or, with the router on the cluster, only the source of the unit the pod is
# rendered from (`deploy.notify-router.unit`).
containerConfig =
{ lib, ... }:
{
imports = [
../container-modules/swarm-container.nix
../container-modules/swarm-journal-forwarder.nix
caBundleModule
];
services.hyperhive.swarmContainer = {
inherit privateNetwork;
writesOwnResolvConf = false;
};
networking.nameservers = [ networkCfg.bridgeIp ];
networking.interfaces.eth0.useDHCP = true;
users.users.${unit} = {
isSystemUser = true;
group = unit;
};
users.groups.${unit} = { };
# Authenticated as the daemon's own OIDC client, whose secret its
# store identity already reads: one identity for the whole container.
services.hyperhive.swarmJournalForwarder = {
enable = true;
serviceName = "notify-router";
endpoint = forwarderEndpoint;
inherit clientId;
tokenUrl = "${autheliaCfg.url}/api/oidc/token";
telemetryPort = forwarderTelemetryPort;
otlpMetricsPort = forwarderOtlpPort;
store = {
package = baoDeploy.package;
addr = "https://${baoCfg.domain}:${toString baoCfg.port}";
secretPath = "secret/swarm/services/${clientId}/oidc/client";
clientCertFile = forwarderCredential "notify-router-bao-cert";
clientKeyFile = forwarderCredential "notify-router-bao-key";
caFile =
if storeFiles ? notify-router-bao-server-ca then
forwarderCredential "notify-router-bao-server-ca"
else
null;
};
};
systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames storeFiles;
systemd.services.${unit} = {
description = "route forge events to agents' todo lists";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
environment = {
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
BAO_CLIENT_CERT = credential "notify-router-bao-cert";
BAO_CLIENT_KEY = credential "notify-router-bao-key";
SWARM_NOTIFY_ROUTER_NATS_URL = "tls://${natsCfg.domain}:${toString natsCfg.port}";
SWARM_NOTIFY_ROUTER_OIDC_TOKEN_ENDPOINT = "${autheliaCfg.url}/api/oidc/token";
SWARM_NOTIFY_ROUTER_OIDC_CLIENT_ID = clientId;
OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:${toString forwarderOtlpPort}";
OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf";
OTEL_RESOURCE_ATTRIBUTES = "service.name=hyperhive-notify-router,swarm=${
if hyperhiveCfg.swarm.name == null then "unknown" else hyperhiveCfg.swarm.name
}";
}
// lib.optionalAttrs (storeFiles ? notify-router-bao-server-ca) {
BAO_CACERT = credential "notify-router-bao-server-ca";
}
// lib.optionalAttrs caTrust.useSelfSigned {
SWARM_NOTIFY_ROUTER_OIDC_CA_FILE = caTrust.bundlePathFor machine;
};
serviceConfig = {
ExecStart = "${cfg.package}/bin/swarm-notify-router";
User = unit;
Group = unit;
UMask = "0077";
# Bare ids: the system credentials nspawn handed in.
LoadCredential = lib.attrNames storeFiles;
Restart = "on-failure";
RestartSec = "10s";
SyslogIdentifier = unit;
};
};
};
# The container's system, evaluated as `containers.<name>.config` evaluates
# it, for a host that has no such container.
podSystem =
(import "${pkgs.path}/nixos/lib/eval-config.nix" {
modules = [
(
{ options, ... }:
{
config = {
nixpkgs =
if options.nixpkgs ? hostPlatform then
{ inherit (pkgs.stdenv) hostPlatform; }
else
{ localSystem = pkgs.stdenv.hostPlatform; };
boot.isNspawnContainer = true;
networking.hostName = lib.mkDefault machine;
networking.useDHCP = false;
};
}
)
containerConfig
];
system = null;
}).config;
in
{
options.services.hyperhive.swarm.notify-router.clientId = lib.mkOption {
@ -99,146 +219,118 @@ in
description = "The notification router daemon package.";
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = haveClientIdentity;
message = ''
services.hyperhive.deploy.notify-router.enable requires the router's
swarm-secret-store client identity: set both
services.hyperhive.deploy.bao.notifyRouterClientCertFile
services.hyperhive.deploy.bao.notifyRouterClientKeyFile
The router reads its queue client secret with it.
Both default to the pair glue-bao-tls.nix mints when
services.hyperhive.deploy.bao.enable is true (the host that runs the
store). Otherwise copy notify-router.pem and notify-router-key.pem
from the store host's /var/lib/swarm-bao-pki/ and name them in those
two options.
'';
}
{
assertion = lib.all oneWord (lib.attrValues storeFiles);
message = ''
The notification router's store identity paths must not contain
whitespace: nspawn's --load-credential receives them through
nixos-container's unquoted extra flags.
'';
}
options.services.hyperhive.deploy.notify-router.runtime = lib.mkOption {
type = lib.types.enum [
"host"
"k8s"
];
# The container hangs off the bridge, takes its address from the gateway's
# DHCP and resolves through its dnsmasq.
services.hyperhive.network.enable = lib.mkDefault true;
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 daemon keeps its state in the queue: its consumer's position and
# its per-issue snapshots.
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 =
{ lib, ... }:
{
imports = [
../container-modules/swarm-container.nix
../container-modules/swarm-journal-forwarder.nix
caBundleModule
];
services.hyperhive.swarmContainer = {
inherit privateNetwork;
writesOwnResolvConf = false;
};
networking.nameservers = [ networkCfg.bridgeIp ];
networking.interfaces.eth0.useDHCP = true;
users.users.${unit} = {
isSystemUser = true;
group = unit;
};
users.groups.${unit} = { };
# Authenticated as the daemon's own OIDC client, whose secret its
# store identity already reads: one identity for the whole container.
services.hyperhive.swarmJournalForwarder = {
enable = true;
serviceName = "notify-router";
endpoint = forwarderEndpoint;
inherit clientId;
tokenUrl = "${autheliaCfg.url}/api/oidc/token";
telemetryPort = forwarderTelemetryPort;
otlpMetricsPort = forwarderOtlpPort;
store = {
package = baoDeploy.package;
addr = "https://${baoCfg.domain}:${toString baoCfg.port}";
secretPath = "secret/swarm/services/${clientId}/oidc/client";
clientCertFile = forwarderCredential "notify-router-bao-cert";
clientKeyFile = forwarderCredential "notify-router-bao-key";
caFile =
if storeFiles ? notify-router-bao-server-ca then
forwarderCredential "notify-router-bao-server-ca"
else
null;
};
};
systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames storeFiles;
systemd.services.${unit} = {
description = "route forge events to agents' todo lists";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
environment = {
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
BAO_CLIENT_CERT = credential "notify-router-bao-cert";
BAO_CLIENT_KEY = credential "notify-router-bao-key";
SWARM_NOTIFY_ROUTER_NATS_URL = "tls://${natsCfg.domain}:${toString natsCfg.port}";
SWARM_NOTIFY_ROUTER_OIDC_TOKEN_ENDPOINT = "${autheliaCfg.url}/api/oidc/token";
SWARM_NOTIFY_ROUTER_OIDC_CLIENT_ID = clientId;
OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:${toString forwarderOtlpPort}";
OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf";
OTEL_RESOURCE_ATTRIBUTES = "service.name=hyperhive-notify-router,swarm=${
if hyperhiveCfg.swarm.name == null then "unknown" else hyperhiveCfg.swarm.name
}";
}
// lib.optionalAttrs (storeFiles ? notify-router-bao-server-ca) {
BAO_CACERT = credential "notify-router-bao-server-ca";
}
// lib.optionalAttrs caTrust.useSelfSigned {
SWARM_NOTIFY_ROUTER_OIDC_CA_FILE = caTrust.bundlePathFor machine;
};
serviceConfig = {
ExecStart = "${cfg.package}/bin/swarm-notify-router";
User = unit;
Group = unit;
UMask = "0077";
# Bare ids: the system credentials nspawn handed in.
LoadCredential = lib.attrNames storeFiles;
Restart = "on-failure";
RestartSec = "10s";
SyslogIdentifier = unit;
};
};
};
};
default = "host";
description = ''
Where the notification router runs: `host` in its `swarm-notify-router`
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 router runs once per swarm, so a switch
moves it: the container goes and its objects are applied, or the objects
are deleted and the container returns. The router keeps nothing on the
host: its consumer and state bucket live in the queue.
'';
};
options.services.hyperhive.deploy.notify-router.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};
description = ''
The router's evaluated systemd unit, which the cluster's manifests are
rendered from (../host-modules/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 router runs in the cluster
and the host has no container.
'';
};
config = lib.mkMerge [
{
assertions = [
{
assertion = !onK8s || hyperhiveCfg.deploy.k8s.enable;
message = ''
services.hyperhive.deploy.notify-router.runtime = "k8s" needs the
cluster: set services.hyperhive.deploy.k8s.enable on this host, or
leave the router's runtime at "host".
'';
}
];
}
(lib.mkIf cfg.enable {
assertions = [
{
assertion = haveClientIdentity;
message = ''
services.hyperhive.deploy.notify-router.enable requires the router's
swarm-secret-store client identity: set both
services.hyperhive.deploy.bao.notifyRouterClientCertFile
services.hyperhive.deploy.bao.notifyRouterClientKeyFile
The router reads its queue client secret with it.
Both default to the pair glue-bao-tls.nix mints when
services.hyperhive.deploy.bao.enable is true (the host that runs the
store). Otherwise copy notify-router.pem and notify-router-key.pem
from the store host's /var/lib/swarm-bao-pki/ and name them in those
two options.
'';
}
{
assertion = lib.all oneWord (lib.attrValues storeFiles);
message = ''
The notification router'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.
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 daemon keeps its state in the queue: its consumer's position and
# its per-issue snapshots.
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;
};
})
];
}