#!/bin/bash pvs=$(oc get pv -o json | jq -r ' .items[] | select(.spec.glusterfs?) | select(.spec.glusterfs.endpoints != "gfs") | "\(.metadata.name) → endpoints=\(.spec.glusterfs.endpoints // "NONE")"') echo "PV usage:" echo #pvs=$(oc get pv --no-headers | awk '{print $1}') for pv in $pvs; do # Extract PVC and namespace bound to PV pvc=$(oc get pv "$pv" -o jsonpath='{.spec.claimRef.name}' 2>/dev/null) ns=$(oc get pv "$pv" -o jsonpath='{.spec.claimRef.namespace}' 2>/dev/null) if [[ -z "$pvc" || -z "$ns" ]]; then echo "$pv → UNUSED" echo continue fi echo "$pv → PVC: $ns/$pvc" # Grep instead of JSONPath filter — much safer pods=$(oc get pods -n "$ns" -o name \ | while read -r pod; do oc get "$pod" -n "$ns" -o json \ | jq -r --arg pvc "$pvc" ' . as $pod | .spec.volumes[]? | select(.persistentVolumeClaim? and .persistentVolumeClaim.claimName == $pvc) | $pod.metadata.name ' 2>/dev/null done \ | sort -u ) if [[ -z "$pods" ]]; then echo " → PVC bound but no running Pod is using it" else echo " → Pods:" echo "$pods" | sed 's/^/ - /' fi echo done