checking if disk is already attached for photon.

This commit is contained in:
Yassine TIJANI 2017-08-02 15:27:56 +02:00
parent e9617b694e
commit bc3ef455dd
2 changed files with 21 additions and 13 deletions

View File

@ -67,14 +67,21 @@ func (attacher *photonPersistentDiskAttacher) Attach(spec *volume.Spec, nodeName
glog.Errorf("Photon Controller attacher: Attach failed to get volume source") glog.Errorf("Photon Controller attacher: Attach failed to get volume source")
return "", err return "", err
} }
attached, err := attacher.photonDisks.DiskIsAttached(volumeSource.PdID, nodeName)
glog.V(4).Infof("Photon Controller: Attach disk called for host %s", hostName)
// TODO: if disk is already attached?
err = attacher.photonDisks.AttachDisk(volumeSource.PdID, nodeName)
if err != nil { if err != nil {
glog.Errorf("Error attaching volume %q to node %q: %+v", volumeSource.PdID, nodeName, err) glog.Warningf("Photon Controller: couldn't check if disk is Attached for host %s, will try attach disk: %+v", hostName, err)
return "", err attached = false
}
if !attached {
glog.V(4).Infof("Photon Controller: Attach disk called for host %s", hostName)
err = attacher.photonDisks.AttachDisk(volumeSource.PdID, nodeName)
if err != nil {
glog.Errorf("Error attaching volume %q to node %q: %+v", volumeSource.PdID, nodeName, err)
return "", err
}
} }
PdidWithNoHypens := strings.Replace(volumeSource.PdID, "-", "", -1) PdidWithNoHypens := strings.Replace(volumeSource.PdID, "-", "", -1)

View File

@ -78,14 +78,14 @@ func TestAttachDetach(t *testing.T) {
nodeName := types.NodeName("instance") nodeName := types.NodeName("instance")
readOnly := false readOnly := false
spec := createVolSpec(diskName, readOnly) spec := createVolSpec(diskName, readOnly)
attachError := errors.New("Fake attach error")
detachError := errors.New("Fake detach error") detachError := errors.New("Fake detach error")
diskCheckError := errors.New("Fake DiskIsAttached error") diskCheckError := errors.New("Fake DiskIsAttached error")
tests := []testcase{ tests := []testcase{
// Successful Attach call // Successful Attach call
{ {
name: "Attach_Positive", name: "Attach_Positive",
attach: attachCall{diskName, nodeName, nil}, diskIsAttached: diskIsAttachedCall{diskName, nodeName, false, diskCheckError},
attach: attachCall{diskName, nodeName, nil},
test: func(testcase *testcase) (string, error) { test: func(testcase *testcase) (string, error) {
attacher := newAttacher(testcase) attacher := newAttacher(testcase)
return attacher.Attach(spec, nodeName) return attacher.Attach(spec, nodeName)
@ -93,15 +93,16 @@ func TestAttachDetach(t *testing.T) {
expectedDevice: "/dev/disk/by-id/wwn-0x000000000", expectedDevice: "/dev/disk/by-id/wwn-0x000000000",
}, },
// Attach call fails // Disk is already attached
{ {
name: "Attach_Negative", name: "Attach_Positive_AlreadyAttached",
attach: attachCall{diskName, nodeName, attachError}, diskIsAttached: diskIsAttachedCall{diskName, nodeName, false, diskCheckError},
attach: attachCall{diskName, nodeName, nil},
test: func(testcase *testcase) (string, error) { test: func(testcase *testcase) (string, error) {
attacher := newAttacher(testcase) attacher := newAttacher(testcase)
return attacher.Attach(spec, nodeName) return attacher.Attach(spec, nodeName)
}, },
expectedError: attachError, expectedDevice: "/dev/disk/by-id/wwn-0x000000000",
}, },
// Detach succeeds // Detach succeeds