k8s: the queue's pod mounts the host unit's store in place
This commit is contained in:
parent
a793df9708
commit
140ee7f38a
4 changed files with 267 additions and 155 deletions
|
|
@ -85,6 +85,10 @@ let
|
|||
in
|
||||
{
|
||||
imports = [
|
||||
(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.
|
||||
'')
|
||||
(lib.mkRemovedOptionModule [ "services" "hyperhive" "deploy" "stt" "k8s" "freshState" ] ''
|
||||
The pod keeps its transcript cache in an emptyDir, so it starts on any
|
||||
node with no state to migrate. Remove this definition.
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ let
|
|||
deployCfg = config.services.hyperhive.deploy;
|
||||
node = deployCfg.k8s;
|
||||
controllerCfg = deployCfg.swarm-controller;
|
||||
natsCfg = deployCfg.nats;
|
||||
|
||||
# The path of a unit's state directory in a pod, which ./render.nix mounts
|
||||
# the claim at.
|
||||
|
|
@ -31,11 +30,11 @@ let
|
|||
controllerStateDir = "/var/lib/swarm-controller";
|
||||
|
||||
# `option` is the service's name under `services.hyperhive.deploy`. `state`
|
||||
# is null for a service whose pod keeps nothing worth moving: no volume, an
|
||||
# `emptyDir`, or a volume that starts empty with no gate.
|
||||
# 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 and ../swarm-nats.nix name
|
||||
# it.
|
||||
# The claim is `<service>-state`, as ./render.nix names it.
|
||||
stateTable = {
|
||||
swarm-controller = {
|
||||
option = "swarm-controller";
|
||||
|
|
@ -52,11 +51,7 @@ let
|
|||
};
|
||||
swarm-nats = {
|
||||
option = "nats";
|
||||
state = {
|
||||
mount = natsCfg.storeDir;
|
||||
hostPath = "/var/lib/nixos-containers/swarm-nats${natsCfg.storeDir}";
|
||||
excludes = [ ];
|
||||
};
|
||||
state = null;
|
||||
};
|
||||
swarm-notify-router = {
|
||||
option = "notify-router";
|
||||
|
|
|
|||
|
|
@ -169,8 +169,7 @@ 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, 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
|
||||
# and images. An `otlp` of `"none"` is a daemon that pushes no telemetry of its
|
||||
# own.
|
||||
services = {
|
||||
swarm-controller = {
|
||||
|
|
@ -213,7 +212,6 @@ let
|
|||
etcFiles = [ ];
|
||||
publicCredentials."bao-ca.pem" = baoServerCaKey;
|
||||
otlp = "none";
|
||||
fresh = freshOf "nats" natsCfg;
|
||||
switch = {
|
||||
inherit (natsCfg) runtime;
|
||||
hostUnit = "container@swarm-nats.service";
|
||||
|
|
@ -779,21 +777,15 @@ let
|
|||
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. A pod that makes its
|
||||
# own volume names it in `state`.
|
||||
# service with a `fresh` option, none for the rest.
|
||||
stateVolume =
|
||||
if !(svc ? fresh) then
|
||||
null
|
||||
if svc ? fresh && stateDir != null then
|
||||
{
|
||||
volume = "state";
|
||||
path = stateDir;
|
||||
}
|
||||
else
|
||||
extra.state or (
|
||||
if stateDir != null then
|
||||
{
|
||||
volume = "state";
|
||||
path = stateDir;
|
||||
}
|
||||
else
|
||||
null
|
||||
);
|
||||
null;
|
||||
|
||||
stateGate = lib.optionalAttrs (stateVolume != null) {
|
||||
name = "state-gate";
|
||||
|
|
|
|||
|
|
@ -237,6 +237,9 @@ let
|
|||
# shell variable `renderConfig` reads its value into.
|
||||
calloutSubstitutions = map (k: ''-e "s|${k.placeholder}|${"$"}${k.var}|g"'') calloutPublicKeys;
|
||||
|
||||
# Hands the store directory back to the server's user.
|
||||
chownStore = nats: "${pkgs.coreutils}/bin/chown -R ${nats.user}:${nats.group} ${nats.dataDir}";
|
||||
|
||||
# Renders the server's config as it starts, so the settings it includes are
|
||||
# always the running system's. Exits non-zero, and so stops the server
|
||||
# starting, while any of the three public keys has not been fetched: the
|
||||
|
|
@ -550,7 +553,14 @@ let
|
|||
# plain override conflicts rather than wins.
|
||||
systemd.services.nats.serviceConfig.ExecStart =
|
||||
lib.mkForce "${pkgs.nats-server}/bin/nats-server -c ${runtimeWrapper}";
|
||||
systemd.services.nats.serviceConfig.ExecStartPre = "+${renderConfig}";
|
||||
# The store directory is also mounted into the pod at the same path (a
|
||||
# local volume, `pod` below), whose process runs as another uid and
|
||||
# leaves its files owned by it. `+` since the unit's own sandbox and user
|
||||
# cannot chown them back.
|
||||
systemd.services.nats.serviceConfig.ExecStartPre = [
|
||||
"+${renderConfig}"
|
||||
"+${chownStore container.config.services.nats}"
|
||||
];
|
||||
|
||||
# The auth-callout responder: the half that lets the server
|
||||
# above say *yes*. Without it the `auth_callout` block is a
|
||||
|
|
@ -764,15 +774,19 @@ let
|
|||
# configuration evaluated alone.
|
||||
natsSystem = if onK8s then podSystem else config.containers.${machine}.config;
|
||||
|
||||
# The JetStream store's directory inside the container and in the pod, and the
|
||||
# container's copy of it on the host.
|
||||
dataDir = natsSystem.services.nats.settings.jetstream.store_dir;
|
||||
hostStoreDir = "/var/lib/nixos-containers/${machine}${dataDir}";
|
||||
|
||||
# What the queue's pod holds besides the responder's container
|
||||
# (../k8s/render.nix gives the responder's own): the server, its exporter and
|
||||
# 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 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.
|
||||
# The server starts only once its files exist: 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,
|
||||
|
|
@ -796,8 +810,8 @@ let
|
|||
field: role:
|
||||
"bao write -field=${field} auth/${podAuth.mount}/login role=${role} jwt=@${podAuth.tokenFile}";
|
||||
|
||||
dataDir = natsSystem.services.nats.settings.jetstream.store_dir;
|
||||
stateClaim = "${machine}-state";
|
||||
stateClaim = "${machine}-store";
|
||||
stateClass = "${machine}-store";
|
||||
|
||||
leaf = pkgs.writeShellScriptBin "swarm-nats-tls-issue" ''
|
||||
export PATH=${lib.makeBinPath tools}
|
||||
|
|
@ -939,12 +953,6 @@ 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;
|
||||
|
|
@ -1034,12 +1042,43 @@ let
|
|||
}
|
||||
];
|
||||
|
||||
# The JetStream store is the host unit's own directory, so a switch moves
|
||||
# the process and not the bytes. A claim bound by name to a static local
|
||||
# volume: the class names no provisioner, so nothing else binds either.
|
||||
# `Retain` keeps the directory when the claim goes.
|
||||
objects."${machine}/persistentvolume" = {
|
||||
apiVersion = "v1";
|
||||
kind = "PersistentVolume";
|
||||
# Cluster-scoped: no namespace.
|
||||
metadata = removeAttrs (metaFor stateClaim) [ "namespace" ];
|
||||
spec = {
|
||||
capacity.storage = "10Gi";
|
||||
accessModes = [ "ReadWriteOnce" ];
|
||||
persistentVolumeReclaimPolicy = "Retain";
|
||||
storageClassName = stateClass;
|
||||
local.path = hostStoreDir;
|
||||
nodeAffinity.required.nodeSelectorTerms = [
|
||||
{
|
||||
matchExpressions = [
|
||||
{
|
||||
key = "kubernetes.io/hostname";
|
||||
operator = "In";
|
||||
values = [ (lib.toLower config.networking.hostName) ];
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
objects."${machine}/persistentvolumeclaim" = {
|
||||
apiVersion = "v1";
|
||||
kind = "PersistentVolumeClaim";
|
||||
metadata = metaFor stateClaim;
|
||||
spec = {
|
||||
accessModes = [ "ReadWriteOnce" ];
|
||||
storageClassName = stateClass;
|
||||
volumeName = stateClaim;
|
||||
resources.requests.storage = "10Gi";
|
||||
};
|
||||
};
|
||||
|
|
@ -1253,25 +1292,13 @@ in
|
|||
moves it: the container goes and the pod is applied, or the pod is
|
||||
deleted and the container returns.
|
||||
|
||||
The pod's JetStream store is its own volume, which starts empty, and
|
||||
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.freshState = import ./lib/fresh-state-option.nix { inherit lib; } "nats";
|
||||
|
||||
storeDir = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = natsSystem.services.nats.settings.jetstream.store_dir;
|
||||
description = ''
|
||||
The JetStream store's directory inside the container, and the path the
|
||||
pod mounts its volume at. The host's copy of it is this path under
|
||||
`/var/lib/nixos-containers/swarm-nats`, which
|
||||
../host-modules/k8s/migrate.nix names for `swarmctl migrate`.
|
||||
The pod's JetStream store is the container's own directory on the host,
|
||||
mounted into the pod as a static local volume, so a switch moves the
|
||||
process and not the bytes: streams, consumers and key-value buckets are
|
||||
the same files on both sides. The container's unit hands the directory
|
||||
back to the server's user at each start, and the pod's group gets read
|
||||
and write access from the pod's `fsGroup`. At most one server holds the
|
||||
directory, since a switch stops the one before it starts the other.
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
@ -1542,7 +1569,13 @@ in
|
|||
# The bind source has to exist before the container starts, leaf or not:
|
||||
# nixos-container refuses to start over a missing one, which would take
|
||||
# the whole queue down rather than only its TLS.
|
||||
systemd.tmpfiles.rules = [ "d ${tlsDir} 0755 root root -" ];
|
||||
#
|
||||
# The pod's local volume needs its directory to exist before the pod
|
||||
# starts, which on a host that never ran the container nothing else makes.
|
||||
systemd.tmpfiles.rules = [
|
||||
"d ${tlsDir} 0755 root root -"
|
||||
]
|
||||
++ lib.optional onK8s "d ${hostStoreDir} 0700 root root -";
|
||||
|
||||
containers.swarm-nats = lib.mkIf (!onK8s) {
|
||||
autoStart = true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue