-
Notifications
You must be signed in to change notification settings - Fork 7
/
drone-cleanup.sh
executable file
·53 lines (43 loc) · 1.67 KB
/
drone-cleanup.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
#!/bin/sh
# Delete jobs 1 hour after they have been completed
timeout=$TTL_TIMEOUT
if [ -z "$timeout" ]; then
timeout=3600
fi
# Note that Alpine / Busybox does not support "-v"
# now=`date -v -${timeout}S -u +"%Y-%m-%dT%H:%M:%SZ"`
then="$(( `date +%s`-${timeout} ))"
now=`date -d@${then} -u +"%Y-%m-%dT%H:%M:%SZ"`
echo "*** Cleaning up Drone's leftovers. Deleting everything older than $now."
echo "=== Deleting jobs ==="
jobs=$(kubectl get jobs -o jsonpath='{range .items[?(@.status.completionTime)]}{.status.completionTime}{"|"}{.metadata.name}{"\n"}' | sort)
for i in $jobs; do
time=$(echo "$i" | cut -f1 -d\|)
name=$(echo "$i" | cut -f2 -d\|)
if ! case $name in drone-job-*) ;; *) false;; esac; then
echo "Skipping job $name, because not a drone job"
continue
fi
if [ "`expr "$time" \> "$now"`" == "1" ]; then
echo "Skipping job $name, because $time > $now"
continue
fi
echo -n "Deleting job $name: "
kubectl delete job $name
done
echo "=== Deleting pods ==="
pods=$(kubectl get pods --field-selector=status.phase!=Running --field-selector=status.phase!=Pending --field-selector=status.phase!=Pending -o jsonpath='{range .items[*]}{.status.startTime}{"|"}{.metadata.name}{"\n"}' | sort)
for i in $pods; do
time=$(echo "$i" | cut -f1 -d\|)
name=$(echo "$i" | cut -f2 -d\|)
if ! case $name in drone-job-*) ;; *) false;; esac; then
echo "Skipping pod $name, because not a drone job"
continue
fi
if [ "`expr "$time" \> "$now"`" == "1" ]; then
echo "Skipping pod $name, because $time > $now"
continue
fi
echo -n ": Deleting pod $name"
kubectl delete pod $name
done