diff --git a/pkg/cloudprovider/providers/vsphere/vsphere.go b/pkg/cloudprovider/providers/vsphere/vsphere.go index ed9164e8051..14a2628e295 100644 --- a/pkg/cloudprovider/providers/vsphere/vsphere.go +++ b/pkg/cloudprovider/providers/vsphere/vsphere.go @@ -766,7 +766,10 @@ func (vs *VSphere) AttachDisk(vmDiskPath string, nodeName k8stypes.NodeName) (di return "", "", err } - attached, _ := checkDiskAttached(vmDiskPath, vmDevices, dc, vs.client) + attached, err := checkDiskAttached(vmDiskPath, vmDevices, dc, vs.client) + if err != nil { + return "", "", err + } if attached { diskID, _ = getVirtualDiskID(vmDiskPath, vmDevices, dc, vs.client) diskUUID, _ = getVirtualDiskUUIDByPath(vmDiskPath, dc, vs.client) @@ -1006,14 +1009,10 @@ func (vs *VSphere) DisksAreAttached(volPaths []string, nodeName k8stypes.NodeNam defer cancel() // Create vSphere client - attached := make(map[string]bool) - for _, volPath := range volPaths { - attached[volPath] = false - } err := vSphereLogin(ctx, vs) if err != nil { glog.Errorf("Failed to login into vCenter, err: %v", err) - return attached, err + return nil, err } // Find VM to detach disk from @@ -1029,14 +1028,14 @@ func (vs *VSphere) DisksAreAttached(volPaths []string, nodeName k8stypes.NodeNam if err != nil { glog.Errorf("Failed to check whether node exist. err: %s.", err) - return attached, err + return nil, err } if !nodeExist { glog.Errorf("DisksAreAttached failed to determine whether disks %v are still attached: node %q does not exist", volPaths, vSphereInstance) - return attached, fmt.Errorf("DisksAreAttached failed to determine whether disks %v are still attached: node %q does not exist", + return nil, fmt.Errorf("DisksAreAttached failed to determine whether disks %v are still attached: node %q does not exist", volPaths, vSphereInstance) } @@ -1045,17 +1044,23 @@ func (vs *VSphere) DisksAreAttached(volPaths []string, nodeName k8stypes.NodeNam _, vmDevices, dc, err := getVirtualMachineDevices(ctx, vs.cfg, vs.client, vSphereInstance) if err != nil { glog.Errorf("Failed to get VM devices for VM %#q. err: %s", vSphereInstance, err) - return attached, err + return nil, err } + attached := make(map[string]bool) for _, volPath := range volPaths { - result, _ := checkDiskAttached(volPath, vmDevices, dc, vs.client) - if result { - attached[volPath] = true + result, err := checkDiskAttached(volPath, vmDevices, dc, vs.client) + if err == nil { + if result { + attached[volPath] = true + } else { + attached[volPath] = false + } + } else { + return nil, err } } - - return attached, err + return attached, nil } func checkDiskAttached(volPath string, vmdevices object.VirtualDeviceList, dc *object.Datacenter, client *govmomi.Client) (bool, error) { diff --git a/pkg/volume/vsphere_volume/attacher.go b/pkg/volume/vsphere_volume/attacher.go index d60619b0aab..fa1abfca504 100644 --- a/pkg/volume/vsphere_volume/attacher.go +++ b/pkg/volume/vsphere_volume/attacher.go @@ -94,9 +94,7 @@ func (attacher *vsphereVMDKAttacher) VolumesAreAttached(specs []*volume.Spec, no glog.Errorf("Error getting volume (%q) source : %v", spec.Name(), err) continue } - volumePathList = append(volumePathList, volumeSource.VolumePath) - volumesAttachedCheck[spec] = true volumeSpecMap[volumeSource.VolumePath] = spec } attachedResult, err := attacher.vsphereVolumes.DisksAreAttached(volumePathList, nodeName) @@ -104,14 +102,17 @@ func (attacher *vsphereVMDKAttacher) VolumesAreAttached(specs []*volume.Spec, no glog.Errorf( "Error checking if volumes (%v) are attached to current node (%q). err=%v", volumePathList, nodeName, err) - return volumesAttachedCheck, err + return nil, err } for volumePath, attached := range attachedResult { + spec := volumeSpecMap[volumePath] if !attached { - spec := volumeSpecMap[volumePath] volumesAttachedCheck[spec] = false - glog.V(2).Infof("VolumesAreAttached: check volume %q (specName: %q) is no longer attached", volumePath, spec.Name()) + glog.V(2).Infof("VolumesAreAttached: volume %q (specName: %q) is no longer attached", volumePath, spec.Name()) + } else { + volumesAttachedCheck[spec] = true + glog.V(2).Infof("VolumesAreAttached: volume %q (specName: %q) is attached", volumePath, spec.Name()) } } return volumesAttachedCheck, nil