Watch
0
0
Fork
You've already forked hyperhive
0

vm-tests: k8s-node pod reaches the hive bridge's listeners by swarm name

This commit is contained in:
atlas 2026-11-21 13:13:30 +01:00 • committed by maia
commit 42bc6e7f2a

View file

@ -12,13 +12,56 @@
# no pod runs one of the images;
# - the node takes pod and service addresses from the module's ranges and the
# hive bridge keeps its own, so the two do not share a subnet;
# - the firewall trusts `cni0`, and still guards the other interfaces.
# - the firewall trusts `cni0`, and still guards the other interfaces;
# - a pod resolves the swarm's bao name and the hive collector's name to the
# hive bridge's address, completes TLS to a listener there that requires a
# client certificate (refused without one), and POSTs one OTLP record to
# another that receives it. The listeners are stand-ins for the store's
# passthrough and the collector: the name, the address and the route from a
# pod are what the test is about.
#
# The poller's own host unit and its trust bundle unit stay as they are on the
# host and fail here, which nothing in this test reads.
{ pkgs, self }:
let
inherit (pkgs) lib;
# What the node's configuration and the script agree on. The collector's name
# is a literal of `services.hyperhive.otel.collector.host`.
bridgeIp = "10.42.0.1";
baoName = "bao.t.local";
baoPort = 8200;
collectorName = "hive-collector.hive";
# The pod the script execs into: a shell, `nslookup` and `curl`, no more.
probeImage = pkgs.dockerTools.buildLayeredImage {
name = "k8s-probe";
tag = "test";
contents = [
pkgs.busybox
pkgs.curl
];
};
# Receives OTLP over HTTP on the address in `argv[1]` and appends each
# request's path and body to a file.
otlpReceiver = pkgs.writeText "otlp-receiver.py" ''
import http.server
import sys
class Handler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
body = self.rfile.read(int(self.headers["Content-Length"]))
with open("/var/lib/otlp-standin/received", "ab") as f:
f.write(self.path.encode() + b" " + body + b"\n")
self.send_response(200)
self.end_headers()
self.wfile.write(b"{}")
http.server.HTTPServer((sys.argv[1], 4318), Handler).serve_forever()
'';
in
pkgs.testers.runNixOSTest {
name = "k8s-node";
@ -39,6 +82,9 @@ pkgs.testers.runNixOSTest {
deploy.bao.ciMetricsClientKeyFile = "/var/lib/ci-metrics/client-key.pem";
deploy.hive-controller.hiveName = "h1";
swarm.domain = "t.local";
network.bridgeIp = bridgeIp;
swarm.bao.domain = baoName;
swarm.bao.port = baoPort;
swarm.hives.h1.domain = "h1.t.local";
};
system.stateVersion = "25.11";
@ -49,6 +95,57 @@ pkgs.testers.runNixOSTest {
];
environment.variables.KUBECONFIG = "/etc/rancher/k3s/k3s.yaml";
# What the standing pod reaches: the names the node's CoreDNS answers
# are `gateway.localNames` plus the hive domain, and a pod gets the
# hive bridge's address for each.
services.hyperhive.gateway.localNames = [
baoName
config.services.hyperhive.otel.collector.host
];
services.k3s.images = [ probeImage ];
# A TLS listener that requires a client certificate where the store's
# passthrough listens, on a throwaway CA the unit mints for itself.
systemd.services.bao-standin = {
wantedBy = [ "multi-user.target" ];
path = [ pkgs.openssl ];
serviceConfig = {
StateDirectory = "bao-standin";
WorkingDirectory = "/var/lib/bao-standin";
Restart = "always";
RestartSec = 2;
};
script = ''
set -eu
if [ ! -f client.pem ]; then
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
-keyout ca.key -out ca.pem -subj /CN=standin-ca -days 2
for n in server client; do
openssl req -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
-keyout $n.key -out $n.csr -subj /CN=$n
done
echo 'subjectAltName=DNS:${baoName}' > server.ext
openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key -CAcreateserial \
-out server.pem -days 2 -extfile server.ext
openssl x509 -req -in client.csr -CA ca.pem -CAkey ca.key -CAcreateserial \
-out client.pem -days 2
fi
exec openssl s_server -accept ${bridgeIp}:${toString baoPort} \
-cert server.pem -key server.key -CAfile ca.pem -Verify 1 -verify_return_error -www
'';
};
# An OTLP/HTTP receiver where the hive collector listens.
systemd.services.otlp-standin = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${pkgs.python3}/bin/python3 ${otlpReceiver} ${bridgeIp}";
StateDirectory = "otlp-standin";
Restart = "always";
RestartSec = 2;
};
};
# The references `lib/k3s.py`'s `wait_images` looks for.
environment.etc."k3s-node/images".text = lib.concatMapStrings (
i: "${i.imageName}:${i.imageTag}\n"
@ -62,6 +159,7 @@ pkgs.testers.runNixOSTest {
};
testScript = builtins.readFile ./lib/k3s.py + ''
import shlex
with subtest("the node is Ready and holds every image the module loads"):
wait_node_ready(machine, timeout=300)
@ -97,8 +195,8 @@ pkgs.testers.runNixOSTest {
cni = machine.wait_until_succeeds("ip -4 -o addr show dev cni0", timeout=60)
assert "10.200.0.1/24" in cni, cni
bridge = machine.succeed("ip -4 -o addr show dev hive-br0")
assert "10.42.0.1/24" in bridge, bridge
assert "10.42.0.1" not in cni, cni
assert "${bridgeIp}/24" in bridge, bridge
assert "${bridgeIp}" not in cni, cni
with subtest("the firewall trusts the pod network and guards the other interfaces"):
rules = machine.succeed("iptables -S nixos-fw")
@ -106,5 +204,71 @@ pkgs.testers.runNixOSTest {
assert "-i flannel.1 " in rules, rules
assert "-i lo " in rules, rules
assert "-i eth0 " not in rules, rules
with subtest("a pod resolves the swarm names to the hive bridge and reaches the listeners there"):
bridge_ip = "${bridgeIp}"
bao = "${baoName}"
collector = "${collectorName}"
for unit in ["bao-standin", "otlp-standin"]:
machine.wait_for_unit(unit)
machine.wait_until_succeeds("test -f /var/lib/bao-standin/client.pem")
kube(
machine,
"create configmap probe-tls --from-file=/var/lib/bao-standin/ca.pem "
"--from-file=/var/lib/bao-standin/client.pem --from-file=/var/lib/bao-standin/client.key",
)
spec = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {"name": "probe"},
"spec": {
"containers": [
{
"name": "probe",
"image": "k8s-probe:test",
"imagePullPolicy": "Never",
"command": ["sleep", "3600"],
"volumeMounts": [{"name": "tls", "mountPath": "/tls"}],
}
],
"volumes": [{"name": "tls", "configMap": {"name": "probe-tls"}}],
},
}
machine.succeed(f"echo {shlex.quote(json.dumps(spec))} | kubectl apply -f -")
wait_pod(machine, "probe", phase="Running", ready=True, timeout=180)
def probe(cmd: str) -> tuple[int, str]:
return machine.execute(f"kubectl exec probe -- sh -c {shlex.quote(cmd + ' 2>&1')}")
# The names the table holds answer with the bridge's address. Cluster
# names still answer, and a name outside the table does not get it.
for name in [bao, collector]:
machine.wait_until_succeeds(
f"kubectl exec probe -- nslookup {name} | grep -F {bridge_ip}", timeout=120
)
machine.succeed(
"kubectl exec probe -- nslookup kubernetes.default.svc.cluster.local "
"| grep -E '10[.]201[.]0[.]1([^0-9]|$)'"
)
_, unlisted = probe("timeout 30 nslookup unlisted.t.local")
assert bridge_ip not in unlisted, unlisted
# TLS completes on the name, with the client certificate, and is
# refused without it.
curl_tls = f"curl -sS -m 10 --cacert /tls/ca.pem https://{bao}:${toString baoPort}/"
status, out = probe(f"{curl_tls} --cert /tls/client.pem --key /tls/client.key")
assert status == 0 and "Cipher" in out, (status, out)
status, out = probe(curl_tls)
assert status != 0 and "Cipher" not in out and "certificate" in out.lower(), (status, out)
# One OTLP record arrives.
status, out = probe(
"curl -sS -m 10 -X POST -H 'Content-Type: application/json' "
"-d '{\"resourceLogs\":[{\"scopeLogs\":[{\"logRecords\":[{\"body\":{\"stringValue\":\"from-the-pod\"}}]}]}]}' "
f"http://{collector}:4318/v1/logs"
)
assert status == 0, (status, out)
received = machine.succeed("cat /var/lib/otlp-standin/received")
assert "/v1/logs " in received and "from-the-pod" in received, received
'';
}