k8s: deploy.swarm-controller.runtime moves the controller between its unit and the cluster
This commit is contained in:
parent
72b437832d
commit
d6f0b07ff1
1 changed files with 407 additions and 89 deletions
|
|
@ -40,9 +40,20 @@ let
|
|||
caConfigMap = "hyperhive-ca-trust";
|
||||
trustBundleKey = "trust-bundle.pem";
|
||||
baoServerCaKey = "bao-server-ca.pem";
|
||||
hiveClientCaKey = "hive-client-ca.pem";
|
||||
servicesRootKey = "swarm-services-root.pem";
|
||||
controllerCfg = deployCfg.swarm-controller;
|
||||
controllerOnCluster = controllerCfg.enable && controllerCfg.runtime == "k8s";
|
||||
controllerTcp = hyperhiveCfg.swarm.controller.tcp;
|
||||
servicesRootPath = "${deployCfg.hive-controller.tls.stateDir}/${servicesRootKey}";
|
||||
caSources = {
|
||||
${trustBundleKey} = if caTrust.useSelfSigned then caTrust.caHostPath else null;
|
||||
${baoServerCaKey} = baoDeployCfg.serverCaFile;
|
||||
# What the controller's pod needs of the hive: the root its listener
|
||||
# trusts the gateway's client leaf by, and the authority it writes into
|
||||
# each hive's store role. Both are public.
|
||||
${servicesRootKey} = if controllerOnCluster && controllerTcp.enable then servicesRootPath else null;
|
||||
${hiveClientCaKey} = if controllerOnCluster then controllerCfg.hiveClientCaFile else null;
|
||||
};
|
||||
|
||||
# `serviceUser` is the user every image runs its daemon as. Mounted files are
|
||||
|
|
@ -67,12 +78,63 @@ let
|
|||
"systemd"
|
||||
];
|
||||
|
||||
# The controller's listener leaf: its pod issues the leaf itself, from the
|
||||
# store, with its ServiceAccount token, as the host's
|
||||
# `swarm-controller-tls.service` does with its certificate
|
||||
# (../lib/swarm-controller-tls-issue.nix is the one script). An init
|
||||
# container writes it before the daemon starts and a second container renews
|
||||
# it, both into an emptyDir only the pod holds.
|
||||
tlsIssue = import ../lib/swarm-controller-tls-issue.nix { inherit lib config; };
|
||||
leafTools = [
|
||||
baoDeployCfg.package
|
||||
pkgs.jq
|
||||
pkgs.openssl
|
||||
pkgs.coreutils
|
||||
pkgs.gnugrep
|
||||
];
|
||||
leafIssueBin = "swarm-controller-tls-issue";
|
||||
leafRenewBin = "swarm-controller-tls-renew";
|
||||
leafImagePackage =
|
||||
let
|
||||
issue = pkgs.writeShellScriptBin leafIssueBin ''
|
||||
export PATH=${lib.makeBinPath leafTools}
|
||||
${tlsIssue.script {
|
||||
login = "bao write -field=token auth/${podAuth.mount}/login role=swarm-controller jwt=@${podAuth.tokenFile}";
|
||||
credential = "ServiceAccount token";
|
||||
group = null;
|
||||
createDir = false;
|
||||
}}
|
||||
'';
|
||||
# The host's timer runs the same check daily: the script leaves a leaf
|
||||
# that is within the first half of its window alone. A failed run (the
|
||||
# store sealed or unreachable) is tried again sooner.
|
||||
renew = pkgs.writeShellScriptBin leafRenewBin ''
|
||||
export PATH=${lib.makeBinPath [ pkgs.coreutils ]}
|
||||
while true; do
|
||||
if ${issue}/bin/${leafIssueBin}; then
|
||||
sleep 86400
|
||||
else
|
||||
sleep 600
|
||||
fi
|
||||
done
|
||||
'';
|
||||
in
|
||||
pkgs.symlinkJoin {
|
||||
name = "swarm-controller-tls";
|
||||
paths = [
|
||||
issue
|
||||
renew
|
||||
];
|
||||
};
|
||||
|
||||
# The swarm services that run in the cluster. `unit` is the evaluated
|
||||
# systemd unit the host runs today; nothing reads it unless `enable` holds.
|
||||
# `healthEnv` names the variable that binds the daemon's /healthz and
|
||||
# /readyz listener; the controller serves /readyz on its unix socket only,
|
||||
# which the kubelet cannot probe. `publicCredentials` maps the ids of the
|
||||
# credentials that hold a public CA file to their key in the CA ConfigMap.
|
||||
# /readyz listener. `publicCredentials` maps the ids of the credentials that
|
||||
# hold a public CA file to their key in the CA ConfigMap, and `publicFiles`
|
||||
# the paths the unit names that hold one to theirs. `listener`, where a
|
||||
# service has one, is the TCP port the pod publishes on the node
|
||||
# (`hostAddress`) and the directory its init container writes the leaf to.
|
||||
# `switch` is null for a service that always runs on the host, and for one
|
||||
# with a `runtime` option names the option's value and the host unit or units
|
||||
# that run the service when it is `"host"`. `otlp` is where the pod pushes
|
||||
|
|
@ -82,14 +144,29 @@ let
|
|||
# not carry one (../swarm-otel.nix, the swarm tier's own pipeline).
|
||||
services = {
|
||||
swarm-controller = {
|
||||
inherit (deployCfg.swarm-controller) enable package;
|
||||
unit = config.systemd.services.swarm-controller;
|
||||
inherit (controllerCfg) enable package unit;
|
||||
kind = "Deployment";
|
||||
healthEnv = null;
|
||||
healthEnv = "SWARM_CONTROLLER_HEALTH_ADDR";
|
||||
etcFiles = [ "hyperhive/swarm-controller.json" ];
|
||||
publicCredentials = { };
|
||||
otlp = "hive";
|
||||
switch = null;
|
||||
publicCredentials = {
|
||||
"bao-ca.pem" = baoServerCaKey;
|
||||
"hive-client-ca.pem" = hiveClientCaKey;
|
||||
};
|
||||
publicFiles = lib.optionalAttrs controllerTcp.enable { ${servicesRootPath} = servicesRootKey; };
|
||||
# The TCP listener, as a `hostPort` on the node's address: the gateway
|
||||
# on the host dials it there.
|
||||
listener = lib.optionalAttrs controllerTcp.enable {
|
||||
port = controllerTcp.port;
|
||||
hostAddress = controllerCfg.tcp.hostAddress;
|
||||
tlsDir = controllerCfg.tcp.tlsDir;
|
||||
};
|
||||
# The unit pushes to the swarm collector's route and mints its own bearer
|
||||
# (../glue-controller-env.nix, `otel_http_client.rs`).
|
||||
otlp = "own";
|
||||
switch = {
|
||||
inherit (controllerCfg) runtime;
|
||||
hostUnit = "swarm-controller.service";
|
||||
};
|
||||
};
|
||||
swarm-notify-router = {
|
||||
inherit (deployCfg.notify-router) enable package;
|
||||
|
|
@ -270,6 +347,9 @@ let
|
|||
inherit (svc) unit;
|
||||
sc = unit.serviceConfig;
|
||||
|
||||
listener = svc.listener or { };
|
||||
publicFiles = svc.publicFiles or { };
|
||||
|
||||
credDir = "/run/credentials/${name}.service";
|
||||
# `LoadCredential` entries are `id:source` on the host and a bare `id`
|
||||
# inside a container. Only the id is rendered.
|
||||
|
|
@ -348,6 +428,8 @@ let
|
|||
++ lib.optional baoLogin podAuth.tokenDir
|
||||
++ lib.optional (runtimeDir != null) runtimeDir
|
||||
++ lib.optional (stateDir != null) stateDir
|
||||
++ lib.attrNames publicFiles
|
||||
++ lib.optional (listener != { }) listener.tlsDir
|
||||
++ map (f: "/etc/${f}") svc.etcFiles;
|
||||
under = root: v: v == root || lib.hasPrefix "${root}/" v;
|
||||
unmounted = lib.filterAttrs (_: v: lib.hasPrefix "/" v && !(lib.any (r: under r v) mountRoots)) env;
|
||||
|
|
@ -438,10 +520,15 @@ let
|
|||
];
|
||||
};
|
||||
}
|
||||
++ lib.optional (caFiles != [ ]) {
|
||||
++ lib.optional (!(caFiles == [ ] && publicFiles == { })) {
|
||||
name = "ca-trust";
|
||||
configMap.name = caConfigMap;
|
||||
}
|
||||
++ lib.optional (listener != { }) {
|
||||
name = "tls";
|
||||
# In memory: the leaf's key never reaches the node's disk.
|
||||
emptyDir.medium = "Memory";
|
||||
}
|
||||
++ lib.optional (runtimeDir != null) {
|
||||
name = "runtime";
|
||||
emptyDir = { };
|
||||
|
|
@ -462,6 +549,10 @@ let
|
|||
];
|
||||
|
||||
etcKey = f: builtins.replaceStrings [ "/" ] [ "_" ] f;
|
||||
tlsMount = {
|
||||
name = "tls";
|
||||
mountPath = listener.tlsDir;
|
||||
};
|
||||
|
||||
volumeMounts =
|
||||
lib.optional (credIds != [ ]) {
|
||||
|
|
@ -480,6 +571,13 @@ let
|
|||
subPath = trustBundleKey;
|
||||
readOnly = true;
|
||||
}) caFiles
|
||||
++ map (path: {
|
||||
name = "ca-trust";
|
||||
mountPath = path;
|
||||
subPath = publicFiles.${path};
|
||||
readOnly = true;
|
||||
}) (lib.attrNames publicFiles)
|
||||
++ lib.optional (listener != { }) tlsMount
|
||||
++ lib.optional (runtimeDir != null) {
|
||||
name = "runtime";
|
||||
mountPath = runtimeDir;
|
||||
|
|
@ -522,6 +620,20 @@ let
|
|||
cpu = "0";
|
||||
};
|
||||
|
||||
# `hostPort` publishes the listener on the node's address. The cluster's
|
||||
# portmap plugin forwards it to the pod, so it reaches this one node.
|
||||
ports =
|
||||
lib.optional (svc.healthEnv != null) {
|
||||
name = "health";
|
||||
containerPort = healthPort;
|
||||
}
|
||||
++ lib.optional (listener != { }) {
|
||||
name = "api";
|
||||
containerPort = listener.port;
|
||||
hostPort = listener.port;
|
||||
hostIP = listener.hostAddress;
|
||||
};
|
||||
|
||||
container = {
|
||||
inherit name volumeMounts;
|
||||
image = "${image.imageName}:${image.imageTag}";
|
||||
|
|
@ -545,13 +657,8 @@ let
|
|||
// lib.optionalAttrs (limits != { }) { inherit limits; };
|
||||
}
|
||||
// lib.optionalAttrs (lib.tail execWords != [ ]) { args = lib.tail execWords; }
|
||||
// lib.optionalAttrs (ports != [ ]) { inherit ports; }
|
||||
// lib.optionalAttrs (svc.healthEnv != null) {
|
||||
ports = [
|
||||
{
|
||||
name = "health";
|
||||
containerPort = healthPort;
|
||||
}
|
||||
];
|
||||
livenessProbe = {
|
||||
httpGet = {
|
||||
path = "/healthz";
|
||||
|
|
@ -568,9 +675,60 @@ let
|
|||
};
|
||||
};
|
||||
|
||||
# The listener's leaf: issued before the daemon starts, renewed beside it.
|
||||
# Both log in as the pod's account, so they hold the token volume the
|
||||
# daemon's login uses, and the store's CA from the credentials volume.
|
||||
leafImage = mkServiceImage {
|
||||
name = "${name}-tls";
|
||||
package = leafImagePackage;
|
||||
bin = leafIssueBin;
|
||||
};
|
||||
leafContainer = {
|
||||
name = "tls-issue";
|
||||
image = "${leafImage.imageName}:${leafImage.imageTag}";
|
||||
imagePullPolicy = "Never";
|
||||
env =
|
||||
lib.mapAttrsToList
|
||||
(n: v: {
|
||||
name = n;
|
||||
value = v;
|
||||
})
|
||||
(
|
||||
lib.filterAttrs (
|
||||
n: _:
|
||||
lib.elem n [
|
||||
"BAO_ADDR"
|
||||
"BAO_CACERT"
|
||||
]
|
||||
) env
|
||||
// {
|
||||
HOME = "/tmp";
|
||||
}
|
||||
);
|
||||
volumeMounts = lib.filter (
|
||||
m:
|
||||
lib.elem m.name [
|
||||
"credentials"
|
||||
"bao-token"
|
||||
"tls"
|
||||
"tmp"
|
||||
]
|
||||
) volumeMounts;
|
||||
inherit (container) securityContext;
|
||||
};
|
||||
|
||||
podSpec = {
|
||||
inherit terminationGracePeriodSeconds volumes;
|
||||
containers = [ container ];
|
||||
containers = [
|
||||
container
|
||||
]
|
||||
++ lib.optional (listener != { }) (
|
||||
leafContainer
|
||||
// {
|
||||
name = "tls-renew";
|
||||
command = [ "/bin/${leafRenewBin}" ];
|
||||
}
|
||||
);
|
||||
# The API token is never mounted; a pod that logs in to the store gets
|
||||
# a token of its own, projected for the store's audience.
|
||||
automountServiceAccountToken = false;
|
||||
|
|
@ -582,10 +740,31 @@ let
|
|||
seccompProfile.type = "RuntimeDefault";
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs baoLogin { serviceAccountName = name; };
|
||||
// lib.optionalAttrs baoLogin { serviceAccountName = name; }
|
||||
// lib.optionalAttrs (listener != { }) { initContainers = [ leafContainer ]; };
|
||||
|
||||
# What the `-etc` ConfigMap holds. A file mounted from it by `subPath`
|
||||
# never sees a later change, so the pod template carries a hash of the
|
||||
# content: a changed file changes the template, and the Deployment rolls.
|
||||
etcData = lib.listToAttrs (
|
||||
map (
|
||||
f:
|
||||
let
|
||||
e = config.environment.etc.${f};
|
||||
in
|
||||
lib.nameValuePair (etcKey f) (
|
||||
if e.text != null then e.text else fail name "/etc/${f} has no `text` to put in a ConfigMap"
|
||||
)
|
||||
) svc.etcFiles
|
||||
);
|
||||
|
||||
podTemplate = restartPolicy: {
|
||||
metadata.labels = labelsFor name;
|
||||
metadata = {
|
||||
labels = labelsFor name;
|
||||
}
|
||||
// lib.optionalAttrs (svc.etcFiles != [ ]) {
|
||||
annotations."checksum/etc" = builtins.hashString "sha256" (builtins.toJSON etcData);
|
||||
};
|
||||
spec = podSpec // {
|
||||
inherit restartPolicy;
|
||||
};
|
||||
|
|
@ -656,17 +835,7 @@ let
|
|||
apiVersion = "v1";
|
||||
kind = "ConfigMap";
|
||||
metadata = metaFor "${name}-etc";
|
||||
data = lib.listToAttrs (
|
||||
map (
|
||||
f:
|
||||
let
|
||||
e = config.environment.etc.${f};
|
||||
in
|
||||
lib.nameValuePair (etcKey f) (
|
||||
if e.text != null then e.text else fail name "/etc/${f} has no `text` to put in a ConfigMap"
|
||||
)
|
||||
) svc.etcFiles
|
||||
);
|
||||
data = etcData;
|
||||
};
|
||||
};
|
||||
in
|
||||
|
|
@ -685,7 +854,11 @@ let
|
|||
else if missingCreds != { } then
|
||||
fail name "${lib.concatStringsSep ", " (lib.attrNames missingCreds)} names a credential the unit does not load"
|
||||
else
|
||||
{ inherit objects image; };
|
||||
{
|
||||
inherit objects image;
|
||||
# What the pod's other containers run.
|
||||
extraImages = lib.optional (listener != { }) leafImage;
|
||||
};
|
||||
|
||||
enabled = lib.mapAttrs workload (lib.filterAttrs (_: svc: svc.enable) services);
|
||||
in
|
||||
|
|
@ -704,7 +877,7 @@ in
|
|||
|
||||
# The image each enabled service's pod runs: what a node has to hold,
|
||||
# since there is no registry to pull from.
|
||||
images = lib.mapAttrsToList (_: w: w.image) enabled;
|
||||
images = lib.concatMap (w: [ w.image ] ++ w.extraImages) (lib.attrValues enabled);
|
||||
|
||||
# What ./runtime.nix moves services with. `switches` holds the services that
|
||||
# have a runtime option and is cheap to read: it names the units that a
|
||||
|
|
|
|||
Loading…
Reference in a new issue