From 6b258763be63fe06b4b0e28999030e227b5c41cc Mon Sep 17 00:00:00 2001 From: Devin Buhl Date: Sat, 9 Dec 2023 09:01:16 -0500 Subject: [PATCH] Update k3s killall and uninstall scripts (#217) * Update k3s killall and uninstall scripts * Update k3s-uninstall.sh.j2 * Update k3s-uninstall.sh.j2 --- templates/k3s-killall.sh.j2 | 39 +++++++++++++++++++++++++++-------- templates/k3s-uninstall.sh.j2 | 22 +++++++++++++------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/templates/k3s-killall.sh.j2 b/templates/k3s-killall.sh.j2 index fa2769d..d044dd2 100644 --- a/templates/k3s-killall.sh.j2 +++ b/templates/k3s-killall.sh.j2 @@ -40,6 +40,27 @@ killtree() { ) 2>/dev/null } +remove_interfaces() { + # Delete network interface(s) that match 'master cni0' + ip link show 2>/dev/null | grep 'master cni0' | while read ignore iface ignore; do + iface=${iface%%@*} + [ -z "$iface" ] || ip link delete $iface + done + + # Delete cni related interfaces + ip link delete cni0 + ip link delete flannel.1 + ip link delete flannel-v6.1 + ip link delete kube-ipvs0 + ip link delete flannel-wg + ip link delete flannel-wg-v6 + + # Restart tailscale + if [ -n "$(command -v tailscale)" ]; then + tailscale set --advertise-routes= + fi +} + getshims() { ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w 'k3s/data/[^/]*/bin/containerd-shim' | cut -f1 } @@ -47,7 +68,11 @@ getshims() { killtree $({ set +x; } 2>/dev/null; getshims; set -x) do_unmount_and_remove() { - awk -v path="$1" '$2 ~ ("^" path) { print $2 }' /proc/self/mounts | sort -r | xargs -r -t -n 1 sh -c 'umount "$0" && rm -rf "$0"' + set +x + while read -r _ path _; do + case "$path" in $1*) echo "$path" ;; esac + done < /proc/self/mounts | sort -r | xargs -r -t -n 1 sh -c 'umount -f "$0" && rm -rf "$0"' + set -x } do_unmount_and_remove '/run/k3s' @@ -59,12 +84,8 @@ do_unmount_and_remove '/run/netns/cni-' # Remove CNI namespaces ip netns show 2>/dev/null | grep cni- | xargs -r -t -n 1 ip netns delete -# Delete network interface(s) that match 'master cni0' -ip link show 2>/dev/null | grep 'master cni0' | while read ignore iface ignore; do - iface=${iface%%@*} - [ -z "$iface" ] || ip link delete $iface -done -ip link delete cni0 -ip link delete flannel.1 +remove_interfaces + rm -rf /var/lib/cni/ -iptables-save | grep -v KUBE- | grep -v CNI- | iptables-restore +iptables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | iptables-restore +ip6tables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | ip6tables-restore diff --git a/templates/k3s-uninstall.sh.j2 b/templates/k3s-uninstall.sh.j2 index 9aa5678..0320f38 100644 --- a/templates/k3s-uninstall.sh.j2 +++ b/templates/k3s-uninstall.sh.j2 @@ -4,16 +4,17 @@ set -x /usr/local/bin/k3s-killall.sh -if which systemctl; then +if command -v systemctl; then systemctl disable k3s systemctl reset-failed k3s systemctl daemon-reload fi -if which rc-update; then +if command -v rc-update; then rc-update delete k3s default fi rm -f {{ k3s_systemd_unit_dir }}/k3s.service +rm -f {{ k3s_systemd_unit_dir }}/k3s.env remove_uninstall() { rm -f /usr/local/bin/k3s-uninstall.sh @@ -31,20 +32,25 @@ for cmd in kubectl crictl ctr; do fi done -for bin in {{ k3s_install_dir }}/k3s*; do - if [ -f "${bin}" ]; then - rm -f "${bin}" - fi -done - rm -rf {{ k3s_config_dir }} rm -rf /run/k3s rm -rf /run/flannel rm -rf {{ k3s_runtime_config['data-dir'] | default(k3s_data_dir) }} rm -rf /var/lib/kubelet +rm -f {{ k3s_install_dir }}/k3s rm -f /usr/local/bin/k3s-killall.sh if type yum >/dev/null 2>&1; then yum remove -y k3s-selinux rm -f /etc/yum.repos.d/rancher-k3s-common*.repo +elif type rpm-ostree >/dev/null 2>&1; then + rpm-ostree uninstall k3s-selinux + rm -f /etc/yum.repos.d/rancher-k3s-common*.repo +elif type zypper >/dev/null 2>&1; then + uninstall_cmd="zypper remove -y k3s-selinux" + if [ "${TRANSACTIONAL_UPDATE=false}" != "true" ] && [ -x /usr/sbin/transactional-update ]; then + uninstall_cmd="transactional-update --no-selfupdate -d run $uninstall_cmd" + fi + $uninstall_cmd + rm -f /etc/zypp/repos.d/rancher-k3s-common*.repo fi