k8s: deploy.notify-router.runtime moves the router between its container and the cluster
This commit is contained in:
parent
485da0c3c7
commit
a1d44ed046
1 changed files with 671 additions and 187 deletions
140
nix/host-modules/k8s/runtime.nix
Normal file
140
nix/host-modules/k8s/runtime.nix
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
# Which of a service's two homes runs it: its host unit, or its objects in the
|
||||
# cluster. A service with a `runtime` option (`swarm-notify-router`) is on the
|
||||
# cluster when the option says `"k8s"`, and then only: its host unit is 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;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (node.enable && cfg.enable) (
|
||||
lib.mkMerge [
|
||||
(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.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.attrValues caFiles;
|
||||
};
|
||||
})
|
||||
|
||||
# A service back on the host first loses its objects. The unit that
|
||||
# starts the host copy requires 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 = [ s.hostUnit ];
|
||||
requiredBy = [ 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 \
|
||||
--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;
|
||||
})
|
||||
]
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue