require k8s: swarm services with a pod run in the cluster only
This commit is contained in:
parent
7819ed2dbd
commit
2e1f037f25
1 changed files with 1044 additions and 3125 deletions
|
|
@ -1,10 +1,10 @@
|
|||
# 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.
|
||||
# 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,
|
||||
|
|
@ -16,8 +16,7 @@ let
|
|||
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;
|
||||
onCluster = rendered.serviceNames;
|
||||
|
||||
kubectl = "${pkgs.kubectl}/bin/kubectl";
|
||||
kubeconfig = "/etc/rancher/k3s/k3s.yaml";
|
||||
|
|
@ -29,19 +28,10 @@ let
|
|||
'';
|
||||
apiReady = waitFor 600 "the cluster's API" "${kubectl} get --raw /readyz >/dev/null 2>&1";
|
||||
|
||||
# The service load balancer binds a Service's port on the node with a
|
||||
# `hostPort` in a pod of `kube-system`, which its controller deletes some time
|
||||
# after the Service. The rule that forwards the port to that pod stays until
|
||||
# the pod is gone, and would swallow what the host unit then listens on.
|
||||
lbPodsGone =
|
||||
name:
|
||||
waitFor 180 "the load balancer's pods of ${name}"
|
||||
"[ -z \"\$(${kubectl} --namespace kube-system get pods --selector svccontroller.k3s.cattle.io/svcname=${name},svccontroller.k3s.cattle.io/svcnamespace=${cfg.namespace} --output name)\" ]";
|
||||
|
||||
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;
|
||||
userNamespaced = lib.filter (name: lib.elem name onCluster) rendered.userNamespaces;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (node.enable && cfg.enable) (
|
||||
|
|
@ -62,25 +52,25 @@ in
|
|||
];
|
||||
})
|
||||
|
||||
(lib.mkIf (onCluster != { }) {
|
||||
(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,
|
||||
# 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.mapAttrs (name: _: {
|
||||
// lib.genAttrs onCluster (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 != { }) {
|
||||
(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" ];
|
||||
|
|
@ -119,51 +109,6 @@ in
|
|||
};
|
||||
})
|
||||
|
||||
# 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
|
||||
${lbPodsGone name}
|
||||
${kubectl} --namespace kube-system delete addons.k3s.cattle.io ${name} --ignore-not-found
|
||||
rm -f "$pending"
|
||||
'';
|
||||
}
|
||||
) onHost;
|
||||
})
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue