Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/vm-tests/k8s-swarm-images.nix

224 lines
7.2 KiB
Nix

# `vmTests.<system>.k8s-swarm-images`: the swarm daemons' images
# (`packages.<system>.image-*`) run as pods on a single-node k3s
# (`lib/k3s-node.nix`) with no store and no queue behind them. The script
# asserts:
#
# - the node is Ready within 300 s of boot, and containerd lists every image;
# - `swarm-logs-auth` and `swarm-metrics-auth` keep running with no restart,
# `/healthz` answers 200 and `/readyz` 503, so the pod is not Ready: the store
# address points at a closed port;
# - `swarm-notify-router` answers the same while it retries its first store
# read, and a pod delete in that retry ends it with exit 0 within 10 s;
# - a delete of the idle `swarm-logs-auth` ends it with exit 0 within 10 s;
# - `swarm-controller`, `swarm-nats-auth` and `swarm-ci-metrics` start their
# own binary, which prints its own error for the missing configuration and
# exits.
#
# Every pod names its image as `imageName:imageTag` with
# `imagePullPolicy: Never`. The pods are JSON files under
# `/etc/k8s-swarm-images/`, so the script has no Nix in it.
{ pkgs, self }:
let
inherit (pkgs) lib;
image = name: self.packages.${pkgs.stdenv.hostPlatform.system}."image-${name}";
daemons = [
"swarm-controller"
"swarm-nats-auth"
"swarm-logs-auth"
"swarm-metrics-auth"
"swarm-notify-router"
"swarm-ci-metrics"
];
healthPort = 8080;
# A store address where nothing listens and identity files that do not
# exist: every login fails at once.
storeEnv = lib.mapAttrsToList lib.nameValuePair {
BAO_ADDR = "https://127.0.0.1:1";
BAO_CLIENT_CERT = "/nonexistent/cert.pem";
BAO_CLIENT_KEY = "/nonexistent/key.pem";
};
probe = path: {
httpGet = {
inherit path;
port = healthPort;
};
periodSeconds = 5;
};
mkPod =
{
name,
args ? [ ],
env ? [ ],
restartPolicy ? "Always",
socketDir ? false,
}:
{
apiVersion = "v1";
kind = "Pod";
metadata = {
inherit name;
labels.app = name;
};
spec = {
inherit restartPolicy;
containers = [
(
{
inherit name args env;
image = "${(image name).imageName}:${(image name).imageTag}";
imagePullPolicy = "Never";
}
// lib.optionalAttrs (restartPolicy == "Always") {
readinessProbe = probe "/readyz";
}
// lib.optionalAttrs socketDir {
volumeMounts = [
{
name = "sock";
mountPath = "/sock";
}
];
}
)
];
volumes = lib.optional socketDir {
name = "sock";
emptyDir = { };
};
};
};
pods = {
swarm-logs-auth = mkPod {
name = "swarm-logs-auth";
args = [
"--socket=/sock/s.sock"
"--surface=logs"
"--health-addr=0.0.0.0:${toString healthPort}"
];
env = storeEnv;
socketDir = true;
};
swarm-metrics-auth = mkPod {
name = "swarm-metrics-auth";
args = [
"--socket=/sock/s.sock"
"--store-path=swarm/stub"
"--health-addr=0.0.0.0:${toString healthPort}"
];
env = storeEnv;
socketDir = true;
};
swarm-notify-router = mkPod {
name = "swarm-notify-router";
env = storeEnv ++ [
(lib.nameValuePair "SWARM_NOTIFY_ROUTER_OIDC_CLIENT_ID" "stub")
(lib.nameValuePair "SWARM_NOTIFY_ROUTER_HEALTH_ADDR" "0.0.0.0:${toString healthPort}")
];
};
}
// lib.genAttrs [ "swarm-controller" "swarm-nats-auth" "swarm-ci-metrics" ] (
name:
mkPod {
inherit name;
restartPolicy = "Never";
}
);
in
pkgs.testers.runNixOSTest {
name = "k8s-swarm-images";
# Under the 20 min budget for one dispatched run; the runner's job bound is 1 h.
globalTimeout = 1500;
nodes.machine = {
imports = [
(import ./lib/k3s-node.nix {
images = map image daemons;
memoryMiB = 4096;
})
];
environment.etc = lib.mapAttrs' (
name: p: lib.nameValuePair "k8s-swarm-images/${name}.json" { text = builtins.toJSON p; }
) pods;
};
testScript = builtins.readFile ./lib/k3s.py + ''
PODS = "/etc/k8s-swarm-images"
HEALTH = 8080
def apply(name: str) -> None:
kube(machine, f"apply -f {PODS}/{name}.json")
def assert_serving_unready(name: str) -> None:
p = wait_pod(machine, name, phase="Running", timeout=60)
assert not is_ready(p), f"{name} is Ready with no store: {p['status']}"
assert http_status(machine, name, HEALTH, "/healthz") == 200
assert http_status(machine, name, HEALTH, "/readyz") == 503
def assert_stops_cleanly(name: str) -> None:
terminated, took = delete_pod(machine, name)
print(f"{name}: deleted in {took:.1f} s, terminated {terminated}")
assert terminated.get("exitCode") == 0, f"{name}: {terminated}"
assert took <= 10, f"{name}: the delete took {took:.1f} s"
with subtest("the node is Ready and holds every image"):
wait_node_ready(machine, timeout=300)
wait_images(machine)
with subtest("the store responders serve health and stay unready"):
apply("swarm-logs-auth")
apply("swarm-metrics-auth")
for name in ["swarm-logs-auth", "swarm-metrics-auth"]:
assert_serving_unready(name)
time.sleep(30)
for name in ["swarm-logs-auth", "swarm-metrics-auth"]:
state = container_state(machine, name)
assert "running" in state["state"], f"{name}: {state}"
assert state["restartCount"] == 0, f"{name}: {state}"
assert http_status(machine, name, HEALTH, "/healthz") == 200
assert http_status(machine, name, HEALTH, "/readyz") == 503
with subtest("an idle swarm-logs-auth stops on a pod delete"):
assert_stops_cleanly("swarm-logs-auth")
with subtest("swarm-notify-router stops on a pod delete during its startup retry"):
apply("swarm-notify-router")
assert_serving_unready("swarm-notify-router")
machine.wait_until_succeeds(
"kubectl logs swarm-notify-router | grep -q 'reading the queue client secret failed'",
timeout=20,
)
state = container_state(machine, "swarm-notify-router")
assert "running" in state["state"], state
assert state["restartCount"] == 0, state
assert_stops_cleanly("swarm-notify-router")
with subtest("the remaining images start their own binary"):
expected = {
"swarm-controller": "binding",
"swarm-nats-auth": "Usage: swarm-nats-auth",
"swarm-ci-metrics": "SWARM_CI_METRICS_OIDC_CLIENT_ID is unset",
}
for name in expected:
apply(name)
for name, line in expected.items():
wait_pod(machine, name, phase="Failed", timeout=60)
terminated = container_state(machine, name)["state"]["terminated"]
assert terminated["reason"] == "Error", f"{name}: {terminated}"
logs = kube(machine, f"logs {name}")
print(f"{name}: exit {terminated['exitCode']}: {logs}")
assert line in logs, f"{name}: no {line!r} in its log"
'';
}