Watch
0
0
Fork
You've already forked hyperhive
0

require k8s: swarm services with a pod run in the cluster only

This commit is contained in:
atlas 2026-11-26 00:36:09 +01:00 • committed by maia
commit 2e1f037f25

View file

@ -8,16 +8,16 @@
# reaches the socket by path, and a socket that is never bound to an
# address cannot be reached from off-host by mistake.
#
# `swarm.controller.tcp.enable` adds a TLS listener beside the socket, which the
# gateway then dials instead of the socket. The controller checks nobody per
# The controller serves a TLS listener beside the socket (`swarm.controller.tcp`,
# on by default), which the gateway dials instead of the socket. The controller checks nobody per
# request, so the listener completes a handshake for one identity only: a
# 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 Service by name.
# The daemon runs as a pod in this host's cluster (./k8s/render.nix renders it
# from the unit below). The unit is disabled on the host, and the gateway dials
# the pod's Service by name.
{
pkgs,
lib,
@ -155,14 +155,8 @@ let
tcpCfg = cfg.tcp;
tcpHost = deployCfg.swarm-controller.tcp;
hiveTls = deployCfg.hive-controller.tls;
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
@ -431,34 +425,44 @@ in
# controller's host serves — three hosts that must agree on one set of
# names, so each reads them from the option every host evaluates alike.
tcp = {
enable = lib.mkEnableOption ''
the controller's TCP listener, TLS with a required client certificate.
Off: the controller serves its unix socket only, which is what the
gateway on the same host reaches it through.
enable = lib.mkOption {
type = lib.types.bool;
default = swarmDomain != null;
defaultText = lib.literalExpression "services.hyperhive.swarm.domain != null";
example = true;
description = ''
The controller's TCP listener, TLS with a required client certificate.
The controller runs as a pod, so the listener is the gateway's only way
to it: the gateway's controller locations dial {option}`domain` over TLS
and present a client leaf, and the controller accepts that one
identity. It defaults on wherever the swarm has a domain, so nothing sets
it, and a host that sets it to `false` while it runs the controller fails
the evaluation. The listener reaches only the gateway: the controller trusts its transport and
checks nobody per request, so a certificate the swarm root signed for
anything else is refused at the handshake.
On: the gateway's controller locations dial {option}`domain` over TLS
and present a client leaf, and the controller accepts that one
identity. The unix socket keeps working beside it. The listener
reaches only the gateway: the controller trusts its transport and
checks nobody per request, so a certificate the swarm root signed for
anything else is refused at the handshake.
Set it identically on every host of the swarm. The store's host
writes the two roles from it, the controller's host issues its server
leaf, and the host fronting the controller issues the gateway's client
leaf, each through an identity it already holds.
'';
Set it identically on every host of the swarm. The store's host
writes the two roles from it, the controller's host issues its server
leaf, and the host fronting the controller issues the gateway's client
leaf, each through an identity it already holds.
'';
};
domain = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
default = if swarmDomain == null then null else "controller.${swarmDomain}";
defaultText = lib.literalExpression ''"controller.''${services.hyperhive.swarm.domain}"'';
example = "controller.example.org";
description = ''
The name the gateway dials the controller by. It becomes the server
The name the gateway dials the controller by, `controller.<swarm.domain>`
unless set. It becomes the server
certificate's name (the store's `swarm-controller-tls` role issues
for this name alone) and what nginx verifies the controller against.
It must resolve, from the gateway's host, to an address the
controller listens on. Required when {option}`enable` is set.
controller listens on, or, with the controller in the cluster, no
address of its own: the gateway dials the pod's Service and checks the
certificate against this name. Required when {option}`enable` is set,
and with no swarm domain there is nothing to derive it from.
'';
};
@ -618,33 +622,6 @@ 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, which a Service in the cluster publishes at
{option}`tcp.serviceHost`. 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.
'';
};
unit = lib.mkOption {
type = lib.types.raw;
readOnly = true;
@ -652,8 +629,8 @@ in
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.
are rendered from (../host-modules/k8s/render.nix). The host does not run
it: the unit is disabled.
'';
};
@ -783,14 +760,12 @@ in
default = "0.0.0.0";
example = "10.0.0.5";
description = ''
Address the controller binds when
{option}`services.hyperhive.swarm.controller.tcp.enable` is set. An
The address the controller binds inside the pod. An
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`.
This is the bind inside the pod, so it must stay `0.0.0.0`.
'';
};
@ -801,8 +776,7 @@ in
default = "swarm-controller.${config.services.hyperhive.k8s.namespace}.svc.${config.services.hyperhive.deploy.k8s.clusterDomain}";
defaultText = lib.literalExpression ''"swarm-controller.<namespace>.svc.<cluster domain>"'';
description = ''
With {option}`services.hyperhive.deploy.swarm-controller.runtime` set
to `k8s`: the name of the Service in front of the pod's listener,
The name of the Service in front of the pod's listener,
which the gateway dials. The host's resolver answers it through the
cluster's DNS. The gateway verifies the listener's certificate as
{option}`services.hyperhive.swarm.controller.tcp.domain`, not as this
@ -827,10 +801,9 @@ in
readOnly = true;
default = "/var/lib/swarm-controller-tls";
description = ''
Where `swarm-controller-tls.service` writes the server leaf and key
the listener serves: `cert.pem` and `key.pem`, the key readable by
the controller's group alone. The daemon re-reads both when they
change, so a rotation needs no restart.
Where the pod's init container writes the server leaf and key the
listener serves: `cert.pem` and `key.pem`, in an in-memory volume. The
daemon re-reads both when they change, so a rotation needs no restart.
'';
};
@ -941,7 +914,7 @@ in
# The cluster's pods reach the host's services through the bridge, and the
# host's resolver (the hive's dnsmasq, on the bridge) answers the Service's name.
services.hyperhive.network.enable = lib.mkIf onK8s (lib.mkDefault true);
services.hyperhive.network.enable = 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
@ -1005,72 +978,60 @@ in
'';
}
{
assertion = !tcpCfg.enable || (tcpCfg.domain != null && tcpCfg.clientName != null);
assertion = tcpCfg.domain != null && tcpCfg.clientName != null;
message = ''
services.hyperhive.swarm.controller.tcp.enable needs
services.hyperhive.swarm.controller.tcp.domain, the name the gateway
dials the controller by, and services.hyperhive.swarm.domain, which
the gateway's client identity is named under. Set both, or leave the
TCP listener off.
services.hyperhive.deploy.swarm-controller.enable needs
services.hyperhive.swarm.domain: the listener's name
(services.hyperhive.swarm.controller.tcp.domain, `controller.<swarm.domain>`
by default) and the gateway's client identity are named under it. Set it,
or set tcp.domain by hand and the swarm domain the client identity needs.
'';
}
{
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";
assertion = 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
"${tcpHost.listenAddress}", and 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".
'';
}
{
assertion = !onK8s || config.services.hyperhive.gateway.dns.enable;
assertion = config.services.hyperhive.gateway.dns.enable;
message = ''
services.hyperhive.deploy.swarm-controller.runtime = "k8s" needs
services.hyperhive.deploy.swarm-controller.enable needs
services.hyperhive.gateway.dns.enable: the gateway dials the pod's
Service by name, and the hive's resolver is what hands the cluster's
domain to its DNS. Set it, or leave the controller's runtime at "host".
domain to its DNS. Set it.
'';
}
{
assertion = !onK8s || tcpCfg.enable;
assertion = 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".
services.hyperhive.swarm.controller.tcp.enable is false, and
services.hyperhive.deploy.swarm-controller.enable needs it: the host
cannot reach a unix socket inside a pod, so the gateway dials the pod's
TCP listener. Remove the setting, which defaults on with the swarm domain.
'';
}
{
assertion = !tcpCfg.enable || caTrust.useSelfSigned;
assertion = caTrust.useSelfSigned;
message = ''
services.hyperhive.swarm.controller.tcp.enable needs the self-signed
services.hyperhive.deploy.swarm-controller.enable needs the self-signed
gateway mode: the controller trusts the secret store's services root
for client certificates, and only a self-signed gateway host keeps
that root on disk. Leave the TCP listener off where the gateway
serves an operator certificate or ACME.
that root on disk. A gateway that serves an operator certificate or
ACME can't front the controller yet.
'';
}
];
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;
# Disabled: the daemon runs in the cluster. The definition stays because the
# pod is rendered from the evaluated unit.
enable = false;
description = "hyperhive swarm-level controller daemon";
wantedBy = [ "multi-user.target" ];
# The listener's certificate files must exist when the daemon opens it. A
# start that finds them missing fails and `Restart=` tries again.
after = [ "network.target" ] ++ lib.optional tcpOn "swarm-controller-tls.service";
wants = lib.optional tcpOn "swarm-controller-tls.service";
after = [ "network.target" ];
serviceConfig = {
ExecStart = "${deployCfg.swarm-controller.package}/bin/swarm-controller";
@ -1221,59 +1182,5 @@ in
networking.firewall.allowedTCPPorts = lib.mkIf (tcpOn && tcpHost.openFirewall) [ tcpCfg.port ];
# The listener's server leaf, from the store's `${tcpCfg.serverPkiRole}`
# role under the controller's own store identity — the one it already logs
# in with, whose policy gains this one issue path when the listener is on.
# 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 && !onK8s) {
description = "Issue the swarm-controller's TCP listener certificate from the secret store's PKI";
wantedBy = [ "multi-user.target" ];
after = [
"container@${baoCfg.machine}.service"
"swarm-bao-pki.service"
"swarm-bao-controller-policy.service"
];
path = [
deployCfg.bao.package
pkgs.jq
pkgs.openssl
pkgs.coreutils
pkgs.gnugrep
];
# The store is sealed or unreachable for as long as it takes an operator
# to unseal it; see ./lib/store-retry.nix.
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
serviceConfig = storeRetry.serviceConfig // {
Type = "oneshot";
UMask = "0077";
SyslogIdentifier = "swarm-controller-tls";
TimeoutStartSec = "5min";
};
environment = {
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
BAO_CLIENT_CERT = deployCfg.swarm-controller.baoClientCertFile;
BAO_CLIENT_KEY = deployCfg.swarm-controller.baoClientKeyFile;
}
// lib.optionalAttrs (deployCfg.bao.serverCaFile != null) {
BAO_CACERT = deployCfg.bao.serverCaFile;
};
script = tlsIssue.script {
login = "bao login -method=cert -token-only";
credential = "certificate";
group = "swarm-controller";
};
};
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 = {
OnCalendar = "daily";
Persistent = true;
};
};
};
}