-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpvc-deletion.sh
More file actions
26 lines (26 loc) · 1.22 KB
/
pvc-deletion.sh
File metadata and controls
26 lines (26 loc) · 1.22 KB
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
#!/bin/sh
# Get all PVCs by running pods
ALL_PVCS=$(kubectl get pvc -n $RELEASE_NAMESPACE -o=jsonpath='{.items[*].metadata.name}' | tr ' ' '\n' | sort -u)
BLUEAPI_PVCS=$( echo $ALL_PVCS | tr ' ' '\n' | grep blueapi-scratch)
NOW=$(date +%s)
#loop through all pvcs.
for pvc in $BLUEAPI_PVCS; do
#check if pvc has last-used annotation
#get last used annotation
LAST_USED=$(kubectl get pvc "$pvc" -n $RELEASE_NAMESPACE -o=jsonpath='{.metadata.annotations.last-used}')
#checking if its not null
if [ -n "$LAST_USED" ]; then
#check if last_used is older than 3 months
if [ $(($NOW - LAST_USED)) -gt 7884000 ]; then
#checking if the pvc is protected, if it is protected skip deletion
if [ "$(kubectl get pvc "$pvc" -n $RELEASE_NAMESPACE -o=jsonpath='{.metadata.annotations.protected}')" = "true" ]; then
echo " PVC $pvc is protected, skipping deletion"
continue
fi
#PVC has not been used for more than three months, delete it
kubectl delete pvc "$pvc" -n $RELEASE_NAMESPACE
fi
else
echo " $pvc has no last-used annotation"
fi
done