Modify Attach method to return device path
This commit is contained in:
@@ -41,38 +41,28 @@ func (plugin *awsElasticBlockStorePlugin) NewAttacher() (volume.Attacher, error)
|
|||||||
return &awsElasticBlockStoreAttacher{host: plugin.host}, nil
|
return &awsElasticBlockStoreAttacher{host: plugin.host}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (attacher *awsElasticBlockStoreAttacher) Attach(spec *volume.Spec, hostName string) error {
|
func (attacher *awsElasticBlockStoreAttacher) Attach(spec *volume.Spec, hostName string) (string, error) {
|
||||||
volumeSource, readOnly, err := getVolumeSource(spec)
|
volumeSource, readOnly, err := getVolumeSource(spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
volumeID := volumeSource.VolumeID
|
volumeID := volumeSource.VolumeID
|
||||||
|
|
||||||
awsCloud, err := getCloudProvider(attacher.host.GetCloudProvider())
|
awsCloud, err := getCloudProvider(attacher.host.GetCloudProvider())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
attached, err := awsCloud.DiskIsAttached(volumeID, hostName)
|
// awsCloud.AttachDisk checks if disk is already attached to node and
|
||||||
|
// succeeds in that case, so no need to do that separately.
|
||||||
|
devicePath, err := awsCloud.AttachDisk(volumeID, hostName, readOnly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Log error and continue with attach
|
|
||||||
glog.Errorf(
|
|
||||||
"Error checking if volume (%q) is already attached to current node (%q). Will continue and try attach anyway. err=%v",
|
|
||||||
volumeID, hostName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil && attached {
|
|
||||||
// Volume is already attached to node.
|
|
||||||
glog.Infof("Attach operation is successful. volume %q is already attached to node %q.", volumeID, hostName)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = awsCloud.AttachDisk(volumeID, hostName, readOnly); err != nil {
|
|
||||||
glog.Errorf("Error attaching volume %q: %+v", volumeID, err)
|
glog.Errorf("Error attaching volume %q: %+v", volumeID, err)
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return devicePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (attacher *awsElasticBlockStoreAttacher) WaitForAttach(spec *volume.Spec, timeout time.Duration) (string, error) {
|
func (attacher *awsElasticBlockStoreAttacher) WaitForAttach(spec *volume.Spec, timeout time.Duration) (string, error) {
|
||||||
|
@@ -45,25 +45,25 @@ func (plugin *cinderPlugin) NewAttacher() (volume.Attacher, error) {
|
|||||||
return &cinderDiskAttacher{host: plugin.host}, nil
|
return &cinderDiskAttacher{host: plugin.host}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) error {
|
func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) (string, error) {
|
||||||
volumeSource, _, err := getVolumeSource(spec)
|
volumeSource, _, err := getVolumeSource(spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
volumeID := volumeSource.VolumeID
|
volumeID := volumeSource.VolumeID
|
||||||
|
|
||||||
cloud, err := getCloudProvider(attacher.host.GetCloudProvider())
|
cloud, err := getCloudProvider(attacher.host.GetCloudProvider())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
instances, res := cloud.Instances()
|
instances, res := cloud.Instances()
|
||||||
if !res {
|
if !res {
|
||||||
return fmt.Errorf("failed to list openstack instances")
|
return "", fmt.Errorf("failed to list openstack instances")
|
||||||
}
|
}
|
||||||
instanceid, err := instances.InstanceID(hostName)
|
instanceid, err := instances.InstanceID(hostName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
if ind := strings.LastIndex(instanceid, "/"); ind >= 0 {
|
if ind := strings.LastIndex(instanceid, "/"); ind >= 0 {
|
||||||
instanceid = instanceid[(ind + 1):]
|
instanceid = instanceid[(ind + 1):]
|
||||||
@@ -79,15 +79,20 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) e
|
|||||||
if err == nil && attached {
|
if err == nil && attached {
|
||||||
// Volume is already attached to node.
|
// Volume is already attached to node.
|
||||||
glog.Infof("Attach operation is successful. volume %q is already attached to node %q.", volumeID, instanceid)
|
glog.Infof("Attach operation is successful. volume %q is already attached to node %q.", volumeID, instanceid)
|
||||||
return nil
|
} else {
|
||||||
|
_, err = cloud.AttachDisk(instanceid, volumeID)
|
||||||
|
if err != nil {
|
||||||
|
glog.Infof("attach volume %q to instance %q gets %v", volumeID, instanceid, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = cloud.AttachDisk(instanceid, volumeID)
|
|
||||||
if err != nil {
|
|
||||||
glog.Infof("attach volume %q to instance %q gets %v", volumeID, instanceid, err)
|
|
||||||
}
|
|
||||||
glog.Infof("attached volume %q to instance %q", volumeID, instanceid)
|
glog.Infof("attached volume %q to instance %q", volumeID, instanceid)
|
||||||
return err
|
devicePath, err := cloud.GetAttachmentDiskPath(instanceid, volumeID)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return devicePath, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (attacher *cinderDiskAttacher) WaitForAttach(spec *volume.Spec, timeout time.Duration) (string, error) {
|
func (attacher *cinderDiskAttacher) WaitForAttach(spec *volume.Spec, timeout time.Duration) (string, error) {
|
||||||
|
@@ -60,10 +60,10 @@ func (plugin *gcePersistentDiskPlugin) NewAttacher() (volume.Attacher, error) {
|
|||||||
// Callers are responsible for retryinging on failure.
|
// Callers are responsible for retryinging on failure.
|
||||||
// Callers are responsible for thread safety between concurrent attach and
|
// Callers are responsible for thread safety between concurrent attach and
|
||||||
// detach operations.
|
// detach operations.
|
||||||
func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName string) error {
|
func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName string) (string, error) {
|
||||||
volumeSource, readOnly, err := getVolumeSource(spec)
|
volumeSource, readOnly, err := getVolumeSource(spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
pdName := volumeSource.PDName
|
pdName := volumeSource.PDName
|
||||||
@@ -79,15 +79,14 @@ func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName st
|
|||||||
if err == nil && attached {
|
if err == nil && attached {
|
||||||
// Volume is already attached to node.
|
// Volume is already attached to node.
|
||||||
glog.Infof("Attach operation is successful. PD %q is already attached to node %q.", pdName, hostName)
|
glog.Infof("Attach operation is successful. PD %q is already attached to node %q.", pdName, hostName)
|
||||||
return nil
|
} else {
|
||||||
|
if err := attacher.gceDisks.AttachDisk(pdName, hostName, readOnly); err != nil {
|
||||||
|
glog.Errorf("Error attaching PD %q to node %q: %+v", pdName, hostName, err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = attacher.gceDisks.AttachDisk(pdName, hostName, readOnly); err != nil {
|
return path.Join(diskByIdPath, diskGooglePrefix+pdName), nil
|
||||||
glog.Errorf("Error attaching PD %q to node %q: %+v", pdName, hostName, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (attacher *gcePersistentDiskAttacher) WaitForAttach(spec *volume.Spec, timeout time.Duration) (string, error) {
|
func (attacher *gcePersistentDiskAttacher) WaitForAttach(spec *volume.Spec, timeout time.Duration) (string, error) {
|
||||||
|
@@ -18,6 +18,7 @@ package gce_pd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
@@ -86,7 +87,11 @@ func TestAttachDetach(t *testing.T) {
|
|||||||
attach: attachCall{diskName, instanceID, readOnly, nil},
|
attach: attachCall{diskName, instanceID, readOnly, nil},
|
||||||
test: func(testcase *testcase) error {
|
test: func(testcase *testcase) error {
|
||||||
attacher := newAttacher(testcase)
|
attacher := newAttacher(testcase)
|
||||||
return attacher.Attach(spec, instanceID)
|
devicePath, err := attacher.Attach(spec, instanceID)
|
||||||
|
if devicePath != "/dev/disk/by-id/google-disk" {
|
||||||
|
return fmt.Errorf("devicePath incorrect. Expected<\"/dev/disk/by-id/google-disk\"> Actual: <%q>", devicePath)
|
||||||
|
}
|
||||||
|
return err
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -96,7 +101,11 @@ func TestAttachDetach(t *testing.T) {
|
|||||||
diskIsAttached: diskIsAttachedCall{diskName, instanceID, true, nil},
|
diskIsAttached: diskIsAttachedCall{diskName, instanceID, true, nil},
|
||||||
test: func(testcase *testcase) error {
|
test: func(testcase *testcase) error {
|
||||||
attacher := newAttacher(testcase)
|
attacher := newAttacher(testcase)
|
||||||
return attacher.Attach(spec, instanceID)
|
devicePath, err := attacher.Attach(spec, instanceID)
|
||||||
|
if devicePath != "/dev/disk/by-id/google-disk" {
|
||||||
|
return fmt.Errorf("devicePath incorrect. Expected<\"/dev/disk/by-id/google-disk\"> Actual: <%q>", devicePath)
|
||||||
|
}
|
||||||
|
return err
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -107,7 +116,11 @@ func TestAttachDetach(t *testing.T) {
|
|||||||
attach: attachCall{diskName, instanceID, readOnly, nil},
|
attach: attachCall{diskName, instanceID, readOnly, nil},
|
||||||
test: func(testcase *testcase) error {
|
test: func(testcase *testcase) error {
|
||||||
attacher := newAttacher(testcase)
|
attacher := newAttacher(testcase)
|
||||||
return attacher.Attach(spec, instanceID)
|
devicePath, err := attacher.Attach(spec, instanceID)
|
||||||
|
if devicePath != "/dev/disk/by-id/google-disk" {
|
||||||
|
return fmt.Errorf("devicePath incorrect. Expected<\"/dev/disk/by-id/google-disk\"> Actual: <%q>", devicePath)
|
||||||
|
}
|
||||||
|
return err
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -118,7 +131,11 @@ func TestAttachDetach(t *testing.T) {
|
|||||||
attach: attachCall{diskName, instanceID, readOnly, attachError},
|
attach: attachCall{diskName, instanceID, readOnly, attachError},
|
||||||
test: func(testcase *testcase) error {
|
test: func(testcase *testcase) error {
|
||||||
attacher := newAttacher(testcase)
|
attacher := newAttacher(testcase)
|
||||||
return attacher.Attach(spec, instanceID)
|
devicePath, err := attacher.Attach(spec, instanceID)
|
||||||
|
if devicePath != "" {
|
||||||
|
return fmt.Errorf("devicePath incorrect. Expected<\"\"> Actual: <%q>", devicePath)
|
||||||
|
}
|
||||||
|
return err
|
||||||
},
|
},
|
||||||
expectedReturn: attachError,
|
expectedReturn: attachError,
|
||||||
},
|
},
|
||||||
|
@@ -358,11 +358,11 @@ func (fv *FakeVolume) TearDownAt(dir string) error {
|
|||||||
return os.RemoveAll(dir)
|
return os.RemoveAll(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fv *FakeVolume) Attach(spec *Spec, hostName string) error {
|
func (fv *FakeVolume) Attach(spec *Spec, hostName string) (string, error) {
|
||||||
fv.Lock()
|
fv.Lock()
|
||||||
defer fv.Unlock()
|
defer fv.Unlock()
|
||||||
fv.AttachCallCount++
|
fv.AttachCallCount++
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fv *FakeVolume) GetAttachCallCount() int {
|
func (fv *FakeVolume) GetAttachCallCount() int {
|
||||||
|
@@ -385,7 +385,7 @@ func (oe *operationExecutor) generateAttachVolumeFunc(
|
|||||||
|
|
||||||
return func() error {
|
return func() error {
|
||||||
// Execute attach
|
// Execute attach
|
||||||
attachErr := volumeAttacher.Attach(
|
_, attachErr := volumeAttacher.Attach(
|
||||||
volumeToAttach.VolumeSpec, volumeToAttach.NodeName)
|
volumeToAttach.VolumeSpec, volumeToAttach.NodeName)
|
||||||
|
|
||||||
if attachErr != nil {
|
if attachErr != nil {
|
||||||
|
@@ -134,8 +134,10 @@ type Deleter interface {
|
|||||||
|
|
||||||
// Attacher can attach a volume to a node.
|
// Attacher can attach a volume to a node.
|
||||||
type Attacher interface {
|
type Attacher interface {
|
||||||
// Attach the volume specified by the given spec to the given host
|
// Attaches the volume specified by the given spec to the given host.
|
||||||
Attach(spec *Spec, hostName string) error
|
// On success, returns the device path where the device was attache don the
|
||||||
|
// node.
|
||||||
|
Attach(spec *Spec, hostName string) (string, error)
|
||||||
|
|
||||||
// WaitForAttach blocks until the device is attached to this
|
// WaitForAttach blocks until the device is attached to this
|
||||||
// node. If it successfully attaches, the path to the device
|
// node. If it successfully attaches, the path to the device
|
||||||
|
Reference in New Issue
Block a user