1# Tests whether container images are imported and auto deploying manifests work
2import ../make-test-python.nix (
3 {
4 pkgs,
5 lib,
6 k3s,
7 ...
8 }:
9 let
10 pauseImageEnv = pkgs.buildEnv {
11 name = "k3s-pause-image-env";
12 paths = with pkgs; [
13 tini
14 (hiPrio coreutils)
15 busybox
16 ];
17 };
18 pauseImage = pkgs.dockerTools.buildImage {
19 name = "test.local/pause";
20 tag = "local";
21 copyToRoot = pauseImageEnv;
22 config.Entrypoint = [
23 "/bin/tini"
24 "--"
25 "/bin/sleep"
26 "inf"
27 ];
28 };
29 helloImage = pkgs.dockerTools.buildImage {
30 name = "test.local/hello";
31 tag = "local";
32 copyToRoot = pkgs.hello;
33 config.Entrypoint = [ "${pkgs.hello}/bin/hello" ];
34 };
35 in
36 {
37 name = "${k3s.name}-auto-deploy";
38
39 nodes.machine =
40 { pkgs, ... }:
41 {
42 environment.systemPackages = [ k3s ];
43
44 # k3s uses enough resources the default vm fails.
45 virtualisation.memorySize = 1536;
46 virtualisation.diskSize = 4096;
47
48 services.k3s.enable = true;
49 services.k3s.role = "server";
50 services.k3s.package = k3s;
51 # Slightly reduce resource usage
52 services.k3s.extraFlags = [
53 "--disable coredns"
54 "--disable local-storage"
55 "--disable metrics-server"
56 "--disable servicelb"
57 "--disable traefik"
58 "--pause-image test.local/pause:local"
59 ];
60 services.k3s.images = [
61 pauseImage
62 helloImage
63 ];
64 services.k3s.manifests = {
65 absent = {
66 enable = false;
67 content = {
68 apiVersion = "v1";
69 kind = "Namespace";
70 metadata.name = "absent";
71 };
72 };
73
74 present = {
75 target = "foo-namespace.yaml";
76 content = {
77 apiVersion = "v1";
78 kind = "Namespace";
79 metadata.name = "foo";
80 };
81 };
82
83 hello.content = {
84 apiVersion = "batch/v1";
85 kind = "Job";
86 metadata.name = "hello";
87 spec = {
88 template.spec = {
89 containers = [
90 {
91 name = "hello";
92 image = "test.local/hello:local";
93 }
94 ];
95 restartPolicy = "OnFailure";
96 };
97 };
98 };
99 };
100 };
101
102 testScript = # python
103 ''
104 start_all()
105
106 machine.wait_for_unit("k3s")
107 # check existence of the manifest files
108 machine.fail("ls /var/lib/rancher/k3s/server/manifests/absent.yaml")
109 machine.succeed("ls /var/lib/rancher/k3s/server/manifests/foo-namespace.yaml")
110 machine.succeed("ls /var/lib/rancher/k3s/server/manifests/hello.yaml")
111
112 # check if container images got imported
113 machine.wait_until_succeeds("crictl img | grep 'test\.local/pause'")
114 machine.wait_until_succeeds("crictl img | grep 'test\.local/hello'")
115
116 # check if resources of manifests got created
117 machine.wait_until_succeeds("kubectl get ns foo")
118 machine.wait_until_succeeds("kubectl wait --for=condition=complete job/hello")
119 machine.fail("kubectl get ns absent")
120 '';
121
122 meta.maintainers = lib.teams.k3s.members;
123 }
124)