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 maia
commit 9752f30f36
8 changed files with 200 additions and 75 deletions

View file

@ -95,7 +95,10 @@ Job:
2. Wait for the cluster's API, then delete the service's Deployment, CronJob, 2. Wait for the cluster's API, then delete the service's Deployment, CronJob,
Service, NetworkPolicy and ServiceAccount (selected by Service, NetworkPolicy and ServiceAccount (selected by
`app.kubernetes.io/name`), and wait until its pods have gone. A pod exits 0 `app.kubernetes.io/name`), and wait until its pods have gone. A pod exits 0
within its termination grace period. within its termination grace period. Then wait until the pods that the
service load balancer bound the service's Services' ports with, in
`kube-system`, have gone too: the rule that forwards a port to one stays
until the pod does.
3. Delete the cluster's AddOn for the file, so that a later switch to `"k8s"` 3. Delete the cluster's AddOn for the file, so that a later switch to `"k8s"`
applies the same manifest again. Then remove the renamed link. applies the same manifest again. Then remove the renamed link.
@ -218,21 +221,27 @@ holds the container back until the pod has gone.
loopback, so the responder dials the server by the name its leaf carries and loopback, so the responder dials the server by the name its leaf carries and
verifies it, with no traffic leaving the pod. Every other client reaches the verifies it, with no traffic leaving the pod. Every other client reaches the
queue by the same name as before: the node's CoreDNS and the bridge's queue by the same name as before: the node's CoreDNS and the bridge's
resolver answer it with the bridge's address, and the published port forwards resolver answer it with the bridge's address, and the load balancer's port
it to the pod. forwards it to the pod.
- **The client port.** The server's port is a `hostPort` on `0.0.0.0`, the - **The client port.** The server's port is a `LoadBalancer` Service,
addresses the container's server listens on, so hives on other hosts keep `swarm-nats`, with no `hostPort` on the pod. k3s's service load balancer
their URL. A `hostPort` is a DNAT rule, and the firewall's INPUT rules, the (servicelb, on by default) binds the port on `0.0.0.0` of the node with a
ones `deploy.nats.openFirewall` and the bridge's port list write, never see `hostPort` of its own, in a pod of `kube-system`, and forwards it to the pod
what it rewrites. The module therefore adds rules to the raw table, which is through the Service, so hives on other hosts keep their URL. The Service
read before the DNAT: a packet for the node's address and the client port is allocates no node port: the forwarding uses the Service's cluster address,
and a node port would be a second way in that the raw-table rules don't name.
The server sees the load balancer's pod as the client, not the client's
address. A `hostPort` is a DNAT rule, and the firewall's INPUT rules, the ones
`deploy.nats.openFirewall` and the bridge's port list write, never see what it
rewrites. The module therefore adds rules to the raw table, which the kernel
reads before the DNAT: a packet for the node's address and the client port is
dropped unless it arrives on the host's loopback, the hive bridge or a pod dropped unless it arrives on the host's loopback, the hive bridge or a pod
interface, or unless `deploy.nats.openFirewall` is on. The container's port interface, or unless `deploy.nats.openFirewall` is on. The container's port
has that reach. The hostPort sends the bridge's agents to the cluster, which a has that reach. The `hostPort` sends the bridge's agents to the cluster, which
host with the egress intercept on refuses, so the module also adds the port to a host with the egress intercept on refuses, so the module also adds the port
`egress.publishedPorts` to `egress.publishedPorts`
([the egress chain](../networking/network.md#agent-bridge-egress-log)): agents ([the egress chain](../networking/network.md#agent-bridge-egress-log)): agents
reach the pod through the published port and no other way. reach the queue through the published port and no other way.
- **The exporter's port.** The exporter's port has no `hostPort`: a ClusterIP - **The exporter's port.** The exporter's port has no `hostPort`: a ClusterIP
Service, `swarm-nats-metrics`, fronts it, and the gateway's `/metrics` Service, `swarm-nats-metrics`, fronts it, and the gateway's `/metrics`
location on the host dials the Service's name through nginx's `resolver` (the location on the host dials the Service's name through nginx's `resolver` (the
@ -306,9 +315,9 @@ identity provider, the queue and the hive collector, each on its one port. It
admits no ingress. The kubelet's probes still reach the pod, because the node's admits no ingress. The kubelet's probes still reach the pod, because the node's
own traffic to a pod is always admitted, and the pod doesn't serve anything own traffic to a pod is always admitted, and the pod doesn't serve anything
else: requests reach it as queue messages, over the connection it opens. Where else: requests reach it as queue messages, over the connection it opens. Where
the queue runs in the cluster, the policy also allows the queue's pod on its the queue runs in the cluster, the policy also allows the pods of the queue's
port, because the node forwards the queue's `hostPort` to the pod before the load balancer, in `kube-system`, on the queue's port, because the node forwards
policy sees the packet. the `hostPort` they bind to them before the policy sees the packet.
## Telemetry and credentials of the poller ## Telemetry and credentials of the poller
@ -382,28 +391,31 @@ that `claude` with `HOME` in the volume writes its config there, as the
minter's real login step does, while `claude` with `HOME` on the root leaves minter's real login step does, while `claude` with `HOME` on the root leaves
nothing there. nothing there.
The VM test `k8s-nats-switch` moves the queue against the real secret store. A The VM test `k8s-nats-switch` moves the queue against the real secret store,
sampler counts servers and responders every 200 ms and sees at most one of with the egress intercept on. A sampler counts servers and responders every
each, and the container and the pod never together. The pod is Ready by its 200 ms and sees at most one of each, and the container and the pod never
exec probe and has the init containers, the `hostPort`s and the volumes the together. The pod is Ready by its exec probe and has the init containers, the
module renders, and its leaf's volume is a tmpfs. A client on the bridge volumes and the Services the module renders and no `hostPort`, and its leaf's
reaches the queue by name, and a second node outside the node reaches the volume is a tmpfs. The load balancer binds the client port from `kube-system`
control port and not the queue's, as with the container; with `openFirewall` and the Service has no node port. A client on the bridge reaches the queue by
it dials the pod by name and verifies the name, and the responder in the pod name and through the published port only, not by the pod's address or the
admits it. A key written on the host before the switch is the pod's to read, a node's, and a second node outside the node reaches the control port and not the
key the pod writes is the host server's after the switch back, and a stream queue's, as with the container; with `openFirewall` it dials the pod by name and
written before a pod delete survives it: the replacement pod reads it from the verifies the name, and the responder in the pod admits it. A key written on the
same directory. The exporter answers the host's own processes through its host before the switch is the pod's to read, a key the pod writes is the host
Service and no pod or bridge client, and the switch back removes the pod before server's after the switch back, and a stream written before a pod delete
survives it: the replacement pod reads it from the same directory. The exporter
answers the host's own processes through its Service and no pod or bridge
client, and the switch back removes the pod and the load balancer's pod before
the container starts. the container starts.
The VM test `k8s-controller-switch` moves the controller. It asserts one copy at The VM test `k8s-controller-switch` moves the controller. It asserts one copy at
each step by the switch's own timestamps and by a sampler, that the pod is Ready each step by the switch's own timestamps and by a sampler, that the pod is Ready
on `/readyz` with the `hostPort` and the volumes the module renders and no on `/readyz` with the volumes the module renders and no Secret, that its
Secret, that its listener's leaf is the one its init container issued, valid as listener's leaf is the one its init container issued, valid as the listener's
the listener's name against the hive's root, that the volume holding its key is name against the hive's root, that the volume holding its key is a tmpfs, and
a tmpfs, and that the listener refuses a client with no leaf. It runs the that the listener refuses a client with no leaf. It runs the gateway's own
gateway's own controller location, from the module's text for each runtime, controller location, from the module's text for each runtime, against the host
against the host unit and the pod: both answer an unsigned webhook 401. A unit and the pod: both answer an unsigned webhook 401. A changed rev rolls the
changed rev rolls the Deployment onto a new pod and keeps the state volume, and Deployment onto a new pod and keeps the state volume, and the switch back
the switch back deletes the pod and its AddOn before the unit starts. deletes the pod and its AddOn before the unit starts.

View file

@ -763,12 +763,12 @@ also refuses the flow.
`services.hyperhive.deploy.hive-controller.egress.publishedPorts`, in both `services.hyperhive.deploy.hive-controller.egress.publishedPorts`, in both
modes, so such a flow leaves no count and no line. A module that publishes a modes, so such a flow leaves no count and no line. A module that publishes a
port adds it: the queue in the cluster adds its client port, because the port adds it: the queue in the cluster adds its client port, because the
pod's `hostPort` rewrites the destination to the pod and the packet then `hostPort` of the cluster's load balancer rewrites the destination, and the
leaves the bridge for the cluster. An agent dials the queue's name, which the packet then leaves the bridge for the cluster. An agent dials the queue's
hive's resolver answers with the bridge's address. A flow to the pod's own name, which the hive's resolver answers with the bridge's address. A flow to
address has no redirect, and one to the node's address has another original the pod's own address has no redirect, and one to the node's address has
destination, so both stay refused. The set is empty, and the chain has no another original destination, so both stay refused. The set is empty, and the
such rule, while no module publishes a port. chain has no such rule, while no module publishes a port.
- **What `reject` refuses.** Everything from the bridge to the uplink that the - **What `reject` refuses.** Everything from the bridge to the uplink that the
chain didn't exempt or allow, whether a flow is new or not: TCP on any port chain didn't exempt or allow, whether a flow is new or not: TCP on any port
but the 80 and 443 that squid takes, UDP (DNS to an outside resolver, QUIC but the 80 and 443 that squid takes, UDP (DNS to an outside resolver, QUIC

View file

@ -370,11 +370,11 @@ let
}; };
# What the policy of a pod that reaches `reaches` allows: the cluster's DNS, # What the policy of a pod that reaches `reaches` allows: the cluster's DNS,
# those host ports, and the queue's own pod where the queue runs in the # those host ports, and the pods of the queue's load balancer where the queue
# cluster, because its `hostPort` is forwarded to the pod before the policy # runs in the cluster, because the `hostPort` they bind is forwarded to them
# sees the packet. The policy selects every pod of the service for ingress # before the policy sees the packet. The policy selects every pod of the
# too and allows none, so only the node's own traffic (the kubelet's probes, # service for ingress too and allows none, so only the node's own traffic
# which the policy engine always admits) reaches it. # (the kubelet's probes, which the policy engine always admits) reaches it.
networkPolicyOf = networkPolicyOf =
name: reaches: name: reaches:
let let
@ -416,7 +416,15 @@ let
} }
] ]
++ lib.optional (queueOnCluster && lib.elem "queue" reaches) { ++ lib.optional (queueOnCluster && lib.elem "queue" reaches) {
to = [ { podSelector.matchLabels = labelsFor "swarm-nats"; } ]; to = [
{
namespaceSelector.matchLabels."kubernetes.io/metadata.name" = "kube-system";
podSelector.matchLabels = {
"svccontroller.k3s.cattle.io/svcname" = "swarm-nats";
"svccontroller.k3s.cattle.io/svcnamespace" = k8sCfg.namespace;
};
}
];
ports = [ (tcp hostPorts.queue) ]; ports = [ (tcp hostPorts.queue) ];
}; };
}; };

