Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the queue's pod mounts the host unit's store in place

This commit is contained in:
atlas 2026-11-23 02:58:52 +01:00 • committed by maia
commit 140ee7f38a

View file

@ -2,24 +2,25 @@
# between their container and the host's cluster
# (`services.hyperhive.deploy.nats.runtime`), against the real secret store.
# The host boots with the queue in its container; the `k8s` specialisation has
# it in the cluster on a fresh JetStream store, and `k8s-open` adds
# it in the cluster on the container's own JetStream store, and `k8s-open` adds
# `deploy.nats.openFirewall`. The script switches to the cluster, opens the
# port, and back, and asserts that:
#
# - at most one server and one responder run at any moment of any switch (a
# sampler counts the processes every 200 ms), and the container and the pod
# never overlap;
# - the pod is Ready by its exec probe on the server's monitor, holds the store
# guard, the leaf and the callout config as init containers, and publishes
# the client port on every address and the exporter's on the bridge's;
# - the pod is Ready by its exec probe on the server's monitor, holds the leaf
# and the callout config as init containers, and publishes the client port
# on every address and the exporter's on the bridge's;
# - a client on the hive's bridge reaches the queue by name through the
# published port, and the responder in the pod admits the router's token;
# - the port is as closed to a host outside the node as the container's is, and
# open with `openFirewall`, where a second node dials it by name and verifies
# the name. The exporter's port answers the host's own processes only;
# - a stream written before the pod is deleted is read after;
# - the pod's state gate marks the empty store as fresh, and passes the volume
# with its marker after a pod delete;
# - the store is the container's own directory: a key the host server wrote is
# read by the pod, and a key the pod wrote is read by the host server after
# the switch back, which hands the files back to the server's user;
# - the switch back leaves no pod, Deployment or AddOn, and the container
# starts after the prune unit.
#
@ -177,13 +178,9 @@ pkgs.testers.runNixOSTest {
};
system.stateVersion = "25.11";
specialisation.k8s.configuration.services.hyperhive.deploy.nats = {
runtime = "k8s";
k8s.freshState = true;
};
specialisation.k8s.configuration.services.hyperhive.deploy.nats.runtime = "k8s";
specialisation.k8s-open.configuration.services.hyperhive.deploy.nats = {
runtime = "k8s";
k8s.freshState = true;
openFirewall = true;
};
@ -367,6 +364,8 @@ pkgs.testers.runNixOSTest {
"&& systemctl --machine=swarm-nats is-active swarm-nats-auth.service "
"&& [ \"$(pgrep -cx nats-server)\" = 1 ] && [ \"$(pgrep -cx swarm-nats-auth)\" = 1 ]"
)
BUCKET = "notify-router-state"
HOST_STORE = "/var/lib/nixos-containers/swarm-nats/var/lib/nats"
SUBJECT = "$SWARM.router.todo.atlas"
INBOX = "_INBOX.client.swarm-notify-router"
@ -422,7 +421,7 @@ pkgs.testers.runNixOSTest {
except Exception:
diagnose(["hyperhive-k8s-ca-trust.service"])
print(machine.execute(f"kubectl -n {NS} describe pod {name}")[1])
for container in ("state-gate", "tls-issue", "callout-config", SERVICE, "nats", "exporter"):
for container in ("tls-issue", "callout-config", SERVICE, "nats", "exporter"):
print(machine.execute(f"kubectl -n {NS} logs {name} -c {container} --tail=60")[1])
raise
return name
@ -574,6 +573,15 @@ pkgs.testers.runNixOSTest {
with subtest("the host's own processes read the exporter through the container"):
machine.succeed(f"curl -sf http://127.0.0.1:{METRICS}/metrics | grep -F gnatsd_varz_connections")
with subtest("on the host, the container's server takes a key into the router's state bucket, and the store's files belong to the server's user"):
nats(machine, f"kv add {BUCKET}")
nats(machine, f"kv put {BUCKET} host-key written-on-host")
assert "written-on-host" in nats(machine, f"kv get {BUCKET} host-key --raw")
HOST_UID = machine.succeed(f"stat -c %u {HOST_STORE}").strip()
# The control that the lookup finds files: the store is not empty.
assert int(machine.succeed(f"find {HOST_STORE} -type f | wc -l")) > 0
assert machine.succeed(f"find {HOST_STORE} ! -uid {HOST_UID} | wc -l").strip() == "0"
with subtest("the switch to the cluster stops the container before it applies the pod"):
out = switch("/run/booted-system/specialisation/k8s")
assert HOST_UNIT in out and "stopping the following units" in out, out
@ -586,9 +594,9 @@ pkgs.testers.runNixOSTest {
machine.fail("test -e /etc/nixos-containers/swarm-nats.conf")
assert counts() == (1, 1), counts()
with subtest("the pod holds the state gate, the leaf and the config as init containers, and publishes the client port on every address and the exporter's on the bridge"):
with subtest("the pod holds the leaf and the config as init containers, and publishes the client port on every address and the exporter's on the bridge"):
spec = pod(machine, pod_name)["spec"]
assert [c["name"] for c in spec["initContainers"]] == ["state-gate", "tls-issue", "callout-config"], spec["initContainers"]
assert [c["name"] for c in spec["initContainers"]] == ["tls-issue", "callout-config"], spec["initContainers"]
assert [c["name"] for c in spec["containers"]] == ["swarm-nats", "nats", "tls-renew", "exporter"], spec["containers"]
assert spec["shareProcessNamespace"] is True, spec
server = next(c for c in spec["containers"] if c["name"] == "nats")
@ -604,7 +612,7 @@ pkgs.testers.runNixOSTest {
# Ready is the exec probe's and the responder's: every container is.
statuses = {c["name"]: c["ready"] for c in pod(machine, pod_name)["status"]["containerStatuses"]}
assert statuses == {"swarm-nats": True, "nats": True, "tls-renew": True, "exporter": True}, statuses
claim = json.loads(kube(machine, f"get pvc {SERVICE}-state -o json"))
claim = json.loads(kube(machine, f"get pvc {SERVICE}-store -o json"))
assert claim["status"]["phase"] == "Bound", claim
STATE_VOLUME = claim["metadata"]["uid"]
# The leaf's key is in memory.
@ -614,8 +622,6 @@ pkgs.testers.runNixOSTest {
machine.fail(f"grep -F '{mount}/nats-config ' /proc/mounts")
issued = machine.succeed(f"kubectl -n {NS} logs {pod_name} -c tls-issue")
assert f"requesting the queue's TLS leaf for {NAME} from the store" in issued, issued
guard = machine.succeed(f"kubectl -n {NS} logs {pod_name} -c state-gate")
assert "the install is fresh: marked" in guard, guard
# The pod's server keeps the descriptors the container's did.
limits = machine.succeed("grep 'Max open files' /proc/$(pgrep -ox nats-server)/limits")
print(limits)
@ -632,6 +638,19 @@ pkgs.testers.runNixOSTest {
assert stream_messages() == 1
machine.wait_until_succeeds(f"[ \"$(grep -c '/api/oidc/introspection ${service}:{SECRET}' ${tokenLog})\" -ge 2 ]", timeout=60)
with subtest("the pod's volume is the container's own directory: it reads the host's key, writes one of its own, and leaves files the host's user does not own"):
pv = json.loads(kube(machine, f"get pv {SERVICE}-store -o json"))
assert pv["spec"]["local"]["path"] == HOST_STORE, pv["spec"]
assert pv["status"]["phase"] == "Bound" and pv["spec"]["claimRef"]["name"] == f"{SERVICE}-store", pv
assert pv["spec"]["persistentVolumeReclaimPolicy"] == "Retain", pv["spec"]
assert claim["spec"]["volumeName"] == pv["metadata"]["name"], claim["spec"]
assert "written-on-host" in nats(machine, f"kv get {BUCKET} host-key --raw")
nats(machine, f"kv put {BUCKET} pod-key written-by-pod")
assert "written-by-pod" in nats(machine, f"kv get {BUCKET} pod-key --raw")
# The stream the pod made is its own, so the host's user has to take it
# back when the container starts.
assert int(machine.succeed(f"find {HOST_STORE} ! -uid {HOST_UID} | wc -l")) > 0
with subtest("a client on the hive's bridge reaches the pod through the published port, and a host outside the node does not"):
assert machine.execute(f"ip netns exec agent nc -z -w 4 {BRIDGE} {PORT}")[0] == 0
assert machine.execute(f"ip netns exec agent nc -z -w 4 {BRIDGE} {METRICS}")[0] != 0
@ -662,11 +681,11 @@ pkgs.testers.runNixOSTest {
old_uid = pod(machine, pod_name)["metadata"]["uid"]
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}-state -o json"))["metadata"]["uid"] == STATE_VOLUME
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)
assert stream_messages() == 1
guard = machine.succeed(f"kubectl -n {NS} logs {pod_name} -c state-gate")
assert "state volume is migrated" in guard, guard
assert "written-on-host" in nats(machine, f"kv get {BUCKET} host-key --raw")
assert "written-by-pod" in nats(machine, f"kv get {BUCKET} pod-key --raw")
assert counts() == (1, 1), counts()
with subtest("with openFirewall a host outside the node dials the pod by name and verifies it, and is admitted"):
@ -702,9 +721,15 @@ pkgs.testers.runNixOSTest {
requires = machine.succeed(f"systemctl show -p Requires --value {HOST_UNIT}")
assert f"{PRUNE}.service" in requires, requires
# The volume is kept for the next switch.
assert json.loads(kube(machine, f"get pvc {SERVICE}-state -o json"))["metadata"]["uid"] == STATE_VOLUME
assert json.loads(kube(machine, f"get pvc {SERVICE}-store -o json"))["metadata"]["uid"] == STATE_VOLUME
settle(QUEUE_UP, 600, [HOST_UNIT])
assert counts() == (1, 1), counts()
# The container's server reads what the pod wrote: its unit took the
# files back before the server started.
machine.wait_until_succeeds(nats_cmd(SERVICES_ROOT, f"kv get {BUCKET} pod-key --raw") + " | grep -F written-by-pod", timeout=120)
assert "written-on-host" in nats(machine, f"kv get {BUCKET} host-key --raw")
assert stream_messages() == 1
assert machine.succeed(f"find {HOST_STORE} ! -uid {HOST_UID} | wc -l").strip() == "0"
# Closed to a host outside the node again, open to the node's own.
assert not reachable(outside, NODE, PORT), "the container's port is open to a host outside the node"
assert reachable(outside, NODE, CONTROL)