From a4015fd03a95fb4a46d8f51c06c85984e1fa3ccd Mon Sep 17 00:00:00 2001 From: atlas Date: Sun, 22 Nov 2026 18:59:22 +0100 Subject: [PATCH] module-eval: the state gate is every stateful pod's first init container --- nix/module-eval/k8s.nix | 143 +++++++++++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 37 deletions(-) diff --git a/nix/module-eval/k8s.nix b/nix/module-eval/k8s.nix index 1ada1b241..78a67ae09 100644 --- a/nix/module-eval/k8s.nix +++ b/nix/module-eval/k8s.nix @@ -69,7 +69,6 @@ let deploy.claude-login.runtime = "k8s"; deploy.swarm-controller.runtime = "k8s"; deploy.nats.runtime = "k8s"; - deploy.nats.k8s.freshStore = true; } ); @@ -109,6 +108,19 @@ 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 = { + 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 # 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)); @@ -354,18 +366,19 @@ let } { # 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, - # init): the eleven are the control that the list is not empty. + # 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. 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 == 11 + lib.length rendered == 12 && lib.all (i: lib.elem i loaded) rendered && 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' @@ -1493,7 +1506,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) - && (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 @@ -1514,7 +1527,7 @@ let createDir = false; }; 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; main = lib.head spec.containers; mountsOf = x: map (m: m.name) x.volumeMounts; @@ -1533,7 +1546,11 @@ let && !(lib.hasInfix "chgrp" podScript) && lib.hasInfix "-checkhost \"$name\"" 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" && renew.name == "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 # `tmp` volume is the control that the lookup of a volume's medium finds # 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 = let o = objects."swarm-nats/deployment"; @@ -1606,7 +1623,7 @@ let ) && map (c: c.name) spec.initContainers == [ - "store-guard" + "state-gate" "tls-issue" "callout-config" ] @@ -1672,36 +1689,88 @@ let && !(lib.any (v: v ? secret) spec.volumes); } { - # The default holds the server back on an empty store; a fresh install - # opts in. Each arm's value is the control for the other. - name = "the store guard reads the volume the server writes, and lets an empty store through only where the install is fresh"; + # The gate is the pod's first init container wherever the pod has a + # state volume, 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 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 = let - guard = c: lib.findFirst (i: i.name == "store-guard") { } (podSpec "swarm-nats").initContainers; - fresh = render onCluster; - freshGuard = lib.findFirst (i: i.name == "store-guard") { } ( - fresh."swarm-nats/deployment".spec.template.spec.initContainers - ); - freshEnv = g: (lib.findFirst (e: e.name == "SWARM_NATS_FRESH_STORE") { } g.env).value; + fresh = render (freshInstall onCluster); + specOf = + objs: name: + let + o = objs."${name}/deployment" or objs."${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"; + }; + 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 - freshEnv (guard null) == "false" - && freshEnv freshGuard == "true" - && - (guard null).volumeMounts == [ - { - name = "tmp"; - 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; + 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")) ungated + && lib.all (n: objects ? "${n}/persistentvolumeclaim") (lib.attrNames gated) + # One image serves every gate. + && lib.length (lib.unique (map (n: (gate objects n).image) (lib.attrNames gated))) == 1; } { # The responder's unit is the container's on the host and the