summaryrefslogtreecommitdiffstats
path: root/scripts/disaster/gluster_endpoints/check_pv.sh
blob: 1f2a7e48f193f0b5f1288e994725bdeb068904b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/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