From f7696a59c12dad57ce62d76130394dfd70b14418 Mon Sep 17 00:00:00 2001 From: atlas Date: Thu, 26 Nov 2026 01:31:33 +0100 Subject: [PATCH] k8s-join: wait through a pull from a peer that fails first, and print the registry state when it keeps failing --- docs/4-implementation/k8s/images.md | 5 ++-- nix/vm-tests/k8s-join.nix | 41 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/docs/4-implementation/k8s/images.md b/docs/4-implementation/k8s/images.md index 4d16d2dc9..b1a928961 100644 --- a/docs/4-implementation/k8s/images.md +++ b/docs/4-implementation/k8s/images.md @@ -83,8 +83,9 @@ enables for mirroring, and the upstream registry doesn't have to exist host and not `"*"`, so the node's pulls of public images stay direct. A pod that lands on a node without its image, a node that joined, pulls the -image from a peer that holds it (`imagePullPolicy: IfNotPresent`). If no peer -holds it, for instance because the main node is down and the node never had +image from a peer that holds it (`imagePullPolicy: IfNotPresent`). The first +pull on a node that has just joined can end in `ErrImagePull`, and the kubelet +retries a failed pull. If no peer holds it, for instance because the main node is down and the node never had the image, the pull fails with the host's name in the error, and the pod retries once a peer has the image. Images a node holds stay: k3s doesn't prune imported tarball images. The mirror doesn't share a `latest` tag, and diff --git a/nix/vm-tests/k8s-join.nix b/nix/vm-tests/k8s-join.nix index 4a6621bac..818b065b3 100644 --- a/nix/vm-tests/k8s-join.nix +++ b/nix/vm-tests/k8s-join.nix @@ -260,6 +260,41 @@ pkgs.testers.runNixOSTest { return pod(main, name)["status"].get("containerStatuses", [{}])[0] + def wait_running(name: str, timeout: int) -> None: + """Waits until pod `name` runs. A pull can fail before the joining machine knows + which peer holds the image, and the kubelet retries it, so those errors are + waited through.""" + deadline = time.monotonic() + timeout + pull_errors: set[str] = set() + while True: + p = pod(main, name) + if p["status"].get("phase") == "Running" and is_ready(p): + print(f"{name}: running after pull errors {sorted(pull_errors)}") + return + reason = first_status(name).get("state", {}).get("waiting", {}).get("reason") + if reason in ("ErrImagePull", "ImagePullBackOff"): + pull_errors.add(reason) + else: + assert reason in (None, "ContainerCreating"), (name, first_status(name)) + assert time.monotonic() < deadline, (name, p["status"]) + time.sleep(3) + + + def dump(title: str, node: QemuMachine, command: str) -> None: + print(f"--- {title}") + print(node.execute(command)[1]) + + + def diagnose_pull() -> None: + for node in (main, joiner): + dump(f"{node.name}: containerd config", node, "cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml") + dump(f"{node.name}: registries", node, "cat /etc/rancher/k3s/registries.yaml; ls /var/lib/rancher/k3s/agent/etc/containerd/certs.d/ 2>&1") + dump(f"{node.name}: images", node, "k3s ctr images ls -q") + dump(f"{node.name}: content", node, "k3s ctr content ls | head -50") + dump("main: pod events", main, "kubectl get events --sort-by=.lastTimestamp | tail -30") + dump("joiner: pull through its own mirror", joiner, "curl -sk -o /dev/null -w '%{http_code}' https://127.0.0.1:6443/v2/ ; echo; journalctl -u k3s --no-pager -n 60 | grep -i -E 'spegel|registry|pull' | tail -30") + + with subtest("the main node is Ready and holds the probe image"): main.succeed(f"install -d -m 0755 {LEAF_DIR}") wait_node_ready(main, timeout=300) @@ -348,7 +383,11 @@ pkgs.testers.runNixOSTest { main.succeed(f"k3s ctr images ls -q | grep -qxF '{containerd_name}'") assert joiner.execute(f"k3s ctr images ls -q | grep -qxF '{containerd_name}'")[0] != 0, "the machine held the image already" main.succeed("kubectl apply -f /etc/k8s-join/pod-on-joiner.json") - wait_pod(main, "on-joiner", phase="Running", ready=True, timeout=300) + try: + wait_running("on-joiner", 300) + except AssertionError: + diagnose_pull() + raise assert pod(main, "on-joiner")["spec"]["nodeName"] == "joiner" joiner.succeed(f"k3s ctr images ls -q | grep -qxF '{containerd_name}'") events = kube(main, "get events --field-selector involvedObject.name=on-joiner -o json")