<p>I would like to calculate the total number of bytes allocated by the persistent volumes (PVs) in a cluster. Using the following:</p>
<pre><code>$ kubectl get pv -A -o json
</code></pre>
<p>I can get a JSON list of all the cluster's PVs and for each PV in the <code>items[]</code> list one can read the <code>spec.capacity.storage</code> key to access the necessary information.
See example below:</p>
<pre><code>{
  &quot;apiVersion&quot;: &quot;v1&quot;,
  &quot;kind&quot;: &quot;PersistentVolume&quot;,
  &quot;spec&quot;: {
    &quot;accessModes&quot;: [
      &quot;ReadWriteOnce&quot;
    ],
    &quot;capacity&quot;: {
      &quot;storage&quot;: &quot;500Gi&quot;
    },
    &quot;claimRef&quot;: {
      &quot;apiVersion&quot;: &quot;v1&quot;,
      &quot;kind&quot;: &quot;PersistentVolumeClaim&quot;,
      &quot;name&quot;: &quot;s3-storage-minio&quot;,
      &quot;namespace&quot;: &quot;default&quot;,
      &quot;resourceVersion&quot;: &quot;515932&quot;,
    },
    &quot;persistentVolumeReclaimPolicy&quot;: &quot;Delete&quot;,
    &quot;volumeMode&quot;: &quot;Filesystem&quot;,
  },
  &quot;status&quot;: {
    &quot;phase&quot;: &quot;Bound&quot;
  }
},
</code></pre>
<p>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).</p>
<p>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?</p>
<p>Otherwise, transforming different suffix to a common one in Bash looks like not very straightforward.</p>
<p>Thanks in advance for your help.</p>

<p>I haven't found a way to transform a value in <code>.spec.capacity.storage</code> using purely <code>kubectl</code>.</p>
<hr />
<p>I've managed to create a code with Python and it's Kubernetes library to extract the data and calculate the size of all used <code>PV</code>'s. Please treat this code as an example and not production ready:</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
import re 

config.load_kube_config() # use .kube/config
v1 = client.CoreV1Api()

multiplier_dict = {&quot;k&quot;: 1000, &quot;Ki&quot;: 1024, &quot;M&quot;: 1000000, &quot;Mi&quot;: 1048576 , &quot;G&quot;: 1000000000, &quot;Gi&quot;: 1073741824} # and so on ... 
size = 0 

# for i in v1.list_persistent_volume_claim_for_all_namespaces(watch=False).items: # PVC

for i in v1.list_persistent_volume(watch=False).items: # PV

    x = i.spec.capacity[&quot;storage&quot;] # PV
    # x = i.spec.resources.requests[&quot;storage&quot;] # PVC
    y = re.findall(r'[A-Za-z]+|\d+', x)
    print(y)

    # try used if no suffix (like Mi) is used
    try: 
        if y[1] in multiplier_dict: 
            size += multiplier_dict.get(y[1]) * int(y[0])
    except IndexError:
            size += int(y[0])
    
print(&quot;The size in bytes of all PV's is: &quot; + str(size))
</code></pre>
<p>Having as an example a cluster that has following <code>PV</code>'s:</p>
<ul>
<li><code>$ kubectl get pv</code></li>
</ul>
<pre class="lang-sh prettyprint-override"><code>NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE
pvc-6b5236ec-547f-4f96-8448-e3dbe01c9039   500Mi      RWO            Delete           Bound    default/pvc-four    hostpath                4m13s
pvc-86d178bc-1673-44e0-9a89-2efb14a1d22c   512M       RWO            Delete           Bound    default/pvc-three   hostpath                4m15s
pvc-89b64f93-6bf4-4987-bdda-0356d19d6f59   1G         RWO            Delete           Bound    default/pvc-one     hostpath                4m15s
pvc-a3455e77-0db0-4cab-99c9-c72721a65632   10Ki       RWO            Delete           Bound    default/pvc-six     hostpath                4m14s
pvc-b47f92ef-f627-4391-943f-efa4241d0811   10k        RWO            Delete           Bound    default/pvc-five    hostpath                4m13s
pvc-c3e13d78-9047-4899-99e7-0b2667ce4698   1Gi        RWO            Delete           Bound    default/pvc-two     hostpath                4m15s
pvc-c57fe2b0-013a-412b-bca9-05050990766a   10         RWO            Delete           Bound    default/pvc-seven   hostpath                113s
</code></pre>
<p>The code would produce the output of:</p>
<pre class="lang-sh prettyprint-override"><code>['500', 'Mi']
['512', 'M']
['1', 'G']
['10', 'Ki']
['10', 'k']
['1', 'Gi']
['10']
The size in bytes of all PV's is: 3110050074
</code></pre>
<hr />
<p>Adding to the whole answer remember that there could be differences on the request of a <code>PVC</code> and the actual <code>PV</code> size. Please refer to the storage documentation of your choosing on that regard.</p>
<ul>
<li><code>pvc.yaml</code>:</li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100M
</code></pre>
<p>Part of the <code>$ kubectl get pvc -o yaml</code> output:</p>
<pre class="lang-yaml prettyprint-override"><code>  spec:
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 100M # &lt;-- REQUEST
    &lt;-- REDACTED --&gt; 
  status:
    accessModes:
    - ReadWriteOnce
    capacity:
      storage: 1Gi # &lt;-- SIZE OF PV
    phase: Bound
</code></pre>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">Kubernetes.io: Docs: Concepts: Storage: Persistent Volumes</a></em></li>
<li><em><a href="https://en.wikipedia.org/wiki/Byte#Multiple-byte_units" rel="nofollow noreferrer">Wikipedia.org: Byte: Multiple byte units</a></em></li>
</ul>
