Last active
July 20, 2023 22:23
-
-
Save timroster/6b7063b02fb315e73c96b4dda3dbee46 to your computer and use it in GitHub Desktop.
Bash script to remove orphaned classic volumes from IBM Cloud Kubernetes service, alternately, remove all volumes from the account.
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-sl-storage.sh (c) 2021,2023 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 cluster that has 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. # | |
# # | |
############################################################################### | |
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. | |
-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/cluster.lst.XXXXX) | |
# build list of current clusters in the account - should work across all RGs | |
ibmcloud ks clusters | grep classic | awk '{ print $ 2}' > ${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 cluster=$(ibmcloud sl block volume-list --column id --column notes --output JSON | jq -r ".[]|select(.id==$block_id)|.notes" | jq -r .cluster) | |
if [ ${cluster} != "null" ] || [ ${ALL_VOLUMES} = "true" ]; then | |
grep $cluster ${TMPFILE} > /dev/null | |
if [ $? -eq 1 ]; then | |
echo "cluster $cluster not found for block volume $block_id" | |
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 cluster=$(ibmcloud sl file volume-list --column id --column notes --output JSON | jq -r ".[]|select(.id==$file_id)|.notes" | jq -r .cluster) | |
if [ ${cluster} != "null" ] || [ ${ALL_VOLUMES} = "true" ]; then | |
grep $cluster ${TMPFILE} > /dev/null | |
if [ $? -eq 1 ]; then | |
echo "cluster $cluster not found for file volume $file_id" | |
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