k8s: render the in-cluster services' manifests from their units
services.hyperhive.k8s.enable renders a Deployment (swarm-controller, swarm-notify-router) or CronJob (swarm-ci-metrics) per service this host deploys into system.build.k8sManifests. The renderer reads each evaluated unit: its environment, LoadCredential ids, state and runtime directories, ExecStart and timer. Files the unit names are mounted at the same paths, so the environment carries over as it is. Secrets are referenced by name only. No unit changes. checks: k8s-manifests-all-local (kubeconform -strict, offline schemas) and module-eval-k8s; the allLocal fixture turns the rendering on.
This commit is contained in:
parent
5a884a8d09
commit
d2acf3080a
2 changed files with 773 additions and 0 deletions
152
nix/module-eval/k8s.nix
Normal file
152
nix/module-eval/k8s.nix
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
# `checks.module-eval-k8s` — see ./lib.nix for the shared rationale. The k8s
|
||||
# manifests ../host-modules/k8s/render.nix builds from the single-host swarm's
|
||||
# units: each workload carries its unit's environment, no credential source
|
||||
# reaches the world-readable YAML, and the CronJob follows the timer.
|
||||
#
|
||||
# Every fixture is a shared one, so this cluster evaluates no host of its own.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
self,
|
||||
nixosSystem,
|
||||
shared,
|
||||
}:
|
||||
let
|
||||
inherit
|
||||
(import ./lib.nix {
|
||||
inherit
|
||||
pkgs
|
||||
lib
|
||||
self
|
||||
nixosSystem
|
||||
;
|
||||
})
|
||||
runGroup
|
||||
;
|
||||
|
||||
allLocal = shared.allLocal;
|
||||
|
||||
render =
|
||||
config:
|
||||
import ../host-modules/k8s/render.nix {
|
||||
inherit lib pkgs config;
|
||||
};
|
||||
objects = render allLocal;
|
||||
|
||||
units = {
|
||||
swarm-controller = allLocal.systemd.services.swarm-controller;
|
||||
swarm-notify-router =
|
||||
allLocal.containers.swarm-notify-router.config.systemd.services.swarm-notify-router;
|
||||
swarm-ci-metrics = allLocal.systemd.services.swarm-ci-metrics;
|
||||
};
|
||||
|
||||
podSpec =
|
||||
name:
|
||||
let
|
||||
o = objects."${name}/deployment" or objects."${name}/cronjob";
|
||||
in
|
||||
if o.kind == "CronJob" then o.spec.jobTemplate.spec.template.spec else o.spec.template.spec;
|
||||
container = name: lib.head (podSpec name).containers;
|
||||
envOf = name: lib.listToAttrs (map (e: lib.nameValuePair e.name e.value) (container name).env);
|
||||
|
||||
# The unit's environment as the pod should see it: no PATH, and `%d`
|
||||
# spelled out as the credential directory.
|
||||
unitEnv =
|
||||
name:
|
||||
lib.mapAttrs (
|
||||
_: v: builtins.replaceStrings [ "%d" ] [ "/run/credentials/${name}.service" ] (toString v)
|
||||
) (removeAttrs units.${name}.environment [ "PATH" ]);
|
||||
|
||||
# What the pod adds on top of the unit's own environment.
|
||||
added = {
|
||||
swarm-controller = {
|
||||
RUNTIME_DIRECTORY = "/run/swarm-controller";
|
||||
STATE_DIRECTORY = "/var/lib/swarm-controller";
|
||||
};
|
||||
swarm-notify-router.SWARM_NOTIFY_ROUTER_HEALTH_ADDR = "0.0.0.0:8080";
|
||||
swarm-ci-metrics.STATE_DIRECTORY = "/var/lib/swarm-ci-metrics";
|
||||
};
|
||||
|
||||
# Host paths the credentials are loaded from: the host units' own
|
||||
# `LoadCredential=id:source`, and the router container's nspawn flags.
|
||||
credentialSources =
|
||||
lib.concatMap
|
||||
(
|
||||
e:
|
||||
let
|
||||
parts = lib.splitString ":" e;
|
||||
in
|
||||
lib.optional (lib.length parts > 1) (lib.last parts)
|
||||
)
|
||||
(
|
||||
units.swarm-controller.serviceConfig.LoadCredential
|
||||
++ units.swarm-ci-metrics.serviceConfig.LoadCredential
|
||||
)
|
||||
++ map (f: lib.last (lib.splitString ":" f)) allLocal.containers.swarm-notify-router.extraFlags;
|
||||
credentialIds = lib.concatMap (
|
||||
u: map (e: lib.head (lib.splitString ":" e)) u.serviceConfig.LoadCredential
|
||||
) (lib.attrValues units);
|
||||
rendered = builtins.unsafeDiscardStringContext (builtins.toJSON objects);
|
||||
|
||||
ciTimer = allLocal.systemd.timers.swarm-ci-metrics.timerConfig;
|
||||
cron = objects."swarm-ci-metrics/cronjob".spec;
|
||||
|
||||
# The same host with one more variable on the poller's unit, naming a path
|
||||
# the pod does not mount.
|
||||
withStrayPath = lib.recursiveUpdate allLocal {
|
||||
systemd.services.swarm-ci-metrics.environment.SWARM_CI_METRICS_STRAY = "/etc/stray.json";
|
||||
};
|
||||
renders =
|
||||
config: (builtins.tryEval (builtins.deepSeq (builtins.toJSON (render config)) true)).success;
|
||||
|
||||
cases = [
|
||||
{
|
||||
# Each service's own credential path is the control: the comparison
|
||||
# reads a non-empty environment.
|
||||
name = "each workload carries its unit's environment, plus only its directories and health listener";
|
||||
ok =
|
||||
lib.all (name: envOf name == unitEnv name // added.${name}) (lib.attrNames units)
|
||||
&&
|
||||
(envOf "swarm-controller").BAO_CLIENT_CERT
|
||||
== "/run/credentials/swarm-controller.service/bao-client.pem"
|
||||
&& (envOf "swarm-notify-router") ? SWARM_NOTIFY_ROUTER_NATS_URL;
|
||||
}
|
||||
{
|
||||
# The ids are the control: they are in the same text, so the search
|
||||
# finds what is there.
|
||||
name = "the manifests name each credential by id and never the host path it is loaded from";
|
||||
ok =
|
||||
lib.length credentialSources >= 7
|
||||
&& !(lib.any (s: lib.hasInfix s rendered) credentialSources)
|
||||
&& lib.all (id: lib.hasInfix "\"${id}\"" rendered) credentialIds
|
||||
&& !(lib.any (o: o.kind == "Secret") (lib.attrValues objects))
|
||||
&&
|
||||
(lib.head (podSpec "swarm-controller").volumes).secret.secretName == "swarm-controller-credentials";
|
||||
}
|
||||
{
|
||||
name = "the poller's CronJob runs on its timer's interval and deadline, never overlapping";
|
||||
ok =
|
||||
ciTimer.OnUnitInactiveSec == "5min"
|
||||
&& cron.schedule == "*/5 * * * *"
|
||||
&& cron.concurrencyPolicy == "Forbid"
|
||||
&& cron.jobTemplate.spec.activeDeadlineSeconds == 1800;
|
||||
}
|
||||
{
|
||||
name = "the router's probes reach the listener its variable binds, and every pod outlasts the drain";
|
||||
ok =
|
||||
let
|
||||
c = container "swarm-notify-router";
|
||||
in
|
||||
(lib.head c.ports).containerPort == 8080
|
||||
&& c.readinessProbe.httpGet.path == "/readyz"
|
||||
&& c.livenessProbe.httpGet.path == "/healthz"
|
||||
&& lib.all (name: (podSpec name).terminationGracePeriodSeconds > 10) (lib.attrNames units);
|
||||
}
|
||||
{
|
||||
# The unchanged host is the control that the render itself succeeds.
|
||||
name = "a unit variable naming a path the pod does not mount fails the render";
|
||||
ok = renders allLocal && !(renders withStrayPath);
|
||||
}
|
||||
];
|
||||
in
|
||||
runGroup "k8s" cases
|
||||
|
|
@ -45,8 +45,10 @@ in
|
|||
|
||||
bareWithController = hiveWithController { };
|
||||
|
||||
# With the k8s manifests rendered too, which changes no unit.
|
||||
allLocal = hiveWithController {
|
||||
deploy.singleHostSwarm = true;
|
||||
k8s.enable = true;
|
||||
deploy.victorialogs.authPackage = marker "logs";
|
||||
deploy.victorialogs.grafanaAuthPackage = marker "logs-grafana";
|
||||
deploy.victoriametrics.authPackage = marker "metrics";
|
||||
|
|
|
|||
Loading…
Reference in a new issue