Finding VMDK path from PV VolumeHandle

I’ve been looking at ways in which we could query the mappings of objects between the Kubernetes layer and the vSphere layer. One thing that I really wanted to figure out is if I have the VolumeHandle from the Persistent Volume in Kubernetes, could I easily find the datastore and path using PowerCLI. It looks like I can.

Let’s begin with a look at the Persistent Volume or PV  for short. Note that this is a Kubernetes cluster that is using the new vSphere CSI driver. 

$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                  STORAGECLASS   REASON   AGE
pvc-8c774366-eb45-11e9-80e4-005056a239d9   1Gi        RWO            Delete           Bound    cassandra/cassandra-data-cassandra-0   cass-sc                 11d
pvc-8c7968d5-eb45-11e9-80e4-005056a239d9   1Gi        RWO            Delete           Bound    cassandra/cassandra-data-cassandra-1   cass-sc                 11d
pvc-8c7b2535-eb45-11e9-80e4-005056a239d9   1Gi        RWO            Delete           Bound    cassandra/cassandra-data-cassandra-2   cass-sc                 11d

$ kubectl describe pv pvc-8c7968d5-eb45-11e9-80e4-005056a239d9
Name:            pvc-8c7968d5-eb45-11e9-80e4-005056a239d9
Labels:          <none>
Annotations:     pv.kubernetes.io/provisioned-by: csi.vsphere.vmware.com
Finalizers:      [kubernetes.io/pv-protection external-attacher/csi-vsphere-vmware-com]
StorageClass:    cass-sc
Status:          Bound
Claim:           cassandra/cassandra-data-cassandra-1
Reclaim Policy:  Delete
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        1Gi
Node Affinity:   <none>
Message:
Source:
    Type:              CSI (a Container Storage Interface (CSI) volume source)
    Driver:            csi.vsphere.vmware.com
    VolumeHandle:      937b445e-bd7d-4576-987e-5f7e32147af1
    ReadOnly:          false
    VolumeAttributes:      fstype=
                           storage.kubernetes.io/csiProvisionerIdentity=1566810378446-8081-csi.vsphere.vmware.com
                           type=vSphere CNS Block Volume
Events:                <none>

Now the objective is to see if we can quickly determine the datastore and path to file from the VolumeHandle above. Note that the CSI driver uses First Class Disks (FCDs) also referred to Improved Virtual Disks (IVDs), a new kind of VMDK. The VolumeHandle above is the same as the FCD ID. Here is how to find that information with the FCD ID using a few PowerCLI commands:

$ pwsh
PowerShell v6.1.0-preview.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /home/cormac/PowerCLI> Connect-VIServer vcsa-06-b.rainpole.com -User administrator@vsphere.local -Password xxx -force

Name                           Port  User
----                           ----  ----
vcsa-06-b.rainpole.com         443   VSPHERE.LOCAL\Administrator

PS /home/cormac/PowerCLI> Get-vDisk | ft -autosize

Name                                     Disk Type CapacityGB Filename
----                                     --------- ----------                                                                                   --------
pvc-8c7b2535-eb45-11e9-80e4-005056a239d9 Flat      1.000      [vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/271c0c4065e545e7a2a1fefdd17cf4d3.vmdk
pvc-8c7968d5-eb45-11e9-80e4-005056a239d9 Flat      1.000      [vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/ff34579cbed54dc994976885e22c7cb9.vmdk
pvc-8c774366-eb45-11e9-80e4-005056a239d9 Flat      1.000      [vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/6a02671fb7ed4358a02f39e71e764bd2.vmdk


PS /home/cormac/PowerCLI> $vd = Get-vDisk

PS /home/cormac/PowerCLI> $vd.id
Datastore-datastore-33:57f69494-24a2-4b28-9d1b-bc43fe08a0d2
Datastore-datastore-33:937b445e-bd7d-4576-987e-5f7e32147af1
Datastore-datastore-33:d638ac80-88f8-40e4-bca7-281bc7b98fde

PS /home/cormac/PowerCLI> $vdFromId = Get-VDisk -Id "Datastore-datastore-33:57f69494-24a2-4b28-9d1b-bc43fe08a0d2"

PS /home/cormac/PowerCLI> $vdFromId.Filename
[vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/271c0c4065e545e7a2a1fefdd17cf4d3.vmdk

And there we have a relatively easy way to figure out what the datastore and path to VMDK are, given that the PV describe command only provides the FCD ID in the VolumeHandle field. Kudos to my colleague Manish Tiwari for his assistance in figuring this out.

[Update] Thanks to some feedback from my good pal William Lam, we are able to get the above into a one liner:

PS /home/cormac/PowerCLI> $VolumeHandleId = "d638ac80-88f8-40e4-bca7-281bc7b98fde"
PS /home/cormac/PowerCLI> (Get-vDisk -Id (Get-vDisk).id | where {$_.id -match $VolumeHandleId}).Filename
[vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/6a02671fb7ed4358a02f39e71e764bd2.vmdk