Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the controller pod keeps no volume; the state gate goes

This commit is contained in:
atlas 2026-11-23 05:45:57 +01:00 • committed by mara
commit 513be8f7d0

View file

@ -108,16 +108,6 @@ let
inherit lib pkgs config;
}).objects;
objects = render allLocal;
# The same configuration with every state gate on a fresh install. The
# fixtures leave the option off, so the cases that compare two hosts'
# manifests differ only by where a unit comes from.
freshInstall =
config:
lib.recursiveUpdate config {
services.hyperhive.deploy = {
swarm-controller.k8s.freshState = true;
};
};
# The queue's pod reads the settings of the system it runs from, which differ
# in the path of the server's key: what a switch must leave alone is the rest.
notQueue = lib.filterAttrs (n: _: !(lib.hasPrefix "swarm-nats/" n));
@ -365,18 +355,17 @@ let
{
# The images are the ones the manifests run, one per rendered service, the
# controller's listener leaf, the queue's three more (server, exporter,
# init) and the one state gate every stateful pod shares: the twelve are
# the control that the list is not empty.
# init): the eleven are the control that the list is not empty.
name = "the node loads the image of every service the manifests run, and k3s's own";
ok =
let
loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages;
in
lib.length rendered == 12
lib.length rendered == 11
&& lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 13;
&& lib.length loaded == 12;
}
{
# Nothing the host already runs differs but the store's side of the pods'
@ -1448,7 +1437,6 @@ let
== [
"ConfigMap"
"Deployment"
"PersistentVolumeClaim"
"ServiceAccount"
];
}
@ -1506,7 +1494,7 @@ let
&& annotationOf rolled != annotationOf onCluster
# A pod with no ConfigMap carries no annotation: the router is the control.
&& !((cluster."swarm-notify-router/deployment".spec.template.metadata) ? annotations)
&& map (c: c.name) (podSpec "swarm-controller").initContainers == [ "state-gate" ];
&& !((podSpec "swarm-controller") ? initContainers);
}
{
# The pod issues its own listener leaf with the unit's script, logging in
@ -1527,7 +1515,7 @@ let
createDir = false;
};
spec = (render onCluster)."swarm-controller/deployment".spec.template.spec;
init = lib.elemAt spec.initContainers 1;
init = lib.head spec.initContainers;
renew = lib.last spec.containers;
main = lib.head spec.containers;
mountsOf = x: map (m: m.name) x.volumeMounts;
@ -1546,11 +1534,7 @@ let
&& !(lib.hasInfix "chgrp" podScript)
&& lib.hasInfix "-checkhost \"$name\"" podScript
&& lib.hasInfix "issue/swarm-controller-tls" podScript
&&
map (c: c.name) spec.initContainers == [
"state-gate"
"tls-issue"
]
&& map (c: c.name) spec.initContainers == [ "tls-issue" ]
&& init.name == "tls-issue"
&& renew.name == "tls-renew"
&& renew.command == [ "/bin/swarm-controller-tls-renew" ]
@ -1688,11 +1672,11 @@ let
&& pvc.spec.accessModes == [ "ReadWriteOnce" ]
# The claim binds by name the static local volume on the container's
# store directory, on this node, and only that: its class is its own.
# The controller's claim is the control that a claim does not.
# The poller's claim is the control that a claim does not.
&& pvc.spec.volumeName == pv.metadata.name
&& pvc.spec.storageClassName == pv.spec.storageClassName
&& pv.spec.storageClassName != ""
&& !(objects."swarm-controller/persistentvolumeclaim".spec ? volumeName)
&& !(objects."swarm-ci-metrics/persistentvolumeclaim".spec ? volumeName)
&& pv.spec.local.path == hostStore
&& pv.spec.persistentVolumeReclaimPolicy == "Retain"
&& !(pv.metadata ? namespace)
@ -1716,81 +1700,48 @@ let
&& !(lib.any (v: v ? secret) spec.volumes);
}
{
# The gate is the first init container of a pod whose state is worth
# moving, ahead of the TLS leaf. The default holds the pod back; a fresh
# install opts in. Each arm's value is the control for the other, and the
# gated services are the control for the ones that start with no gate.
name = "every pod whose state is worth moving starts with the state gate on that volume, and the fresh option sets what it reads";
# No pod starts on a gate: its first init container is the leaf's or
# none. Each pod's state directory is read off the manifest: an emptyDir
# where the service re-derives what it keeps, a claim where it does not
# (the poller's cursor), and the queue's claim is its own, bound to the
# host's directory. The claims are the control for the emptyDirs.
name = "no pod starts on a state gate, and the state directories are emptyDirs except the poller's claim and the queue's volume";
ok =
let
fresh = render (freshInstall onCluster);
specOf =
objs: name:
name:
let
o = objs."${name}/deployment" or objs."${name}/cronjob";
o = objects."${name}/deployment" or objects."${name}/cronjob";
in
if o.kind == "CronJob" then o.spec.jobTemplate.spec.template.spec else o.spec.template.spec;
inits = objs: name: (specOf objs name).initContainers or [ ];
gate = objs: name: lib.findFirst (i: i.name == "state-gate") null (inits objs name);
envOfGate = g: lib.listToAttrs (map (e: lib.nameValuePair e.name e.value) g.env);
gated = {
swarm-controller = {
path = "/var/lib/swarm-controller";
volume = "state";
option = "deploy.swarm-controller.k8s.freshState";
};
};
# The poller keeps a claim for its cursor and the queue one on the host's
# directory; the speech-to-text pod keeps none. None is gated.
unclaimed = [
stateVolume = name: lib.findFirst (v: v.name == "state") null (specOf name).volumes;
services = [
"swarm-controller"
"swarm-nats"
"swarm-notify-router"
"swarm-stt"
"swarm-review-classifier"
"swarm-claude-login"
"swarm-stt"
];
ungated = unclaimed ++ [
"swarm-ci-metrics"
"swarm-nats"
];
one =
name: g:
let
held = gate objects name;
open = gate fresh name;
full = "services.hyperhive.${g.option}";
in
(lib.head (inits objects name)).name == "state-gate"
&& (lib.head (inits fresh name)).name == "state-gate"
&& (envOfGate held).STATE_GATE_DIR == g.path
&& (envOfGate held).STATE_GATE_SERVICE == name
&& (envOfGate held).STATE_GATE_FRESH == "false"
&& (envOfGate open).STATE_GATE_FRESH == "true"
&& (envOfGate held).STATE_GATE_OPTION == full
# The option the refusal names exists, and is the one that is set.
&& lib.hasAttrByPath (lib.splitString "." full) onCluster
&& lib.attrByPath (lib.splitString "." full) null (freshInstall onCluster) == true
&& lib.attrByPath (lib.splitString "." full) null onCluster == false
&& lib.attrByPath (lib.splitString "." full) null allLocal == false
# The gate mounts the volume the daemon's pod claims, writable.
&&
held.volumeMounts == [
{
name = g.volume;
mountPath = g.path;
}
]
&& lib.any (v: v.name == g.volume && v ? persistentVolumeClaim) (specOf objects name).volumes
&& held.image == open.image
&& !(held ? resources);
in
lib.all (n: one n gated.${n}) (lib.attrNames gated)
&& lib.all (n: gate objects n == null && gate fresh n == null) ungated
&& lib.all (n: !(objects ? "${n}/persistentvolumeclaim")) unclaimed
&& lib.all (n: objects ? "${n}/persistentvolumeclaim") (
lib.attrNames gated ++ [ "swarm-ci-metrics" ]
)
# One image serves every gate.
&& lib.length (lib.unique (map (n: (gate objects n).image) (lib.attrNames gated))) == 1;
lib.all (n: !(lib.any (i: i.name == "state-gate") ((specOf n).initContainers or [ ]))) services
&&
stateVolume "swarm-controller" == {
name = "state";
emptyDir = { };
}
&&
stateVolume "swarm-stt" == {
name = "state";
emptyDir = { };
}
&& !(objects ? "swarm-controller/persistentvolumeclaim")
&& !(objects ? "swarm-stt/persistentvolumeclaim")
&& (stateVolume "swarm-ci-metrics").persistentVolumeClaim.claimName == "swarm-ci-metrics-state"
&& objects ? "swarm-ci-metrics/persistentvolumeclaim"
&& !(lib.any (n: lib.hasInfix "state-gate" n) (map toString withNode.system.build.k8sImages))
&& objects ? "swarm-nats/persistentvolumeclaim";
}
{
# The responder's unit is the container's on the host and the
@ -1875,67 +1826,28 @@ let
&& has "-t raw -F nixos-fw-nats" onCluster.networking.firewall.extraStopCommands;
}
{
# Each state block is read against what the render makes: the claim is
# the PVC's name and the mount is where the state gate looks. The
# stateless services are the control that an entry can carry no state. The
# poller has a claim and no entry, since it starts without a gate.
name = "migrate.json names every service, with a state block exactly for the ones whose state is gated";
ok =
let
services = (migrateOf withNode).services;
stateful = [
"swarm-controller"
];
stateless = [
"swarm-nats"
"swarm-notify-router"
"swarm-review-classifier"
"swarm-claude-login"
"swarm-stt"
"swarm-ci-metrics"
];
objs = render withNode;
gateDir =
n:
let
o = objs."${n}/deployment" or objs."${n}/cronjob";
spec = if o.kind == "CronJob" then o.spec.jobTemplate.spec.template.spec else o.spec.template.spec;
gate = lib.head (lib.filter (c: c.name == "state-gate") spec.initContainers);
in
(lib.head (lib.filter (e: e.name == "STATE_GATE_DIR") gate.env)).value;
in
lib.sort lib.lessThan (lib.attrNames services) == lib.sort lib.lessThan (stateful ++ stateless)
&& lib.all (
n:
let
s = services.${n}.state;
in
s != null
&& s.claim == objs."${n}/persistentvolumeclaim".metadata.name
&& s.mount == gateDir n
&& s ? hostPath
&& s ? excludes
) stateful
&& lib.all (n: services.${n}.state == null) stateless
# The poller's claim is the control that a state of null is no claim's absence.
&& objs ? "swarm-ci-metrics/persistentvolumeclaim"
&& lib.all (n: lib.hasPrefix "services.hyperhive.deploy." services.${n}.option) (
lib.attrNames services
);
}
{
name = "migrate.json gives the host path of each state, the host units of each service and the runtime the configuration wants";
# The file names the runtime and the host units of every service and no
# state: nothing is copied. The units of the poller (timer and service)
# are the control for a list of more than one.
name = "migrate.json names every service with its option, host units and the runtime the configuration wants, and no state";
ok =
let
host = (migrateOf withNode).services;
cluster = (migrateOf onCluster).services;
in
(migrateOf withNode).namespace == "hyperhive"
&& host.swarm-controller.state.hostPath == "/var/lib/swarm-controller"
# The queue's store is mounted in place, so nothing is copied.
&& host.swarm-nats.state == null
&& host.swarm-ci-metrics.state == null
&& host.swarm-stt.state == null
&&
lib.sort lib.lessThan (lib.attrNames host) == [
"swarm-ci-metrics"
"swarm-claude-login"
"swarm-controller"
"swarm-nats"
"swarm-notify-router"
"swarm-review-classifier"
"swarm-stt"
]
&& lib.all (s: !(s ? state)) (lib.attrValues host)
&& lib.all (n: lib.hasPrefix "services.hyperhive.deploy." host.${n}.option) (lib.attrNames host)
&&
host.swarm-ci-metrics.hostUnits == [
"swarm-ci-metrics.timer"
@ -1945,35 +1857,7 @@ let
&& host.swarm-ci-metrics.option == "services.hyperhive.deploy.ci-metrics.runtime"
&& lib.all (s: s.runtime == "host") (lib.attrValues host)
# The runtime follows the option: the cluster fixture has every one on k8s.
&& lib.all (s: s.runtime == "k8s") (lib.attrValues cluster)
&& cluster.swarm-controller.state == host.swarm-controller.state;
}
{
# A pure function, so the case needs no host of its own. A path inside the
# directory is the presence control; a path elsewhere, a null and the
# directory itself are left out.
name = "the controller's bao identity is excluded from its copy only where it sits in the state directory";
ok =
let
within = import ../host-modules/lib/paths-within.nix { inherit lib; };
dir = "/var/lib/swarm-controller";
in
within dir [
"/var/lib/swarm-controller/bao-client.pem"
"/var/lib/swarm-controller/sub/key.pem"
] == [
"bao-client.pem"
"sub/key.pem"
]
&&
within dir [
"/var/lib/swarm-bao-pki/controller.pem"
null
dir
"/var/lib/swarm-controller-tls/x.pem"
] == [ ]
# The module's own default puts them outside it.
&& (migrateOf withNode).services.swarm-controller.state.excludes == [ ];
&& lib.all (s: s.runtime == "k8s") (lib.attrValues cluster);
}
{
name = "swarmctl is wrapped with kubectl and the cluster's kubeconfig, by one wrapper per host";