Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the queue's exporter is a ClusterIP service only the node reaches

The exporter's metrics port was a hostPort on the bridge's address, which Pod Security baseline and restricted forbid. It is a ClusterIP Service now, and the pod has a NetworkPolicy that admits the client port from any source and nothing else, so only the node reads the exporter, as the raw-table rules made it. The gateway's /metrics location dials the Service by name through nginx's resolver. The raw-table rules no longer name the exporter's port.
This commit is contained in:
atlas 2026-11-23 18:02:25 +01:00 • committed by maia
commit 7ef9444e42

View file

@ -66,6 +66,9 @@ let
deploy.swarm-controller.runtime = "k8s";
deploy.swarm-controller.tcp.listenAddress = "10.0.0.5";
deploy.nats.runtime = "k8s";
deploy.nats.enable = true;
deploy.nats.authPackage = pkgs.emptyDirectory;
gateway.dns.enable = lib.mkForce false;
};
runtimeRefusals = lib.filter (a: !a.assertion) runtimeRefused.assertions;
@ -1120,9 +1123,14 @@ let
ports = [ (tcp 4222) ];
}
]
&& !(lib.any (n: lib.hasSuffix "/networkpolicy" n) (
lib.attrNames (removeAttrs objects [ "swarm-stt/networkpolicy" ])
));
# Only the queue and this pod have a policy here: the others have no
# egress and, with no listener, no ingress rule to give one.
&&
lib.sort lib.lessThan (lib.filter (n: lib.hasSuffix "/networkpolicy" n) (lib.attrNames objects))
== [
"swarm-nats/networkpolicy"
"swarm-stt/networkpolicy"
];
}
{
# Other spellings of a ceiling: bytes, megabytes, a fractional core, no
@ -1414,6 +1422,7 @@ let
a: lib.hasInfix "tcp.listenAddress is" a.message && lib.hasInfix "\"10.0.0.5\"" a.message
) runtimeRefusals
&& lib.any (a: lib.hasInfix "gateway.dns.enable: the gateway dials" a.message) runtimeRefusals
&& lib.any (a: lib.hasInfix "metrics Service by name" a.message) runtimeRefusals
# The all-local host binds the default, which the refusal lets through.
&& onCluster.services.hyperhive.deploy.swarm-controller.tcp.listenAddress == "0.0.0.0"
&& lib.all (a: a.assertion) onCluster.assertions;
@ -1773,8 +1782,8 @@ let
"-c"
"/var/lib/nats-callout/nats.conf"
]
# The client port is published on every address of the node, and the
# exporter's on the bridge's.
# The client port is published on every address of the node. The
# exporter's port is named for its Service and has no `hostPort`.
&&
(named "nats").ports == [
{
@ -1789,8 +1798,6 @@ let
{
name = "metrics";
containerPort = 7777;
hostPort = 7777;
hostIP = "10.42.0.1";
}
]
&& lib.elem "0.0.0.0" (named "exporter").args
@ -1907,10 +1914,11 @@ let
{
# The host with the queue on it is the control for each absence: it has
# the container, both delivery units and no rule in the raw table.
name = "the queue in the cluster leaves the host no container and no delivery unit, keeps the leaf for the gateway's vhost, and points the vhost at the exporter's published port";
name = "the queue in the cluster leaves the host no container and no delivery unit, keeps the leaf for the gateway's vhost, and points the vhost at the exporter's Service by name";
ok =
let
vhost = c: c.services.nginx.virtualHosts."nats.t.local".locations."= /metrics".proxyPass;
extra = c: c.services.nginx.virtualHosts."nats.t.local".locations."= /metrics".extraConfig;
tls = onCluster.systemd.services.swarm-bao-nats-tls;
in
withNode.containers ? swarm-nats
@ -1924,7 +1932,7 @@ let
&& !(lib.hasInfix "nixos-fw-nats" withNode.networking.firewall.extraCommands)
&& vhost withNode == "http://127.0.0.1:7777/metrics"
# In the cluster: nothing to start the server or deliver to it, a leaf
# that restarts nothing, the exporter on the bridge, the bridge up.
# that restarts nothing, the exporter behind its Service, the bridge up.
&& !(onCluster.containers ? swarm-nats)
&& !(onCluster.systemd.services ? "container@swarm-nats")
&& !onCluster.systemd.services.swarm-nats-auth-secrets.enable
@ -1932,20 +1940,68 @@ let
&& !(lib.elem "container@swarm-nats.service" tls.before)
&& !(lib.elem "container@swarm-nats.service" tls.wantedBy)
&& onCluster.systemd.services ? swarm-nats-gateway-cert
&& vhost onCluster == "http://10.42.0.1:7777/metrics"
&& vhost onCluster == "http://\$swarm_nats_metrics_upstream/metrics"
&& lib.hasInfix "resolver 10.42.0.1 valid=10s ipv6=off;" (extra onCluster)
&& lib.hasInfix "set \$swarm_nats_metrics_upstream swarm-nats-metrics.hyperhive.svc.cluster.local:7777;" (
extra onCluster
)
&& !(lib.hasInfix "resolver" (extra withNode))
&& onCluster.services.hyperhive.network.enable;
}
{
# The host with the queue on it renders the same objects, the control that
# a `deploy.nats.runtime` changes none of them.
name = "the queue's exporter is a ClusterIP Service only the node reaches, and the pod admits the client port from anywhere and nothing else";
ok =
let
cluster = render onCluster;
svc = cluster."swarm-nats/service";
policy = cluster."swarm-nats/networkpolicy";
pod = cluster."swarm-nats/deployment".spec.template.metadata.labels;
in
svc.kind == "Service"
&& svc.metadata.name == "swarm-nats-metrics"
&& svc.metadata.labels."app.kubernetes.io/name" == "swarm-nats"
&& svc.spec.type == "ClusterIP"
&& !(svc.spec ? clusterIP)
&& svc.spec.selector == pod
&&
svc.spec.ports == [
{
name = "metrics";
protocol = "TCP";
port = 7777;
targetPort = "metrics";
}
]
&& policy.spec.podSelector.matchLabels == pod
&& policy.spec.policyTypes == [ "Ingress" ]
&&
policy.spec.ingress == [
{
ports = [
{
protocol = "TCP";
port = 4222;
}
];
}
]
&& !(policy.spec ? egress)
&& (render withNode)."swarm-nats/service" == svc
&& (render withNode)."swarm-nats/networkpolicy" == policy
&& lib.hasInfix "networkpolicy,service \\" withNode.systemd.services.swarm-nats-k8s-prune.script;
}
{
# Both arms of `openFirewall`, from the function the module calls, and
# the module's own text for the default.
name = "the pod's raw-table rules close the client port as the container's INPUT rules do, and the exporter's port to all but the host's loopback";
name = "the pod's raw-table rules close the client port as the container's INPUT rules do, and name no other port";
ok =
let
rules = import ../host-modules/lib/swarm-nats-raw-rules.nix { inherit lib; };
args = {
chain = "c";
clientPort = 4222;
metricsPort = 7777;
trusted = [
"lo"
"br0"
@ -1959,13 +2015,11 @@ let
has "-p tcp --dport 4222 -i br0 -j RETURN" closed
&& has "-p tcp --dport 4222 -i cni0 -j RETURN" closed
&& has "-p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" closed
# Open, the client port has no rule; the exporter's still has.
# Open, the client port has no rule. The exporter's port, behind a
# Service, has none in either.
&& !(has "--dport 4222" open)
&& has "-p tcp --dport 7777 -i lo -j RETURN" open
&& has "-p tcp --dport 7777 -m addrtype --dst-type LOCAL -j DROP" open
# The exporter's port is not returned for the bridge or the pods.
&& !(has "--dport 7777 -i br0" closed)
&& !(has "--dport 7777 -i cni0" closed)
&& !(has "7777" closed)
&& !(has "7777" onCluster.networking.firewall.extraCommands)
&& has "-t raw -I PREROUTING -j c" closed
&& has "-t raw -A nixos-fw-nats -p tcp --dport 4222 -i cni0 -j RETURN" 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