I would like to calculate the total number of bytes allocated by the persistent volumes (PVs) in a cluster. Using the following:
$ kubectl get pv -A -o json
I can get a JSON list of all the cluster's PVs and for each PV in the items[]
list one can read the spec.capacity.storage
key to access the necessary information.
See example below:
{
"apiVersion": "v1",
"kind": "PersistentVolume",
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"capacity": {
"storage": "500Gi"
},
"claimRef": {
"apiVersion": "v1",
"kind": "PersistentVolumeClaim",
"name": "s3-storage-minio",
"namespace": "default",
"resourceVersion": "515932",
},
"persistentVolumeReclaimPolicy": "Delete",
"volumeMode": "Filesystem",
},
"status": {
"phase": "Bound"
}
},
However, the returned values can be represented in different suffix (storage as a plain integer or as a fixed-point number using one of these suffixes: E, P, T, G, M, K. Or similarly, power-of-two equivalents: Ei, Pi, Ti, Gi, Mi, Ki).
Is there a neat way to request the capacity in integer format (or any other format but consistent among all the PVs) using the kubectl?
Otherwise, transforming different suffix to a common one in Bash looks like not very straightforward.
Thanks in advance for your help.