Watch
0
0
Fork
You've already forked hyperhive
0

module-eval: the state gate is every stateful pod's first init container

This commit is contained in:
atlas 2026-11-22 18:59:22 +01:00 • committed by maia
commit 10c22d9ffa

View file

@ -69,7 +69,6 @@ let
deploy.claude-login.runtime = "k8s"; deploy.claude-login.runtime = "k8s";
deploy.swarm-controller.runtime = "k8s"; deploy.swarm-controller.runtime = "k8s";
deploy.nats.runtime = "k8s"; deploy.nats.runtime = "k8s";
deploy.nats.k8s.freshStore = true;
} }
); );
@ -109,6 +108,19 @@ let
inherit lib pkgs config; inherit lib pkgs config;
}).objects; }).objects;
objects = render allLocal; 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 = {
nats.k8s.freshState = true;
stt.k8s.freshState = true;
ci-metrics.k8s.freshState = true;
swarm-controller.k8s.freshState = true;
};
};
# The queue's pod reads the settings of the system it runs from, which differ # 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. # 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)); notQueue = lib.filterAttrs (n: _: !(lib.hasPrefix "swarm-nats/" n));
@ -354,18 +366,19 @@ let
} }
{ {
# The images are the ones the manifests run, one per rendered service, the # The images are the ones the manifests run, one per rendered service, the
# controller's listener leaf and the queue's three more (server, exporter, # controller's listener leaf, the queue's three more (server, exporter,
# init): the eleven are the control that the list is not empty. # init) and the one state gate every stateful pod shares: the twelve 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"; name = "the node loads the image of every service the manifests run, and k3s's own";
ok = ok =
let let
loaded = map toString withNode.services.k3s.images; loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages; rendered = map toString withNode.system.build.k8sImages;
in in
lib.length rendered == 11 lib.length rendered == 12
&& lib.all (i: lib.elem i loaded) rendered && lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded && lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 12; && lib.length loaded == 13;
} }
{ {
# Nothing the host already runs differs but the store's side of the pods' # Nothing the host already runs differs but the store's side of the pods'
@ -1493,7 +1506,7 @@ let
&& annotationOf rolled != annotationOf onCluster && annotationOf rolled != annotationOf onCluster
# A pod with no ConfigMap carries no annotation: the router is the control. # A pod with no ConfigMap carries no annotation: the router is the control.
&& !((cluster."swarm-notify-router/deployment".spec.template.metadata) ? annotations) && !((cluster."swarm-notify-router/deployment".spec.template.metadata) ? annotations)
&& (podSpec "swarm-controller" ? initContainers) == false; && map (c: c.name) (podSpec "swarm-controller").initContainers == [ "state-gate" ];
} }
{ {
# The pod issues its own listener leaf with the unit's script, logging in # The pod issues its own listener leaf with the unit's script, logging in
@ -1514,7 +1527,7 @@ let
createDir = false; createDir = false;
}; };
spec = (render onCluster)."swarm-controller/deployment".spec.template.spec; spec = (render onCluster)."swarm-controller/deployment".spec.template.spec;
init = lib.head spec.initContainers; init = lib.elemAt spec.initContainers 1;
renew = lib.last spec.containers; renew = lib.last spec.containers;
main = lib.head spec.containers; main = lib.head spec.containers;
mountsOf = x: map (m: m.name) x.volumeMounts; mountsOf = x: map (m: m.name) x.volumeMounts;
@ -1533,7 +1546,11 @@ let
&& !(lib.hasInfix "chgrp" podScript) && !(lib.hasInfix "chgrp" podScript)
&& lib.hasInfix "-checkhost \"$name\"" podScript && lib.hasInfix "-checkhost \"$name\"" podScript
&& lib.hasInfix "issue/swarm-controller-tls" podScript && lib.hasInfix "issue/swarm-controller-tls" podScript
&& lib.length spec.initContainers == 1 &&
map (c: c.name) spec.initContainers == [
"state-gate"
"tls-issue"
]
&& init.name == "tls-issue" && init.name == "tls-issue"
&& renew.name == "tls-renew" && renew.name == "tls-renew"
&& renew.command == [ "/bin/swarm-controller-tls-renew" ] && renew.command == [ "/bin/swarm-controller-tls-renew" ]
@ -1577,7 +1594,7 @@ let
# Everything of the pod is read off the manifest the module renders. The # Everything of the pod is read off the manifest the module renders. The
# `tmp` volume is the control that the lookup of a volume's medium finds # `tmp` volume is the control that the lookup of a volume's medium finds
# a plain one. # a plain one.
name = "the queue's pod is one replaced copy of the responder, the server and the exporter, started after the store guard, the leaf and the callout config"; name = "the queue's pod is one replaced copy of the responder, the server and the exporter, started after the state gate, the leaf and the callout config";
ok = ok =
let let
o = objects."swarm-nats/deployment"; o = objects."swarm-nats/deployment";
@ -1606,7 +1623,7 @@ let
) )
&& &&
map (c: c.name) spec.initContainers == [ map (c: c.name) spec.initContainers == [
"store-guard" "state-gate"
"tls-issue" "tls-issue"
"callout-config" "callout-config"
] ]
@ -1672,36 +1689,88 @@ let
&& !(lib.any (v: v ? secret) spec.volumes); && !(lib.any (v: v ? secret) spec.volumes);
} }
{ {
# The default holds the server back on an empty store; a fresh install # The gate is the pod's first init container wherever the pod has a
# opts in. Each arm's value is the control for the other. # state volume, ahead of the TLS leaf. The default holds the pod back; a
name = "the store guard reads the volume the server writes, and lets an empty store through only where the install is fresh"; # fresh install opts in. Each arm's value is the control for the other,
# and the list of gated services is the control for the services with no
# volume.
name = "every pod with a state volume starts with the state gate on that volume, and the fresh option sets what it reads";
ok = ok =
let let
guard = c: lib.findFirst (i: i.name == "store-guard") { } (podSpec "swarm-nats").initContainers; fresh = render (freshInstall onCluster);
fresh = render onCluster; specOf =
freshGuard = lib.findFirst (i: i.name == "store-guard") { } ( objs: name:
fresh."swarm-nats/deployment".spec.template.spec.initContainers let
); o = objs."${name}/deployment" or objs."${name}/cronjob";
freshEnv = g: (lib.findFirst (e: e.name == "SWARM_NATS_FRESH_STORE") { } g.env).value; 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";
};
swarm-stt = {
path = "/var/lib/swarm-stt";
volume = "state";
option = "deploy.stt.k8s.freshState";
};
swarm-ci-metrics = {
path = "/var/lib/swarm-ci-metrics";
volume = "state";
option = "deploy.ci-metrics.k8s.freshState";
};
swarm-nats = {
path = "/var/lib/nats";
volume = "nats-data";
option = "deploy.nats.k8s.freshState";
};
};
ungated = [
"swarm-notify-router"
"swarm-review-classifier"
"swarm-claude-login"
];
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 in
freshEnv (guard null) == "false" lib.all (n: one n gated.${n}) (lib.attrNames gated)
&& freshEnv freshGuard == "true" && lib.all (n: gate objects n == null && gate fresh n == null) ungated
&& && lib.all (n: !(objects ? "${n}/persistentvolumeclaim")) ungated
(guard null).volumeMounts == [ && lib.all (n: objects ? "${n}/persistentvolumeclaim") (lib.attrNames gated)
{ # One image serves every gate.
name = "tmp"; && lib.length (lib.unique (map (n: (gate objects n).image) (lib.attrNames gated))) == 1;
mountPath = "/tmp";
}
{
name = "nats-data";
mountPath = "/var/lib/nats";
readOnly = true;
}
]
# The server mounts the same volume, writable, at the same path.
&&
lib.any (m: m.name == "nats-data" && m.mountPath == "/var/lib/nats" && !(m.readOnly or false))
(lib.findFirst (c: c.name == "nats") { } (podSpec "swarm-nats").containers).volumeMounts;
} }
{ {
# The responder's unit is the container's on the host and the # The responder's unit is the container's on the host and the