Watch
0
0
Fork
You've already forked hyperhive
0

k8s: deploy.ci-metrics.runtime moves the CI run poller between its timer and the cluster

This commit is contained in:
atlas 2026-11-22 02:52:20 +01:00 • committed by maia
commit 5454ebbd4d
4 changed files with 351 additions and 107 deletions

View file

@ -74,8 +74,12 @@ let
# which the kubelet cannot probe. `publicCredentials` maps the ids of the # which the kubelet cannot probe. `publicCredentials` maps the ids of the
# credentials that hold a public CA file to their key in the CA ConfigMap. # credentials that hold a public CA file to their key in the CA ConfigMap.
# `switch` is null for a service that always runs on the host, and for one # `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 that # with a `runtime` option names the option's value and the host unit or units
# runs the service when it is `"host"`. # that run the service when it is `"host"`. `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
# not carry one (../swarm-otel.nix, the swarm tier's own pipeline).
services = { services = {
swarm-controller = { swarm-controller = {
inherit (deployCfg.swarm-controller) enable package; inherit (deployCfg.swarm-controller) enable package;
@ -84,6 +88,7 @@ let
healthEnv = null; healthEnv = null;
etcFiles = [ "hyperhive/swarm-controller.json" ]; etcFiles = [ "hyperhive/swarm-controller.json" ];
publicCredentials = { }; publicCredentials = { };
otlp = "hive";
switch = null; switch = null;
}; };
swarm-notify-router = { swarm-notify-router = {
@ -93,6 +98,7 @@ let
healthEnv = "SWARM_NOTIFY_ROUTER_HEALTH_ADDR"; healthEnv = "SWARM_NOTIFY_ROUTER_HEALTH_ADDR";
etcFiles = [ ]; etcFiles = [ ];
publicCredentials.notify-router-bao-server-ca = baoServerCaKey; publicCredentials.notify-router-bao-server-ca = baoServerCaKey;
otlp = "hive";
switch = { switch = {
inherit (deployCfg.notify-router) runtime; inherit (deployCfg.notify-router) runtime;
hostUnit = "container@swarm-notify-router.service"; hostUnit = "container@swarm-notify-router.service";
@ -105,6 +111,7 @@ let
healthEnv = "SWARM_STT_HEALTH_ADDR"; healthEnv = "SWARM_STT_HEALTH_ADDR";
etcFiles = [ ]; etcFiles = [ ];
publicCredentials.stt-bao-server-ca = baoServerCaKey; publicCredentials.stt-bao-server-ca = baoServerCaKey;
otlp = "hive";
switch = { switch = {
inherit (deployCfg.stt) runtime; inherit (deployCfg.stt) runtime;
hostUnit = "container@swarm-stt.service"; hostUnit = "container@swarm-stt.service";
@ -112,21 +119,32 @@ let
}; };
swarm-ci-metrics = { swarm-ci-metrics = {
inherit (deployCfg.ci-metrics) enable package; inherit (deployCfg.ci-metrics) enable package;
unit = config.systemd.services.swarm-ci-metrics; unit = deployCfg.ci-metrics.unit;
timer = config.systemd.timers.swarm-ci-metrics; timer = deployCfg.ci-metrics.timer;
kind = "CronJob"; kind = "CronJob";
healthEnv = null; healthEnv = null;
etcFiles = [ ]; etcFiles = [ ];
publicCredentials = { }; publicCredentials."bao-ca.pem" = baoServerCaKey;
switch = null; 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"
];
};
}; };
}; };
# Where a pod pushes telemetry: the hive collector, by the name the node's # Where a pod pushes telemetry. A `"hive"` service pushes to the hive
# CoreDNS answers with the bridge address (./host-services.nix). A unit's own # collector, by the name the node's CoreDNS answers with the bridge address
# endpoint is a loopback forwarder or a gateway route that needs a token, and # (./host-services.nix): a unit's own endpoint is a loopback forwarder or a
# a pod has neither. `otlpEndpointVars` are the variables a unit may name its # gateway route that needs a token, and a pod has neither. An `"own"` service
# endpoint in; the first is the standard one and is always set. # keeps its unit's endpoint, a swarm route it logs in to by itself.
# `otlpEndpointVars` are the variables a unit may name its endpoint in; the
# first is the standard one and is always set for a `"hive"` service.
otelCfg = hyperhiveCfg.otel; otelCfg = hyperhiveCfg.otel;
collectorEndpoint = "http://${otelCfg.collector.host}:${toString hyperhiveCfg.swarm.hiveOtel.collectorPort}"; collectorEndpoint = "http://${otelCfg.collector.host}:${toString hyperhiveCfg.swarm.hiveOtel.collectorPort}";
otlpEndpointVars = [ otlpEndpointVars = [
@ -134,8 +152,8 @@ let
"SWARM_CI_METRICS_OTLP_ENDPOINT" "SWARM_CI_METRICS_OTLP_ENDPOINT"
]; ];
podOtlpEnv = podOtlpEnv =
unitEnv: svc: unitEnv:
lib.optionalAttrs otelCfg.collector.enable ( lib.optionalAttrs (svc.otlp == "hive" && otelCfg.collector.enable) (
lib.genAttrs (lib.filter ( lib.genAttrs (lib.filter (
n: n == "OTEL_EXPORTER_OTLP_ENDPOINT" || unitEnv ? ${n} n: n == "OTEL_EXPORTER_OTLP_ENDPOINT" || unitEnv ? ${n}
) otlpEndpointVars) (_: collectorEndpoint) ) otlpEndpointVars) (_: collectorEndpoint)
@ -289,7 +307,7 @@ let
else else
unitEnv unitEnv
) )
// podOtlpEnv unitEnv // podOtlpEnv svc unitEnv
// lib.optionalAttrs (runtimeDir != null) { RUNTIME_DIRECTORY = runtimeDir; } // lib.optionalAttrs (runtimeDir != null) { RUNTIME_DIRECTORY = runtimeDir; }
// lib.optionalAttrs (stateDir != null) { STATE_DIRECTORY = stateDir; } // lib.optionalAttrs (stateDir != null) { STATE_DIRECTORY = stateDir; }
// lib.optionalAttrs (svc.healthEnv != null) { // lib.optionalAttrs (svc.healthEnv != null) {
@ -628,6 +646,8 @@ let
in in
if !(lib.all (p: lib.elem (lib.getName p) nixosDefaultPath) unit.path) then if !(lib.all (p: lib.elem (lib.getName p) nixosDefaultPath) unit.path) then
fail name "the unit puts packages on PATH, which the image does not carry" fail name "the unit puts packages on PATH, which the image does not carry"
else if svc.otlp == "own" && !(lib.any (n: unitEnv ? ${n}) otlpEndpointVars) then
fail name "the unit names no OTLP endpoint of its own, which the pod would push to"
else if svc.healthEnv != null && unitEnv ? ${svc.healthEnv} then else if svc.healthEnv != null && unitEnv ? ${svc.healthEnv} then
fail name "the unit already sets ${svc.healthEnv}" fail name "the unit already sets ${svc.healthEnv}"
else if lib.head execWords != entrypoint then else if lib.head execWords != entrypoint then

View file

@ -1,7 +1,7 @@
# Which of a service's two homes runs it: its host unit, or its objects in the # 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. A service with a `runtime` option is on the cluster when the option
# cluster when the option says `"k8s"`, and then only: its host unit is gone # says `"k8s"`, and then only: its host unit or units are gone from the
# from the configuration, and its manifests are in `services.k3s.manifests`. # configuration, and its manifests are in `services.k3s.manifests`.
# The cluster does not remove what a manifest created once the manifest is # 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 # 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. # unit starts. docs/4-implementation/k8s/runtime.md has the design.
@ -91,8 +91,8 @@ in
}; };
}) })
# A service back on the host first loses its objects. The unit that # A service back on the host first loses its objects. The units that
# starts the host copy requires this one, so the pod is gone before the # 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. # host copy starts, and a failure here leaves the host copy stopped.
# #
# Its marker is the manifest's link in the cluster's manifests # Its marker is the manifest's link in the cluster's manifests
@ -107,8 +107,8 @@ in
lib.nameValuePair "${name}-k8s-prune" { lib.nameValuePair "${name}-k8s-prune" {
description = "delete ${name}'s objects from the cluster before its host unit starts"; description = "delete ${name}'s objects from the cluster before its host unit starts";
after = [ "k3s.service" ]; after = [ "k3s.service" ];
before = [ s.hostUnit ]; before = lib.toList s.hostUnit;
requiredBy = [ s.hostUnit ]; requiredBy = lib.toList s.hostUnit;
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;

View file

@ -7,7 +7,9 @@
# sees and pushes each newly finished run's duration and queue wait to the # sees and pushes each newly finished run's duration and queue wait to the
# swarm collector, stamped at the run's end. The first pass, with no cursor in # swarm collector, stamped at the run's end. The first pass, with no cursor in
# the state directory, walks every run back to the first. docs/4-implementation/swarm/ci-metrics.md # the state directory, walks every run back to the first. docs/4-implementation/swarm/ci-metrics.md
# has the design. # has the design. With `deploy.ci-metrics.runtime = "k8s"` the host has neither
# the service nor the timer: the poller is a CronJob in the host's cluster,
# rendered from the unit and timer below (docs/4-implementation/k8s/runtime.md).
# #
# It reaches the forge, authelia, the store and the collector by name, so # It reaches the forge, authelia, the store and the collector by name, so
# nothing here asks which host runs any of them. Its one credential on disk is # nothing here asks which host runs any of them. Its one credential on disk is
@ -56,84 +58,26 @@ let
// lib.optionalAttrs (baoDeploy.serverCaFile != null) { // lib.optionalAttrs (baoDeploy.serverCaFile != null) {
"bao-ca.pem" = baoDeploy.serverCaFile; "bao-ca.pem" = baoDeploy.serverCaFile;
}; };
in
{ onK8s = cfg.runtime == "k8s";
# `hostUnit`: a host service, so the bundle reads the host CA path and waits # `hostUnit`: a host service, so the bundle reads the host CA path and waits
# on `hive-tls-ca.service`. `enable`: `imports` is unconditional, so without # on `hive-tls-ca.service`. `enable`: `imports` is unconditional, so without
# it a host with the poller off gets the bundle unit and an `ExecStart`-less # it a host with the poller off gets the bundle unit and an `ExecStart`-less
# `swarm-ci-metrics` service. # `swarm-ci-metrics` service.
imports = [ trustBundle =
(caTrust.trustBundle { enable:
inherit pkgs; caTrust.trustBundle {
inherit pkgs enable;
name = unit; name = unit;
consumers = [ unit ]; consumers = [ unit ];
hostUnit = true; hostUnit = true;
inherit (cfg) enable;
})
];
options.services.hyperhive.swarm.ci-metrics.clientId = lib.mkOption {
type = lib.types.str;
readOnly = true;
default = "swarm-ci-metrics";
description = ''
OAuth2 client id the CI run poller authenticates to the swarm
collector as, and the audience it asks its token for.
Read-only because three hosts have to agree on it: authelia registers
the client (`glue-ci-metrics-otel-oidc-client.nix`), the swarm collector
admits it on the poller's receiver (`swarm.otel.serviceProducers`), and
authelia's mint stores its secret in the store.
'';
};
options.services.hyperhive.deploy.ci-metrics = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Run the CI run poller on this host: every 5 minutes it pushes each
newly finished forge Actions run's duration and queue wait to the
swarm collector, for the Grafana CI dashboard. Its first run pushes
every run since the first.
Requires the poller's store identity,
{option}`services.hyperhive.deploy.bao.ciMetricsClientCertFile` and
`ciMetricsClientKeyFile`. Run it on one host per swarm: two pollers
keep two cursors and push every run twice.
'';
}; };
package = lib.mkOption { # The poller's service and timer. The host runs them; with the poller in the
type = lib.types.package; # cluster they are only the source of the CronJob (`deploy.ci-metrics.unit`
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarm-ci-metrics"; # and `.timer`).
description = "The CI run poller package."; unitModule = {
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = haveClientIdentity;
message = ''
services.hyperhive.deploy.ci-metrics.enable requires the poller's
swarm-secret-store client identity: set both
services.hyperhive.deploy.bao.ciMetricsClientCertFile
services.hyperhive.deploy.bao.ciMetricsClientKeyFile
swarm-ci-metrics.service reads its forge token and its OIDC client
secret with it.
Both default to the pair glue-bao-tls.nix mints when
services.hyperhive.deploy.bao.enable is true (the host that runs
the store). Otherwise copy ci-metrics.pem and ci-metrics-key.pem
from the store host's /var/lib/swarm-bao-pki/ and name them in
those two options.
'';
}
];
systemd.services.${unit} = { systemd.services.${unit} = {
description = "push finished forge CI runs to the swarm metrics store"; description = "push finished forge CI runs to the swarm metrics store";
after = [ "network-online.target" ]; after = [ "network-online.target" ];
@ -184,4 +128,164 @@ in
}; };
}; };
}; };
# The unit and timer evaluated as a host evaluates them, for a host that
# does not run them.
podSystem =
(import "${pkgs.path}/nixos/lib/eval-config.nix" {
modules = [
(
{ options, ... }:
{
config = {
nixpkgs =
if options.nixpkgs ? hostPlatform then
{ inherit (pkgs.stdenv) hostPlatform; }
else
{ localSystem = pkgs.stdenv.hostPlatform; };
boot.isNspawnContainer = true;
networking.hostName = lib.mkDefault unit;
networking.useDHCP = false;
};
}
)
(trustBundle true)
unitModule
];
system = null;
}).config;
in
{
imports = [ (trustBundle (cfg.enable && !onK8s)) ];
options.services.hyperhive.swarm.ci-metrics.clientId = lib.mkOption {
type = lib.types.str;
readOnly = true;
default = "swarm-ci-metrics";
description = ''
OAuth2 client id the CI run poller authenticates to the swarm
collector as, and the audience it asks its token for.
Read-only because three hosts have to agree on it: authelia registers
the client (`glue-ci-metrics-otel-oidc-client.nix`), the swarm collector
admits it on the poller's receiver (`swarm.otel.serviceProducers`), and
authelia's mint stores its secret in the store.
'';
};
options.services.hyperhive.deploy.ci-metrics = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Run the CI run poller on this host: every 5 minutes it pushes each
newly finished forge Actions run's duration and queue wait to the
swarm collector, for the Grafana CI dashboard. Its first run pushes
every run since the first.
Requires the poller's store identity,
{option}`services.hyperhive.deploy.bao.ciMetricsClientCertFile` and
`ciMetricsClientKeyFile`. Run it on one host per swarm: two pollers
keep two cursors and push every run twice.
'';
};
package = lib.mkOption {
type = lib.types.package;
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarm-ci-metrics";
description = "The CI run poller package.";
};
runtime = lib.mkOption {
type = lib.types.enum [
"host"
"k8s"
];
default = "host";
description = ''
Where the CI run poller runs: `host` as the `swarm-ci-metrics` service
and timer, or `k8s` as a CronJob in this host's cluster
({option}`services.hyperhive.deploy.k8s.enable`), which the host's
configuration must enable. A switch moves the poller: the service and
timer go and the CronJob is applied, or the CronJob is deleted and the
service and timer return, so one poller runs at any time. Its cursor is
in the service's state directory on the host, and in a volume of the
cluster in a pod; neither moves with the poller, so the first pass after
a switch pushes every run again, as the same points.
The poller keeps pushing to the swarm collector's route of its own,
not to the hive collector the cluster's other pods push to.
'';
};
unit = lib.mkOption {
type = lib.types.raw;
readOnly = true;
internal = true;
default = if onK8s then podSystem.systemd.services.${unit} else config.systemd.services.${unit};
description = ''
The poller's evaluated systemd service, which the cluster's CronJob is
rendered from (./k8s/render.nix). It is the service the host runs, or,
with the poller in the cluster, the same service evaluated alone.
'';
};
timer = lib.mkOption {
type = lib.types.raw;
readOnly = true;
internal = true;
default = if onK8s then podSystem.systemd.timers.${unit} else config.systemd.timers.${unit};
description = ''
The poller's evaluated systemd timer, which the CronJob's schedule is
rendered from, taken as {option}`unit` is.
'';
};
};
config = lib.mkMerge [
{
assertions = [
{
assertion = !onK8s || hyperhiveCfg.deploy.k8s.enable;
message = ''
services.hyperhive.deploy.ci-metrics.runtime = "k8s" needs the
cluster: set services.hyperhive.deploy.k8s.enable on this host, or
leave the poller's runtime at "host".
'';
}
];
}
(lib.mkIf cfg.enable {
assertions = [
{
assertion = haveClientIdentity;
message = ''
services.hyperhive.deploy.ci-metrics.enable requires the poller's
swarm-secret-store client identity: set both
services.hyperhive.deploy.bao.ciMetricsClientCertFile
services.hyperhive.deploy.bao.ciMetricsClientKeyFile
swarm-ci-metrics.service reads its forge token and its OIDC client
secret with it.
Both default to the pair glue-bao-tls.nix mints when
services.hyperhive.deploy.bao.enable is true (the host that runs
the store). Otherwise copy ci-metrics.pem and ci-metrics-key.pem
from the store host's /var/lib/swarm-bao-pki/ and name them in
those two options.
'';
}
];
})
(lib.mkIf (cfg.enable && !onK8s) unitModule)
# The pod reaches the store, authelia, the forge and the collector by
# name, which the cluster resolves to the hive bridge.
(lib.mkIf (cfg.enable && onK8s) {
services.hyperhive.network.enable = lib.mkDefault true;
})
];
} }

