From 07107bbcd14414560b5cdbbffb36f9e89b5fd657 Mon Sep 17 00:00:00 2001 From: Stefan Reimer Date: Mon, 5 Dec 2022 15:29:06 +0000 Subject: [PATCH] feat: add emergency cgroup cleanup script --- scripts/gc_cgroups.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 scripts/gc_cgroups.sh diff --git a/scripts/gc_cgroups.sh b/scripts/gc_cgroups.sh new file mode 100755 index 00000000..366782af --- /dev/null +++ b/scripts/gc_cgroups.sh @@ -0,0 +1,32 @@ +#!/bin/bash +#set -x + +POD_IDS=($(crictl pods -q)) +POD_UIDS=() + +for POD_ID in ${POD_IDS[@]}; do + JSONDUMP="`crictl inspectp ${POD_ID}`" + POD_NAME="`echo ${JSONDUMP} | jq -r '.status.metadata.name'`" + POD_UID="`echo ${JSONDUMP} | jq -r '.info.runtimeSpec.annotations."io.kubernetes.pod.uid"'`" + POD_UIDS+=($POD_UID) +done + +# echo ${POD_UIDS[*]} + +CGROUPS=($(find /sys/fs/cgroup/pids/kubepods/*/pod* -type d -depth)) +CGROUPS+=($(find /sys/fs/cgroup/kubepods/*/pod* -type d -depth)) + +DELETED=0 +for cg in ${CGROUPS[*]}; do + valid=0 + for uid in ${POD_UIDS[*]}; do + echo $cg | grep -q $uid && { valid=1; break; } + done + + if [ $valid -eq 0 ]; then + rmdir $cg + ((DELETED=DELETED+1)) + fi +done + +echo "Removed $DELETED left over cgroup folders."