From 67bf51809a1b3f0b937e2f788d5e8e206c247413 Mon Sep 17 00:00:00 2001 From: atlas Date: Thu, 26 Nov 2026 09:12:15 +0100 Subject: [PATCH] k8s vm tests: create the agent netns before k3s starts a pod, and delete pods strictly again --- docs/4-implementation/k8s/node.md | 4 ++++ nix/vm-tests/k8s-bao-login.nix | 6 ++++-- nix/vm-tests/k8s-nats.nix | 22 ++++++++-------------- nix/vm-tests/lib/agent-netns.nix | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 nix/vm-tests/lib/agent-netns.nix diff --git a/docs/4-implementation/k8s/node.md b/docs/4-implementation/k8s/node.md index 02884024c..3946c661f 100644 --- a/docs/4-implementation/k8s/node.md +++ b/docs/4-implementation/k8s/node.md @@ -44,6 +44,10 @@ cluster of its own beyond that one main node, and it replicates no service. pods' ServiceAccount tokens, described under [Logging in to the secret store](#logging-in-to-the-secret-store). +The first `ip netns add` since boot, which makes `/run/netns` a mount point, +leaves the node's running pods stuck Terminating until a reboot: it mounts +`/run/netns` over their network namespaces, which containerd then can't remove. + ## What it leaves alone The cluster holds the workload of the swarm services the host enables, and no diff --git a/nix/vm-tests/k8s-bao-login.nix b/nix/vm-tests/k8s-bao-login.nix index 464350a9e..b571da2ba 100644 --- a/nix/vm-tests/k8s-bao-login.nix +++ b/nix/vm-tests/k8s-bao-login.nix @@ -127,7 +127,10 @@ pkgs.testers.runNixOSTest { }; in { - imports = [ self.nixosModules.default ]; + imports = [ + self.nixosModules.default + ./lib/agent-netns.nix + ]; services.hyperhive = { deploy.k8s.enable = true; @@ -370,7 +373,6 @@ pkgs.testers.runNixOSTest { with subtest("a client on the hive bridge reaches the control port and is refused at the pods' port"): machine.wait_for_unit("bridge-control.service") machine.succeed( - "ip netns add agent; " "ip link add agent0 type veth peer name agent1; " "ip link set agent0 master hive-br0; ip link set agent0 up; " "ip link set agent1 netns agent; " diff --git a/nix/vm-tests/k8s-nats.nix b/nix/vm-tests/k8s-nats.nix index aace6d217..cc79a0458 100644 --- a/nix/vm-tests/k8s-nats.nix +++ b/nix/vm-tests/k8s-nats.nix @@ -134,7 +134,10 @@ pkgs.testers.runNixOSTest { nodes.machine = { config, ... }: { - imports = [ self.nixosModules.default ]; + imports = [ + self.nixosModules.default + ./lib/agent-netns.nix + ]; services.hyperhive = { deploy.k8s.enable = true; @@ -367,15 +370,6 @@ pkgs.testers.runNixOSTest { return machine.succeed(f"kubectl -n {NS} get pods --selector app.kubernetes.io/name={SERVICE} -o name || true").split() - def live_pods() -> list[str]: - """The pods that are not terminating: a deleted pod whose sandbox the - runtime can't remove stays in the list.""" - return machine.succeed( - f"kubectl -n {NS} get pods --selector app.kubernetes.io/name={SERVICE} -o json " - "| jq -r '.items[] | select(.metadata.deletionTimestamp == null) | .metadata.name'" - ).split() - - def pod_pid(container: str) -> str: id_ = machine.succeed(f"k3s crictl ps --name '^{container}$' --state running -q").split()[0] return machine.succeed(f"k3s crictl inspect {id_} | jq -r .info.pid").strip() @@ -543,7 +537,7 @@ pkgs.testers.runNixOSTest { # The bridge's client: an agent's network namespace on the hive's bridge. BRIDGE_IF = machine.succeed("ip -o -4 addr show | grep -F 'inet ${bridgeIp}/' | cut -d' ' -f2").strip() machine.succeed( - "ip netns add agent && ip link add va type veth peer name vb && ip link set vb netns agent " + "ip link add va type veth peer name vb && ip link set vb netns agent " f"&& ip link set va master {BRIDGE_IF} && ip link set va up " "&& ip -n agent addr add ${agentIp}/24 dev vb && ip -n agent link set vb up && ip -n agent link set lo up " "&& ip -n agent route add default via ${bridgeIp}" @@ -600,7 +594,7 @@ pkgs.testers.runNixOSTest { old_uid = pod(machine, pod_name)["metadata"]["uid"] kube(machine, f"label {node} {STATE_LABEL}-") assert STATE_LABEL not in node_labels() - kube(machine, f"delete pod {pod_name} --wait=false") + kube(machine, f"delete pod {pod_name} --wait=true --timeout=120s", 150) unscheduled = ( f"kubectl -n {NS} get pods --selector app.kubernetes.io/name={SERVICE} -o json " "| jq -r '.items[].status.conditions[]? | select(.type == \"PodScheduled\" and .status == \"False\") | .message'" @@ -712,7 +706,7 @@ pkgs.testers.runNixOSTest { with subtest("deleting the pod keeps the stream: the volume is the same and the message is read after"): old_uid = pod(machine, pod_name)["metadata"]["uid"] - kube(machine, f"delete pod {pod_name} --wait=false") + kube(machine, f"delete pod {pod_name} --wait=true --timeout=120s", 150) pod_name = wait_pod_ready(old_uid) assert json.loads(kube(machine, f"get pvc {SERVICE}-store -o json"))["metadata"]["uid"] == STATE_VOLUME machine.wait_until_succeeds(nats_cmd(SERVICES_ROOT, f"stream info {STREAM}"), timeout=120) @@ -722,7 +716,7 @@ pkgs.testers.runNixOSTest { with subtest("with openFirewall a host outside the node dials the pod by name and verifies it, and is admitted"): switch("/run/booted-system/specialisation/k8s-open") - assert len(live_pods()) == 1, live_pods() + assert len(pods()) == 1, pods() machine.wait_until_succeeds(f"nc -z -w 4 {NODE} {PORT}", timeout=60) assert reachable(outside, NODE, PORT), "openFirewall left the pod's port closed to a host outside the node" root = machine.succeed(f"cat {SERVICES_ROOT}") diff --git a/nix/vm-tests/lib/agent-netns.nix b/nix/vm-tests/lib/agent-netns.nix new file mode 100644 index 000000000..a6cb20466 --- /dev/null +++ b/nix/vm-tests/lib/agent-netns.nix @@ -0,0 +1,19 @@ +# A node module for a VM test whose script gives the hive bridge a client in the +# network namespace `agent`. The namespace is created before k3s starts, so +# the `ip netns add` that makes `/run/netns` a mount point has run before any +# pod's namespace is mounted there. Run later, it mounts `/run/netns` over the +# pods' namespaces, and containerd then cannot remove them: a deleted pod stays +# Terminating until the node reboots. +{ pkgs, ... }: +{ + systemd.services.agent-netns = { + wantedBy = [ "multi-user.target" ]; + before = [ "k3s.service" ]; + path = [ pkgs.iproute2 ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = "ip netns add agent"; + }; +}