k8s: the controller pod keeps no volume; the state gate goes
This commit is contained in:
parent
b69f8c1de0
commit
513be8f7d0
7 changed files with 233 additions and 1110 deletions
|
|
@ -85,6 +85,12 @@ let
|
|||
in
|
||||
{
|
||||
imports = [
|
||||
(lib.mkRemovedOptionModule [ "services" "hyperhive" "deploy" "swarm-controller" "k8s" "freshState" ]
|
||||
''
|
||||
The pod keeps its state directory in an emptyDir, so it starts on any
|
||||
node with no state to migrate. Remove this definition.
|
||||
''
|
||||
)
|
||||
(lib.mkRemovedOptionModule [ "services" "hyperhive" "deploy" "nats" "k8s" "freshState" ] ''
|
||||
The pod mounts the host unit's own store directory, so there is no empty
|
||||
volume to start on and no state gate. Remove this definition.
|
||||
|
|
|
|||
|
|
@ -4,11 +4,8 @@
|
|||
#
|
||||
# The file has one entry per service that has a `runtime` option: the
|
||||
# option's path, the runtime the configuration wants, the host units that run
|
||||
# the service when it is on the host, and, where the service keeps state in a
|
||||
# volume, that state: the claim, the mount in the pod, where the data lives
|
||||
# on the host, and what a copy leaves out. Paths are computed here, never
|
||||
# derived by `swarmctl`. Both exist only on a host with the node
|
||||
# (./node.nix): without a cluster there is nothing to read.
|
||||
# the service when it is on the host. The file and the wrapper exist only on a
|
||||
# host with the node (./node.nix): without a cluster there is nothing to read.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
|
|
@ -21,71 +18,22 @@ let
|
|||
node = deployCfg.k8s;
|
||||
controllerCfg = deployCfg.swarm-controller;
|
||||
|
||||
# The path of a unit's state directory in a pod, which ./render.nix mounts
|
||||
# the claim at.
|
||||
stateMount = unit: "/var/lib/${unit.serviceConfig.StateDirectory}";
|
||||
|
||||
pathsWithin = import ../lib/paths-within.nix { inherit lib; };
|
||||
|
||||
controllerStateDir = "/var/lib/swarm-controller";
|
||||
|
||||
# `option` is the service's name under `services.hyperhive.deploy`. `state`
|
||||
# is null for a service with nothing to copy: no volume, an `emptyDir`, a
|
||||
# volume that starts empty with no gate, or (the queue) the host unit's own
|
||||
# directory, mounted into the pod.
|
||||
#
|
||||
# The claim is `<service>-state`, as ./render.nix names it.
|
||||
stateTable = {
|
||||
swarm-controller = {
|
||||
option = "swarm-controller";
|
||||
state = {
|
||||
mount = stateMount controllerCfg.unit;
|
||||
hostPath = controllerStateDir;
|
||||
# The pod issues its own leaf with its own store login, so a bao
|
||||
# identity an operator keeps in this directory stays behind.
|
||||
excludes = pathsWithin controllerStateDir [
|
||||
controllerCfg.baoClientCertFile
|
||||
controllerCfg.baoClientKeyFile
|
||||
];
|
||||
};
|
||||
};
|
||||
swarm-nats = {
|
||||
option = "nats";
|
||||
state = null;
|
||||
};
|
||||
swarm-notify-router = {
|
||||
option = "notify-router";
|
||||
state = null;
|
||||
};
|
||||
swarm-stt = {
|
||||
option = "stt";
|
||||
state = null;
|
||||
};
|
||||
swarm-review-classifier = {
|
||||
option = "review-classifier";
|
||||
state = null;
|
||||
};
|
||||
swarm-claude-login = {
|
||||
option = "claude-login";
|
||||
state = null;
|
||||
};
|
||||
swarm-ci-metrics = {
|
||||
option = "ci-metrics";
|
||||
state = null;
|
||||
};
|
||||
# Each service's name under `services.hyperhive.deploy`.
|
||||
optionOf = {
|
||||
swarm-controller = "swarm-controller";
|
||||
swarm-nats = "nats";
|
||||
swarm-notify-router = "notify-router";
|
||||
swarm-stt = "stt";
|
||||
swarm-review-classifier = "review-classifier";
|
||||
swarm-claude-login = "claude-login";
|
||||
swarm-ci-metrics = "ci-metrics";
|
||||
};
|
||||
|
||||
entry =
|
||||
name: switch:
|
||||
let
|
||||
row = stateTable.${name};
|
||||
in
|
||||
{
|
||||
option = "services.hyperhive.deploy.${row.option}.runtime";
|
||||
inherit (switch) runtime;
|
||||
hostUnits = lib.toList switch.hostUnit;
|
||||
state = lib.mapNullable (s: s // { claim = "${name}-state"; }) row.state;
|
||||
};
|
||||
entry = name: switch: {
|
||||
option = "services.hyperhive.deploy.${optionOf.${name}}.runtime";
|
||||
inherit (switch) runtime;
|
||||
hostUnits = lib.toList switch.hostUnit;
|
||||
};
|
||||
|
||||
migrateJson = {
|
||||
inherit (cfg) namespace;
|
||||
|
|
|
|||
|
|
@ -128,22 +128,6 @@ 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
|
||||
|
|
@ -152,11 +136,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 a service whose state is worth moving: 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. A service
|
||||
# without it starts on whatever volume it gets, and one with
|
||||
# `ephemeralState` gets an `emptyDir` in place of a claim.
|
||||
# `ephemeralState` gives a service an `emptyDir` in place of a claim for its
|
||||
# state directory: what it keeps there is re-derived at start. A pod starts on
|
||||
# whatever volume it gets.
|
||||
# `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
|
||||
|
|
@ -192,7 +174,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;
|
||||
ephemeralState = true;
|
||||
switch = {
|
||||
inherit (controllerCfg) runtime;
|
||||
hostUnit = "swarm-controller.service";
|
||||
|
|
@ -776,43 +758,6 @@ let
|
|||
package = leafImagePackage;
|
||||
bin = leafIssueBin;
|
||||
};
|
||||
# The volume the state gate checks, and where it is mounted: that of a
|
||||
# service with a `fresh` option, none for the rest.
|
||||
stateVolume =
|
||||
if svc ? fresh && 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 = [ ];
|
||||
|
|
@ -890,11 +835,8 @@ let
|
|||
};
|
||||
}
|
||||
// lib.optionalAttrs baoLogin { serviceAccountName = name; }
|
||||
// lib.optionalAttrs (stateVolume != null || listener != { } || extra.initContainers != [ ]) {
|
||||
initContainers =
|
||||
lib.optional (stateVolume != null) stateGate
|
||||
++ lib.optional (listener != { }) leafContainer
|
||||
++ extra.initContainers;
|
||||
// lib.optionalAttrs (listener != { } || extra.initContainers != [ ]) {
|
||||
initContainers = lib.optional (listener != { }) leafContainer ++ extra.initContainers;
|
||||
}
|
||||
// lib.optionalAttrs (extra.hostAliases != [ ]) { inherit (extra) hostAliases; }
|
||||
// extra.podSpec;
|
||||
|
|
@ -1014,10 +956,7 @@ let
|
|||
{
|
||||
inherit objects image;
|
||||
# What the pod's other containers run.
|
||||
extraImages =
|
||||
lib.optional (stateVolume != null) stateGateImage
|
||||
++ lib.optional (listener != { }) leafImage
|
||||
++ extra.images;
|
||||
extraImages = lib.optional (listener != { }) leafImage ++ extra.images;
|
||||
};
|
||||
|
||||
enabled = lib.mapAttrs workload (lib.filterAttrs (_: svc: svc.enable) services);
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
# 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"
|
||||
''
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
# The `k8s.freshState` option of a service whose pod keeps state in a volume.
|
||||
# `name` is the service's key under `services.hyperhive.deploy`. ../k8s/render.nix
|
||||
# reads it into the pod's state gate.
|
||||
{ lib }:
|
||||
name:
|
||||
lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
example = true;
|
||||
description = ''
|
||||
With {option}`services.hyperhive.deploy.${name}.runtime` set to `k8s`: let
|
||||
the pod start on an empty state volume. Off, the pod's first init container
|
||||
stops it before the service starts and says so, until the volume holds
|
||||
the `.hyperhive-migrated` marker that `swarmctl migrate` writes after it
|
||||
copies the host's state in. On, an empty volume gets the marker at once
|
||||
and the service starts with no state. Set it on a fresh install, which
|
||||
has no state to lose. A volume that holds files and no marker is refused
|
||||
either way, since a copy may have been cut off.
|
||||
'';
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
# `pathsWithin dir paths`: the entries of `paths` that sit under `dir`, relative
|
||||
# to it. A null entry, and a path elsewhere (or `dir` itself), is left out.
|
||||
{ lib }:
|
||||
dir: paths:
|
||||
lib.concatMap (
|
||||
path:
|
||||
let
|
||||
prefix = "${dir}/";
|
||||
in
|
||||
lib.optional (path != null && lib.hasPrefix prefix path) (lib.removePrefix prefix path)
|
||||
) paths
|
||||
|
|
@ -635,17 +635,15 @@ in
|
|||
The pod needs the TCP listener
|
||||
({option}`services.hyperhive.swarm.controller.tcp.enable`): the host
|
||||
cannot reach a unix socket inside a pod, so the gateway dials the
|
||||
listener on {option}`tcp.hostAddress`. The pod's state is its own
|
||||
volume, which starts empty: nothing moves the host's state directory
|
||||
into it, and the pod does not start on it until `swarmctl migrate`
|
||||
marks it or
|
||||
{option}`services.hyperhive.deploy.swarm-controller.k8s.freshState`
|
||||
is set.
|
||||
listener on {option}`tcp.hostAddress`. The pod's state directory is an
|
||||
`emptyDir`: the operator grant record is in the queue's key-value
|
||||
store, and what is left in the directory (the forge avatar markers, the
|
||||
legacy webhook secret the store also holds) is made again at start. A
|
||||
host's legacy grant file is imported into the store by the first start
|
||||
of the controller on the host, so run it there once before the switch.
|
||||
'';
|
||||
};
|
||||
|
||||
k8s.freshState = import ./lib/fresh-state-option.nix { inherit lib; } "swarm-controller";
|
||||
|
||||
unit = lib.mkOption {
|
||||
type = lib.types.raw;
|
||||
readOnly = true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue