Change:
When user creates a static PV using folder name in path: on vSAN backed datastore then detach disk does not work because check fails - The code expects folder name to be UUID but it does not and hence it determines disk is not attached to the node and hence no detach is ever performed. /sig storage cc

Discussion:
func (detacher *vsphereVMDKDetacher) Detach(volumeName string, nodeName types.NodeName) error { volPath := getVolPathfromVolumeName(volumeName) <del> attached, err := detacher.vsphereVolumes.DiskIsAttached(volPath, nodeName) </del> <ins> attached, newVolumePath, err := detacher.vsphereVolumes.DiskIsAttached(volPath, nodeName) </ins> if err != nil { // Log error and continue with detach klog.Errorf(

attachdetachMutex.LockKey(string(nodeName)) defer attachdetachMutex.UnlockKey(string(nodeName)) <del> if err := detacher.vsphereVolumes.DetachDisk(volPath, nodeName); err != nil { </del> <ins> if err := detacher.vsphereVolumes.DetachDisk(newVolumePath, nodeName); err != nil { </ins> klog.Errorf("Error detaching volume %q: %v", volPath, err) return err }

"errors" "testing" <del> "k8s.io/api/core/v1" </del> <ins> v1 "k8s.io/api/core/v1" </ins> "k8s.io/apimachinery/pkg/types" "k8s.io/kubernetes/pkg/volume" volumetest "k8s.io/kubernetes/pkg/volume/testing"

return expected.ret } <del> func (testcase *testcase) DiskIsAttached(diskName string, nodeName types.NodeName) (bool, error) { </del> <ins> func (testcase *testcase) DiskIsAttached(diskName string, nodeName types.NodeName) (bool, string, error) { </ins> expected := &testcase.diskIsAttached if expected.diskName == "" && expected.nodeName == "" { // testcase.diskIsAttached looks uninitialized, test did not expect to // call DiskIsAttached testcase.t.Errorf("Unexpected DiskIsAttached call!") <del> return false, errors.New("Unexpected DiskIsAttached call!") </del> <ins> return false, diskName, errors.New("Unexpected DiskIsAttached call!") </ins> } if expected.diskName != diskName { testcase.t.Errorf("Unexpected DiskIsAttached call: expected diskName %s, got %s", expected.diskName, diskName) <del> return false, errors.New("Unexpected DiskIsAttached call: wrong diskName") </del> <ins> return false, diskName, errors.New("Unexpected DiskIsAttached call: wrong diskName") </ins> } if expected.nodeName != nodeName { testcase.t.Errorf("Unexpected DiskIsAttached call: expected nodeName %s, got %s", expected.nodeName, nodeName) <del> return false, errors.New("Unexpected DiskIsAttached call: wrong nodeName") </del> <ins> return false, diskName, errors.New("Unexpected DiskIsAttached call: wrong nodeName") </ins> } klog.V(4).Infof("DiskIsAttached call: %s, %s, returning %v, %v", diskName, nodeName, expected.isAttached, expected.ret) <del> return expected.isAttached, expected.ret </del> <ins> return expected.isAttached, diskName, expected.ret </ins> } func (testcase *testcase) DisksAreAttached(nodeVolumes map[types.NodeName][]string) (map[types.NodeName]map[string]bool, error) {

// DetachDisk detaches the disk specified by vmDiskPath func (vm *VirtualMachine) DetachDisk(ctx context.Context, vmDiskPath string) error { <del> vmDiskPath = RemoveStorageClusterORFolderNameFromVDiskPath(vmDiskPath) </del> device, err := vm.getVirtualDeviceByPath(ctx, vmDiskPath) if err != nil { klog.Errorf("Disk ID not found for VM: %q with diskPath: %q", vm.InventoryPath, vmDiskPath)

// DiskIsAttached checks if a disk is attached to the given node. // Assumption: If node doesn't exist, disk is not attached to the node. <del> DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, error) </del> <ins> DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, string, error) </ins> // DisksAreAttached checks if a list disks are attached to the given node. // Assumption: If node doesn't exist, disks are not attached to the node.

return "", err } <ins> // try and get canonical path for disk and if we can't throw error vmDiskPath, err = getcanonicalVolumePath(ctx, vm.Datacenter, vmDiskPath) if err != nil { klog.Errorf("failed to get canonical path for %s on node %s: %v", vmDiskPath, convertToString(nodeName), err) return "", err } </ins> diskUUID, err = vm.AttachDisk(ctx, vmDiskPath, &vclib.VolumeOptions{SCSIControllerType: vclib.PVSCSIControllerType, StoragePolicyName: storagePolicyName}) if err != nil { klog.Errorf("Failed to attach disk: %s for node: %s. err: +%v", vmDiskPath, convertToString(nodeName), err)

} // DiskIsAttached returns if disk is attached to the VM using controllers supported by the plugin. <del> func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, error) { diskIsAttachedInternal := func(volPath string, nodeName k8stypes.NodeName) (bool, error) { </del> <ins> func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, string, error) { diskIsAttachedInternal := func(volPath string, nodeName k8stypes.NodeName) (bool, string, error) { </ins> var vSphereInstance string if nodeName == "" { vSphereInstance = vs.hostName

defer cancel() vsi, err := vs.getVSphereInstance(nodeName) if err != nil { <del> return false, err </del> <ins> return false, volPath, err </ins> } // Ensure client is logged in and session is valid err = vs.nodeManager.vcConnect(ctx, vsi) if err != nil { <del> return false, err </del> <ins> return false, volPath, err </ins> } vm, err := vs.getVMFromNodeName(ctx, nodeName) if err != nil { if err == vclib.ErrNoVMFound { klog.Warningf("Node %q does not exist, vsphere CP will assume disk %v is not attached to it.", nodeName, volPath) // make the disk as detached and return false without error. <del> return false, nil </del> <ins> return false, volPath, nil </ins> } klog.Errorf("Failed to get VM object for node: %q. err: +%v", vSphereInstance, err) <del> return false, err </del> <ins> return false, volPath, err </ins> } volPath = vclib.RemoveStorageClusterORFolderNameFromVDiskPath(volPath) <ins> canonicalPath, pathFetchErr := getcanonicalVolumePath(ctx, vm.Datacenter, volPath) // if canonicalPath is not empty string and pathFetchErr is nil then we can use canonical path to perform detach if canonicalPath != "" && pathFetchErr == nil { volPath = canonicalPath } </ins> attached, err := vm.IsDiskAttached(ctx, volPath) if err != nil { klog.Errorf("DiskIsAttached failed to determine whether disk %q is still attached on node %q",

vSphereInstance) } klog.V(4).Infof("DiskIsAttached result: %v and error: %q, for volume: %q", attached, err, volPath) <del> return attached, err </del> <ins> return attached, volPath, err </ins> } requestTime := time.Now() <del> isAttached, err := diskIsAttachedInternal(volPath, nodeName) </del> <ins> isAttached, newVolumePath, err := diskIsAttachedInternal(volPath, nodeName) </ins> if err != nil { if vclib.IsManagedObjectNotFoundError(err) { err = vs.nodeManager.RediscoverNode(nodeName) if err == vclib.ErrNoVMFound { isAttached, err = false, nil } else if err == nil { <del> isAttached, err = diskIsAttachedInternal(volPath, nodeName) </del> <ins> isAttached, newVolumePath, err = diskIsAttachedInternal(volPath, nodeName) </ins> } } } vclib.RecordvSphereMetric(vclib.OperationDiskIsAttached, requestTime, err) <del> return isAttached, err </del> <ins> return isAttached, newVolumePath, err </ins> } // DisksAreAttached returns if disks are attached to the VM using controllers supported by the plugin.