Watch
0
0
Fork
You've already forked hyperhive
0

nats: runtime k8s runs the queue and its auth callout as one pod

This commit is contained in:
atlas 2026-11-22 17:31:51 +01:00 • committed by maia
commit dd7de4d1e1

View file

@ -45,6 +45,7 @@ let
controllerCfg = deployCfg.swarm-controller;
controllerOnCluster = controllerCfg.enable && controllerCfg.runtime == "k8s";
controllerTcp = hyperhiveCfg.swarm.controller.tcp;
natsCfg = deployCfg.nats;
servicesRootPath = "${deployCfg.hive-controller.tls.stateDir}/${servicesRootKey}";
caSources = {
${trustBundleKey} = if caTrust.useSelfSigned then caTrust.caHostPath else null;
@ -142,6 +143,13 @@ let
# `"own"` and keeps the route its unit names, because the hive collector
# stamps the `hive` label on what it forwards and a swarm-level series must
# not carry one (../swarm-otel.nix, the swarm tier's own pipeline).
# `daemon` is the unit's binary and systemd name where the service's own name
# differs, `baoRole` the store role the daemon logs in with, and `pod`, where
# a pod holds more than the daemon, a function of the daemon's container
# (its environment, mounts and security context) giving the rest: further
# containers, init containers, volumes, host aliases, pod settings, objects and
# images. An `otlp` of `"none"` is a daemon that pushes no telemetry of its
# own.
services = {
swarm-controller = {
inherit (controllerCfg) enable package unit;
@ -168,6 +176,25 @@ let
hostUnit = "swarm-controller.service";
};
};
# The queue and its auth-callout responder are one pod: the responder is the
# daemon below, and the server (with its exporter) is `pod`'s.
swarm-nats = {
inherit (natsCfg) enable;
inherit (natsCfg) pod;
package = natsCfg.authPackage;
daemon = "swarm-nats-auth";
baoRole = "swarm-nats-auth";
unit = natsCfg.unit;
kind = "Deployment";
healthEnv = "SWARM_NATS_AUTH_HEALTH_ADDR";
etcFiles = [ ];
publicCredentials."bao-ca.pem" = baoServerCaKey;
otlp = "none";
switch = {
inherit (natsCfg) runtime;
hostUnit = "container@swarm-nats.service";
};
};
swarm-notify-router = {
inherit (deployCfg.notify-router) enable package;
unit = deployCfg.notify-router.unit;
@ -264,6 +291,41 @@ let
fail = name: msg: throw "services.hyperhive.k8s: ${name}: ${msg}";
# A unit's `ExecStart` as argv, split as systemd splits it: on spaces, with
# single quotes grouping a word (`lib.escapeShellArg` writes them) and `$$`
# standing for `$`. A pod's arguments are not expanded by a shell.
splitExec =
name: line:
let
quotes = w: lib.length (lib.splitString "'" w) - 1;
step =
st: w:
let
text = if st.open == null then w else "${st.open} ${w}";
in
if lib.mod (quotes text) 2 == 1 then
st // { open = text; }
else
{
open = null;
words = st.words ++ [ text ];
};
split = lib.foldl' step {
open = null;
words = [ ];
} (lib.filter (w: w != "") (lib.splitString " " line));
unquote =
w:
if lib.hasInfix "'\\''" w then
fail name "ExecStart has an argument with a single quote in it"
else
builtins.replaceStrings [ "'" "$$" ] [ "" "$" ] w;
in
if split.open != null then
fail name "ExecStart has an unterminated quote"
else
map unquote split.words;
labelsFor = name: {
"app.kubernetes.io/name" = name;
"app.kubernetes.io/part-of" = "hyperhive";
@ -349,8 +411,10 @@ let
listener = svc.listener or { };
publicFiles = svc.publicFiles or { };
daemon = svc.daemon or name;
baoRole = svc.baoRole or name;
credDir = "/run/credentials/${name}.service";
credDir = "/run/credentials/${daemon}.service";
# `LoadCredential` entries are `id:source` on the host and a bare `id`
# inside a container. Only the id is rendered.
unitCredIds = map (e: lib.head (lib.splitString ":" e)) (lib.toList (sc.LoadCredential or [ ]));
@ -369,7 +433,7 @@ let
# logs in with its pod's ServiceAccount token: the pod holds no client
# certificate. It reaches the store on the listener that asks for none
# (`deploy.bao.podListenerPort`), by the name every reader dials.
baoLogin = lib.elem name baoDeployCfg.k8sPrincipals && unitEnv ? BAO_CLIENT_CERT;
baoLogin = lib.elem baoRole baoDeployCfg.k8sPrincipals && unitEnv ? BAO_CLIENT_CERT;
baoClientCredIds =
map
(
@ -391,7 +455,7 @@ let
BAO_ADDR = "https://${hyperhiveCfg.swarm.bao.domain}:${toString baoDeployCfg.podListenerPort}";
BAO_AUTH = "jwt";
BAO_JWT_FILE = podAuth.tokenFile;
BAO_JWT_ROLE = name;
BAO_JWT_ROLE = baoRole;
BAO_JWT_MOUNT = podAuth.mount;
};
@ -443,13 +507,14 @@ let
image = mkServiceImage {
inherit name;
inherit (svc) package;
bin = daemon;
extraContents = lib.optional (storeValues != [ ]) (
pkgs.writeTextDir "share/hyperhive/${name}.env-refs" (lib.concatLines storeValues)
);
};
execWords = lib.filter (w: w != "") (lib.splitString " " (toString sc.ExecStart));
entrypoint = "${svc.package}/bin/${name}";
execWords = splitExec name (toString sc.ExecStart);
entrypoint = "${svc.package}/bin/${daemon}";
# The credentials that are public CA files come from the CA ConfigMap, the
# rest from the service's Secret. A volume holding both is a projected
@ -683,6 +748,23 @@ let
package = leafImagePackage;
bin = leafIssueBin;
};
# What the service adds to the daemon's pod.
extra = {
containers = [ ];
initContainers = [ ];
volumes = [ ];
hostAliases = [ ];
podSpec = { };
objects = { };
images = [ ];
}
// lib.optionalAttrs (svc ? pod) (
svc.pod {
inherit env volumeMounts metaFor;
inherit (container) securityContext;
}
);
leafContainer = {
name = "tls-issue";
image = "${leafImage.imageName}:${leafImage.imageTag}";
@ -718,7 +800,8 @@ let
};
podSpec = {
inherit terminationGracePeriodSeconds volumes;
inherit terminationGracePeriodSeconds;
volumes = volumes ++ extra.volumes;
containers = [
container
]
@ -728,7 +811,8 @@ let
name = "tls-renew";
command = [ "/bin/${leafRenewBin}" ];
}
);
)
++ extra.containers;
# The API token is never mounted; a pod that logs in to the store gets
# a token of its own, projected for the store's audience.
automountServiceAccountToken = false;
@ -741,7 +825,11 @@ let
};
}
// lib.optionalAttrs baoLogin { serviceAccountName = name; }
// lib.optionalAttrs (listener != { }) { initContainers = [ leafContainer ]; };
// lib.optionalAttrs (listener != { } || extra.initContainers != [ ]) {
initContainers = lib.optional (listener != { }) leafContainer ++ extra.initContainers;
}
// lib.optionalAttrs (extra.hostAliases != [ ]) { inherit (extra) hostAliases; }
// extra.podSpec;
# What the `-etc` ConfigMap holds. A file mounted from it by `subPath`
# never sees a later change, so the pod template carries a hash of the
@ -810,6 +898,7 @@ let
objects = {
"${name}/${lib.toLower svc.kind}" = main;
}
// extra.objects
// lib.optionalAttrs baoLogin {
# Named after the service: the store's jwt role binds this account.
"${name}/serviceaccount" = {
@ -847,8 +936,8 @@ let
fail name "the unit already sets ${svc.healthEnv}"
else if lib.head execWords != entrypoint then
fail name "ExecStart is not ${entrypoint} with plain arguments"
else if lib.any (w: lib.hasInfix "\"" w || lib.hasInfix "'" w) execWords then
fail name "ExecStart has quoted arguments, which the split on spaces would break"
else if lib.any (w: lib.hasInfix "\"" w) execWords then
fail name "ExecStart has double-quoted arguments, which the split on spaces would break"
else if unmounted != { } then
fail name "the pod mounts nothing at the path ${lib.concatStringsSep ", " (lib.attrNames unmounted)} names"
else if missingCreds != { } then
@ -857,7 +946,7 @@ let
{
inherit objects image;
# What the pod's other containers run.
extraImages = lib.optional (listener != { }) leafImage;
extraImages = lib.optional (listener != { }) leafImage ++ extra.images;
};
enabled = lib.mapAttrs workload (lib.filterAttrs (_: svc: svc.enable) services);