Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the queue's client port is a loadbalancer service

The queue's client port was a hostPort on 0.0.0.0, which Pod Security baseline and restricted forbid. It is a LoadBalancer Service now, which k3s's service load balancer binds on every address of the node with a hostPort of its own in kube-system, so hives on other hosts keep their URL. No node port is allocated. The stt policy allows the load balancer's pods in place of the queue's, and the prune unit waits for them to go before the host unit starts. The egress published-port rule and the raw-table rules apply to the load balancer's hostPort as to the pod's.
This commit is contained in:
atlas 2026-11-23 19:09:02 +01:00 • committed by mara
commit 796fd6201f

View file

@ -10,8 +10,8 @@
# 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 leaf
# and the callout config as init containers, and publishes the client port
# on every address, with no `hostPort` on the exporter;
# and the callout config as init containers, and has no `hostPort`: the client
# port is a LoadBalancer Service with no node port, bound by the load balancer;
# - with the egress intercept on, a client on the hive's bridge reaches the
# queue by name through the published port and not by the pod's address or
# the node's, and the responder in the pod admits the router's token;
@ -22,8 +22,8 @@
# - 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, Service, NetworkPolicy or AddOn,
# and the container starts after the prune unit.
# - the switch back leaves no pod, Service, NetworkPolicy or AddOn, and the
# container starts after the prune unit.
#
# One stub is the identity provider: it introspects the test's one token as the
# notification router, whose permissions create and fill a stream. The test
@ -623,14 +623,14 @@ pkgs.testers.runNixOSTest {
machine.fail("test -e /etc/nixos-containers/swarm-nats.conf")
assert counts() == (1, 1), counts()
with subtest("the pod holds the leaf and the config as init containers, publishes the client port on every address, and has no hostPort on the exporter"):
with subtest("the pod holds the leaf and the config as init containers, and has no hostPort on the server or the exporter"):
spec = pod(machine, pod_name)["spec"]
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")
client = next(p for p in server["ports"] if p["name"] == "client")
assert client["hostPort"] == PORT and client["hostIP"] == "0.0.0.0", client
assert client == {"name": "client", "containerPort": PORT, "protocol": "TCP"}, client
command = server["readinessProbe"]["exec"]["command"]
assert command[0] == "/bin/wget" and command[-1].endswith("/healthz?js-enabled-only=true"), command
exporter = next(c for c in spec["containers"] if c["name"] == "exporter")
@ -664,6 +664,26 @@ pkgs.testers.runNixOSTest {
assert policy["policyTypes"] == ["Ingress"], policy
assert policy["ingress"] == [{"ports": [{"port": PORT, "protocol": "TCP"}]}], policy
with subtest("the client port is a LoadBalancer Service with no node port, which the cluster's load balancer binds on the node from kube-system"):
machine.wait_until_succeeds(
f"kubectl -n {NS} get service {SERVICE} -o jsonpath='{{.status.loadBalancer.ingress[0].ip}}' | grep -q .",
timeout=300,
)
svc = json.loads(kube(machine, f"get service {SERVICE} -o json"))
assert svc["spec"]["type"] == "LoadBalancer", svc["spec"]
assert svc["spec"]["allocateLoadBalancerNodePorts"] is False, svc["spec"]
assert [(p["port"], p["targetPort"]) for p in svc["spec"]["ports"]] == [(PORT, "client")], svc["spec"]
assert not any("nodePort" in p for p in svc["spec"]["ports"]), svc["spec"]
lb_pods = json.loads(kube(machine, f"-n kube-system get pods --selector svccontroller.k3s.cattle.io/svcname={SERVICE} -o json"))["items"]
assert len(lb_pods) == 1, lb_pods
lb_ports = [p for c in lb_pods[0]["spec"]["containers"] for p in c.get("ports", [])]
assert [(p["hostPort"], p["containerPort"]) for p in lb_ports] == [(PORT, PORT)], lb_ports
assert all(p.get("hostIP", "") in ("", "0.0.0.0") for p in lb_ports), lb_ports
# No node port: the Service has a rule for its cluster address, the
# control that the lookup finds it, and none in the chain of node ports.
assert f"hyperhive/{SERVICE}:client" in machine.succeed("iptables -t nat -S KUBE-SERVICES")
assert f"hyperhive/{SERVICE}:client" not in machine.succeed("iptables -t nat -S KUBE-NODEPORTS")
with subtest("one server and one responder run, and the queue answers by name with its name verified"):
assert counts() == (1, 1), counts()
assert probe(machine, BRIDGE)[0], probe(machine, BRIDGE)
@ -769,7 +789,12 @@ pkgs.testers.runNixOSTest {
assert pods() == [], pods()
assert machine.execute(f"kubectl -n {NS} get deployment {SERVICE}")[0] != 0
assert machine.execute(f"kubectl -n {NS} get service {METRICS_SERVICE}")[0] != 0
assert machine.execute(f"kubectl -n {NS} get service {SERVICE}")[0] != 0
assert machine.execute(f"kubectl -n {NS} get networkpolicy {SERVICE}")[0] != 0
# The load balancer's pod and its `hostPort` rule are gone before the
# container listens on the port.
assert machine.succeed(f"kubectl -n kube-system get pods --selector svccontroller.k3s.cattle.io/svcname={SERVICE} -o name").strip() == ""
assert f"--dport {PORT} " not in machine.succeed("iptables -t nat -S")
assert machine.execute(f"kubectl -n kube-system get addons.k3s.cattle.io {SERVICE}")[0] != 0
assert machine.execute(f"test -e {MANIFESTS}/{SERVICE}.yaml")[0] != 0
assert machine.execute(f"test -e {MANIFESTS}/{SERVICE}.yaml.pruning")[0] != 0