248 lines
9.5 KiB
Nix
248 lines
9.5 KiB
Nix
# The swarm notification router, a pod in the host's cluster on the
|
|
# host that sets `deploy.notify-router.enable`, which `deploy.allSwarmServices`
|
|
# defaults on where the host has the router's store identity. It reads the
|
|
# forge events swarm-controller keeps from its webhooks and queues todo ops on
|
|
# each agent's subject of `ROUTER_TODOS`, which the agent's harness applies.
|
|
# docs/4-implementation/swarm/notify-router.md has the design.
|
|
#
|
|
# The pod is rendered (./k8s/render.nix) from the unit this file defines;
|
|
# docs/4-implementation/k8s/runtime.md has the design. 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.
|
|
#
|
|
# The unit names its store leaf by credential id; the pod logs in to the store
|
|
# with its ServiceAccount token instead, and reads its queue client secret with it.
|
|
{
|
|
pkgs,
|
|
lib,
|
|
config,
|
|
...
|
|
}:
|
|
let
|
|
hyperhiveCfg = config.services.hyperhive;
|
|
|
|
cfg = hyperhiveCfg.deploy.notify-router;
|
|
baoCfg = hyperhiveCfg.swarm.bao;
|
|
baoDeploy = hyperhiveCfg.deploy.bao;
|
|
natsCfg = hyperhiveCfg.swarm.nats;
|
|
autheliaCfg = hyperhiveCfg.swarm.authelia;
|
|
gatewayCfg = hyperhiveCfg.gateway;
|
|
networkCfg = hyperhiveCfg.network;
|
|
tlsCfg = hyperhiveCfg.deploy.hive-controller.tls;
|
|
|
|
machine = "swarm-notify-router";
|
|
unit = "swarm-notify-router";
|
|
clientId = hyperhiveCfg.swarm.notify-router.clientId;
|
|
|
|
# The container's journal forwarder pushes to its own receiver on the swarm
|
|
# collector, the route `serviceProducers.swarm-notify-router` in
|
|
# ./swarm-otel-service.nix declares, as ./swarm-claude-login.nix does.
|
|
swarmOtelCfg = hyperhiveCfg.swarm.otel;
|
|
forwarderEndpoint = "https://${swarmOtelCfg.domain}/${machine}";
|
|
forwarderUnit = "swarm-journal-forwarder-oidc";
|
|
# Loopback in the container's own netns, and distinct from the other service
|
|
# containers' forwarders all the same.
|
|
forwarderTelemetryPort = 8902;
|
|
# The standard OTLP/HTTP port, on the container's loopback: the forwarder
|
|
# takes the router's metrics there and pushes them to the swarm collector.
|
|
forwarderOtlpPort = 4318;
|
|
|
|
# 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; };
|
|
caBundleModule = caTrust.trustBundle {
|
|
inherit pkgs;
|
|
name = machine;
|
|
consumers = [
|
|
unit
|
|
# Go: `SSL_CERT_FILE` replaces its trust store, for the swarm
|
|
# collector's and authelia's names.
|
|
"opentelemetry-collector"
|
|
];
|
|
};
|
|
|
|
haveClientIdentity =
|
|
baoDeploy.notifyRouterClientCertFile != null && baoDeploy.notifyRouterClientKeyFile != null;
|
|
|
|
# Credential id → host source, as ./swarm-claude-login.nix hands its leaf in.
|
|
storeFiles = lib.filterAttrs (_: source: source != null) {
|
|
notify-router-bao-cert = baoDeploy.notifyRouterClientCertFile;
|
|
notify-router-bao-key = baoDeploy.notifyRouterClientKeyFile;
|
|
notify-router-bao-server-ca = baoDeploy.serverCaFile;
|
|
};
|
|
credential = id: "/run/credentials/${unit}.service/${id}";
|
|
forwarderCredential = id: "/run/credentials/${forwarderUnit}.service/${id}";
|
|
|
|
privateNetwork = true;
|
|
|
|
# The unit the pod is rendered from (`deploy.notify-router.unit`) is
|
|
# defined in this module.
|
|
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, which the pod's login replaces.
|
|
LoadCredential = lib.attrNames storeFiles;
|
|
Restart = "on-failure";
|
|
RestartSec = "10s";
|
|
SyslogIdentifier = unit;
|
|
};
|
|
};
|
|
};
|
|
|
|
# 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 = [
|
|
(
|
|
{ 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 {
|
|
type = lib.types.str;
|
|
readOnly = true;
|
|
default = "swarm-notify-router";
|
|
description = ''
|
|
The OAuth2 client id the notification router presents to the swarm
|
|
queue. Read-only: the host running authelia registers it, the queue's
|
|
auth-callout responder grants it the router's subjects, and
|
|
authelia's mint stores its secret in the store, all from this one value.
|
|
'';
|
|
};
|
|
|
|
options.services.hyperhive.deploy.notify-router.package = lib.mkOption {
|
|
type = lib.types.package;
|
|
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarm-notify-router";
|
|
description = "The notification router daemon package.";
|
|
};
|
|
|
|
options.services.hyperhive.deploy.notify-router.unit = lib.mkOption {
|
|
type = lib.types.raw;
|
|
readOnly = true;
|
|
internal = true;
|
|
default = podSystem.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 of the
|
|
pod's own configuration, evaluated alone: no container runs it.
|
|
'';
|
|
};
|
|
|
|
config = lib.mkMerge [
|
|
(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.
|
|
'';
|
|
}
|
|
];
|
|
|
|
# The cluster's pods reach the host's services through the bridge.
|
|
services.hyperhive.network.enable = lib.mkDefault true;
|
|
})
|
|
];
|
|
}
|