View file

@ -29,6 +29,15 @@ let
''; '';
apiReady = waitFor 600 "the cluster's API" "${kubectl} get --raw /readyz >/dev/null 2>&1"; apiReady = waitFor 600 "the cluster's API" "${kubectl} get --raw /readyz >/dev/null 2>&1";
# The service load balancer binds a Service's port on the node with a
# `hostPort` in a pod of `kube-system`, which its controller deletes some time
# after the Service. The rule that forwards the port to that pod stays until
# the pod is gone, and would swallow what the host unit then listens on.
lbPodsGone =
name:
waitFor 180 "the load balancer's pods of ${name}"
"[ -z \"\$(${kubectl} --namespace kube-system get pods --selector svccontroller.k3s.cattle.io/svcname=${name},svccontroller.k3s.cattle.io/svcnamespace=${cfg.namespace} --output name)\" ]";
caFiles = lib.filterAttrs (_: path: path != null) rendered.caSources; caFiles = lib.filterAttrs (_: path: path != null) rendered.caSources;
# The services on the cluster whose pods run in a user namespace. # The services on the cluster whose pods run in a user namespace.
@ -148,6 +157,7 @@ in
${kubectl} --namespace ${cfg.namespace} delete deployment,cronjob,serviceaccount,networkpolicy,service \ ${kubectl} --namespace ${cfg.namespace} delete deployment,cronjob,serviceaccount,networkpolicy,service \
--selector=app.kubernetes.io/name=${name} \ --selector=app.kubernetes.io/name=${name} \
--cascade=foreground --wait=true --timeout=180s --ignore-not-found --cascade=foreground --wait=true --timeout=180s --ignore-not-found
${lbPodsGone name}
${kubectl} --namespace kube-system delete addons.k3s.cattle.io ${name} --ignore-not-found ${kubectl} --namespace kube-system delete addons.k3s.cattle.io ${name} --ignore-not-found
rm -f "$pending" rm -f "$pending"
''; '';

