diff options
-rw-r--r-- | recipes-containers/k3s/k3s/k3s-killall.sh | 82 | ||||
-rw-r--r-- | recipes-containers/k3s/k3s/k3s.service | 12 | ||||
-rw-r--r-- | recipes-containers/k3s/k3s_git.bb | 2 |
3 files changed, 95 insertions, 1 deletions
diff --git a/recipes-containers/k3s/k3s/k3s-killall.sh b/recipes-containers/k3s/k3s/k3s-killall.sh new file mode 100644 index 00000000..9e726153 --- /dev/null +++ b/recipes-containers/k3s/k3s/k3s-killall.sh | |||
@@ -0,0 +1,82 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | # Based on: k3s-killall.sh installed when running Rancher Lab's K3S install.sh | ||
4 | # In open-source project: https://github.com/k3s-io/k3s | ||
5 | # | ||
6 | # Original file: Copyright (c) 2021 Rancher Labs and Contributors. | ||
7 | # Modifications: Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. | ||
8 | # | ||
9 | # Modifications: | ||
10 | # - Change systemd service directory location | ||
11 | # - Fix PID parsing to run on core image | ||
12 | # - Remove service stopping code (as this is intended to run as part of service | ||
13 | # stop) | ||
14 | # - Changes to resolve warnings from the ShellCheck static analysis tool | ||
15 | # | ||
16 | # SPDX-License-Identifier: Apache License 2.0 | ||
17 | |||
18 | [ "$(id -u)" -eq 0 ] || exec sudo "$0" "$@" | ||
19 | |||
20 | for bin in /var/lib/rancher/k3s/data/**/bin/; do | ||
21 | [ -d "$bin" ] && export PATH=$PATH:$bin:$bin/aux | ||
22 | done | ||
23 | |||
24 | set -x | ||
25 | |||
26 | pschildren() { | ||
27 | ps -e -o ppid= -o pid= | sed -e 's/^\s*//g; s/\s\s*/\t/g;' | grep -w "^$1" | cut -f2 | ||
28 | } | ||
29 | |||
30 | pstree() { | ||
31 | for pid in "$@"; do | ||
32 | echo "$pid" | ||
33 | for child in $(pschildren "$pid"); do | ||
34 | pstree "$child" | ||
35 | done | ||
36 | done | ||
37 | } | ||
38 | |||
39 | killtree() { | ||
40 | while read -r pid; do | ||
41 | if [ -n "${pid}" ]; then | ||
42 | kill -9 "${pid}" 2>/dev/null | ||
43 | fi | ||
44 | done <<EOF | ||
45 | $({ set +x; } 2>/dev/null; pstree "$@"; set -x;) | ||
46 | EOF | ||
47 | } | ||
48 | |||
49 | getshims() { | ||
50 | ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w '[^/]*/bin/containerd-shim' | cut -f1 | ||
51 | } | ||
52 | |||
53 | killtree "$({ set +x; } 2>/dev/null; getshims; set -x)" | ||
54 | |||
55 | # shellcheck disable=SC2016 | ||
56 | do_unmount_and_remove() { | ||
57 | set +x | ||
58 | while read -r _ path _; do | ||
59 | case "$path" in $1*) echo "$path" ;; esac | ||
60 | done < /proc/self/mounts | sort -r | xargs -r -t -n 1 sh -c 'umount "$0" && rm -rf "$0"' | ||
61 | set -x | ||
62 | } | ||
63 | |||
64 | do_unmount_and_remove '/run/k3s' | ||
65 | do_unmount_and_remove '/var/lib/rancher/k3s' | ||
66 | do_unmount_and_remove '/var/lib/kubelet/pods' | ||
67 | do_unmount_and_remove '/var/lib/kubelet/plugins' | ||
68 | do_unmount_and_remove '/run/netns/cni-' | ||
69 | |||
70 | # Remove CNI namespaces | ||
71 | ip netns show 2>/dev/null | grep cni- | xargs -r -t -n 1 ip netns delete | ||
72 | |||
73 | # Delete network interface(s) that match 'master cni0' | ||
74 | ip link show 2>/dev/null | grep 'master cni0' | while read -r _ iface _; do | ||
75 | iface=${iface%%@*} | ||
76 | [ -z "$iface" ] || ip link delete "$iface" | ||
77 | done | ||
78 | ip link delete cni0 | ||
79 | ip link delete flannel.1 | ||
80 | ip link delete flannel-v6.1 | ||
81 | rm -rf /var/lib/cni/ | ||
82 | iptables-save | grep -v KUBE- | grep -v CNI- | iptables-restore | ||
diff --git a/recipes-containers/k3s/k3s/k3s.service b/recipes-containers/k3s/k3s/k3s.service index 34c7a804..33d3ee74 100644 --- a/recipes-containers/k3s/k3s/k3s.service +++ b/recipes-containers/k3s/k3s/k3s.service | |||
@@ -4,12 +4,17 @@ Description=Lightweight Kubernetes | |||
4 | Documentation=https://k3s.io | 4 | Documentation=https://k3s.io |
5 | Requires=containerd.service | 5 | Requires=containerd.service |
6 | After=containerd.service | 6 | After=containerd.service |
7 | After=network-online.target | ||
8 | Wants=network-online.target | ||
7 | 9 | ||
8 | [Install] | 10 | [Install] |
9 | WantedBy=multi-user.target | 11 | WantedBy=multi-user.target |
10 | 12 | ||
11 | [Service] | 13 | [Service] |
12 | Type=notify | 14 | Type=notify |
15 | EnvironmentFile=-/etc/default/%N | ||
16 | EnvironmentFile=-/etc/sysconfig/%N | ||
17 | EnvironmentFile=-/etc/systemd/system/k3s.service.env | ||
13 | KillMode=process | 18 | KillMode=process |
14 | Delegate=yes | 19 | Delegate=yes |
15 | # Having non-zero Limit*s causes performance problems due to accounting overhead | 20 | # Having non-zero Limit*s causes performance problems due to accounting overhead |
@@ -21,7 +26,12 @@ TasksMax=infinity | |||
21 | TimeoutStartSec=0 | 26 | TimeoutStartSec=0 |
22 | Restart=always | 27 | Restart=always |
23 | RestartSec=5s | 28 | RestartSec=5s |
29 | ExecStartPre=/bin/sh -xc '! systemctl is-enabled --quiet nm-cloud-setup.service' | ||
24 | ExecStartPre=-/sbin/modprobe br_netfilter | 30 | ExecStartPre=-/sbin/modprobe br_netfilter |
25 | ExecStartPre=-/sbin/modprobe overlay | 31 | ExecStartPre=-/sbin/modprobe overlay |
26 | ExecStart=/usr/local/bin/k3s server | 32 | ExecStart=/usr/local/bin/k3s server |
27 | 33 | # Avoid any delay due to this service when the system is rebooting or shutting | |
34 | # down by using the k3s-killall.sh script to kill all of the running k3s | ||
35 | # services and containers | ||
36 | ExecStopPost=/bin/sh -c "if systemctl is-system-running | grep -i \ | ||
37 | 'stopping'; then /usr/local/bin/k3s-killall.sh; fi" | ||
diff --git a/recipes-containers/k3s/k3s_git.bb b/recipes-containers/k3s/k3s_git.bb index 5971ddec..0300fc3c 100644 --- a/recipes-containers/k3s/k3s_git.bb +++ b/recipes-containers/k3s/k3s_git.bb | |||
@@ -11,6 +11,7 @@ SRC_URI = "git://github.com/rancher/k3s.git;branch=release-1.22;name=k3s \ | |||
11 | file://k3s-clean \ | 11 | file://k3s-clean \ |
12 | file://cni-containerd-net.conf \ | 12 | file://cni-containerd-net.conf \ |
13 | file://0001-Finding-host-local-in-usr-libexec.patch;patchdir=src/import \ | 13 | file://0001-Finding-host-local-in-usr-libexec.patch;patchdir=src/import \ |
14 | file://k3s-killall.sh \ | ||
14 | " | 15 | " |
15 | SRC_URI[k3s.md5sum] = "363d3a08dc0b72ba6e6577964f6e94a5" | 16 | SRC_URI[k3s.md5sum] = "363d3a08dc0b72ba6e6577964f6e94a5" |
16 | SRCREV_k3s = "737f722315b9832e9180fa232253d28ae1f2272f" | 17 | SRCREV_k3s = "737f722315b9832e9180fa232253d28ae1f2272f" |
@@ -60,6 +61,7 @@ do_install() { | |||
60 | # ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/ctr" | 61 | # ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/ctr" |
61 | ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/kubectl" | 62 | ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/kubectl" |
62 | install -m 755 "${WORKDIR}/k3s-clean" "${D}${BIN_PREFIX}/bin" | 63 | install -m 755 "${WORKDIR}/k3s-clean" "${D}${BIN_PREFIX}/bin" |
64 | install -m 755 "${WORKDIR}/k3s-killall.sh" "${D}${BIN_PREFIX}/bin" | ||
63 | 65 | ||
64 | if ${@bb.utils.contains('DISTRO_FEATURES','systemd','true','false',d)}; then | 66 | if ${@bb.utils.contains('DISTRO_FEATURES','systemd','true','false',d)}; then |
65 | install -D -m 0644 "${WORKDIR}/k3s.service" "${D}${systemd_system_unitdir}/k3s.service" | 67 | install -D -m 0644 "${WORKDIR}/k3s.service" "${D}${systemd_system_unitdir}/k3s.service" |