The controller's api port was a hostPort on the node, which Pod Security baseline and restricted forbid. It is a ClusterIP Service now, behind a NetworkPolicy that admits only the node. The host's resolver (the hive's dnsmasq) forwards cluster.local to CoreDNS, and the gateway dials the Service by name through nginx's resolver. tcp.hostAddress goes.
159 lines
6.7 KiB
Nix
159 lines
6.7 KiB
Nix
# Which of a service's two homes runs it: its host unit, or its objects in the
|
|
# cluster. A service with a `runtime` option is on the cluster when the option
|
|
# says `"k8s"`, and then only: its host unit or units are gone from the
|
|
# configuration, and its manifests are in `services.k3s.manifests`.
|
|
# The cluster does not remove what a manifest created once the manifest is
|
|
# gone, so a service back on the host has its objects deleted before its host
|
|
# unit starts. 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 = lib.filterAttrs (_: s: s.runtime == "k8s") rendered.switches;
|
|
onHost = lib.filterAttrs (_: s: s.runtime == "host") rendered.switches;
|
|
|
|
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: onCluster ? ${name}) 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 when its 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.mapAttrs (name: _: {
|
|
content = lib.attrValues rendered.services.${name}.objects;
|
|
}) onCluster;
|
|
})
|
|
|
|
# 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);
|
|
};
|
|
})
|
|
|
|
# A service back on the host first loses its objects. The units that
|
|
# start the host copy require this one, so the pod is gone before the
|
|
# host copy starts, and a failure here leaves the host copy stopped.
|
|
#
|
|
# Its marker is the manifest's link in the cluster's manifests
|
|
# directory, which nothing else removes: no link, nothing was applied,
|
|
# and the unit needs no cluster. The link is renamed first, so the
|
|
# cluster cannot apply it again, and the rename is the record of what is
|
|
# left to delete. Deleting the AddOn the cluster keeps for the file lets
|
|
# a later switch to the cluster apply the same manifest again.
|
|
(lib.mkIf (onHost != { }) {
|
|
systemd.services = lib.mapAttrs' (
|
|
name: s:
|
|
lib.nameValuePair "${name}-k8s-prune" {
|
|
description = "delete ${name}'s objects from the cluster before its host unit starts";
|
|
after = [ "k3s.service" ];
|
|
before = lib.toList s.hostUnit;
|
|
requiredBy = lib.toList s.hostUnit;
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
RemainAfterExit = true;
|
|
SyslogIdentifier = "${name}-k8s-prune";
|
|
};
|
|
environment.KUBECONFIG = kubeconfig;
|
|
script = ''
|
|
set -euo pipefail
|
|
link=${node.manifestDir}/${name}.yaml
|
|
pending=$link.pruning
|
|
if [ -e "$link" ] || [ -L "$link" ]; then
|
|
mv -f "$link" "$pending"
|
|
fi
|
|
if [ ! -e "$pending" ] && [ ! -L "$pending" ]; then
|
|
exit 0
|
|
fi
|
|
${apiReady}
|
|
${kubectl} --namespace ${cfg.namespace} delete deployment,cronjob,serviceaccount,networkpolicy,service \
|
|
--selector=app.kubernetes.io/name=${name} \
|
|
--cascade=foreground --wait=true --timeout=180s --ignore-not-found
|
|
${kubectl} --namespace kube-system delete addons.k3s.cattle.io ${name} --ignore-not-found
|
|
rm -f "$pending"
|
|
'';
|
|
}
|
|
) onHost;
|
|
})
|
|
]
|
|
);
|
|
}
|