114 lines
4.5 KiB
Nix
114 lines
4.5 KiB
Nix
# Applies the swarm services this host enables to its cluster: their manifests go
|
|
# into `services.k3s.manifests`, and the CA files the pods trust are published
|
|
# to the cluster. Each service's host unit or container is gone from the
|
|
# configuration, so the pod is the only copy: `switch-to-configuration` stops
|
|
# every unit the new configuration drops, and waits for each stop, before it runs
|
|
# the activation that links the manifests in. docs/4-implementation/k8s/runtime.md
|
|
# has the design.
|
|
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
let
|
|
cfg = config.services.hyperhive.k8s;
|
|
node = config.services.hyperhive.deploy.k8s;
|
|
rendered = cfg.rendered;
|
|
|
|
onCluster = rendered.serviceNames;
|
|
|
|
kubectl = "${pkgs.kubectl}/bin/kubectl";
|
|
kubeconfig = "/etc/rancher/k3s/k3s.yaml";
|
|
|
|
# Runs `command` until it succeeds, for at most `seconds`.
|
|
waitFor = seconds: description: command: ''
|
|
echo "waiting for ${description}" >&2
|
|
timeout ${toString seconds} ${pkgs.bash}/bin/bash -c ${lib.escapeShellArg "until ${command}; do sleep 2; done"}
|
|
'';
|
|
apiReady = waitFor 600 "the cluster's API" "${kubectl} get --raw /readyz >/dev/null 2>&1";
|
|
|
|
caFiles = lib.filterAttrs (_: path: path != null) rendered.caSources;
|
|
|
|
# The services on the cluster whose pods run in a user namespace.
|
|
userNamespaced = lib.filter (name: lib.elem name onCluster) rendered.userNamespaces;
|
|
in
|
|
{
|
|
config = lib.mkIf (node.enable && cfg.enable) (
|
|
lib.mkMerge [
|
|
# A pod in a user namespace needs idmapped mounts for its volumes, which the
|
|
# kernel has for tmpfs from 6.3 on (the projected token and the Secrets
|
|
# are tmpfs).
|
|
(lib.mkIf (userNamespaced != [ ]) {
|
|
assertions = [
|
|
{
|
|
assertion = lib.versionAtLeast config.boot.kernelPackages.kernel.version "6.3";
|
|
message = ''
|
|
The pods of ${lib.concatStringsSep ", " userNamespaced} run in a user
|
|
namespace (`hostUsers = false`), which needs a kernel of 6.3 or
|
|
later; this host's kernel is ${config.boot.kernelPackages.kernel.version}.
|
|
'';
|
|
}
|
|
];
|
|
})
|
|
|
|
(lib.mkIf (onCluster != [ ]) {
|
|
# The cluster applies each file of the manifests directory when it
|
|
# appears: a service's objects go in after its old host unit has stopped,
|
|
# because `switch-to-configuration` stops every unit the new
|
|
# configuration drops before it activates the new configuration, and
|
|
# the files are linked in by the activation.
|
|
services.k3s.manifests = {
|
|
hyperhive-namespace.content = rendered.namespaceObject;
|
|
}
|
|
// lib.genAttrs onCluster (name: {
|
|
content = lib.attrValues rendered.services.${name}.objects;
|
|
});
|
|
})
|
|
|
|
# The pods' CA files are public, so the host publishes them as they are.
|
|
# A pod waits in ContainerCreating until this has run. The unit has no
|
|
# `RemainAfterExit`, so the path unit below starts it again whenever a
|
|
# file changes.
|
|
(lib.mkIf (onCluster != [ ] && caFiles != { }) {
|
|
systemd.services.hyperhive-k8s-ca-trust = {
|
|
description = "publish the hive's CA files to the cluster's pods";
|
|
wantedBy = [ "multi-user.target" ];
|
|
after = [
|
|
"k3s.service"
|
|
"hive-tls-ca.service"
|
|
"swarm-bao-pki.service"
|
|
];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
Restart = "on-failure";
|
|
RestartSec = "10s";
|
|
SyslogIdentifier = "hyperhive-k8s-ca-trust";
|
|
};
|
|
environment.KUBECONFIG = kubeconfig;
|
|
script = ''
|
|
set -euo pipefail
|
|
${apiReady}
|
|
${waitFor 600 "the namespace" "${kubectl} get namespace ${cfg.namespace} >/dev/null 2>&1"}
|
|
${lib.concatMapStrings (path: waitFor 600 "${path}" "[ -s ${lib.escapeShellArg path} ]") (
|
|
lib.unique (lib.attrValues caFiles)
|
|
)}
|
|
${kubectl} --namespace ${cfg.namespace} create configmap ${rendered.caConfigMap} \
|
|
${
|
|
lib.concatStringsSep " " (
|
|
lib.mapAttrsToList (key: path: "--from-file=${key}=${lib.escapeShellArg path}") caFiles
|
|
)
|
|
} \
|
|
--dry-run=client --output=yaml \
|
|
| ${kubectl} --namespace ${cfg.namespace} apply --server-side --force-conflicts --filename=-
|
|
'';
|
|
};
|
|
systemd.paths.hyperhive-k8s-ca-trust = {
|
|
wantedBy = [ "multi-user.target" ];
|
|
pathConfig.PathChanged = lib.unique (lib.attrValues caFiles);
|
|
};
|
|
})
|
|
|
|
]
|
|
);
|
|
}
|