k8s: deploy.ci-metrics.runtime moves the CI run poller between its timer and the cluster
This commit is contained in:
parent
a542498510
commit
6bb155e5cf
1 changed files with 351 additions and 107 deletions
|
|
@ -7,7 +7,9 @@
|
|||
# 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
|
||||
# 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
|
||||
# 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) {
|
||||
"bao-ca.pem" = baoDeploy.serverCaFile;
|
||||
};
|
||||
in
|
||||
{
|
||||
|
||||
onK8s = cfg.runtime == "k8s";
|
||||
|
||||
# `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
|
||||
# it a host with the poller off gets the bundle unit and an `ExecStart`-less
|
||||
# `swarm-ci-metrics` service.
|
||||
imports = [
|
||||
(caTrust.trustBundle {
|
||||
inherit pkgs;
|
||||
trustBundle =
|
||||
enable:
|
||||
caTrust.trustBundle {
|
||||
inherit pkgs enable;
|
||||
name = unit;
|
||||
consumers = [ unit ];
|
||||
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 {
|
||||
type = lib.types.package;
|
||||
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarm-ci-metrics";
|
||||
description = "The CI run poller package.";
|
||||
};
|
||||
};
|
||||
|
||||
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.
|
||||
'';
|
||||
}
|
||||
];
|
||||
|
||||
# The poller's service and timer. The host runs them; with the poller in the
|
||||
# cluster they are only the source of the CronJob (`deploy.ci-metrics.unit`
|
||||
# and `.timer`).
|
||||
unitModule = {
|
||||
systemd.services.${unit} = {
|
||||
description = "push finished forge CI runs to the swarm metrics store";
|
||||
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;
|
||||
})
|
||||
];
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue