Skip to content

Commit e799c85

Browse files
authored
Merge pull request kubernetes#95447 from gnufied/fix-disk-detach-failure
Fix vsphere disk detach failure
2 parents da777a6 + 5627771 commit e799c85

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

pkg/volume/vsphere_volume/attacher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (plugin *vsphereVolumePlugin) NewDeviceUnmounter() (volume.DeviceUnmounter,
277277
func (detacher *vsphereVMDKDetacher) Detach(volumeName string, nodeName types.NodeName) error {
278278

279279
volPath := getVolPathfromVolumeName(volumeName)
280-
attached, err := detacher.vsphereVolumes.DiskIsAttached(volPath, nodeName)
280+
attached, newVolumePath, err := detacher.vsphereVolumes.DiskIsAttached(volPath, nodeName)
281281
if err != nil {
282282
// Log error and continue with detach
283283
klog.Errorf(
@@ -293,7 +293,7 @@ func (detacher *vsphereVMDKDetacher) Detach(volumeName string, nodeName types.No
293293

294294
attachdetachMutex.LockKey(string(nodeName))
295295
defer attachdetachMutex.UnlockKey(string(nodeName))
296-
if err := detacher.vsphereVolumes.DetachDisk(volPath, nodeName); err != nil {
296+
if err := detacher.vsphereVolumes.DetachDisk(newVolumePath, nodeName); err != nil {
297297
klog.Errorf("Error detaching volume %q: %v", volPath, err)
298298
return err
299299
}

pkg/volume/vsphere_volume/attacher_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"errors"
2323
"testing"
2424

25-
"k8s.io/api/core/v1"
25+
v1 "k8s.io/api/core/v1"
2626
"k8s.io/apimachinery/pkg/types"
2727
"k8s.io/kubernetes/pkg/volume"
2828
volumetest "k8s.io/kubernetes/pkg/volume/testing"
@@ -285,29 +285,29 @@ func (testcase *testcase) DetachDisk(diskName string, nodeName types.NodeName) e
285285
return expected.ret
286286
}
287287

288-
func (testcase *testcase) DiskIsAttached(diskName string, nodeName types.NodeName) (bool, error) {
288+
func (testcase *testcase) DiskIsAttached(diskName string, nodeName types.NodeName) (bool, string, error) {
289289
expected := &testcase.diskIsAttached
290290

291291
if expected.diskName == "" && expected.nodeName == "" {
292292
// testcase.diskIsAttached looks uninitialized, test did not expect to
293293
// call DiskIsAttached
294294
testcase.t.Errorf("Unexpected DiskIsAttached call!")
295-
return false, errors.New("Unexpected DiskIsAttached call!")
295+
return false, diskName, errors.New("Unexpected DiskIsAttached call!")
296296
}
297297

298298
if expected.diskName != diskName {
299299
testcase.t.Errorf("Unexpected DiskIsAttached call: expected diskName %s, got %s", expected.diskName, diskName)
300-
return false, errors.New("Unexpected DiskIsAttached call: wrong diskName")
300+
return false, diskName, errors.New("Unexpected DiskIsAttached call: wrong diskName")
301301
}
302302

303303
if expected.nodeName != nodeName {
304304
testcase.t.Errorf("Unexpected DiskIsAttached call: expected nodeName %s, got %s", expected.nodeName, nodeName)
305-
return false, errors.New("Unexpected DiskIsAttached call: wrong nodeName")
305+
return false, diskName, errors.New("Unexpected DiskIsAttached call: wrong nodeName")
306306
}
307307

308308
klog.V(4).Infof("DiskIsAttached call: %s, %s, returning %v, %v", diskName, nodeName, expected.isAttached, expected.ret)
309309

310-
return expected.isAttached, expected.ret
310+
return expected.isAttached, diskName, expected.ret
311311
}
312312

313313
func (testcase *testcase) DisksAreAttached(nodeVolumes map[types.NodeName][]string) (map[types.NodeName]map[string]bool, error) {

staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/virtualmachine.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ func (vm *VirtualMachine) AttachDisk(ctx context.Context, vmDiskPath string, vol
168168

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

staging/src/k8s.io/legacy-cloud-providers/vsphere/vsphere.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ type Volumes interface {
221221

222222
// DiskIsAttached checks if a disk is attached to the given node.
223223
// Assumption: If node doesn't exist, disk is not attached to the node.
224-
DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, error)
224+
DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, string, error)
225225

226226
// DisksAreAttached checks if a list disks are attached to the given node.
227227
// Assumption: If node doesn't exist, disks are not attached to the node.
@@ -924,6 +924,13 @@ func (vs *VSphere) AttachDisk(vmDiskPath string, storagePolicyName string, nodeN
924924
return "", err
925925
}
926926

927+
// try and get canonical path for disk and if we can't throw error
928+
vmDiskPath, err = getcanonicalVolumePath(ctx, vm.Datacenter, vmDiskPath)
929+
if err != nil {
930+
klog.Errorf("failed to get canonical path for %s on node %s: %v", vmDiskPath, convertToString(nodeName), err)
931+
return "", err
932+
}
933+
927934
diskUUID, err = vm.AttachDisk(ctx, vmDiskPath, &vclib.VolumeOptions{SCSIControllerType: vclib.PVSCSIControllerType, StoragePolicyName: storagePolicyName})
928935
if err != nil {
929936
klog.Errorf("Failed to attach disk: %s for node: %s. err: +%v", vmDiskPath, convertToString(nodeName), err)
@@ -1004,8 +1011,8 @@ func (vs *VSphere) DetachDisk(volPath string, nodeName k8stypes.NodeName) error
10041011
}
10051012

10061013
// DiskIsAttached returns if disk is attached to the VM using controllers supported by the plugin.
1007-
func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, error) {
1008-
diskIsAttachedInternal := func(volPath string, nodeName k8stypes.NodeName) (bool, error) {
1014+
func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (bool, string, error) {
1015+
diskIsAttachedInternal := func(volPath string, nodeName k8stypes.NodeName) (bool, string, error) {
10091016
var vSphereInstance string
10101017
if nodeName == "" {
10111018
vSphereInstance = vs.hostName
@@ -1018,48 +1025,53 @@ func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (b
10181025
defer cancel()
10191026
vsi, err := vs.getVSphereInstance(nodeName)
10201027
if err != nil {
1021-
return false, err
1028+
return false, volPath, err
10221029
}
10231030
// Ensure client is logged in and session is valid
10241031
err = vs.nodeManager.vcConnect(ctx, vsi)
10251032
if err != nil {
1026-
return false, err
1033+
return false, volPath, err
10271034
}
10281035
vm, err := vs.getVMFromNodeName(ctx, nodeName)
10291036
if err != nil {
10301037
if err == vclib.ErrNoVMFound {
10311038
klog.Warningf("Node %q does not exist, vsphere CP will assume disk %v is not attached to it.", nodeName, volPath)
10321039
// make the disk as detached and return false without error.
1033-
return false, nil
1040+
return false, volPath, nil
10341041
}
10351042
klog.Errorf("Failed to get VM object for node: %q. err: +%v", vSphereInstance, err)
1036-
return false, err
1043+
return false, volPath, err
10371044
}
10381045

10391046
volPath = vclib.RemoveStorageClusterORFolderNameFromVDiskPath(volPath)
1047+
canonicalPath, pathFetchErr := getcanonicalVolumePath(ctx, vm.Datacenter, volPath)
1048+
// if canonicalPath is not empty string and pathFetchErr is nil then we can use canonical path to perform detach
1049+
if canonicalPath != "" && pathFetchErr == nil {
1050+
volPath = canonicalPath
1051+
}
10401052
attached, err := vm.IsDiskAttached(ctx, volPath)
10411053
if err != nil {
10421054
klog.Errorf("DiskIsAttached failed to determine whether disk %q is still attached on node %q",
10431055
volPath,
10441056
vSphereInstance)
10451057
}
10461058
klog.V(4).Infof("DiskIsAttached result: %v and error: %q, for volume: %q", attached, err, volPath)
1047-
return attached, err
1059+
return attached, volPath, err
10481060
}
10491061
requestTime := time.Now()
1050-
isAttached, err := diskIsAttachedInternal(volPath, nodeName)
1062+
isAttached, newVolumePath, err := diskIsAttachedInternal(volPath, nodeName)
10511063
if err != nil {
10521064
if vclib.IsManagedObjectNotFoundError(err) {
10531065
err = vs.nodeManager.RediscoverNode(nodeName)
10541066
if err == vclib.ErrNoVMFound {
10551067
isAttached, err = false, nil
10561068
} else if err == nil {
1057-
isAttached, err = diskIsAttachedInternal(volPath, nodeName)
1069+
isAttached, newVolumePath, err = diskIsAttachedInternal(volPath, nodeName)
10581070
}
10591071
}
10601072
}
10611073
vclib.RecordvSphereMetric(vclib.OperationDiskIsAttached, requestTime, err)
1062-
return isAttached, err
1074+
return isAttached, newVolumePath, err
10631075
}
10641076

10651077
// DisksAreAttached returns if disks are attached to the VM using controllers supported by the plugin.

0 commit comments

Comments
 (0)