summary refs log tree commit diff
path: root/nixos/tests/k3s-single-node-docker.nix
blob: 7f3d15788b04351437d8c250a7f8b129f120a105 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import ./make-test-python.nix ({ pkgs, ... }:

  let
    imageEnv = pkgs.buildEnv {
      name = "k3s-pause-image-env";
      paths = with pkgs; [ tini (hiPrio coreutils) busybox ];
    };
    pauseImage = pkgs.dockerTools.streamLayeredImage {
      name = "test.local/pause";
      tag = "local";
      contents = imageEnv;
      config.Entrypoint = [ "/bin/tini" "--" "/bin/sleep" "inf" ];
    };
    # Don't use the default service account because there's a race where it may
    # not be created yet; make our own instead.
    testPodYaml = pkgs.writeText "test.yml" ''
      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: test
      ---
      apiVersion: v1
      kind: Pod
      metadata:
        name: test
      spec:
        serviceAccountName: test
        containers:
        - name: test
          image: test.local/pause:local
          imagePullPolicy: Never
          command: ["sh", "-c", "sleep inf"]
    '';
  in
  {
    name = "k3s";
    meta = with pkgs.lib.maintainers; {
      maintainers = [ euank ];
    };

    machine = { pkgs, ... }: {
      environment.systemPackages = with pkgs; [ k3s gzip ];

      # k3s uses enough resources the default vm fails.
      virtualisation.memorySize = 1536;
      virtualisation.diskSize = 4096;

      services.k3s = {
        enable = true;
        role = "server";
        docker = true;
        # Slightly reduce resource usage
        extraFlags = "--no-deploy coredns,servicelb,traefik,local-storage,metrics-server --pause-image test.local/pause:local";
      };

      users.users = {
        noprivs = {
          isNormalUser = true;
          description = "Can't access k3s by default";
          password = "*";
        };
      };
    };

    testScript = ''
      start_all()

      machine.wait_for_unit("k3s")
      machine.succeed("k3s kubectl cluster-info")
      machine.fail("sudo -u noprivs k3s kubectl cluster-info")
      # FIXME: this fails with the current nixos kernel config; once it passes, we should uncomment it
      # machine.succeed("k3s check-config")

      machine.succeed(
          "${pauseImage} | docker load"
      )

      machine.succeed("k3s kubectl apply -f ${testPodYaml}")
      machine.succeed("k3s kubectl wait --for 'condition=Ready' pod/test")
      machine.succeed("k3s kubectl delete -f ${testPodYaml}")

      machine.shutdown()
    '';
  })