Last active
June 25, 2024 21:10
-
-
Save timroster/6862807a7dd30a78d39fa2f4212a895e to your computer and use it in GitHub Desktop.
Bash script to remove orphaned classic volumes from IBM Cloud Kubernetes service
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-orphans.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. # | |
# # | |
# The script only detects and removes "Classic" storage block and file # | |
# volumes. A similar approach could be taken to remove vpc block volumes. # | |
# # | |
############################################################################### | |
DELETE="${DELETE:-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. | |
-d, --delete remove the volume after listing it | |
-h --help display this help and exit | |
EOF | |
} | |
for OPT in "$@"; do | |
case "$OPT" in | |
-d|--delete) | |
DELETE="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" ]; 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" ]; 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} |
updated to remove the option to delete all volumes from an account. contact the author for a link to a higher function version.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fixed a couple of logic errors... changed search for clusters to just classic and passing correct arguments for volume-cancel...