View file

@ -49,14 +49,15 @@ let
}; };
withNode = hiveWithController nodeSettings; withNode = hiveWithController nodeSettings;
# The same host with the router and the speech-to-text service in the # The same host with the router, the speech-to-text service and the CI run
# cluster, so it has neither container. A second full host, because the claim # poller in the cluster, so it has neither container nor poller unit. A
# is about what the configuration drops; the other cases of this group read # second full host, because the claim is about what the configuration drops;
# `withNode`. # the other cases of this group read `withNode`.
onCluster = hiveWithController ( onCluster = hiveWithController (
lib.recursiveUpdate nodeSettings { lib.recursiveUpdate nodeSettings {
deploy.notify-router.runtime = "k8s"; deploy.notify-router.runtime = "k8s";
deploy.stt.runtime = "k8s"; deploy.stt.runtime = "k8s";
deploy.ci-metrics.runtime = "k8s";
} }
); );
@ -64,6 +65,7 @@ let
runtimeRefused = hive { runtimeRefused = hive {
deploy.notify-router.runtime = "k8s"; deploy.notify-router.runtime = "k8s";
deploy.stt.runtime = "k8s"; deploy.stt.runtime = "k8s";
deploy.ci-metrics.runtime = "k8s";
}; };
runtimeRefusals = lib.filter (a: !a.assertion) runtimeRefused.assertions; runtimeRefusals = lib.filter (a: !a.assertion) runtimeRefused.assertions;
@ -150,9 +152,8 @@ let
SWARM_STT_HEALTH_ADDR = "0.0.0.0:8080"; SWARM_STT_HEALTH_ADDR = "0.0.0.0:8080";
STATE_DIRECTORY = "/var/lib/swarm-stt"; STATE_DIRECTORY = "/var/lib/swarm-stt";
}; };
# No endpoint: the poller keeps the route its unit names.
swarm-ci-metrics = { swarm-ci-metrics = {
OTEL_EXPORTER_OTLP_ENDPOINT = collectorEndpoint;
SWARM_CI_METRICS_OTLP_ENDPOINT = collectorEndpoint;
STATE_DIRECTORY = "/var/lib/swarm-ci-metrics"; STATE_DIRECTORY = "/var/lib/swarm-ci-metrics";
}; };
}; };
@ -197,7 +198,12 @@ let
# The same host with one more variable on the poller's unit, naming a path # The same host with one more variable on the poller's unit, naming a path
# the pod does not mount. # the pod does not mount.
withStrayPath = lib.recursiveUpdate allLocal { withStrayPath = lib.recursiveUpdate allLocal {
systemd.services.swarm-ci-metrics.environment.SWARM_CI_METRICS_STRAY = "/etc/stray.json"; services.hyperhive.deploy.ci-metrics.unit.environment.SWARM_CI_METRICS_STRAY = "/etc/stray.json";
};
# The same host with the poller's unit naming no endpoint of its own.
withoutOwnEndpoint = lib.recursiveUpdate allLocal {
services.hyperhive.deploy.ci-metrics.unit.environment.SWARM_CI_METRICS_OTLP_ENDPOINT = null;
}; };
renders = renders =
config: (builtins.tryEval (builtins.deepSeq (builtins.toJSON (render config)) true)).success; config: (builtins.tryEval (builtins.deepSeq (builtins.toJSON (render config)) true)).success;
@ -206,6 +212,7 @@ let
manifestNames = c: lib.sort lib.lessThan (lib.attrNames c.services.k3s.manifests); manifestNames = c: lib.sort lib.lessThan (lib.attrNames c.services.k3s.manifests);
routerPrune = withNode.systemd.services.swarm-notify-router-k8s-prune; routerPrune = withNode.systemd.services.swarm-notify-router-k8s-prune;
sttPrune = withNode.systemd.services.swarm-stt-k8s-prune; sttPrune = withNode.systemd.services.swarm-stt-k8s-prune;
ciPrune = withNode.systemd.services.swarm-ci-metrics-k8s-prune;
clusterRouterSpec = (render onCluster)."swarm-notify-router/deployment".spec.template.spec; clusterRouterSpec = (render onCluster)."swarm-notify-router/deployment".spec.template.spec;
cases = [ cases = [
{ {
@ -314,7 +321,7 @@ let
# granter's role unit (which enables the login's auth mount) starts the # granter's role unit (which enables the login's auth mount) starts the
# same command, and the manifests are the same derivation as without the # same command, and the manifests are the same derivation as without the
# node. # node.
name = "the node adds only k3s, the jwt unit and the prune units of the router and the speech-to-text service to the all-local host, and changes no container, timer or manifest"; name = "the node adds only k3s, the jwt unit and the prune units of the router, the speech-to-text service and the CI run poller to the all-local host, and changes no container, timer or manifest";
ok = ok =
let let
names = a: lib.attrNames a; names = a: lib.attrNames a;
@ -330,10 +337,12 @@ let
|| n == "swarm-bao-k8s-jwt-policy" || n == "swarm-bao-k8s-jwt-policy"
|| n == "swarm-notify-router-k8s-prune" || n == "swarm-notify-router-k8s-prune"
|| n == "swarm-stt-k8s-prune" || n == "swarm-stt-k8s-prune"
|| n == "swarm-ci-metrics-k8s-prune"
) added ) added
&& lib.elem "swarm-bao-k8s-jwt-policy" added && lib.elem "swarm-bao-k8s-jwt-policy" added
&& lib.elem "swarm-notify-router-k8s-prune" added && lib.elem "swarm-notify-router-k8s-prune" added
&& lib.elem "swarm-stt-k8s-prune" added && lib.elem "swarm-stt-k8s-prune" added
&& lib.elem "swarm-ci-metrics-k8s-prune" added
&& &&
lib.all lib.all
( (
@ -410,21 +419,35 @@ let
{ {
# The units' own endpoints are the control: each differs from the # The units' own endpoints are the control: each differs from the
# collector's, so the override replaces something. The host's units keep # collector's, so the override replaces something. The host's units keep
# theirs. # theirs. The poller is a swarm-tier producer: its pod keeps the swarm
name = "every pod pushes telemetry to the hive collector, and the host units keep their endpoints"; # collector's route of its own, spelled out here, and names no hive
# collector in any variable.
name = "every hive-tier pod pushes telemetry to the hive collector, the poller's pod keeps its swarm route, and the host units keep their endpoints";
ok = ok =
lib.all ( let
hiveTier = lib.filter (n: n != "swarm-ci-metrics") (lib.attrNames units);
ownRoute = "https://${allLocal.services.hyperhive.swarm.otel.domain}/swarm-ci-metrics";
in
lib.length hiveTier == 3
&& lib.all (
name: name:
(envOf name).OTEL_EXPORTER_OTLP_ENDPOINT == collectorEndpoint (envOf name).OTEL_EXPORTER_OTLP_ENDPOINT == collectorEndpoint
&& (units.${name}.environment.OTEL_EXPORTER_OTLP_ENDPOINT or null) != collectorEndpoint && (units.${name}.environment.OTEL_EXPORTER_OTLP_ENDPOINT or null) != collectorEndpoint
) (lib.attrNames units) ) hiveTier
&& units.swarm-ci-metrics.environment.SWARM_CI_METRICS_OTLP_ENDPOINT != collectorEndpoint && units.swarm-ci-metrics.environment.SWARM_CI_METRICS_OTLP_ENDPOINT == ownRoute
&& (envOf "swarm-ci-metrics").SWARM_CI_METRICS_OTLP_ENDPOINT == collectorEndpoint && (envOf "swarm-ci-metrics").SWARM_CI_METRICS_OTLP_ENDPOINT == ownRoute
&& !((envOf "swarm-ci-metrics") ? OTEL_EXPORTER_OTLP_ENDPOINT)
&& !(lib.any (e: lib.hasInfix "hive-collector" (e.value or "")) (container "swarm-ci-metrics").env)
&& !(envOf "swarm-notify-router" ? SWARM_CI_METRICS_OTLP_ENDPOINT) && !(envOf "swarm-notify-router" ? SWARM_CI_METRICS_OTLP_ENDPOINT)
&& &&
allLocal.systemd.services.swarm-controller.environment.OTEL_EXPORTER_OTLP_ENDPOINT allLocal.systemd.services.swarm-controller.environment.OTEL_EXPORTER_OTLP_ENDPOINT
== "https://${allLocal.services.hyperhive.swarm.otel.domain}/${allLocal.services.hyperhive.swarm.otel.producerName}"; == "https://${allLocal.services.hyperhive.swarm.otel.domain}/${allLocal.services.hyperhive.swarm.otel.producerName}";
} }
{
# The unchanged host is the control that the render itself succeeds.
name = "a poller whose unit names no endpoint of its own fails the render, as the pod would have nowhere to push";
ok = renders allLocal && !(renders withoutOwnEndpoint);
}
{ {
# Off, the collector's configuration holds nothing of the pod logs, and # Off, the collector's configuration holds nothing of the pod logs, and
# on, taking that out leaves the configuration it has off. The host with # on, taking that out leaves the configuration it has off. The host with
@ -701,7 +724,7 @@ let
&& !(allLocal.systemd.services ? hyperhive-k8s-ca-trust); && !(allLocal.systemd.services ? hyperhive-k8s-ca-trust);
} }
{ {
name = "a router or a speech-to-text service in the cluster leaves the host no container and no prune unit, and applies the namespace and its objects"; name = "a router, a speech-to-text service or a poller in the cluster leaves the host no container, service, timer or prune unit, and applies the namespace and its objects";
ok = ok =
!(onCluster.containers ? swarm-notify-router) !(onCluster.containers ? swarm-notify-router)
&& !(onCluster.systemd.services ? "container@swarm-notify-router") && !(onCluster.systemd.services ? "container@swarm-notify-router")
@ -709,10 +732,15 @@ let
&& !(onCluster.containers ? swarm-stt) && !(onCluster.containers ? swarm-stt)
&& !(onCluster.systemd.services ? "container@swarm-stt") && !(onCluster.systemd.services ? "container@swarm-stt")
&& !(onCluster.systemd.services ? swarm-stt-k8s-prune) && !(onCluster.systemd.services ? swarm-stt-k8s-prune)
&& !(onCluster.systemd.services ? swarm-ci-metrics)
&& !(onCluster.systemd.timers ? swarm-ci-metrics)
&& !(onCluster.systemd.services ? swarm-ci-metrics-k8s-prune)
&& !(onCluster.systemd.services ? swarm-ci-metrics-ca-bundle)
&& &&
manifestNames onCluster == [ manifestNames onCluster == [
"coredns-custom" "coredns-custom"
"hyperhive-namespace" "hyperhive-namespace"
"swarm-ci-metrics"
"swarm-notify-router" "swarm-notify-router"
"swarm-stt" "swarm-stt"
] ]
@ -728,6 +756,13 @@ let
"Deployment" "Deployment"
"PersistentVolumeClaim" "PersistentVolumeClaim"
"ServiceAccount" "ServiceAccount"
]
&&
lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-ci-metrics.content)
== [
"CronJob"
"PersistentVolumeClaim"
"ServiceAccount"
]; ];
} }
{ {
@ -975,12 +1010,97 @@ let
&& spec.securityContext.seccompProfile.type == "RuntimeDefault"; && spec.securityContext.seccompProfile.type == "RuntimeDefault";
} }
{ {
name = "the router's and the speech-to-text service's runtime \"k8s\" needs the node"; name = "the router's, the speech-to-text service's and the poller's runtime \"k8s\" needs the node";
ok = ok =
lib.any (a: lib.hasInfix "notify-router.runtime = \"k8s\" needs the" a.message) runtimeRefusals lib.any (a: lib.hasInfix "notify-router.runtime = \"k8s\" needs the" a.message) runtimeRefusals
&& lib.any (a: lib.hasInfix "stt.runtime = \"k8s\" needs the" a.message) runtimeRefusals && lib.any (a: lib.hasInfix "stt.runtime = \"k8s\" needs the" a.message) runtimeRefusals
&& lib.any (a: lib.hasInfix "ci-metrics.runtime = \"k8s\" needs the" a.message) runtimeRefusals
&& lib.all (a: a.assertion) onCluster.assertions; && lib.all (a: a.assertion) onCluster.assertions;
} }
{
# The host with the poller on it is the control for the stand-alone
# evaluation: the unit and timer read there are what the host runs.
name = "the poller's unit and timer, and so its CronJob, are the same whether the host or the stand-alone evaluation supplies them";
ok =
let
unitOf = c: c.services.hyperhive.deploy.ci-metrics.unit;
timerOf = c: c.services.hyperhive.deploy.ci-metrics.timer;
in
(unitOf withNode).serviceConfig.Type == "oneshot"
&& (unitOf withNode).environment ? SWARM_CI_METRICS_OTLP_ENDPOINT
&& (unitOf onCluster).serviceConfig == (unitOf withNode).serviceConfig
&& (unitOf onCluster).environment == (unitOf withNode).environment
&& (timerOf withNode).timerConfig.OnUnitInactiveSec == "5min"
&& (timerOf onCluster).timerConfig == (timerOf withNode).timerConfig
&& (render onCluster)."swarm-ci-metrics/cronjob" == (render withNode)."swarm-ci-metrics/cronjob";
}
{
# The host's own unit and timer are pinned by value, and both are held
# back by the prune unit. The host with the poller in the cluster is the
# control that the lookups find what is there.
name = "with the poller on the host, its service, timer and CA bundle are as the module defines them, and its prune unit runs before both";
ok =
let
sc = withNode.systemd.services.swarm-ci-metrics.serviceConfig;
timer = withNode.systemd.timers.swarm-ci-metrics;
in
sc.Type == "oneshot"
&& sc.DynamicUser
&& sc.StateDirectory == "swarm-ci-metrics"
&& sc.TimeoutStartSec == "30min"
&& lib.any (e: lib.hasPrefix "bao-client.pem:" e) sc.LoadCredential
&& timer.timerConfig.OnUnitInactiveSec == "5min"
&& lib.elem "timers.target" timer.wantedBy
&& withNode.systemd.services.swarm-ci-metrics.environment ? SSL_CERT_FILE
&& withNode.systemd.services ? swarm-ci-metrics-ca-bundle
&&
ciPrune.before == [
"swarm-ci-metrics.timer"
"swarm-ci-metrics.service"
]
&&
ciPrune.requiredBy == [
"swarm-ci-metrics.timer"
"swarm-ci-metrics.service"
]
&& lib.hasInfix "swarm-ci-metrics.yaml" ciPrune.script
&& lib.hasInfix "delete deployment,cronjob,serviceaccount" ciPrune.script
&& !(allLocal.systemd.services ? swarm-ci-metrics-k8s-prune);
}
{
# The poller's one credential in the pod is the store's public CA, from
# the ConfigMap the host publishes, and no volume of it names a Secret,
# which nothing creates. The controller's pod is the control that the
# lookup finds a Secret where there is one.
name = "the poller's pod gets the store's CA from the ConfigMap, drops its client certificate and key, and mounts no Secret";
ok =
let
spec = (render onCluster)."swarm-ci-metrics/cronjob".spec.jobTemplate.spec.template.spec;
volumesOf = s: s.volumes;
usesSecret = v: v ? secret || lib.any (src: src ? secret) (v.projected.sources or [ ]);
cred = lib.findFirst (v: v.name == "credentials") null spec.volumes;
serverCa = onCluster.services.hyperhive.deploy.bao.serverCaFile;
in
serverCa != null
&& cred != null
&&
map (s: s.configMap.items) cred.projected.sources == [
[
{
key = "bao-server-ca.pem";
path = "bao-ca.pem";
}
]
]
&& !(lib.any usesSecret (volumesOf spec))
&& lib.any usesSecret (volumesOf (podSpec "swarm-controller"))
&& (lib.head spec.containers).env != [ ]
&& lib.any (
e: e.name == "BAO_CACERT" && e.value == "/run/credentials/swarm-ci-metrics.service/bao-ca.pem"
) (lib.head spec.containers).env
&& lib.all (v: lib.all (e: e.name != v) (lib.head spec.containers).env) clientVars
&& !(lib.hasInfix "swarm-ci-metrics-credentials" rendered);
}
]; ];
in in
runGroup "k8s" cases runGroup "k8s" cases