Watch
0
0
Fork
You've already forked hyperhive
0

pin queue and poller state to per-service node labels instead of the hostname

This commit is contained in:
atlas 2026-11-25 21:48:02 +01:00 • committed by maia
commit 89308e8ba5
3 changed files with 348 additions and 40 deletions

View file

@ -0,0 +1,36 @@
# The node labels that pin pods to a node, and the selectors that find them.
# ./node.nix puts the labels on the node; the renderer and the queue's module
# select on them. docs/4-implementation/k8s/node.md has the scheme.
rec {
# The node that holds the data a service keeps in the cluster. One label per
# service, so moving one service's data moves one label.
stateLabel = service: "hyperhive.darkest.space/state-${service}";
# The node the hosts' DNS points at: the pods that hosts dial run there.
entryLabel = "hyperhive.darkest.space/entry";
# k3s's own (docs.k3s.io/networking/networking-services, "Controlling servicelb
# node selection"): once any node carries it, the service load balancer binds
# a published port on the labelled nodes only.
loadBalancerLabel = "svccontroller.k3s.cattle.io/enablelb";
# A pod's `nodeSelector` for a service's state label.
stateSelector = service: {
${stateLabel service} = "true";
};
# A static local volume's `nodeAffinity` for the same label.
stateAffinity = service: {
required.nodeSelectorTerms = [
{
matchExpressions = [
{
key = stateLabel service;
operator = "In";
values = [ "true" ];
}
];
}
];
};
}

View file

@ -67,6 +67,15 @@ let
# is passed explicitly below, so the address the host's resolver forwards
# to and the one CoreDNS gets agree by construction.
clusterDns = intToIp (networkOf cfg.serviceCidr + 10);
labels = import ./labels.nix;
deployCfg = config.services.hyperhive.deploy;
# 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") {
inherit (deployCfg) nats ci-metrics;
};
in
{
options.services.hyperhive.deploy.k8s = {
@ -176,6 +185,37 @@ in
'';
};
nodeLabels = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
readOnly = true;
default =
lib.genAttrs [
labels.entryLabel
labels.loadBalancerLabel
] (_: "true")
// lib.mapAttrs' (name: _: lib.nameValuePair (labels.stateLabel name) "true") stateful;
defaultText = lib.literalMD "computed from the services whose `runtime` is `k8s`";
description = ''
The labels this node carries, which the module sets with
`services.k3s.nodeLabel`. The pods and volumes that need a node select
on them, so a node that joins the cluster and has none of them takes
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.
- `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.
'';
};
manifestDir = lib.mkOption {
type = lib.types.str;
readOnly = true;
@ -232,6 +272,7 @@ in
"traefik"
"metrics-server"
];
nodeLabel = lib.mapAttrsToList (k: v: "${k}=${v}") cfg.nodeLabels;
extraFlags = [
"--secrets-encryption"
"--cluster-cidr=${cfg.podCidr}"

View file

@ -46,6 +46,7 @@ let
controllerOnCluster = controllerCfg.enable && controllerCfg.runtime == "k8s";
controllerTcp = hyperhiveCfg.swarm.controller.tcp;
natsCfg = deployCfg.nats;
labels = import ./labels.nix;
servicesRootPath = "${deployCfg.hive-controller.tls.stateDir}/${servicesRootKey}";
caSources = {
${trustBundleKey} = if caTrust.useSelfSigned then caTrust.caHostPath else null;
@ -275,6 +276,13 @@ let
healthEnv = null;
etcFiles = [ ];
publicCredentials."bao-ca.pem" = baoServerCaKey;
# A static local volume on the unit's own state directory, in place of the
# default class: it binds only on the node with the service's state label
# (./labels.nix).
localState = {
service = "ci-metrics";
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.
@ -551,6 +559,8 @@ let
runtimeDir = if sc ? RuntimeDirectory then "/run/${sc.RuntimeDirectory}" else null;
stateDir = if sc ? StateDirectory then "/var/lib/${sc.StateDirectory}" else null;
ephemeral = svc.ephemeralState or false;
localState = svc.localState or null;
stateClaim = if localState != null then "${name}-store" else "${name}-state";
# PATH holds only NixOS's default tools (checked below), which the image
# does not carry. `%d` is systemd's specifier for the credential
@ -732,9 +742,7 @@ let
{
name = "state";
}
// (
if ephemeral then { emptyDir = { }; } else { persistentVolumeClaim.claimName = "${name}-state"; }
)
// (if ephemeral then { emptyDir = { }; } else { persistentVolumeClaim.claimName = stateClaim; })
)
++ lib.optional (svc.etcFiles != [ ]) {
name = "etc";
@ -957,6 +965,9 @@ let
}
// lib.optionalAttrs baoLogin { serviceAccountName = name; }
// lib.optionalAttrs (svc ? hostUsers && !svc.hostUsers) { hostUsers = false; }
// lib.optionalAttrs (localState != null) {
nodeSelector = labels.stateSelector localState.service;
}
// lib.optionalAttrs (listener != { } || extra.initContainers != [ ]) {
initContainers = lib.optional (listener != { }) leafContainer ++ extra.initContainers;
}
@ -1048,10 +1059,30 @@ let
"${name}/persistentvolumeclaim" = {
apiVersion = "v1";
kind = "PersistentVolumeClaim";
metadata = metaFor "${name}-state";
metadata = metaFor stateClaim;
spec = {
accessModes = [ "ReadWriteOnce" ];
resources.requests.storage = "1Gi";
}
// lib.optionalAttrs (localState != null) {
storageClassName = stateClaim;
volumeName = stateClaim;
};
};
}
// lib.optionalAttrs (stateDir != null && !ephemeral && localState != null) {
"${name}/persistentvolume" = {
apiVersion = "v1";
kind = "PersistentVolume";
# Cluster-scoped: no namespace.
metadata = removeAttrs (metaFor stateClaim) [ "namespace" ];
spec = {
capacity.storage = "1Gi";
accessModes = [ "ReadWriteOnce" ];
persistentVolumeReclaimPolicy = "Retain";
storageClassName = stateClaim;
local.path = localState.hostDir;
nodeAffinity = labels.stateAffinity localState.service;
};
};
}