about summary refs log tree commit diff
path: root/pkgs/applications/networking/cluster/k3s
diff options
context:
space:
mode:
authorRobert Rose <robert.rose@mailbox.org>2024-05-04 21:35:17 +0200
committerRobert Rose <robert.rose@mailbox.org>2024-05-07 11:53:15 +0200
commit2b0b15ec94d24a1991ead8312802b8f8884235c6 (patch)
tree34ee8d909428e2544a8c68ab5abe6cdbe89a651a /pkgs/applications/networking/cluster/k3s
parent6c2d986176e8f6545263fa1467012ebf51880c0f (diff)
k3s: package k3s-killall script
Provide the k3s-killall.sh script for orderly shutdown of k3s.
Diffstat (limited to 'pkgs/applications/networking/cluster/k3s')
-rw-r--r--pkgs/applications/networking/cluster/k3s/builder.nix57
1 files changed, 57 insertions, 0 deletions
diff --git a/pkgs/applications/networking/cluster/k3s/builder.nix b/pkgs/applications/networking/cluster/k3s/builder.nix
index e4c7a42fd5781..44910857c3e46 100644
--- a/pkgs/applications/networking/cluster/k3s/builder.nix
+++ b/pkgs/applications/networking/cluster/k3s/builder.nix
@@ -56,6 +56,14 @@ lib:
 , nixosTests
 , pkgsBuildBuild
 , go
+, runCommand
+, bash
+, procps
+, coreutils
+, gnugrep
+, findutils
+, gnused
+, systemd
 }:
 
 # k3s is a kinda weird derivation. One of the main points of k3s is the
@@ -157,6 +165,42 @@ let
     rev = "v${k3sVersion}";
     sha256 = k3sRepoSha256;
   };
+
+  # Modify the k3s installer script so that we can let it install only
+  # killall.sh
+  k3sKillallSh = runCommand "k3s-killall.sh" { } ''
+    # Copy the upstream k3s install script except for the last lines that
+    # actually run the install process
+    sed --quiet '/# --- run the install process --/q;p' ${k3sRepo}/install.sh > install.sh
+
+    # Let killall expect "containerd-shim" in the Nix store
+    to_replace="k3s/data/\[\^/\]\*/bin/containerd-shim"
+    replacement="/nix/store/.*k3s-containerd.*/bin/containerd-shim"
+    changes=$(sed -i "s|$to_replace|$replacement| w /dev/stdout" install.sh)
+    if [ -z "$changes" ]; then
+      echo "failed to replace \"$to_replace\" in k3s installer script (install.sh)"
+      exit 1
+    fi
+
+    remove_matching_line() {
+      line_to_delete=$(grep -n "$1" install.sh | cut -d : -f 1 || true)
+      if [ -z $line_to_delete ]; then
+        echo "failed to find expression \"$1\" in k3s installer script (install.sh)"
+        exit 1
+      fi
+      sed -i "''${line_to_delete}d" install.sh
+    }
+
+    # Don't change mode and owner of killall
+    remove_matching_line "chmod.*KILLALL_K3S_SH"
+    remove_matching_line "chown.*KILLALL_K3S_SH"
+
+    # Execute only the "create_killall" function of the installer script
+    sed -i '$acreate_killall' install.sh
+
+    KILLALL_K3S_SH=$out bash install.sh
+  '';
+
   # Stage 1 of the k3s build:
   # Let's talk about how k3s is structured.
   # One of the ideas of k3s is that there's the single "k3s" binary which can
@@ -278,6 +322,16 @@ buildGoModule rec {
     runc
   ];
 
+  k3sKillallDeps = [
+    bash
+    systemd
+    procps
+    coreutils
+    gnugrep
+    findutils
+    gnused
+  ];
+
   buildInputs = k3sRuntimeDeps;
 
   nativeBuildInputs = [
@@ -334,6 +388,9 @@ buildGoModule rec {
     ln -s $out/bin/k3s $out/bin/kubectl
     ln -s $out/bin/k3s $out/bin/crictl
     ln -s $out/bin/k3s $out/bin/ctr
+    install -m 0755 ${k3sKillallSh} -D $out/bin/k3s-killall.sh
+    wrapProgram $out/bin/k3s-killall.sh \
+      --prefix PATH : ${lib.makeBinPath (k3sRuntimeDeps ++ k3sKillallDeps)}
   '';
 
   doInstallCheck = true;