Watch
0
0
Fork
You've already forked hyperhive
0

k8s: deploy.swarm-controller.runtime moves the controller between its unit and the cluster

This commit is contained in:
atlas 2026-11-22 12:28:03 +01:00 • committed by maia
commit 452f1af675

View file

@ -14,6 +14,10 @@
# client certificate the swarm's store issued for the gateway, with the
# `clientAuth` usage. ./swarm-ui.nix still declares the vhost on the host that
# runs this daemon, so the gateway stays on that host.
#
# `deploy.swarm-controller.runtime = "k8s"` runs the daemon as a pod in this
# host's cluster instead (./k8s/render.nix renders it from the unit below). The
# unit is then disabled, and the gateway dials the pod's `hostPort`.
{
pkgs,
lib,
@ -154,6 +158,11 @@ let
storeRetry = import ./lib/store-retry.nix { };
tcpOn = deployCfg.swarm-controller.enable && tcpCfg.enable;
# Where the daemon runs: its unit on this host, or a pod in this host's
# cluster, which ./k8s/render.nix renders from the same unit.
onK8s = deployCfg.swarm-controller.runtime == "k8s";
tlsIssue = import ./lib/swarm-controller-tls-issue.nix { inherit lib config; };
# The swarm the host's `swarmctl` talks to: this host's own, through the
# gateway, as the operator's own SSO account (`swarmctl login`). The
# controller socket then serves the gateway alone, which always sets
@ -609,6 +618,42 @@ in
'';
};
runtime = lib.mkOption {
type = lib.types.enum [
"host"
"k8s"
];
default = "host";
description = ''
Where the controller runs: `host` as the `swarm-controller` systemd
unit, or `k8s` as a pod in this host's cluster
({option}`services.hyperhive.deploy.k8s.enable`), which the host's
configuration must enable. A swarm runs one controller, so a switch
moves it: the unit goes and the pod is applied, or the pod is deleted
and the unit returns.
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. `swarmctl` on the host dials the unix socket only, so its
socket verbs have no controller to reach while the pod runs.
'';
};
unit = lib.mkOption {
type = lib.types.raw;
readOnly = true;
internal = true;
default = config.systemd.services.swarm-controller;
description = ''
The controller's evaluated systemd unit, which the cluster's manifests
are rendered from (../host-modules/k8s/render.nix). The host runs it
unless {option}`runtime` is `k8s`, and then the unit is disabled.
'';
};
swarmctlPackage = lib.mkOption {
type = lib.types.package;
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarmctl";
@ -740,6 +785,26 @@ in
IPv6 address is written in brackets. Binding a port opens nothing:
{option}`openFirewall` decides who can reach it, and the TLS
handshake decides who may talk once they do.
With {option}`services.hyperhive.deploy.swarm-controller.runtime` set
to `k8s` this is the bind inside the pod, so it must stay `0.0.0.0`.
The node's address is {option}`hostAddress`.
'';
};
hostAddress = lib.mkOption {
type = lib.types.str;
default = config.services.hyperhive.network.bridgeIp;
defaultText = lib.literalExpression "config.services.hyperhive.network.bridgeIp";
description = ''
With {option}`services.hyperhive.deploy.swarm-controller.runtime` set
to `k8s`: the node address the pod's `hostPort` binds and the gateway
dials. The default is the hive bridge's address, the one the pods
already reach the host's services on. The gateway verifies the
listener's certificate as
{option}`services.hyperhive.swarm.controller.tcp.domain`, not as
this address. A `hostPort` reaches one node: a cluster of several
nodes needs a Service instead.
'';
};
@ -872,6 +937,10 @@ in
users.groups.swarm-controller = { };
users.groups.${clientsGroup} = { };
# The pod's `hostPort` is on the hive bridge's address by default, and the
# cluster's pods reach the host's services through the bridge.
services.hyperhive.network.enable = lib.mkIf onK8s (lib.mkDefault true);
# Through the unit, not `users.users.nginx.extraGroups`: systemd resolves
# a unit's groups at start, and only a changed unit file makes the switch
# restart nginx (it does not reload) onto the new group.
@ -943,6 +1012,33 @@ in
TCP listener off.
'';
}
{
assertion = !onK8s || config.services.hyperhive.deploy.k8s.enable;
message = ''
services.hyperhive.deploy.swarm-controller.runtime = "k8s" needs the
cluster: set services.hyperhive.deploy.k8s.enable on this host, or
leave the controller's runtime at "host".
'';
}
{
assertion = !onK8s || tcpHost.listenAddress == "0.0.0.0";
message = ''
services.hyperhive.deploy.swarm-controller.tcp.listenAddress is
"${tcpHost.listenAddress}", and with runtime = "k8s" it is the bind
inside the pod, where any address but "0.0.0.0" leaves the listener
unbound and the pod NotReady. Leave it at "0.0.0.0"; the node's
address is services.hyperhive.deploy.swarm-controller.tcp.hostAddress.
'';
}
{
assertion = !onK8s || tcpCfg.enable;
message = ''
services.hyperhive.deploy.swarm-controller.runtime = "k8s" needs
services.hyperhive.swarm.controller.tcp.enable: the host cannot reach
a unix socket inside a pod, so the gateway dials the pod's TCP
listener. Set it, or leave the controller's runtime at "host".
'';
}
{
assertion = !tcpCfg.enable || caTrust.useSelfSigned;
message = ''
@ -956,6 +1052,9 @@ in
];
systemd.services.swarm-controller = {
# Disabled with the controller in the cluster, so nothing can start it; the definition stays
# because the pod is rendered from the evaluated unit.
enable = !onK8s;
description = "hyperhive swarm-level controller daemon";
wantedBy = [ "multi-user.target" ];
# The listener's certificate files must exist when the daemon opens it. A
@ -1118,7 +1217,7 @@ in
# Same shape as the queue's leaf unit (./swarm-nats.nix,
# `swarm-bao-nats-tls`): the daemon re-reads the files, so rotating needs
# no restart, which for this daemon would drop its job graph.
systemd.services.swarm-controller-tls = lib.mkIf tcpOn {
systemd.services.swarm-controller-tls = lib.mkIf (tcpOn && !onK8s) {
description = "Issue the swarm-controller's TCP listener certificate from the secret store's PKI";
wantedBy = [ "multi-user.target" ];
after = [
@ -1150,63 +1249,15 @@ in
// lib.optionalAttrs (deployCfg.bao.serverCaFile != null) {
BAO_CACERT = deployCfg.bao.serverCaFile;
};
script = ''
set -euo pipefail
d=${lib.escapeShellArg tcpHost.tlsDir}
name=${lib.escapeShellArg tcpCfg.domain}
install -d -m 0750 -g swarm-controller "$d"
script = tlsIssue.script {
login = "bao login -method=cert -token-only";
credential = "certificate";
group = "swarm-controller";
};
# Missing, past half its window, or naming something other than the
# configured domain.
reissue=0
{ [ -s "$d/cert.pem" ] && [ -s "$d/key.pem" ]; } || reissue=1
openssl x509 -in "$d/cert.pem" -noout -checkend ${
toString (deployCfg.bao.servicesPkiLeafTtlHours * 3600 / 2)
} >/dev/null 2>&1 || reissue=1
openssl x509 -in "$d/cert.pem" -noout -checkhost "$name" 2>/dev/null | grep -q ' does match ' || reissue=1
if [ "$reissue" = 0 ]; then
echo "controller listener leaf valid for $name — leaving it alone"
exit 0
fi
err="$(mktemp)"
resp="$(mktemp "$d/issue.json.XXXXXX")"
trap 'rm -f "$err" "$resp"' EXIT
if ! BAO_TOKEN="$(bao login -method=cert -token-only 2>"$err")"; then
echo "could not log in to the swarm secret store with the controller's certificate." >&2
cat "$err" >&2
exit 1
fi
export BAO_TOKEN
echo "requesting the controller's listener leaf for $name from the store"
if ! bao write -format=json \
${lib.escapeShellArg "${deployCfg.bao.servicesPkiMountPath}/issue/${tcpCfg.serverPkiRole}"} \
common_name="$name" > "$resp" 2>"$err"; then
echo "the store refused to issue the controller's listener certificate." >&2
cat "$err" >&2
exit 1
fi
jq -r '.data.private_key // empty' < "$resp" > "$d/key.pem.new"
jq -r '.data.certificate // empty' < "$resp" > "$d/cert.pem.new"
for f in "$d/key.pem.new" "$d/cert.pem.new"; do
if [ ! -s "$f" ]; then
echo "the store's response was missing a field: $f is empty" >&2
exit 1
fi
done
chgrp swarm-controller "$d/key.pem.new" "$d/cert.pem.new"
chmod 0640 "$d/key.pem.new" "$d/cert.pem.new"
# Key first: the daemon pairs whatever it reads, and a new key beside
# the old certificate is a pair it refuses and retries.
mv -f "$d/key.pem.new" "$d/key.pem"
mv -f "$d/cert.pem.new" "$d/cert.pem"
'';
};
systemd.timers.swarm-controller-tls = lib.mkIf tcpOn {
systemd.timers.swarm-controller-tls = lib.mkIf (tcpOn && !onK8s) {
description = "Daily renewal check for the swarm-controller's TCP listener certificate";
wantedBy = [ "timers.target" ];
timerConfig = {