summaryrefslogtreecommitdiff
path: root/pkgs/applications/networking/cluster/rke2/builder.nix
blob: 215423e3ad5969a61c83703c16996ec14d54192d (about) (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
lib:
{
  rke2Version,
  rke2Commit,
  rke2TarballHash,
  rke2VendorHash,
  updateScript,
  k8sImageTag,
  etcdVersion,
  pauseVersion,
  ccmVersion,
  dockerizedVersion,
  imagesVersions,
}:

# Build dependencies
{
  lib,
  stdenv,
  buildGoModule,
  go,
  makeWrapper,
  fetchzip,
  fetchurl,

  # Runtime dependencies
  procps,
  coreutils,
  util-linux,
  ethtool,
  socat,
  iptables,
  bridge-utils,
  iproute2,
  kmod,
  lvm2,

  # Killall Script dependencies
  systemd,
  gnugrep,
  gnused,

  # Testing dependencies
  nixosTests,
  testers,
}:
let
  rke2 = buildGoModule rec {
    pname = "rke2";
    version = rke2Version;

    src = fetchzip {
      url = "https://github.com/rancher/rke2/archive/refs/tags/v${rke2Version}.tar.gz";
      hash = "${rke2TarballHash}";
    };

    vendorHash = rke2VendorHash;

    nativeBuildInputs = [ makeWrapper ];

    # Important utilities used by the kubelet.
    # See: https://github.com/kubernetes/kubernetes/issues/26093#issuecomment-237202494
    # Notice the list in that issue is stale, but as a redundancy reservation.
    buildInputs = [
      procps # pidof pkill
      coreutils # uname touch env nice du
      util-linux # lsblk fsck mkfs nsenter mount umount
      ethtool # ethtool
      socat # socat
      iptables # iptables iptables-restore iptables-save
      bridge-utils # brctl
      iproute2 # ip tc
      kmod # modprobe
      lvm2 # dmsetup
    ];

    # Passing boringcrypto to GOEXPERIMENT variable to build with goboring library
    GOEXPERIMENT = "boringcrypto";

    # See: https://github.com/rancher/rke2/blob/e7f87c6dd56fdd76a7dab58900aeea8946b2c008/scripts/build-binary#L27-L38
    ldflags = [
      "-w"
      "-X github.com/k3s-io/k3s/pkg/version.GitCommit=${lib.substring 0 6 rke2Commit}"
      "-X github.com/k3s-io/k3s/pkg/version.Program=${pname}"
      "-X github.com/k3s-io/k3s/pkg/version.Version=v${version}"
      "-X github.com/k3s-io/k3s/pkg/version.UpstreamGolang=go${go.version}"
      "-X github.com/rancher/rke2/pkg/images.DefaultRegistry=docker.io"
      "-X github.com/rancher/rke2/pkg/images.DefaultEtcdImage=rancher/hardened-etcd:${etcdVersion}"
      "-X github.com/rancher/rke2/pkg/images.DefaultKubernetesImage=rancher/hardened-kubernetes:${k8sImageTag}"
      "-X github.com/rancher/rke2/pkg/images.DefaultPauseImage=rancher/mirrored-pause:${pauseVersion}"
      "-X github.com/rancher/rke2/pkg/images.DefaultRuntimeImage=rancher/rke2-runtime:${dockerizedVersion}"
      "-X github.com/rancher/rke2/pkg/images.DefaultCloudControllerManagerImage=rancher/rke2-cloud-provider:${ccmVersion}"
    ];

    tags = [
      "no_cri_dockerd"
      "no_embedded_executor"
      "no_stage"
      "sqlite_omit_load_extension"
      "selinux"
      "netgo"
      "osusergo"
    ];

    subPackages = [ "." ];

    installPhase = ''
      install -D $GOPATH/bin/rke2 $out/bin/rke2
      wrapProgram $out/bin/rke2 \
        --prefix PATH : ${lib.makeBinPath buildInputs}

      install -D ./bundle/bin/rke2-killall.sh $out/bin/rke2-killall.sh
      wrapProgram $out/bin/rke2-killall.sh \
        --prefix PATH : ${
          lib.makeBinPath [
            systemd
            gnugrep
            gnused
          ]
        } \
        --prefix PATH : ${lib.makeBinPath buildInputs}
    '';

    doCheck = false;

    doInstallCheck = true;
    installCheckPhase = ''
      runHook preInstallCheck
      # Verify that the binary uses BoringCrypto
      go tool nm $out/bin/.rke2-wrapped | grep '_Cfunc__goboringcrypto_' > /dev/null
      runHook postInstallCheck
    '';

    passthru = {
      inherit updateScript;
      tests =
        let
          moduleTests =
            let
              package_version =
                "rke2_" + lib.replaceStrings [ "." ] [ "_" ] (lib.versions.majorMinor rke2Version);
            in
            lib.mapAttrs (name: value: nixosTests.rke2.${name}.${package_version}) nixosTests.rke2;
        in
        {
          version = testers.testVersion {
            package = rke2;
            version = "v${version}";
          };
        }
        // moduleTests;
    } // (lib.mapAttrs (_: value: fetchurl value) imagesVersions);

    meta = with lib; {
      homepage = "https://github.com/rancher/rke2";
      description = "RKE2, also known as RKE Government, is Rancher's next-generation Kubernetes distribution";
      changelog = "https://github.com/rancher/rke2/releases/tag/v${version}";
      license = licenses.asl20;
      maintainers = with maintainers; [
        rorosen
        zimbatm
        zygot
      ];
      mainProgram = "rke2";
      platforms = platforms.linux;
    };
  };
in
rke2