From a0bcc831bc018bb3956cb3c72200dfa7bb781406 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= <valtri@civ.zcu.cz>
Date: Wed, 10 Jul 2024 14:21:43 +0000
Subject: [PATCH] More robust pods cleaner script - kill everything in
 Terminating state

---
 .../playbooks/files/usr/local/bin/k8s-pods-cleaner.sh    | 9 ++++-----
 cesnet-central/playbooks/notebooks.yaml                  | 2 +-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/cesnet-central/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh b/cesnet-central/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh
index 5a0e482..6285933 100755
--- a/cesnet-central/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh
+++ b/cesnet-central/playbooks/files/usr/local/bin/k8s-pods-cleaner.sh
@@ -5,18 +5,17 @@
 #
 # Example:
 #
-# k8s-pods-cleaner.sh hub app=jupyterhub,component=singleuser-server --yes
+# k8s-pods-cleaner.sh hub --yes
 #
 
 if [ -z "$1" ]; then
-	echo "Usage: $0 NAMESPACE [QUERY [--yes]]"
+	echo "Usage: $0 NAMESPACE [--yes]"
 	exit 0
 fi
 
 NS="$1"
-QUERY="$2"
-PERFORM="$3"
-for pod in $(kubectl get pod -n "$NS" -l "$QUERY" --field-selector="status.phase==Failed" -ojsonpath='{.items[*].metadata.name}'); do
+PERFORM="$2"
+for pod in $(kubectl get pod -n "$NS" | awk 'NR>1 && $3=="Terminating" {print $1}'); do
 	if [ -n "$PERFORM" ]; then
 		kubectl get pod -n "$NS" "$pod" -oyaml > "/tmp/$NS-$pod.yaml"
 		kubectl delete pod -n "$NS" "$pod" --force
diff --git a/cesnet-central/playbooks/notebooks.yaml b/cesnet-central/playbooks/notebooks.yaml
index 5da0f74..0c432cd 100644
--- a/cesnet-central/playbooks/notebooks.yaml
+++ b/cesnet-central/playbooks/notebooks.yaml
@@ -133,7 +133,7 @@
         name: "Notebooks {{ name }} cleanup"
         minute: "*"
         hour: "*"
-        job: "KUBECONFIG=$HOME/.kube/config /usr/local/bin/k8s-pods-cleaner.sh '{{ name }}' app=jupyterhub,component=singleuser-server --yes >/dev/null 2>&1"
+        job: "KUBECONFIG=$HOME/.kube/config /usr/local/bin/k8s-pods-cleaner.sh '{{ name }}' --yes >/dev/null 2>&1"
         user: egi
       with_fileglob:
         - "../deployments/*.yaml"
-- 
GitLab