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 mara
commit 7e15cd492b

View file

@ -769,9 +769,10 @@ let
# the init containers that make the server's files, from the units and
# settings the container runs on the host.
#
# The server starts only once its files exist. The store guard checks the
# JetStream volume, then the leaf is issued and the callout's keys and
# config written, each into a volume of its own that the server reads.
# The server starts only once its files exist. The state gate
# (../k8s/render.nix) checks the JetStream volume first, then the leaf is
# issued and the callout's keys and config written, each into a volume of its
# own that the server reads.
pod =
{
env,
@ -798,22 +799,6 @@ let
dataDir = natsSystem.services.nats.settings.jetstream.store_dir;
stateClaim = "${machine}-state";
guard = pkgs.writeShellScriptBin "swarm-nats-store-guard" ''
export PATH=${lib.makeBinPath [ pkgs.coreutils ]}
set -eu
store=${lib.escapeShellArg dataDir}
if [ -d "$store/jetstream" ] && [ -n "$(ls -A "$store/jetstream")" ]; then
echo "the JetStream store at $store holds data"
exit 0
fi
if [ "''${SWARM_NATS_FRESH_STORE:-false}" = true ]; then
echo "the JetStream store at $store is empty and marked fresh: the server starts on it"
exit 0
fi
echo "empty JetStream store at $store: run \`swarmctl migrate\` first, or set services.hyperhive.deploy.nats.k8s.freshStore for a fresh install" >&2
exit 1
'';
leaf = pkgs.writeShellScriptBin "swarm-nats-tls-issue" ''
export PATH=${lib.makeBinPath tools}
${tlsIssue.script {
@ -877,13 +862,12 @@ let
package = pkgs.symlinkJoin {
name = "${machine}-init";
paths = [
guard
leaf
keys
renew
];
};
bin = "swarm-nats-store-guard";
bin = "swarm-nats-tls-issue";
};
# `renderConfig` links upstream's rendered settings into the config
@ -955,6 +939,12 @@ let
}
];
# The JetStream volume, which the state gate checks before anything else.
state = {
volume = "nats-data";
path = dataDir;
};
# The server and the responder dial the queue by its name, which the pod
# answers with its own loopback: the leaf carries the name and no IP.
podSpec.shareProcessNamespace = true;
@ -967,9 +957,6 @@ let
];
initContainers = [
(fromInit "store-guard" "swarm-nats-store-guard" {
SWARM_NATS_FRESH_STORE = lib.boolToString deployCfg.nats.k8s.freshStore;
} [ "tmp" ] [ (volumeNamed "nats-data" dataDir true) ])
(fromInit "tls-issue" "swarm-nats-tls-issue" storeEnv login [
(volumeNamed "nats-tls" tlsDir false)
])
@ -1267,24 +1254,13 @@ in
deleted and the container returns.
The pod's JetStream store is its own volume, which starts empty, and
the pod refuses to start the server on an empty store unless
{option}`services.hyperhive.deploy.nats.k8s.freshStore` is set.
the pod refuses to start until the volume holds the migration marker
or {option}`services.hyperhive.deploy.nats.k8s.freshState` is set.
Nothing here copies the container's store into it.
'';
};
k8s.freshStore = lib.mkOption {
type = lib.types.bool;
default = false;
example = true;
description = ''
With {option}`services.hyperhive.deploy.nats.runtime` set to `k8s`:
let the pod start the server on an empty JetStream store. Off, the pod
stops before the server and says so: an empty store loses every
hive's declared desired state, which nothing regenerates. Set it on a
fresh install, which has none to lose.
'';
};
k8s.freshState = import ./lib/fresh-state-option.nix { inherit lib; } "nats";
unit = lib.mkOption {
type = lib.types.raw;