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:
parent
3cf89f25f1
commit
7ef9444e42
4 changed files with 228 additions and 63 deletions
|
|
@ -1,4 +1,4 @@
|
|||
# The iptables rules that give the queue pod's `hostPort`s the reach the
|
||||
# The iptables rules that give the queue pod's client `hostPort` the reach the
|
||||
# queue's container has. A `hostPort` is a DNAT rule, and what a DNAT rule
|
||||
# rewrites is forwarded, never delivered to the host: the INPUT rules (the
|
||||
# firewall's `allowedTCPPorts` and per-interface rules) never see it, and the
|
||||
|
|
@ -9,8 +9,7 @@
|
|||
# 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
|
||||
# pods' interfaces), or `openClient` is set, which is
|
||||
# `deploy.nats.openFirewall`. The exporter's port answers with every client's
|
||||
# name, so it is dropped for all but the host's own loopback.
|
||||
# `deploy.nats.openFirewall`.
|
||||
#
|
||||
# Pure function — NOT a NixOS module. Returns the text for the firewall's
|
||||
# `extraCommands`.
|
||||
|
|
@ -18,7 +17,6 @@
|
|||
{
|
||||
chain,
|
||||
clientPort,
|
||||
metricsPort,
|
||||
trusted,
|
||||
openClient,
|
||||
}:
|
||||
|
|
@ -28,8 +26,6 @@ let
|
|||
in
|
||||
''
|
||||
iptables -t raw -N ${chain} 2>/dev/null || iptables -t raw -F ${chain}
|
||||
${rule} -p tcp --dport ${toString metricsPort} -i lo -j RETURN
|
||||
${rule} -p tcp --dport ${toString metricsPort} ${local}
|
||||
${lib.optionalString (!openClient) ''
|
||||
${lib.concatMapStrings (i: ''
|
||||
${rule} -p tcp --dport ${toString clientPort} -i ${i} -j RETURN
|
||||
|
|
|
|||
|
|
@ -354,6 +354,11 @@ let
|
|||
# host's cluster, which ./k8s/render.nix renders from the units below.
|
||||
onK8s = deployCfg.nats.runtime == "k8s";
|
||||
|
||||
# The Service in front of the pod's exporter, and the name the host's resolver
|
||||
# answers for it through the cluster's DNS.
|
||||
metricsService = "${machine}-metrics";
|
||||
metricsHost = "${metricsService}.${config.services.hyperhive.k8s.namespace}.svc.${deployCfg.k8s.clusterDomain}";
|
||||
|
||||
# The server's monitoring endpoint, which the exporter reads, and the
|
||||
# collectors it serves.
|
||||
monitorUrl = "http://127.0.0.1:${toString deployCfg.nats.monitorPort}";
|
||||
|
|
@ -1027,15 +1032,11 @@ let
|
|||
]
|
||||
++ exporterFlags
|
||||
++ [ monitorUrl ];
|
||||
# On the bridge's address, for the host's gateway: the exporter
|
||||
# answers a request with every client's name, so the host's firewall
|
||||
# lets only the host's own processes reach it.
|
||||
# The port the metrics Service targets.
|
||||
ports = [
|
||||
{
|
||||
name = "metrics";
|
||||
containerPort = deployCfg.nats.metricsPort;
|
||||
hostPort = deployCfg.nats.metricsPort;
|
||||
hostIP = networkCfg.bridgeIp;
|
||||
}
|
||||
];
|
||||
inherit securityContext;
|
||||
|
|
@ -1083,6 +1084,50 @@ let
|
|||
};
|
||||
};
|
||||
|
||||
# The exporter answers a request with every client's name, so only the
|
||||
# node may read it: the Service is reached by name from the host's
|
||||
# gateway, and the policy admits the node's own traffic (which the
|
||||
# cluster's policy engine always lets in, ./k8s/render.nix) and, from
|
||||
# anywhere else, the client port alone.
|
||||
objects."${machine}/service" = {
|
||||
apiVersion = "v1";
|
||||
kind = "Service";
|
||||
metadata = metaFor machine // {
|
||||
name = metricsService;
|
||||
};
|
||||
spec = {
|
||||
type = "ClusterIP";
|
||||
selector = (metaFor machine).labels;
|
||||
ports = [
|
||||
{
|
||||
name = "metrics";
|
||||
protocol = "TCP";
|
||||
port = deployCfg.nats.metricsPort;
|
||||
targetPort = "metrics";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
objects."${machine}/networkpolicy" = {
|
||||
apiVersion = "networking.k8s.io/v1";
|
||||
kind = "NetworkPolicy";
|
||||
metadata = metaFor machine;
|
||||
spec = {
|
||||
podSelector.matchLabels = (metaFor machine).labels;
|
||||
policyTypes = [ "Ingress" ];
|
||||
ingress = [
|
||||
{
|
||||
ports = [
|
||||
{
|
||||
protocol = "TCP";
|
||||
port = cfg.port;
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
images = [
|
||||
init
|
||||
server
|
||||
|
|
@ -1138,6 +1183,10 @@ in
|
|||
{option}`services.hyperhive.swarm.nats.domain` proxies it, behind
|
||||
the swarm collector's authelia check.
|
||||
|
||||
With the queue in the cluster this is the exporter container's port in
|
||||
the pod, which the Service `swarm-nats-metrics` publishes on the same
|
||||
number and only the node reaches.
|
||||
|
||||
⚠️ This and {option}`services.hyperhive.deploy.nats.monitorPort` are two
|
||||
more claims on a port
|
||||
space every swarm container shares — they run in this container but
|
||||
|
|
@ -1379,14 +1428,24 @@ in
|
|||
# Authelia's rules for it admit the collector's client and deny
|
||||
# every other subject (`metricsRules` in ./swarm-authelia.nix).
|
||||
"= /metrics" = {
|
||||
# In the cluster the exporter is a container of the pod, published on
|
||||
# the bridge's address.
|
||||
proxyPass = "http://${
|
||||
if onK8s then networkCfg.bridgeIp else "127.0.0.1"
|
||||
}:${toString deployCfg.nats.metricsPort}/metrics";
|
||||
extraConfig = ''
|
||||
auth_request /__nats_metrics_authz;
|
||||
'';
|
||||
# In the cluster the exporter is behind a Service, which the host's
|
||||
# resolver answers through the cluster's DNS. The upstream is a
|
||||
# variable, so nginx asks the resolver per request: a name in the
|
||||
# `proxy_pass` gets one lookup when the configuration loads, which
|
||||
# fails the load while the cluster is down.
|
||||
proxyPass =
|
||||
if onK8s then
|
||||
"http://\$swarm_nats_metrics_upstream/metrics"
|
||||
else
|
||||
"http://127.0.0.1:${toString deployCfg.nats.metricsPort}/metrics";
|
||||
extraConfig =
|
||||
lib.optionalString onK8s ''
|
||||
resolver ${networkCfg.bridgeIp} valid=10s ipv6=off;
|
||||
set $swarm_nats_metrics_upstream ${metricsHost}:${toString deployCfg.nats.metricsPort};
|
||||
''
|
||||
+ ''
|
||||
auth_request /__nats_metrics_authz;
|
||||
'';
|
||||
};
|
||||
|
||||
# The subrequest, same shape as the forge's `= /__forge_metrics_authz`
|
||||
|
|
@ -1470,6 +1529,18 @@ in
|
|||
this host, or leave the queue's runtime at "host".
|
||||
'';
|
||||
}
|
||||
{
|
||||
# The gateway's `/metrics` location dials the exporter's Service by
|
||||
# name, which only the hive's resolver answers.
|
||||
assertion = !onK8s || config.services.hyperhive.gateway.dns.enable;
|
||||
message = ''
|
||||
services.hyperhive.deploy.nats.runtime = "k8s" needs
|
||||
services.hyperhive.gateway.dns.enable: the gateway dials the pod's
|
||||
metrics Service by name, and the hive's resolver is what hands the
|
||||
cluster's domain to its DNS. Set it, or leave the queue's runtime at
|
||||
"host".
|
||||
'';
|
||||
}
|
||||
{
|
||||
# The callout xkey and the responder's OIDC client secret are read
|
||||
# from the store under this identity; without either the server or
|
||||
|
|
@ -1544,14 +1615,13 @@ in
|
|||
# sets no listen host), so reachability is the firewall alone.
|
||||
networking.firewall.allowedTCPPorts = lib.mkIf deployCfg.nats.openFirewall [ cfg.port ];
|
||||
|
||||
# The pod's `hostPort`s are DNAT rules, which the INPUT rules above never
|
||||
# see: ./lib/swarm-nats-raw-rules.nix gives them the container's reach.
|
||||
# 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);
|
||||
networking.firewall.extraCommands = lib.mkIf onK8s (
|
||||
import ./lib/swarm-nats-raw-rules.nix { inherit lib; } {
|
||||
chain = "nixos-fw-nats";
|
||||
clientPort = cfg.port;
|
||||
metricsPort = deployCfg.nats.metricsPort;
|
||||
trusted = [
|
||||
"lo"
|
||||
networkCfg.bridgeName
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -11,18 +11,18 @@
|
|||
# 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 and the exporter's on the bridge's;
|
||||
# on every address, with no `hostPort` on the exporter;
|
||||
# - 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;
|
||||
# the name. The exporter answers the host only, by its Service's name;
|
||||
# - a stream written before the pod is deleted is read after;
|
||||
# - 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.
|
||||
# - the switch back leaves no pod, Deployment, 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
|
||||
|
|
@ -352,6 +352,8 @@ pkgs.testers.runNixOSTest {
|
|||
NAME = "${natsName}"
|
||||
PORT = ${toString natsPort}
|
||||
METRICS = ${toString metricsPort}
|
||||
METRICS_SERVICE = "${service}-metrics"
|
||||
METRICS_HOST = f"{METRICS_SERVICE}.{NS}.svc.cluster.local"
|
||||
NODE = "${nodeIp}"
|
||||
BRIDGE = "${bridgeIp}"
|
||||
CONTROL = ${toString controlPort}
|
||||
|
|
@ -394,6 +396,17 @@ pkgs.testers.runNixOSTest {
|
|||
return machine.succeed(f"kubectl -n {NS} get pods --selector app.kubernetes.io/name={SERVICE} -o name || true").split()
|
||||
|
||||
|
||||
def pod_pid(container: str) -> str:
|
||||
id_ = machine.succeed(f"k3s crictl ps --name '^{container}$' --state running -q").split()[0]
|
||||
return machine.succeed(f"k3s crictl inspect {id_} | jq -r .info.pid").strip()
|
||||
|
||||
|
||||
def dials(pid: str, address: str, port: int) -> str:
|
||||
"""A command that opens a TCP connection from the network namespace of
|
||||
the process `pid`, as the pod in it would."""
|
||||
return f"nsenter --target {pid} --net -- timeout 5 bash -c '</dev/tcp/{address}/{port}'"
|
||||
|
||||
|
||||
def host_state() -> str:
|
||||
return machine.execute(f"systemctl is-active {HOST_UNIT}")[1].strip()
|
||||
|
||||
|
|
@ -545,6 +558,8 @@ pkgs.testers.runNixOSTest {
|
|||
assert machine.succeed(f"systemctl is-active {PRUNE}.service").strip() == "active"
|
||||
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 networkpolicy {SERVICE}")[0] != 0
|
||||
# The control for its absence under the cluster.
|
||||
machine.succeed("test -e /etc/nixos-containers/swarm-nats.conf")
|
||||
assert counts() == (1, 1), counts()
|
||||
|
|
@ -594,7 +609,7 @@ 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, 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, publishes the client port on every address, and has no hostPort on 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"]
|
||||
|
|
@ -605,7 +620,7 @@ pkgs.testers.runNixOSTest {
|
|||
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")
|
||||
assert exporter["ports"][0]["hostIP"] == BRIDGE, exporter["ports"]
|
||||
assert exporter["ports"] == [{"name": "metrics", "containerPort": METRICS, "protocol": "TCP"}], exporter["ports"]
|
||||
assert not any(c.get("resources") for c in spec["containers"]), spec["containers"]
|
||||
assert not any("secret" in v for v in spec["volumes"]), spec["volumes"]
|
||||
assert spec["hostAliases"] == [{"ip": "127.0.0.1", "hostnames": [NAME]}], spec["hostAliases"]
|
||||
|
|
@ -627,6 +642,14 @@ pkgs.testers.runNixOSTest {
|
|||
print(limits)
|
||||
assert int(limits.split()[3]) >= 800000, limits
|
||||
|
||||
with subtest("the exporter is a ClusterIP Service, and the pod's policy admits the client port from any source and nothing else"):
|
||||
svc = json.loads(kube(machine, f"get service {METRICS_SERVICE} -o json"))
|
||||
assert svc["spec"]["type"] == "ClusterIP", svc["spec"]
|
||||
assert [(p["port"], p["targetPort"]) for p in svc["spec"]["ports"]] == [(METRICS, "metrics")], svc["spec"]
|
||||
policy = json.loads(kube(machine, f"get networkpolicy {SERVICE} -o json"))["spec"]
|
||||
assert policy["policyTypes"] == ["Ingress"], policy
|
||||
assert policy["ingress"] == [{"ports": [{"port": PORT, "protocol": "TCP"}]}], policy
|
||||
|
||||
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)
|
||||
|
|
@ -657,9 +680,21 @@ pkgs.testers.runNixOSTest {
|
|||
assert reachable(outside, NODE, CONTROL)
|
||||
assert not reachable(outside, NODE, PORT), "the pod's port is open to a host outside the node"
|
||||
|
||||
with subtest("the exporter answers the host's own processes through the pod's published port"):
|
||||
body = machine.succeed(f"curl -sf http://{BRIDGE}:{METRICS}/metrics")
|
||||
with subtest("the exporter answers the host's own processes by its Service's name, and refuses a client of the bridge and a pod of the cluster"):
|
||||
ip = kube(machine, f"get service {METRICS_SERVICE} -o jsonpath='{{.spec.clusterIP}}'").strip()
|
||||
assert machine.succeed(f"getent ahostsv4 {METRICS_HOST}").split()[0] == ip
|
||||
body = machine.succeed(f"curl -sf http://{METRICS_HOST}:{METRICS}/metrics")
|
||||
assert "gnatsd_varz_connections" in body, body[:500]
|
||||
# The bridge's client has the route to the Service's address through the
|
||||
# host, and the pod's policy refuses it. A pod of the cluster reaches the
|
||||
# queue's client port, the control that it can dial out, and not the
|
||||
# exporter.
|
||||
assert machine.execute(f"ip netns exec agent nc -z -w 4 {ip} {METRICS}")[0] != 0
|
||||
other = pod_pid("coredns")
|
||||
machine.succeed(dials(other, BRIDGE, PORT))
|
||||
machine.wait_until_fails(dials(other, ip, METRICS), timeout=120)
|
||||
for _ in range(3):
|
||||
assert machine.execute(dials(other, ip, METRICS))[0] != 0
|
||||
|
||||
with subtest("a leaf that has to be issued again is issued beside the running server, which reloads it for new connections"):
|
||||
uid = pod(machine, pod_name)["metadata"]["uid"]
|
||||
|
|
@ -712,6 +747,8 @@ pkgs.testers.runNixOSTest {
|
|||
settle(f"[ \"$(systemctl is-active {HOST_UNIT})\" = active ]", 600, [HOST_UNIT, f"{PRUNE}.service"])
|
||||
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 networkpolicy {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.pruning")[0] != 0
|
||||
|
|
|
|||
Loading…
Reference in a new issue