View file

@ -1,10 +1,11 @@
# The iptables rules that give the queue pod's client `hostPort` the reach the # The iptables rules that give the `hostPort` the cluster's load balancer binds
# queue's container has. A `hostPort` is a DNAT rule, and what a DNAT rule # for the queue's client port the reach the queue's container has. A
# rewrites is forwarded, never delivered to the host: the INPUT rules (the # `hostPort` is a DNAT rule, and what a DNAT rule rewrites is forwarded, never
# firewall's `allowedTCPPorts` and per-interface rules) never see it, and the # delivered to the host: the INPUT rules (the firewall's `allowedTCPPorts` and
# cluster's own forwarding rules accept it. The raw table is read before the # per-interface rules) never see it, and the cluster's own forwarding rules
# DNAT and before any of those, so a rule there reads the original # accept it. The raw table is read before the DNAT and before any of those, so a
# destination, as INPUT does for the container's listener. # rule there reads the original destination, as INPUT does for the container's
# listener.
# #
# The client port is dropped for a packet addressed to the host unless it # The client port is dropped for a packet addressed to the host unless it
# arrives on one of `trusted` (the host's loopback, the hive bridge and the # arrives on one of `trusted` (the host's loopback, the hive bridge and the

View file

@ -984,13 +984,11 @@ let
image = ref server; image = ref server;
imagePullPolicy = "Never"; imagePullPolicy = "Never";
args = lib.tail (lib.splitString " " natsSystem.systemd.services.nats.serviceConfig.ExecStart); args = lib.tail (lib.splitString " " natsSystem.systemd.services.nats.serviceConfig.ExecStart);
# Every address of the node, which is where the queue listens today. # The port the client Service targets.
ports = [ ports = [
{ {
name = "client"; name = "client";
containerPort = cfg.port; containerPort = cfg.port;
hostPort = cfg.port;
hostIP = "0.0.0.0";
} }
]; ];
# The monitor is on the pod's loopback, so the probe runs in the # The monitor is on the pod's loopback, so the probe runs in the
@ -1108,6 +1106,34 @@ let
]; ];
}; };
}; };
# The client port, on every address of the node, which is where the
# queue listens in its container, so the hives of other hosts keep their
# URL. The cluster's service load balancer (k3s's servicelb) binds the
# Service's port on each node with a `hostPort` of its own, in
# `kube-system`, and forwards it to the pod through the Service. It does
# that by the Service's cluster address, so no node port is needed, and
# none is allocated: a node port would be a second way in, which the
# raw-table rules (./lib/swarm-nats-raw-rules.nix) don't name. The server
# sees the load balancer's pod as the client, not the client's address.
objects."${machine}/loadbalancer" = {
apiVersion = "v1";
kind = "Service";
metadata = metaFor machine;
spec = {
type = "LoadBalancer";
allocateLoadBalancerNodePorts = false;
selector = (metaFor machine).labels;
ports = [
{
name = "client";
protocol = "TCP";
port = cfg.port;
targetPort = "client";
}
];
};
};
objects."${machine}/networkpolicy" = { objects."${machine}/networkpolicy" = {
apiVersion = "networking.k8s.io/v1"; apiVersion = "networking.k8s.io/v1";
kind = "NetworkPolicy"; kind = "NetworkPolicy";
@ -1615,13 +1641,15 @@ in
# sets no listen host), so reachability is the firewall alone. # sets no listen host), so reachability is the firewall alone.
networking.firewall.allowedTCPPorts = lib.mkIf deployCfg.nats.openFirewall [ cfg.port ]; networking.firewall.allowedTCPPorts = lib.mkIf deployCfg.nats.openFirewall [ cfg.port ];
# The pod's client `hostPort` is a DNAT rule, which the INPUT rules above
# never see: ./lib/swarm-nats-raw-rules.nix gives it the container's reach.
services.hyperhive.network.enable = lib.mkIf onK8s (lib.mkDefault true); services.hyperhive.network.enable = lib.mkIf onK8s (lib.mkDefault true);
# An agent dials the queue at the bridge's address; the pod's `hostPort` is # An agent dials the queue at the bridge's address, and the load
# forwarded to the cluster, which the egress forward chain would refuse. # balancer's `hostPort` forwards that to the cluster, which the egress
# forward chain would refuse.
services.hyperhive.deploy.hive-controller.egress.publishedPorts = lib.mkIf onK8s [ cfg.port ]; services.hyperhive.deploy.hive-controller.egress.publishedPorts = lib.mkIf onK8s [ cfg.port ];
# The load balancer's `hostPort` is a DNAT rule, which the INPUT rules above
# never see: ./lib/swarm-nats-raw-rules.nix gives it the container's reach.
networking.firewall.extraCommands = lib.mkIf onK8s ( networking.firewall.extraCommands = lib.mkIf onK8s (
import ./lib/swarm-nats-raw-rules.nix { inherit lib; } { import ./lib/swarm-nats-raw-rules.nix { inherit lib; } {
chain = "nixos-fw-nats"; chain = "nixos-fw-nats";

View file

@ -1115,8 +1115,10 @@ let
{ {
to = [ to = [
{ {
podSelector.matchLabels = objects."swarm-stt/deployment".spec.selector.matchLabels // { namespaceSelector.matchLabels."kubernetes.io/metadata.name" = "kube-system";
"app.kubernetes.io/name" = "swarm-nats"; podSelector.matchLabels = {
"svccontroller.k3s.cattle.io/svcname" = "swarm-nats";
"svccontroller.k3s.cattle.io/svcnamespace" = "hyperhive";
}; };
} }
]; ];
@ -1782,15 +1784,13 @@ let
"-c" "-c"
"/var/lib/nats-callout/nats.conf" "/var/lib/nats-callout/nats.conf"
] ]
# The client port is published on every address of the node. The # Both ports are named for their Services, and neither has a `hostPort`:
# exporter's port is named for its Service and has no `hostPort`. # the load balancer publishes the client port on the node.
&& &&
(named "nats").ports == [ (named "nats").ports == [
{ {
name = "client"; name = "client";
containerPort = 4222; containerPort = 4222;
hostPort = 4222;
hostIP = "0.0.0.0";
} }
] ]
&& &&
@ -2025,6 +2025,47 @@ let
&& has "-t raw -A nixos-fw-nats -p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" onCluster.networking.firewall.extraCommands && has "-t raw -A nixos-fw-nats -p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" onCluster.networking.firewall.extraCommands
&& has "-t raw -F nixos-fw-nats" onCluster.networking.firewall.extraStopCommands; && has "-t raw -F nixos-fw-nats" onCluster.networking.firewall.extraStopCommands;
} }
{
# The host with the queue on it renders the same Service, the control that
# a `deploy.nats.runtime` changes none of it.
name = "the queue's client port is a LoadBalancer Service with no node port, and the pod has no hostPort";
ok =
let
cluster = render onCluster;
svc = cluster."swarm-nats/loadbalancer";
pod = cluster."swarm-nats/deployment".spec.template;
ports = lib.concatMap (c: c.ports or [ ]) pod.spec.containers;
in
svc.kind == "Service"
&& svc.metadata.name == "swarm-nats"
&& svc.metadata.labels."app.kubernetes.io/name" == "swarm-nats"
&& svc.spec.type == "LoadBalancer"
&& svc.spec.allocateLoadBalancerNodePorts == false
&& svc.spec.selector == pod.metadata.labels
&&
svc.spec.ports == [
{
name = "client";
protocol = "TCP";
port = 4222;
targetPort = "client";
}
]
&& !(svc.spec ? loadBalancerClass)
&& !(svc.spec ? externalTrafficPolicy)
&& !(lib.any (p: p ? hostPort || p ? hostIP) ports)
&&
lib.sort lib.lessThan (map (p: p.name) ports) == [
"client"
"health"
"metrics"
]
&& (render withNode)."swarm-nats/loadbalancer" == svc
# Waiting for the load balancer's pods to go is part of every prune, and
# names the service's own Service and namespace.
&& lib.hasInfix "--selector svccontroller.k3s.cattle.io/svcname=swarm-nats,svccontroller.k3s.cattle.io/svcnamespace=hyperhive" withNode.systemd.services.swarm-nats-k8s-prune.script
&& !(lib.hasInfix "svcname=swarm-nats," withNode.systemd.services.swarm-controller-k8s-prune.script);
}
{ {
# The host with the queue on it is the control: it publishes no port, and # The host with the queue on it is the control: it publishes no port, and
# its chain has no rule of the kind. # its chain has no rule of the kind.

View file

@ -10,8 +10,8 @@
# sampler counts the processes every 200 ms), and the container and the pod # sampler counts the processes every 200 ms), and the container and the pod
# never overlap; # never overlap;
# - the pod is Ready by its exec probe on the server's monitor, holds the leaf # - 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 # and the callout config as init containers, and has no `hostPort`: the client
# on every address, with no `hostPort` on the exporter; # 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 # - 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 # 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; # 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 # - 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 # 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, which hands the files back to the server's user;
# - the switch back leaves no pod, Deployment, Service, NetworkPolicy or AddOn, # - the switch back leaves no pod, Service, NetworkPolicy or AddOn, and the
# and the container starts after the prune unit. # container starts after the prune unit.
# #
# One stub is the identity provider: it introspects the test's one token as the # 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 # 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") machine.fail("test -e /etc/nixos-containers/swarm-nats.conf")
assert counts() == (1, 1), counts() 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"] 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["initContainers"]] == ["tls-issue", "callout-config"], spec["initContainers"]
assert [c["name"] for c in spec["containers"]] == ["swarm-nats", "nats", "tls-renew", "exporter"], spec["containers"] assert [c["name"] for c in spec["containers"]] == ["swarm-nats", "nats", "tls-renew", "exporter"], spec["containers"]
assert spec["shareProcessNamespace"] is True, spec assert spec["shareProcessNamespace"] is True, spec
server = next(c for c in spec["containers"] if c["name"] == "nats") server = next(c for c in spec["containers"] if c["name"] == "nats")
client = next(p for p in server["ports"] if p["name"] == "client") 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"] command = server["readinessProbe"]["exec"]["command"]
assert command[0] == "/bin/wget" and command[-1].endswith("/healthz?js-enabled-only=true"), 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") 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["policyTypes"] == ["Ingress"], policy
assert policy["ingress"] == [{"ports": [{"port": PORT, "protocol": "TCP"}]}], 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"): with subtest("one server and one responder run, and the queue answers by name with its name verified"):
assert counts() == (1, 1), counts() assert counts() == (1, 1), counts()
assert probe(machine, BRIDGE)[0], probe(machine, BRIDGE) assert probe(machine, BRIDGE)[0], probe(machine, BRIDGE)
@ -769,7 +789,12 @@ pkgs.testers.runNixOSTest {
assert pods() == [], pods() assert pods() == [], pods()
assert machine.execute(f"kubectl -n {NS} get deployment {SERVICE}")[0] != 0 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 {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 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"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")[0] != 0
assert machine.execute(f"test -e {MANIFESTS}/{SERVICE}.yaml.pruning")[0] != 0 assert machine.execute(f"test -e {MANIFESTS}/{SERVICE}.yaml.pruning")[0] != 0