require k8s: swarm services with a pod run in the cluster only
This commit is contained in:
parent
7819ed2dbd
commit
2e1f037f25
4 changed files with 1044 additions and 3125 deletions
|
|
@ -1,7 +1,6 @@
|
|||
# Kubernetes manifests for the swarm services this host deploys, rendered by
|
||||
# ./render.nix from their systemd units, and the per-service switch that moves
|
||||
# a service between its host unit and the cluster (./runtime.nix). Rendering
|
||||
# alone changes no unit and applies nothing. docs/4-implementation/k8s/manifests.md
|
||||
# ./render.nix from their systemd units, and the module that applies them to the
|
||||
# cluster (./runtime.nix). docs/4-implementation/k8s/manifests.md
|
||||
# and runtime.md have the design.
|
||||
{
|
||||
pkgs,
|
||||
|
|
@ -41,9 +40,7 @@ in
|
|||
object, and the images their pods run into `system.build.k8sImages`.
|
||||
The manifests reference their Secrets by name only.
|
||||
|
||||
Rendering only: the host's units stay as they are, and nothing
|
||||
applies the manifests to a cluster. A service with a `runtime` option
|
||||
is applied when it says `k8s`, on a host with
|
||||
The manifests are applied on a host with
|
||||
{option}`services.hyperhive.deploy.k8s.enable`.
|
||||
'';
|
||||
};
|
||||
|
|
@ -59,8 +56,8 @@ in
|
|||
readOnly = true;
|
||||
internal = true;
|
||||
description = ''
|
||||
What ./runtime.nix moves services with: each enabled service's
|
||||
objects and switch, the namespace object, and the CA ConfigMap's name
|
||||
What ./runtime.nix applies: each enabled service's
|
||||
objects, the namespace object, and the CA ConfigMap's name
|
||||
and host files.
|
||||
'';
|
||||
};
|
||||
|
|
@ -71,7 +68,7 @@ in
|
|||
inherit (rendered)
|
||||
caConfigMap
|
||||
caSources
|
||||
switches
|
||||
serviceNames
|
||||
userNamespaces
|
||||
;
|
||||
services = withoutContext rendered.services;
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@
|
|||
#
|
||||
# The main node holds the images of the services ./render.nix renders and
|
||||
# shares them with the nodes that join through k3s's embedded registry mirror
|
||||
# of the images' registry host. No service's objects are applied until its `runtime` is `k8s`
|
||||
# (./runtime.nix): until then its host unit runs.
|
||||
# of the images' registry host. Every swarm service this host enables runs on it,
|
||||
# so enabling one turns the node on, and a host that turns the node off while it
|
||||
# enables one fails at evaluation (./runtime.nix applies the manifests).
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
|
|
@ -118,9 +119,22 @@ let
|
|||
'') nodePorts
|
||||
) addresses;
|
||||
|
||||
# The services whose data the module keeps in the cluster: those with a
|
||||
# `runtime` that is `k8s`. Each puts its state label on this node.
|
||||
stateful = lib.filterAttrs (_: svc: svc.enable && svc.runtime == "k8s") {
|
||||
# The swarm services that run in the cluster, by the name of their
|
||||
# `deploy.<name>.enable` option.
|
||||
swarmServices = [
|
||||
"notify-router"
|
||||
"stt"
|
||||
"ci-metrics"
|
||||
"review-classifier"
|
||||
"claude-login"
|
||||
"nats"
|
||||
"swarm-controller"
|
||||
];
|
||||
enabledServices = lib.filter (name: deployCfg.${name}.enable) swarmServices;
|
||||
|
||||
# The services whose data the module keeps in the cluster. Each puts its state
|
||||
# label on this node.
|
||||
stateful = lib.filterAttrs (_: svc: svc.enable) {
|
||||
inherit (deployCfg) nats ci-metrics;
|
||||
};
|
||||
in
|
||||
|
|
@ -129,16 +143,17 @@ in
|
|||
enable = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
defaultText = lib.literalMD "`true` where the host enables a swarm service that runs in the cluster, else `false`";
|
||||
description = ''
|
||||
Run a k3s server on this host, the cluster's main node, with the images of the
|
||||
swarm services that run in the cluster loaded into it, and render
|
||||
their manifests (`services.hyperhive.k8s.enable`). Set it on the host
|
||||
that runs the swarm's services, which is the one
|
||||
`services.hyperhive.deploy.singleHostSwarm` names.
|
||||
their manifests (`services.hyperhive.k8s.enable`).
|
||||
|
||||
The cluster starts with no hyperhive workload: a service keeps
|
||||
running as its systemd unit unless its `runtime` option is `k8s`.
|
||||
Its secrets are encrypted at rest, and traefik and metrics-server
|
||||
Defaults on where this host enables any swarm service that runs in the
|
||||
cluster: `deploy.notify-router`, `stt`, `ci-metrics`, `review-classifier`,
|
||||
`claude-login`, `nats` and `swarm-controller`, which
|
||||
`services.hyperhive.deploy.singleHostSwarm` and `allSwarmServices` turn
|
||||
on. Setting it to `false` on such a host fails the evaluation. Its secrets are encrypted at rest, and traefik and metrics-server
|
||||
stay off. Pods and services take their
|
||||
addresses from {option}`services.hyperhive.deploy.k8s.podCidr` and
|
||||
{option}`services.hyperhive.deploy.k8s.serviceCidr`. The firewall
|
||||
|
|
@ -241,7 +256,7 @@ in
|
|||
labels.loadBalancerLabel
|
||||
] (_: "true")
|
||||
// lib.mapAttrs' (name: _: lib.nameValuePair (labels.stateLabel name) "true") stateful;
|
||||
defaultText = lib.literalMD "computed from the services whose `runtime` is `k8s`";
|
||||
defaultText = lib.literalMD "computed from the enabled services whose data the cluster keeps";
|
||||
description = ''
|
||||
The labels this node carries, which the module sets with
|
||||
`services.k3s.nodeLabel`. The pods and volumes that need a node select
|
||||
|
|
@ -249,17 +264,16 @@ in
|
|||
none of those pods.
|
||||
|
||||
- `hyperhive.darkest.space/state-<service>` for each service whose data the
|
||||
cluster keeps: `nats` and `ci-metrics`, each when its `runtime` is
|
||||
`k8s`. A volume's `nodeAffinity` and the pod's `nodeSelector` name it.
|
||||
cluster keeps: `nats` and `ci-metrics`, each when enabled. A volume's `nodeAffinity` and the pod's `nodeSelector` name it.
|
||||
- `hyperhive.darkest.space/entry`: the node the hosts' DNS points at.
|
||||
- `svccontroller.k3s.cattle.io/enablelb`: with it on any node, the
|
||||
service load balancer binds a published port on the labelled nodes
|
||||
only, so a joined node does not publish the queue's port.
|
||||
|
||||
k3s puts these on the node's object at every start, registered or not.
|
||||
It never takes one off: a label left from a service that has moved
|
||||
back to `host` stays and does nothing, and `kubectl label node <name>
|
||||
<label>-` removes one.
|
||||
It never takes one off: a label left from a service that is disabled
|
||||
stays and does nothing, and `kubectl label node <name> <label>-`
|
||||
removes one.
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
@ -336,6 +350,24 @@ in
|
|||
};
|
||||
|
||||
config = lib.mkMerge [
|
||||
{
|
||||
services.hyperhive.deploy.k8s.enable = lib.mkDefault (enabledServices != [ ] && cfg.join == null);
|
||||
|
||||
assertions = [
|
||||
{
|
||||
assertion = cfg.enable || cfg.join != null || enabledServices == [ ];
|
||||
message = ''
|
||||
services.hyperhive.deploy.k8s.enable is false, and this host enables
|
||||
swarm services that run only in the cluster: ${
|
||||
lib.concatMapStringsSep ", " (n: "deploy.${n}.enable") enabledServices
|
||||
}.
|
||||
Set services.hyperhive.deploy.k8s.enable to true (it is the default
|
||||
where one is enabled) or disable those services on this host.
|
||||
'';
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
(lib.mkIf cfg.enable {
|
||||
assertions = [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ let
|
|||
hiveClientCaKey = "hive-client-ca.pem";
|
||||
servicesRootKey = "swarm-services-root.pem";
|
||||
controllerCfg = deployCfg.swarm-controller;
|
||||
controllerOnCluster = controllerCfg.enable && controllerCfg.runtime == "k8s";
|
||||
controllerOnCluster = controllerCfg.enable;
|
||||
controllerTcp = hyperhiveCfg.swarm.controller.tcp;
|
||||
natsCfg = deployCfg.nats;
|
||||
labels = import ./labels.nix;
|
||||
|
|
@ -81,11 +81,9 @@ let
|
|||
];
|
||||
|
||||
# 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.
|
||||
# store, with its ServiceAccount token (../lib/swarm-controller-tls-issue.nix is
|
||||
# the 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
|
||||
|
|
@ -103,8 +101,6 @@ let
|
|||
${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
|
||||
|
|
@ -140,9 +136,7 @@ let
|
|||
# `ephemeralState` gives a service an `emptyDir` in place of a claim for its
|
||||
# state directory: what it keeps there is re-derived at start. A pod starts on
|
||||
# whatever volume it gets.
|
||||
# `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
|
||||
# `otlp` is where the pod pushes
|
||||
# its telemetry: `"hive"` is the hive collector. A swarm-tier producer is
|
||||
# `"own"` and keeps the route its unit names, because the hive collector
|
||||
# stamps the `hive` label on what it forwards and a swarm-level series must
|
||||
|
|
@ -179,10 +173,6 @@ let
|
|||
# (../glue-controller-env.nix, `otel_http_client.rs`).
|
||||
otlp = "own";
|
||||
ephemeralState = true;
|
||||
switch = {
|
||||
inherit (controllerCfg) runtime;
|
||||
hostUnit = "swarm-controller.service";
|
||||
};
|
||||
};
|
||||
# The queue and its auth-callout responder are one pod: the responder is the
|
||||
# daemon below, and the server (with its exporter) is `pod`'s.
|
||||
|
|
@ -198,10 +188,6 @@ let
|
|||
etcFiles = [ ];
|
||||
publicCredentials."bao-ca.pem" = baoServerCaKey;
|
||||
otlp = "none";
|
||||
switch = {
|
||||
inherit (natsCfg) runtime;
|
||||
hostUnit = "container@swarm-nats.service";
|
||||
};
|
||||
};
|
||||
swarm-notify-router = {
|
||||
inherit (deployCfg.notify-router) enable package;
|
||||
|
|
@ -211,10 +197,6 @@ let
|
|||
etcFiles = [ ];
|
||||
publicCredentials.notify-router-bao-server-ca = baoServerCaKey;
|
||||
otlp = "hive";
|
||||
switch = {
|
||||
inherit (deployCfg.notify-router) runtime;
|
||||
hostUnit = "container@swarm-notify-router.service";
|
||||
};
|
||||
};
|
||||
swarm-stt = {
|
||||
inherit (deployCfg.stt) enable package;
|
||||
|
|
@ -237,10 +219,6 @@ let
|
|||
# Stands in for the unit's `PrivateUsers`: the decoder reads audio that
|
||||
# strangers sent.
|
||||
hostUsers = false;
|
||||
switch = {
|
||||
inherit (deployCfg.stt) runtime;
|
||||
hostUnit = "container@swarm-stt.service";
|
||||
};
|
||||
};
|
||||
swarm-review-classifier = {
|
||||
inherit (deployCfg.review-classifier) enable package;
|
||||
|
|
@ -250,10 +228,6 @@ let
|
|||
etcFiles = [ ];
|
||||
publicCredentials.review-classifier-bao-server-ca = baoServerCaKey;
|
||||
otlp = "hive";
|
||||
switch = {
|
||||
inherit (deployCfg.review-classifier) runtime;
|
||||
hostUnit = "container@swarm-review-classifier.service";
|
||||
};
|
||||
};
|
||||
swarm-claude-login = {
|
||||
inherit (deployCfg.claude-login) enable package;
|
||||
|
|
@ -263,10 +237,6 @@ let
|
|||
etcFiles = [ ];
|
||||
publicCredentials.claude-login-bao-server-ca = baoServerCaKey;
|
||||
otlp = "hive";
|
||||
switch = {
|
||||
inherit (deployCfg.claude-login) runtime;
|
||||
hostUnit = "container@swarm-claude-login.service";
|
||||
};
|
||||
};
|
||||
swarm-ci-metrics = {
|
||||
inherit (deployCfg.ci-metrics) enable package;
|
||||
|
|
@ -284,15 +254,6 @@ let
|
|||
hostDir = deployCfg.ci-metrics.hostStateDir;
|
||||
};
|
||||
otlp = "own";
|
||||
# The timer and the service both: the prune unit holds back either, and a
|
||||
# service started by hand cannot run beside the CronJob.
|
||||
switch = {
|
||||
inherit (deployCfg.ci-metrics) runtime;
|
||||
hostUnit = [
|
||||
"swarm-ci-metrics.timer"
|
||||
"swarm-ci-metrics.service"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -390,7 +351,7 @@ let
|
|||
protocol = "TCP";
|
||||
inherit port;
|
||||
};
|
||||
queueOnCluster = natsCfg.enable && natsCfg.runtime == "k8s";
|
||||
queueOnCluster = natsCfg.enable;
|
||||
in
|
||||
{
|
||||
apiVersion = "networking.k8s.io/v1";
|
||||
|
|
@ -1148,15 +1109,12 @@ in
|
|||
# for the others to pull from it.
|
||||
images = lib.unique (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
|
||||
# switch adds or removes, so it cannot depend on rendering a unit. `services`
|
||||
# holds the objects. `caSources` is the host file behind each key of the CA
|
||||
# ConfigMap, null where there is none.
|
||||
switches = lib.mapAttrs (_: svc: svc.switch) (
|
||||
lib.filterAttrs (_: svc: svc.enable && svc.switch != null) services
|
||||
);
|
||||
# The services whose pods run in a user namespace. Cheap to read, like `switches`.
|
||||
# What ./runtime.nix applies. `serviceNames` and `userNamespaces` are cheap to
|
||||
# read: they force no unit, which ./runtime.nix's own units depend on.
|
||||
# `services` holds the objects. `caSources` is the host file behind each key of
|
||||
# the CA ConfigMap, null where there is none.
|
||||
serviceNames = lib.attrNames (lib.filterAttrs (_: svc: svc.enable) services);
|
||||
|
||||
userNamespaces = lib.attrNames (
|
||||
lib.filterAttrs (_: svc: svc.enable && svc ? hostUsers && !svc.hostUsers) services
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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