From fa1d6f38388ebf0def8eebe49fa4e40b4f1b487b Mon Sep 17 00:00:00 2001 From: Angus Lees Date: Wed, 21 Dec 2016 11:57:10 +1100 Subject: [PATCH] Forcibly detach an attached volume before attaching elsewhere Fixes #33288 Co-Authored-By: @Rotwang --- .../providers/openstack/openstack_volumes.go | 13 ++++++++----- pkg/volume/cinder/attacher.go | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/cloudprovider/providers/openstack/openstack_volumes.go b/pkg/cloudprovider/providers/openstack/openstack_volumes.go index bde05d38734..1e9b9d2f63f 100644 --- a/pkg/cloudprovider/providers/openstack/openstack_volumes.go +++ b/pkg/cloudprovider/providers/openstack/openstack_volumes.go @@ -52,18 +52,21 @@ func (os *OpenStack) AttachDisk(instanceID string, diskName string) (string, err if instanceID == disk.Attachments[0]["server_id"] { glog.V(4).Infof("Disk: %q is already attached to compute: %q", diskName, instanceID) return disk.ID, nil - } else { - errMsg := fmt.Sprintf("Disk %q is attached to a different compute: %q, should be detached before proceeding", diskName, disk.Attachments[0]["server_id"]) - glog.Errorf(errMsg) - return "", errors.New(errMsg) + } + + glog.V(2).Infof("Disk %q is attached to a different compute (%q), detaching", diskName, disk.Attachments[0]["server_id"]) + err = os.DetachDisk(fmt.Sprintf("%s", disk.Attachments[0]["server_id"]), diskName) + if err != nil { + return "", err } } + // add read only flag here if possible spothanis _, err = volumeattach.Create(cClient, instanceID, &volumeattach.CreateOpts{ VolumeID: disk.ID, }).Extract() if err != nil { - glog.Errorf("Failed to attach %s volume to %s compute", diskName, instanceID) + glog.Errorf("Failed to attach %s volume to %s compute: %v", diskName, instanceID, err) return "", err } glog.V(2).Infof("Successfully attached %s volume to %s compute", diskName, instanceID) diff --git a/pkg/volume/cinder/attacher.go b/pkg/volume/cinder/attacher.go index ee60642a7e2..09ec080fd39 100644 --- a/pkg/volume/cinder/attacher.go +++ b/pkg/volume/cinder/attacher.go @@ -177,7 +177,7 @@ func (attacher *cinderDiskAttacher) WaitForAttach(spec *volume.Spec, devicePath return devicePath, nil } else { // Log an error, and continue checking periodically - glog.Errorf("Error: could not find attached Cinder disk %q: %v", volumeID, err) + glog.Errorf("Error: could not find attached Cinder disk %q (path: %q): %v", volumeID, devicePath, err) } case <-timer.C: return "", fmt.Errorf("Could not find attached Cinder disk %q. Timeout waiting for mount paths to be created.", volumeID)