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
1 changed files with 228 additions and 63 deletions
|
|
@ -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