-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.sh
32 lines (25 loc) · 1.02 KB
/
script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
set -euo pipefail
# gracefully handle the TERM signal sent when deleting the daemonset
trap 'exit' TERM
# Get the list of evicted pods
evicted_pods=$(kubectl get pods --all-namespaces --field-selector=status.phase=Failed -o json | jq -r '.items[] | select(.status.reason == "Evicted") | .metadata.name + "," + .metadata.namespace')
# Delete the evicted pods
IFS=$'\n'
for pod in $evicted_pods; do
IFS=',' read -ra pod_data <<< "$pod"
pod_name=${pod_data[0]}
namespace=${pod_data[1]}
# Check if the pod exists before attempting deletion
if kubectl get pod $pod_name --namespace=$namespace >/dev/null 2>&1; then
echo "Deleting pod: $pod_name --namespace=$namespace"
kubectl delete pod $pod_name --namespace=$namespace
else
echo "Pod not found: $pod_name --namespace=$namespace"
fi
done
# let the monitoring script know we're done'
echo "done"
# this is a workaround to prevent the container from exiting
# and k8s restarting the daemonset pod
while true; do sleep 1; done