a pod with a state volume starts only on a migrated or fresh volume
This commit is contained in:
parent
7a7491a85a
commit
7e15cd492b
2 changed files with 175 additions and 46 deletions
|
|
@ -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
|
||||
|
|
|
|||
47
nix/host-modules/k8s/state-gate.nix
Normal file
47
nix/host-modules/k8s/state-gate.nix
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
# The state gate: the first init container of every pod that keeps state in a
|
||||
# volume. It reads `STATE_GATE_DIR` (the volume's mount), `STATE_GATE_SERVICE`,
|
||||
# `STATE_GATE_FRESH` (`true` or `false`) and `STATE_GATE_OPTION` (the option
|
||||
# that sets the fresh install). It exits 0 only with `.hyperhive-migrated` in
|
||||
# the volume, or on an empty volume where the install is fresh, which gets the
|
||||
# marker. Any other volume stops the pod in `Init:Error`, with the reason in
|
||||
# the container's termination message.
|
||||
{ pkgs, lib }:
|
||||
pkgs.writeShellScriptBin "hyperhive-state-gate" ''
|
||||
export PATH=${lib.makeBinPath [ pkgs.coreutils ]}
|
||||
set -eu
|
||||
dir=''${STATE_GATE_DIR:?}
|
||||
svc=''${STATE_GATE_SERVICE:?}
|
||||
fresh=''${STATE_GATE_FRESH:?}
|
||||
option=''${STATE_GATE_OPTION:?}
|
||||
marker="$dir/.hyperhive-migrated"
|
||||
|
||||
refuse() {
|
||||
echo "$1" >&2
|
||||
echo "$1" > /dev/termination-log 2>/dev/null || true
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [ -e "$marker" ]; then
|
||||
echo "$svc: state volume is migrated"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
empty=true
|
||||
for f in "$dir"/* "$dir"/.[!.]* "$dir"/..?*; do
|
||||
[ -e "$f" ] || [ -L "$f" ] || continue
|
||||
case "''${f##*/}" in
|
||||
lost+found | .hyperhive-migrated.new) ;;
|
||||
*) empty=false ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$empty" = false ]; then
|
||||
refuse "$svc: state volume has not been migrated: it holds files and no .hyperhive-migrated marker, so a copy may have been cut off: run \`swarmctl migrate $svc\` again"
|
||||
fi
|
||||
if [ "$fresh" != true ]; then
|
||||
refuse "$svc: state volume has not been migrated: run \`swarmctl migrate $svc\`, or set $option for a fresh install"
|
||||
fi
|
||||
printf '{"fresh": true, "at": "%s"}\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$marker.new"
|
||||
mv "$marker.new" "$marker"
|
||||
echo "$svc: state volume is empty and the install is fresh: marked"
|
||||
''
|
||||
Loading…
Reference in a new issue