Watch
0
0
Fork
You've already forked hyperhive
0

a pod with a state volume starts only on a migrated or fresh volume

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

View file

@ -128,6 +128,22 @@ let
];
};
# The state gate: one image and script for every pod that keeps state in a
# volume (./state-gate.nix).
stateGateBin = "hyperhive-state-gate";
stateGateImage = mkServiceImage {
name = "state-gate";
package = import ./state-gate.nix { inherit pkgs lib; };
bin = stateGateBin;
};
# What a service's state gate reads of the install: whether its
# `k8s.freshState` holds, and the option's path, which the refusal names.
freshOf = name: cfg: {
enabled = cfg.k8s.freshState;
option = "services.hyperhive.deploy.${name}.k8s.freshState";
};
# The swarm services that run in the cluster. `unit` is the evaluated
# systemd unit the host runs today; nothing reads it unless `enable` holds.
# `healthEnv` names the variable that binds the daemon's /healthz and
@ -136,6 +152,9 @@ let
# the paths the unit names that hold one to theirs. `listener`, where a
# service has one, is the TCP port the pod publishes on the node
# (`hostAddress`) and the directory its init container writes the leaf to.
# `fresh` (`freshOf`) is set on every service whose pod gets a state volume:
# the volume's first init container, the state gate, refuses to start the pod
# on one that holds no migrated state unless the install is fresh.
# `switch` is null for a service that always runs on the host, and for one
# with a `runtime` option names the option's value and the host unit or units
# that run the service when it is `"host"`. `otlp` is where the pod pushes
@ -148,7 +167,8 @@ let
# 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
# images, and `state`, the `volume` and mount `path` of a state volume the
# pod makes itself, which the state gate checks. An `otlp` of `"none"` is a daemon that pushes no telemetry of its
# own.
services = {
swarm-controller = {
@ -171,6 +191,7 @@ let
# The unit pushes to the swarm collector's route and mints its own bearer
# (../glue-controller-env.nix, `otel_http_client.rs`).
otlp = "own";
fresh = freshOf "swarm-controller" controllerCfg;
switch = {
inherit (controllerCfg) runtime;
hostUnit = "swarm-controller.service";
@ -190,6 +211,7 @@ let
etcFiles = [ ];
publicCredentials."bao-ca.pem" = baoServerCaKey;
otlp = "none";
fresh = freshOf "nats" natsCfg;
switch = {
inherit (natsCfg) runtime;
hostUnit = "container@swarm-nats.service";
@ -216,6 +238,7 @@ let
etcFiles = [ ];
publicCredentials.stt-bao-server-ca = baoServerCaKey;
otlp = "hive";
fresh = freshOf "stt" deployCfg.stt;
switch = {
inherit (deployCfg.stt) runtime;
hostUnit = "container@swarm-stt.service";
@ -256,6 +279,7 @@ let
etcFiles = [ ];
publicCredentials."bao-ca.pem" = baoServerCaKey;
otlp = "own";
fresh = freshOf "ci-metrics" deployCfg.ci-metrics;
# The timer and the service both: the prune unit holds back either, and a
# service started by hand cannot run beside the CronJob.
switch = {
@ -748,6 +772,45 @@ let
package = leafImagePackage;
bin = leafIssueBin;
};
# The volume that holds the service's state, and where it is mounted. A
# pod that makes its own volume names it in `state`.
stateVolume =
extra.state or (
if stateDir != null then
{
volume = "state";
path = stateDir;
}
else
null
);
stateGate = lib.optionalAttrs (stateVolume != null) {
name = "state-gate";
image = "${stateGateImage.imageName}:${stateGateImage.imageTag}";
imagePullPolicy = "Never";
command = [ "/bin/${stateGateBin}" ];
env =
lib.mapAttrsToList
(n: v: {
name = n;
value = v;
})
{
STATE_GATE_DIR = stateVolume.path;
STATE_GATE_SERVICE = name;
STATE_GATE_FRESH = lib.boolToString svc.fresh.enabled;
STATE_GATE_OPTION = svc.fresh.option;
};
volumeMounts = [
{
name = stateVolume.volume;
mountPath = stateVolume.path;
}
];
inherit (container) securityContext;
};
# What the service adds to the daemon's pod.
extra = {
containers = [ ];
@ -825,8 +888,11 @@ let
};
}
// lib.optionalAttrs baoLogin { serviceAccountName = name; }
// lib.optionalAttrs (listener != { } || extra.initContainers != [ ]) {
initContainers = lib.optional (listener != { }) leafContainer ++ extra.initContainers;
// lib.optionalAttrs (stateVolume != null || listener != { } || extra.initContainers != [ ]) {
initContainers =
lib.optional (stateVolume != null) stateGate
++ lib.optional (listener != { }) leafContainer
++ extra.initContainers;
}
// lib.optionalAttrs (extra.hostAliases != [ ]) { inherit (extra) hostAliases; }
// extra.podSpec;
@ -934,6 +1000,8 @@ let
fail name "the unit names no OTLP endpoint of its own, which the pod would push to"
else if svc.healthEnv != null && unitEnv ? ${svc.healthEnv} then
fail name "the unit already sets ${svc.healthEnv}"
else if stateVolume != null && !(svc ? fresh) then
fail name "the pod has a state volume and the service names no `fresh` option for its state gate"
else if lib.head execWords != entrypoint then
fail name "ExecStart is not ${entrypoint} with plain arguments"
else if lib.any (w: lib.hasInfix "\"" w) execWords then
@ -946,7 +1014,10 @@ let
{
inherit objects image;
# What the pod's other containers run.
extraImages = lib.optional (listener != { }) leafImage ++ extra.images;
extraImages =
lib.optional (stateVolume != null) stateGateImage
++ lib.optional (listener != { }) leafImage
++ extra.images;
};
enabled = lib.mapAttrs workload (lib.filterAttrs (_: svc: svc.enable) services);
@ -966,7 +1037,7 @@ in
# The image each enabled service's pod runs: what a node has to hold,
# since there is no registry to pull from.
images = lib.concatMap (w: [ w.image ] ++ w.extraImages) (lib.attrValues enabled);
images = lib.unique (lib.concatMap (w: [ w.image ] ++ w.extraImages) (lib.attrValues enabled));
# What ./runtime.nix moves services with. `switches` holds the services that
# have a runtime option and is cheap to read: it names the units that a