Was unable to find any K8s API which can query all volumeIDs related to specific AWS account region wide. My primary intention is to clean all stale volumes. For doing this, I'm collecting volume information from AWS which are in Available state using below shell script.
for regions in $(aws ec2 describe-regions --output text|awk {'print $4'})
do
for volumes in $(aws ec2 describe-volumes --region $regions --output text| grep available | awk '{print $9}' | grep vol| tr '\n' ' ')
do
echo "$regions" "$volumes"
done
done
But that alone isn't sufficient as sometimes I keep some of my environments down and that time pods are not running which in turn marks the volumes as Available but they will be in use/attached to pods once the environment comes up. Hence, I need to get both the lists (from AWS and K8s) and diff them. Finally, I get the volumes which are actually not associated to any of my environments. Any help is greatly appreciated.
N.B: It is known the below k8s API can fetch volumes taking namespaces as input which I'm not looking for.
GET /api/v1/namespaces/{namespace}/persistentvolumeclaims
Two answers: first you can kubectl get pvc —all-namespaces
. Second, the PVs themselves are not in namespaces so kubectl get pv
.