2024-07-12 14:21:16 +01:00
|
|
|
# A test that runs a single node k3s cluster and verify a pod can run
|
2023-02-09 09:57:37 +00:00
|
|
|
import ../make-test-python.nix (
|
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
k3s,
|
|
|
|
...
|
|
|
|
}:
|
2021-10-24 02:34:10 +01:00
|
|
|
let
|
2021-10-26 01:43:27 +01:00
|
|
|
imageEnv = pkgs.buildEnv {
|
|
|
|
name = "k3s-pause-image-env";
|
|
|
|
paths = with pkgs; [
|
|
|
|
tini
|
|
|
|
(hiPrio coreutils)
|
|
|
|
busybox
|
|
|
|
];
|
|
|
|
};
|
|
|
|
pauseImage = pkgs.dockerTools.streamLayeredImage {
|
2021-10-24 02:34:10 +01:00
|
|
|
name = "test.local/pause";
|
|
|
|
tag = "local";
|
2021-10-26 01:43:27 +01:00
|
|
|
contents = imageEnv;
|
2021-10-24 02:34:10 +01:00
|
|
|
config.Entrypoint = [
|
|
|
|
"/bin/tini"
|
|
|
|
"--"
|
|
|
|
"/bin/sleep"
|
|
|
|
"inf"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
testPodYaml = pkgs.writeText "test.yml" ''
|
|
|
|
apiVersion: v1
|
|
|
|
kind: Pod
|
|
|
|
metadata:
|
|
|
|
name: test
|
|
|
|
spec:
|
|
|
|
containers:
|
|
|
|
- name: test
|
|
|
|
image: test.local/pause:local
|
|
|
|
imagePullPolicy: Never
|
|
|
|
command: ["sh", "-c", "sleep inf"]
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
2023-02-09 09:57:37 +00:00
|
|
|
name = "${k3s.name}-single-node";
|
2020-04-17 15:39:54 +01:00
|
|
|
|
2022-03-20 23:15:30 +00:00
|
|
|
nodes.machine =
|
|
|
|
{ pkgs, ... }:
|
|
|
|
{
|
2021-10-24 02:34:10 +01:00
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
k3s
|
|
|
|
gzip
|
|
|
|
];
|
2020-04-17 15:39:54 +01:00
|
|
|
|
2021-10-24 02:34:10 +01:00
|
|
|
# k3s uses enough resources the default vm fails.
|
2021-10-26 01:43:27 +01:00
|
|
|
virtualisation.memorySize = 1536;
|
|
|
|
virtualisation.diskSize = 4096;
|
2020-04-17 15:39:54 +01:00
|
|
|
|
2021-10-24 02:34:10 +01:00
|
|
|
services.k3s.enable = true;
|
|
|
|
services.k3s.role = "server";
|
2023-02-09 09:57:37 +00:00
|
|
|
services.k3s.package = k3s;
|
2021-10-24 02:34:10 +01:00
|
|
|
# Slightly reduce resource usage
|
2024-07-15 21:46:52 +01:00
|
|
|
services.k3s.extraFlags = [
|
|
|
|
"--disable coredns"
|
|
|
|
"--disable local-storage"
|
|
|
|
"--disable metrics-server"
|
|
|
|
"--disable servicelb"
|
|
|
|
"--disable traefik"
|
|
|
|
"--pause-image test.local/pause:local"
|
2022-10-03 14:56:51 +01:00
|
|
|
];
|
2020-04-17 15:39:54 +01:00
|
|
|
|
2021-10-24 02:34:10 +01:00
|
|
|
users.users = {
|
|
|
|
noprivs = {
|
|
|
|
isNormalUser = true;
|
|
|
|
description = "Can't access k3s by default";
|
|
|
|
password = "*";
|
2024-05-10 22:55:54 +01:00
|
|
|
};
|
2021-10-24 02:34:10 +01:00
|
|
|
};
|
2020-04-17 15:39:54 +01:00
|
|
|
};
|
|
|
|
|
2021-10-24 02:34:10 +01:00
|
|
|
testScript =
|
|
|
|
''
|
|
|
|
start_all()
|
2020-04-17 15:39:54 +01:00
|
|
|
|
2021-10-24 02:34:10 +01:00
|
|
|
machine.wait_for_unit("k3s")
|
2023-04-27 20:24:37 +01:00
|
|
|
machine.succeed("kubectl cluster-info")
|
|
|
|
machine.fail("sudo -u noprivs kubectl cluster-info")
|
2022-09-29 13:58:45 +01:00
|
|
|
'' # Fix-Me: Tests fail for 'aarch64-linux' as: "CONFIG_CGROUP_FREEZER: missing (fail)"
|
|
|
|
+ lib.optionalString (!pkgs.stdenv.isAarch64) ''machine.succeed("k3s check-config")''
|
|
|
|
+ ''
|
2020-04-17 15:39:54 +01:00
|
|
|
|
2021-10-24 02:34:10 +01:00
|
|
|
machine.succeed(
|
2023-04-27 20:24:37 +01:00
|
|
|
"${pauseImage} | ctr image import -"
|
2021-10-24 02:34:10 +01:00
|
|
|
)
|
2020-04-17 15:39:54 +01:00
|
|
|
|
2022-07-22 07:38:44 +01:00
|
|
|
# Also wait for our service account to show up; it takes a sec
|
2023-04-27 20:24:37 +01:00
|
|
|
machine.wait_until_succeeds("kubectl get serviceaccount default")
|
|
|
|
machine.succeed("kubectl apply -f ${testPodYaml}")
|
|
|
|
machine.succeed("kubectl wait --for 'condition=Ready' pod/test")
|
|
|
|
machine.succeed("kubectl delete -f ${testPodYaml}")
|
2021-10-24 02:05:32 +01:00
|
|
|
|
2022-06-06 07:56:21 +01:00
|
|
|
# regression test for #176445
|
|
|
|
machine.fail("journalctl -o cat -u k3s.service | grep 'ipset utility not found'")
|
|
|
|
|
2024-05-04 20:35:17 +01:00
|
|
|
with subtest("Run k3s-killall"):
|
|
|
|
# Call the killall script with a clean path to assert that
|
|
|
|
# all required commands are wrapped
|
|
|
|
output = machine.succeed("PATH= ${k3s}/bin/k3s-killall.sh 2>&1 | tee /dev/stderr")
|
|
|
|
assert "command not found" not in output, "killall script contains unknown command"
|
|
|
|
|
|
|
|
# Check that killall cleaned up properly
|
|
|
|
machine.fail("systemctl is-active k3s.service")
|
|
|
|
machine.fail("systemctl list-units | grep containerd")
|
|
|
|
machine.fail("ip link show | awk -F': ' '{print $2}' | grep -e flannel -e cni0")
|
|
|
|
machine.fail("ip netns show | grep cni-")
|
|
|
|
|
2021-10-24 02:34:10 +01:00
|
|
|
machine.shutdown()
|
|
|
|
'';
|
2024-07-12 13:01:55 +01:00
|
|
|
|
|
|
|
meta.maintainers = lib.teams.k3s.members;
|
2021-10-24 02:34:10 +01:00
|
|
|
}
|
|
|
|
)
|