Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/module-eval/k8s.nix

267 lines
11 KiB
Nix

# `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. And the
# k3s node ../host-modules/k8s/node.nix adds to that host: present with
# `deploy.k8s.enable`, absent and changing nothing without it.
#
# The all-local host with the node on is this cluster's own fixture; the
# rest are shared ones.
{
pkgs,
lib,
self,
nixosSystem,
shared,
}:
let
inherit
(import ./lib.nix {
inherit
pkgs
lib
self
nixosSystem
;
})
runGroup
hive
hiveWithController
;
allLocal = shared.allLocal;
# The four auth responder packages pinned to four distinct derivations, as
# in ./shared.nix: the all-local host needs them set.
marker = name: pkgs.runCommand "auth-marker-${name}" { } "mkdir $out";
# The all-local host with the node on. It does not set
# `services.hyperhive.k8s.enable`: the node turns the rendering on.
withNode = hiveWithController {
deploy.singleHostSwarm = true;
deploy.k8s.enable = true;
deploy.victorialogs.authPackage = marker "logs";
deploy.victorialogs.grafanaAuthPackage = marker "logs-grafana";
deploy.victoriametrics.authPackage = marker "metrics";
deploy.victoriametrics.agentAuthPackage = marker "metrics-agent";
};
# A node every assertion refuses: no rendering, a pod range containing the
# hive bridge's subnet, and a service range inside the pod range.
nodeRefused = hive {
deploy.k8s.enable = true;
k8s.enable = false;
network.enable = true;
deploy.k8s.podCidr = "10.42.0.0/16";
deploy.k8s.serviceCidr = "10.42.128.0/17";
};
refusals = lib.filter (a: !a.assertion) nodeRefused.assertions;
refuses = needle: lib.any (a: lib.hasInfix needle a.message) refusals;
render =
config:
(import ../host-modules/k8s/render.nix {
inherit lib pkgs config;
}).objects;
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);
}
{
# The host without the node is the control that the lookups find what
# is there: the same paths on the host with it on.
name = "the node exists only where deploy.k8s.enable is set";
ok =
!allLocal.services.k3s.enable
&& !(allLocal.systemd.services ? k3s)
&& allLocal.services.k3s.images == [ ]
&& !(lib.elem "cni0" allLocal.networking.firewall.trustedInterfaces)
&& withNode.services.k3s.enable
&& withNode.services.k3s.role == "server"
&& withNode.systemd.services ? k3s
&& lib.elem "cni0" withNode.networking.firewall.trustedInterfaces
&& lib.elem "flannel.1" withNode.networking.firewall.trustedInterfaces;
}
{
name = "the node runs k3s with traefik and metrics-server off and its secrets encrypted";
ok =
let
start = withNode.systemd.services.k3s.serviceConfig.ExecStart;
in
lib.hasInfix "--disable=traefik" start
&& lib.hasInfix "--disable=metrics-server" start
&& lib.hasInfix "--secrets-encryption" start
&& lib.hasInfix "--cluster-cidr=10.200.0.0/16" start
&& lib.hasInfix "--service-cidr=10.201.0.0/16" start
&& !(lib.hasInfix "--disable=coredns" start)
&& !(lib.hasInfix "--disable=local-storage" start);
}
{
# The images are the ones the manifests run, one per rendered service:
# the three workloads are the control that the list is not empty.
name = "the node loads the image of every service the manifests run, and k3s's own";
ok =
let
loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages;
in
lib.length rendered == 3
&& lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 4;
}
{
# Nothing the host already runs differs: every unit, timer and
# container is still there, the only new units are k3s's, every unit but
# the firewall (whose rules carry the trusted interfaces) starts the same
# command, and the manifests are the same derivation as without the node.
name = "the node changes no unit, container, timer or manifest of the all-local host";
ok =
let
names = a: lib.attrNames a;
added = lib.subtractLists (names allLocal.systemd.services) (names withNode.systemd.services);
in
names allLocal.systemd.timers == names withNode.systemd.timers
&& names allLocal.containers == names withNode.containers
&& lib.subtractLists (names withNode.systemd.services) (names allLocal.systemd.services) == [ ]
&& lib.elem "k3s" added
&& lib.all (n: lib.hasPrefix "k3s" n) added
&& lib.all (
n:
withNode.systemd.services.${n}.serviceConfig.ExecStart or null
== allLocal.systemd.services.${n}.serviceConfig.ExecStart or null
) (lib.remove "firewall" (names allLocal.systemd.services))
&& withNode.system.build.k8sManifests.drvPath == allLocal.system.build.k8sManifests.drvPath;
}
{
# The all-local host's assertions are the control: they all hold.
name = "the node refuses a host that does not render the manifests it loads";
ok = lib.all (a: a.assertion) withNode.assertions && refuses "deploy.k8s.enable loads the images";
}
{
# The default ranges on the all-local host, whose bridge is up, are the
# control: the same assertions hold there, and the flags carry the ranges.
name = "the node refuses pod and service ranges that overlap the hive bridge or each other";
ok =
withNode.services.hyperhive.network.enable
&& withNode.services.hyperhive.network.networkCidr == "10.42.0.0/24"
&& withNode.services.hyperhive.deploy.k8s.podCidr == "10.200.0.0/16"
&& withNode.services.hyperhive.deploy.k8s.serviceCidr == "10.201.0.0/16"
&& refuses "overlap the hive bridge"
&& refuses "overlap each other";
}
];
in
runGroup "k8s" cases