a pod with a state volume starts only on a migrated or fresh volume
This commit is contained in:
parent
7a7491a85a
commit
7e15cd492b
1 changed files with 175 additions and 46 deletions
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