diff --git a/pkg/cloudprovider/providers/vsphere/vclib/virtualmachine.go b/pkg/cloudprovider/providers/vsphere/vclib/virtualmachine.go index 8077b5583e6..db45b8e1935 100644 --- a/pkg/cloudprovider/providers/vsphere/vclib/virtualmachine.go +++ b/pkg/cloudprovider/providers/vsphere/vclib/virtualmachine.go @@ -19,6 +19,7 @@ package vclib import ( "context" "fmt" + "strings" "time" "github.com/golang/glog" @@ -362,12 +363,14 @@ func (vm *VirtualMachine) getVirtualDeviceByPath(ctx context.Context, diskPath s glog.Errorf("Failed to get the devices for VM: %q. err: %+v", vm.InventoryPath, err) return nil, err } + // filter vm devices to retrieve device for the given vmdk file identified by disk path for _, device := range vmDevices { if vmDevices.TypeName(device) == "VirtualDisk" { virtualDevice := device.GetVirtualDevice() if backing, ok := virtualDevice.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok { - if backing.FileName == diskPath { + if matchVirtualDiskAndVolPath(backing.FileName, diskPath) { + glog.V(LogLevel).Infof("Found VirtualDisk backing with filename %q for diskPath %q", backing.FileName, diskPath) return device, nil } } @@ -376,6 +379,13 @@ func (vm *VirtualMachine) getVirtualDeviceByPath(ctx context.Context, diskPath s return nil, nil } +func matchVirtualDiskAndVolPath(diskPath, volPath string) bool { + fileExt := ".vmdk" + diskPath = strings.TrimSuffix(diskPath, fileExt) + volPath = strings.TrimSuffix(volPath, fileExt) + return diskPath == volPath +} + // deleteController removes latest added SCSI controller from VM. func (vm *VirtualMachine) deleteController(ctx context.Context, controllerDevice types.BaseVirtualDevice, vmDevices object.VirtualDeviceList) error { controllerDeviceList := vmDevices.SelectByType(controllerDevice) diff --git a/pkg/cloudprovider/providers/vsphere/vsphere.go b/pkg/cloudprovider/providers/vsphere/vsphere.go index 6ed9bce8808..94f9b5019c4 100644 --- a/pkg/cloudprovider/providers/vsphere/vsphere.go +++ b/pkg/cloudprovider/providers/vsphere/vsphere.go @@ -835,6 +835,7 @@ func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (b glog.Errorf("Failed to get VM object for node: %q. err: +%v", vSphereInstance, err) return false, err } + volPath = vclib.RemoveStorageClusterORFolderNameFromVDiskPath(volPath) attached, err := vm.IsDiskAttached(ctx, volPath) if err != nil { @@ -842,6 +843,7 @@ func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (b volPath, vSphereInstance) } + glog.V(4).Infof("DiskIsAttached result: %q and error: %q, for volume: %q", attached, err, volPath) return attached, err } requestTime := time.Now()