@@ -221,7 +221,7 @@ type Volumes interface {
221
221
222
222
// DiskIsAttached checks if a disk is attached to the given node.
223
223
// 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 )
225
225
226
226
// DisksAreAttached checks if a list disks are attached to the given node.
227
227
// 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
924
924
return "" , err
925
925
}
926
926
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
+
927
934
diskUUID , err = vm .AttachDisk (ctx , vmDiskPath , & vclib.VolumeOptions {SCSIControllerType : vclib .PVSCSIControllerType , StoragePolicyName : storagePolicyName })
928
935
if err != nil {
929
936
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
1004
1011
}
1005
1012
1006
1013
// 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 ) {
1009
1016
var vSphereInstance string
1010
1017
if nodeName == "" {
1011
1018
vSphereInstance = vs .hostName
@@ -1018,48 +1025,53 @@ func (vs *VSphere) DiskIsAttached(volPath string, nodeName k8stypes.NodeName) (b
1018
1025
defer cancel ()
1019
1026
vsi , err := vs .getVSphereInstance (nodeName )
1020
1027
if err != nil {
1021
- return false , err
1028
+ return false , volPath , err
1022
1029
}
1023
1030
// Ensure client is logged in and session is valid
1024
1031
err = vs .nodeManager .vcConnect (ctx , vsi )
1025
1032
if err != nil {
1026
- return false , err
1033
+ return false , volPath , err
1027
1034
}
1028
1035
vm , err := vs .getVMFromNodeName (ctx , nodeName )
1029
1036
if err != nil {
1030
1037
if err == vclib .ErrNoVMFound {
1031
1038
klog .Warningf ("Node %q does not exist, vsphere CP will assume disk %v is not attached to it." , nodeName , volPath )
1032
1039
// make the disk as detached and return false without error.
1033
- return false , nil
1040
+ return false , volPath , nil
1034
1041
}
1035
1042
klog .Errorf ("Failed to get VM object for node: %q. err: +%v" , vSphereInstance , err )
1036
- return false , err
1043
+ return false , volPath , err
1037
1044
}
1038
1045
1039
1046
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
+ }
1040
1052
attached , err := vm .IsDiskAttached (ctx , volPath )
1041
1053
if err != nil {
1042
1054
klog .Errorf ("DiskIsAttached failed to determine whether disk %q is still attached on node %q" ,
1043
1055
volPath ,
1044
1056
vSphereInstance )
1045
1057
}
1046
1058
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
1048
1060
}
1049
1061
requestTime := time .Now ()
1050
- isAttached , err := diskIsAttachedInternal (volPath , nodeName )
1062
+ isAttached , newVolumePath , err := diskIsAttachedInternal (volPath , nodeName )
1051
1063
if err != nil {
1052
1064
if vclib .IsManagedObjectNotFoundError (err ) {
1053
1065
err = vs .nodeManager .RediscoverNode (nodeName )
1054
1066
if err == vclib .ErrNoVMFound {
1055
1067
isAttached , err = false , nil
1056
1068
} else if err == nil {
1057
- isAttached , err = diskIsAttachedInternal (volPath , nodeName )
1069
+ isAttached , newVolumePath , err = diskIsAttachedInternal (volPath , nodeName )
1058
1070
}
1059
1071
}
1060
1072
}
1061
1073
vclib .RecordvSphereMetric (vclib .OperationDiskIsAttached , requestTime , err )
1062
- return isAttached , err
1074
+ return isAttached , newVolumePath , err
1063
1075
}
1064
1076
1065
1077
// DisksAreAttached returns if disks are attached to the VM using controllers supported by the plugin.
0 commit comments