Created
March 21, 2022 17:55
-
-
Save timroster/5b675ece537e4a128b4822aa74045278 to your computer and use it in GitHub Desktop.
Script to remove IBM Cloud classic volumes left behind by using Retain storageclasses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################################### | |
# # | |
# delete-pvc-orphans.sh (c) 2022 IBM Corporation and Tim Robinson # | |
# # | |
# IBM Cloud Kubernetes service can leave behind block and file volumes in # | |
# some cases. Whether by using -retain storage classes or deleting clusters # | |
# from the API without specifying to remove cluster volumes, these volumes # | |
# can accumulate in an account and prevent creation of new volumes. # | |
# # | |
# This script can list volumes in an IBM Cloud account that are associated # | |
# to a pvcs that have been removed. It can also be used to delete these # | |
# volumes. This script requires the ibmcloud cli and jq to be installed where # | |
# it is run. The ibmcloud cli session must be logged in with an account that # | |
# has infrastructure permissions. # | |
# # | |
# # | |
# Note: this requires a valid ibmcloud login session as well as a login # | |
# to the OpenShift / IKS cluster # | |
############################################################################### | |
DELETE="${DELETE:-false}" | |
ALL_VOLUMES="${ALL_VOLUMES:-false}" | |
print_help(){ | |
cat << EOF | |
Usage: $0 [OPTIONS]... | |
Check for classic block and file volumes that were once associated with a | |
cluster but now are not. Optionally, delete them. | |
Also optionally, ignore a blank notes field on the volume meaning it may | |
never have been associated with a cluster. Use with extreme caution. | |
Requires an active cluster login session | |
-d, --delete remove the volume after listing it | |
--all-volumes remove volumes that have a blank notes field - dangerous! | |
-h --help display this help and exit | |
EOF | |
} | |
for OPT in "$@"; do | |
case "$OPT" in | |
-d|--delete) | |
DELETE="true" | |
;; | |
--all-volumes) | |
ALL_VOLUMES="true" | |
;; | |
-h|--help) | |
print_help | |
exit 0 | |
;; | |
*) | |
echo "Unexpected flag $OPT" | |
print_help | |
exit 2 | |
;; | |
esac | |
done | |
TMPFILE=$(mktemp /tmp/pvc.lst.XXXXX) | |
# build list of current clusters in the account - should work across all RGs | |
# ibmcloud ks clusters | grep classic | awk '{ print $ 2}' > ${TMPFILE} | |
kubectl get pvc --all-namespaces | awk '{ print $ 4}' > ${TMPFILE} | |
# list (and delete) orphaned block volumes | |
for block_id in $(ibmcloud sl block volume-list --column id --column notes --output JSON | jq '.[].id') | |
do pvc=$(ibmcloud sl block volume-list --column id --column notes --output JSON | jq -r ".[]|select(.id==$block_id)|.notes" | jq -r .pv) | |
if [ ${pvc} != "null" ] || [ ${ALL_VOLUMES} = "true" ]; then | |
grep $pvc ${TMPFILE} > /dev/null | |
if [ $? -eq 1 ]; then | |
vname=$(ibmcloud sl block volume-detail $block_id --output JSON | jq -r ".username") | |
echo "claim $pvc not found for block volume $block_id with name $vname" | |
if [ ${DELETE} = true ]; then | |
echo "deleting the volume..." | |
ibmcloud sl block volume-cancel $block_id --immediate -f | |
fi | |
fi | |
fi | |
done | |
# list (and delete) orphaned file volumes | |
for file_id in $(ibmcloud sl file volume-list --column id --column notes --output JSON | jq '.[].id') | |
do pvc=$(ibmcloud sl file volume-list --column id --column notes --output JSON | jq -r ".[]|select(.id==$file_id)|.notes" | jq -r .pv) | |
if [ ${pvc} != "null" ] || [ ${ALL_VOLUMES} = "true" ]; then | |
grep $pvc ${TMPFILE} > /dev/null | |
if [ $? -eq 1 ]; then | |
vname=$(ibmcloud sl file volume-detail $file_id --output JSON | jq -r ".username") | |
echo "claim $pvc not found for file volume $file_id with name $vname" | |
if [ ${DELETE} = true ]; then | |
echo "deleting the volume..." | |
ibmcloud sl file volume-cancel $file_id --immediate -f | |
fi | |
fi | |
fi | |
done | |
# clean up by removing the cluster list | |
rm -rf ${TMPFILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment