Watch
0
0
Fork
You've already forked hyperhive
0

run flannel's wireguard-native backend on every k8s node

This commit is contained in:
atlas 2026-11-27 22:48:56 +01:00 • committed by mara
commit d09e2b403c
3 changed files with 225 additions and 29 deletions

View file

@ -365,7 +365,7 @@ pkgs.testers.runNixOSTest {
rules = machine.succeed("iptables -S nixos-fw")
port_rules = [r for r in rules.splitlines() if f"--dport {POD_PORT}" in r]
assert port_rules, rules
assert all("-i cni0 " in r or "-i flannel.1 " in r for r in port_rules), port_rules
assert all("-i cni0 " in r or "-i flannel-wg " in r for r in port_rules), port_rules
bridge_rules = [r for r in rules.splitlines() if "-i hive-br0 " in r and "--dport" in r]
assert any(f"--dport {CONTROL_PORT}" in r for r in bridge_rules), rules
assert not any(f"--dport {POD_PORT}" in r for r in bridge_rules), bridge_rules

View file

@ -10,7 +10,12 @@
# - the joining machine goes Ready, carries no `state-*`, `entry` or
# `enablelb` label, and the main node carries them;
# - the node-to-node ports are open on the main node from the joining
# machine's address only;
# machine's address only, and VXLAN's 8472 is open on neither node;
# - the pod network is flannel's WireGuard tunnel (`flannel-wg`, no `flannel.1`):
# each node holds the other's key and has shaken hands, a pod on the joining
# machine fetches a page from a pod on the main node through it, and a peer
# with a key of its own, sending from the main node's own address, gets no
# handshake and no answer;
# - a pod with the `state-ci-metrics` selector, the one the poller's pod
# carries, runs on the main node, and a pod forced onto the joining machine
# pulls its image from the main node through the embedded registry mirror
@ -47,6 +52,33 @@ let
probeRef = "${probeImage.imageName}:${probeImage.imageTag}";
missingRef = "${images.registryHost}/nobody-has-this:v1";
# A shell and `wget`, in both nodes' containerd, so a pod on each node can
# talk to the other's.
netImage = pkgs.dockerTools.buildLayeredImage {
name = "k8s-join-net";
tag = "test";
contents = [ pkgs.busybox ];
};
netPort = 8080;
netPage = "page-of-the-pod-on-main";
netPod = name: node: command: {
apiVersion = "v1";
kind = "Pod";
metadata = { inherit name; };
spec = {
nodeSelector."kubernetes.io/hostname" = node;
restartPolicy = "Never";
containers = [
{
name = "net";
image = "${netImage.imageName}:${netImage.imageTag}";
imagePullPolicy = "Never";
inherit command;
}
];
};
};
pod = name: image: selector: {
apiVersion = "v1";
kind = "Pod";
@ -116,11 +148,15 @@ pkgs.testers.runNixOSTest {
networking.hosts."127.0.0.1" = [ baoName ];
networking.firewall.allowedTCPPorts = [ baoPort ];
services.k3s.images = [ probeImage ];
services.k3s.images = [
probeImage
netImage
];
environment.systemPackages = [
pkgs.kubectl
pkgs.jq
pkgs.openssl
pkgs.wireguard-tools
];
environment.variables.KUBECONFIG = "/etc/rancher/k3s/k3s.yaml";
environment.etc = {
@ -134,6 +170,19 @@ pkgs.testers.runNixOSTest {
"k8s-join/pod-missing.json".text = builtins.toJSON (
pod "missing" missingRef { "kubernetes.io/hostname" = "joiner"; }
);
"k8s-join/pod-net-main.json".text = builtins.toJSON (
netPod "net-main" "main" [
"sh"
"-c"
"mkdir /www && echo ${netPage} > /www/index.html && exec httpd -f -p ${toString netPort} -h /www"
]
);
"k8s-join/pod-net-joiner.json".text = builtins.toJSON (
netPod "net-joiner" "joiner" [
"sleep"
"3600"
]
);
};
networking.interfaces.eth1.ipv4.addresses = lib.mkForce [
@ -185,7 +234,11 @@ pkgs.testers.runNixOSTest {
}
];
environment.systemPackages = [ pkgs.jq ];
services.k3s.images = [ netImage ];
environment.systemPackages = [
pkgs.jq
pkgs.wireguard-tools
];
virtualisation = {
memorySize = 3072;
@ -204,6 +257,7 @@ pkgs.testers.runNixOSTest {
SERVER_TOKEN = "/var/lib/rancher/k3s/server/agent-token"
JOIN_TOKEN = "/run/k3s-join/token"
JOINER_IP = "${joinIp}"
MAIN_IP = "${mainIp}"
PROBE_REF = main.succeed("cat /etc/k8s-join/probe-ref").strip()
MISSING_REF = "${missingRef}"
HOST = "${images.registryHost}"
@ -367,10 +421,14 @@ pkgs.testers.runNixOSTest {
with subtest("the node-to-node ports are open to the joining machine's address only"):
rules = main.succeed("iptables -S nixos-fw").splitlines()
for port, proto in [("6443", "tcp"), ("10250", "tcp"), ("8472", "udp"), ("5001", "tcp")]:
for port, proto in [("6443", "tcp"), ("10250", "tcp"), ("51820", "udp"), ("5001", "tcp")]:
matching = [r for r in rules if f"--dport {port} " in r and f"-p {proto} " in r]
assert matching, (port, rules)
assert all(f"-s {JOINER_IP}/32" in r for r in matching), (port, matching)
assert not [r for r in rules if "--dport 8472" in r], rules
joiner_rules = joiner.succeed("iptables -S nixos-fw").splitlines()
assert [r for r in joiner_rules if "--dport 51820 " in r and f"-s {MAIN_IP}/32" in r], joiner_rules
assert not [r for r in joiner_rules if "--dport 8472" in r], joiner_rules
with subtest("a pod with the poller's state selector runs on the main node"):
main.succeed("kubectl apply -f /etc/k8s-join/pod-stateful.json")
@ -393,6 +451,63 @@ pkgs.testers.runNixOSTest {
events = kube(main, "get events --field-selector involvedObject.name=on-joiner -o json")
assert any(e["reason"] == "Pulled" for e in json.loads(events)["items"]), events
def wg_transfer(node: QemuMachine, dev: str) -> tuple[int, int]:
"""Bytes received and sent over the device's only peer."""
fields = node.succeed(f"wg show {dev} transfer").split()
assert len(fields) == 3, fields
return int(fields[1]), int(fields[2])
with subtest("the pod network is flannel's WireGuard tunnel, each node holds the other's key, and VXLAN is gone"):
keys = {}
for node in [main, joiner]:
assert "wireguard" in node.succeed("ip -d link show flannel-wg")
assert node.succeed("wg show flannel-wg listen-port").strip() == "51820"
node.fail("ip link show flannel.1")
assert node.succeed("ss -H -lun 'sport = :8472'").strip() == "", "something listens on UDP 8472"
assert "-i flannel-wg " in node.succeed("iptables -S nixos-fw")
keys[node.name] = node.succeed("wg show flannel-wg public-key").strip()
assert keys["main"] != keys["joiner"], keys
assert main.succeed("wg show flannel-wg peers").split() == [keys["joiner"]]
assert joiner.succeed("wg show flannel-wg peers").split() == [keys["main"]]
with subtest("a pod on the joining machine fetches a page from a pod on the main node, through the tunnel"):
main.succeed("kubectl apply -f /etc/k8s-join/pod-net-main.json -f /etc/k8s-join/pod-net-joiner.json")
wait_pod(main, "net-main", phase="Running", ready=True, timeout=180)
wait_pod(main, "net-joiner", phase="Running", ready=True, timeout=180)
assert pod(main, "net-main")["spec"]["nodeName"] == "main"
assert pod(main, "net-joiner")["spec"]["nodeName"] == "joiner"
target = pod(main, "net-main")["status"]["podIP"]
before = wg_transfer(joiner, "flannel-wg")
main.wait_until_succeeds(
f"kubectl exec net-joiner -- wget -qO- -T 5 http://{target}:${toString netPort}/ | grep -qx ${netPage}",
timeout=180,
)
after = wg_transfer(joiner, "flannel-wg")
assert after[0] > before[0] and after[1] > before[1], (before, after)
for node in [main, joiner]:
stamps = [int(s) for s in node.succeed("wg show flannel-wg latest-handshakes").split()[1::2]]
assert stamps and all(s > 0 for s in stamps), stamps
with subtest("a peer with a key of its own, sending from the main node's address, gets no handshake and no answer"):
victim = pod(main, "net-joiner")["status"]["podIP"]
rogue_pub = main.succeed("wg genkey | tee /root/rogue.key | wg pubkey").strip()
assert rogue_pub not in keys.values()
main.succeed(
"ip link add rogue type wireguard && "
f"wg set rogue private-key /root/rogue.key listen-port 51999 peer {keys['joiner']} "
f"endpoint {JOINER_IP}:51820 allowed-ips {victim}/32 && "
"ip addr add 10.99.0.1/32 dev rogue && ip link set rogue up && "
f"ip route add {victim}/32 dev rogue"
)
status, _ = main.execute(f"ping -I rogue -c 5 -W 2 {victim}")
assert status != 0, "a peer without a listed key got an answer"
received, sent = wg_transfer(main, "rogue")
assert sent > 0, "the control: the rogue peer sent its handshake"
assert received == 0, received
assert main.succeed("wg show rogue latest-handshakes").split()[1] == "0"
assert rogue_pub not in joiner.succeed("wg show flannel-wg peers")
main.succeed("ip link del rogue")
with subtest("an image no node holds fails to pull, naming the images' host and not docker.io"):
main.succeed("kubectl apply -f /etc/k8s-join/pod-missing.json")
main.wait_until_succeeds(

View file

@ -211,7 +211,7 @@ pkgs.testers.runNixOSTest {
with subtest("the firewall trusts the pod network and guards the other interfaces"):
rules = machine.succeed("iptables -S nixos-fw")
assert "-i cni0 " in rules, rules
assert "-i flannel.1 " in rules, rules
assert "-i flannel-wg " in rules, rules
assert "-i lo " in rules, rules
assert "-i eth0 " not in rules, rules