1 import ../make-test-python.nix ({ pkgs, lib, rke2, ... }:
3 pauseImage = pkgs.dockerTools.streamLayeredImage {
4 name = "test.local/pause";
6 contents = pkgs.buildEnv {
7 name = "rke2-pause-image-env";
8 paths = with pkgs; [ tini bashInteractive coreutils socat ];
10 config.Entrypoint = [ "/bin/tini" "--" "/bin/sleep" "inf" ];
12 # A daemonset that responds 'server' on port 8000
13 networkTestDaemonset = pkgs.writeText "test.yml" ''
31 image: test.local/pause:local
32 imagePullPolicy: Never
36 command: ["socat", "TCP4-LISTEN:8000,fork", "EXEC:echo server"]
38 tokenFile = pkgs.writeText "token" "p@s$w0rd";
39 agentTokenFile = pkgs.writeText "agent-token" "p@s$w0rd";
42 name = "${rke2.name}-multi-node";
43 meta.maintainers = rke2.meta.maintainers;
46 server1 = { pkgs, ... }: {
47 networking.firewall.enable = false;
48 networking.useDHCP = false;
49 networking.defaultGateway = "192.168.1.1";
50 networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
51 { address = "192.168.1.1"; prefixLength = 24; }
54 virtualisation.memorySize = 1536;
55 virtualisation.diskSize = 4096;
61 inherit agentTokenFile;
62 nodeName = "${rke2.name}-server1";
64 nodeIP = "192.168.1.1";
76 server2 = { pkgs, ... }: {
77 networking.firewall.enable = false;
78 networking.useDHCP = false;
79 networking.defaultGateway = "192.168.1.2";
80 networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
81 { address = "192.168.1.2"; prefixLength = 24; }
84 virtualisation.memorySize = 1536;
85 virtualisation.diskSize = 4096;
90 serverAddr = "https://192.168.1.1:6443";
92 inherit agentTokenFile;
93 nodeName = "${rke2.name}-server2";
95 nodeIP = "192.168.1.2";
104 agent1 = { pkgs, ... }: {
105 networking.firewall.enable = false;
106 networking.useDHCP = false;
107 networking.defaultGateway = "192.168.1.3";
108 networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
109 { address = "192.168.1.3"; prefixLength = 24; }
112 virtualisation.memorySize = 1536;
113 virtualisation.diskSize = 4096;
118 tokenFile = agentTokenFile;
119 serverAddr = "https://192.168.1.2:6443";
120 nodeName = "${rke2.name}-agent1";
122 nodeIP = "192.168.1.3";
128 kubectl = "${pkgs.kubectl}/bin/kubectl --kubeconfig=/etc/rancher/rke2/rke2.yaml";
129 ctr = "${pkgs.containerd}/bin/ctr -a /run/k3s/containerd/containerd.sock";
130 jq = "${pkgs.jq}/bin/jq";
131 ping = "${pkgs.iputils}/bin/ping";
133 machines = [server1, server2, agent1]
135 for machine in machines:
137 machine.wait_for_unit("rke2")
139 # wait for the agent to show up
140 server1.succeed("${kubectl} get node ${rke2.name}-agent1")
142 for machine in machines:
143 machine.succeed("${pauseImage} | ${ctr} image import -")
145 server1.succeed("${kubectl} cluster-info")
146 server1.wait_until_succeeds("${kubectl} get serviceaccount default")
148 # Now create a pod on each node via a daemonset and verify they can talk to each other.
149 server1.succeed("${kubectl} apply -f ${networkTestDaemonset}")
150 server1.wait_until_succeeds(
151 f'[ "$(${kubectl} get ds test -o json | ${jq} .status.numberReady)" -eq {len(machines)} ]'
155 pods = server1.succeed("${kubectl} get po -o json | ${jq} '.items[].metadata.name' -r").splitlines()
157 server1.succeed(f"${kubectl} get po {n} -o json | ${jq} '.status.podIP' -cr").strip() for n in pods
160 # Verify each server can ping each pod ip
161 for pod_ip in pod_ips:
162 server1.succeed(f"${ping} -c 1 {pod_ip}")
163 agent1.succeed(f"${ping} -c 1 {pod_ip}")
165 # Verify the pods can talk to each other
166 resp = server1.wait_until_succeeds(f"${kubectl} exec {pods[0]} -- socat TCP:{pod_ips[1]}:8000 -")
167 assert resp.strip() == "server"
168 resp = server1.wait_until_succeeds(f"${kubectl} exec {pods[1]} -- socat TCP:{pod_ips[0]}:8000 -")
169 assert resp.strip() == "server"
172 server1.succeed("${kubectl} delete -f ${networkTestDaemonset}")
173 for machine in machines: