-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdelete-error-pod-openshift.sh
60 lines (44 loc) · 1.29 KB
/
delete-error-pod-openshift.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# Date: 04/18/2017
#
# Script Purpose: Cleanup completed or dead pods
# Version : 1.0
#
if [ "$#" -gt 1 ]; then
echo "
Usage: delete-dead-pods.sh [Dry Run]
Example: delete-dead-pods.sh false
NOTE: If not specified, Dry Run is assumed true.
"
exit 1
fi
dryrun=true
if [ "$#" -gt 0 ]; then
dryrun=$1
fi
if [ "$dryrun" = "true" ]; then
echo "Executing dry run"
#Just echo the namespace and pod.
oc get pods --all-namespaces --no-headers | awk '$4 == "Error" \
|| $4 == "Completed" \
|| $4 == "DeadlineExceeded" \
|| $4 == "ContainerCannotRun" \
|| $4 == "Terminating" \
{system("bash -c '\''echo namespace: "$1", pod: "$2" '\''")}'
exit 0
fi
if [ "$dryrun" = "false" ]; then
echo "Executing actual delete"
#Delete all Error, Completed, DeadlineExceeded, or ContainerCannotRun pods.
oc get pods --all-namespaces --no-headers | awk '$4 == "Error" \
|| $4 == "Completed" \
|| $4 == "DeadlineExceeded" \
|| $4 == "ContainerCannotRun" \
{system("bash -c '\''oc delete pod -n "$1" "$2" '\''")}'
#Force kill any hanging pods
oc get pods --no-headers | awk '$4 == "Terminating" \
{system("bash -c '\''oc delete pod -n "$1" "$2" --grace-period=0 '\''")}'
exit 0
fi
echo "Invalid command: $dryrun"
exit 1