Remove recursion in waitForVolumeDetachmentInternal

This commit is contained in:
caiweidong 2019-08-27 20:09:56 +08:00
parent 71245db133
commit 28dc53f727
2 changed files with 29 additions and 16 deletions

View File

@ -490,14 +490,9 @@ func (c *csiAttacher) waitForVolumeDetachmentInternal(volumeHandle, attachID str
if err != nil {
return fmt.Errorf("watch error:%v for volume %v", err, volumeHandle)
}
var watcherClosed bool
ch := watcher.ResultChan()
defer func() {
if !watcherClosed {
watcher.Stop()
}
}()
ch := watcher.ResultChan()
defer watcher.Stop()
for {
select {
case event, ok := <-ch:
@ -521,10 +516,7 @@ func (c *csiAttacher) waitForVolumeDetachmentInternal(volumeHandle, attachID str
return nil
case watch.Error:
watcher.Stop()
watcherClosed = true
// start another cycle
return c.waitForVolumeDetachmentInternal(volumeHandle, attachID, timer, timeout)
klog.Warningf("waitForVolumeDetachmentInternal received watch error: %v", event)
}
case <-timer.C:

View File

@ -894,11 +894,12 @@ func TestAttacherDetach(t *testing.T) {
nodeName := "test-node"
testCases := []struct {
name string
volID string
attachID string
shouldFail bool
reactor func(action core.Action) (handled bool, ret runtime.Object, err error)
name string
volID string
attachID string
shouldFail bool
watcherError bool
reactor func(action core.Action) (handled bool, ret runtime.Object, err error)
}{
{name: "normal test", volID: "vol-001", attachID: getAttachmentName("vol-001", testDriver, nodeName)},
{name: "normal test 2", volID: "vol-002", attachID: getAttachmentName("vol-002", testDriver, nodeName)},
@ -916,6 +917,19 @@ func TestAttacherDetach(t *testing.T) {
return false, nil, nil
},
},
{
name: "API watch error happen",
volID: "vol-005",
attachID: getAttachmentName("vol-005", testDriver, nodeName),
shouldFail: true,
watcherError: true,
reactor: func(action core.Action) (handled bool, ret runtime.Object, err error) {
if action.Matches("get", "volumeattachments") {
return true, makeTestAttachment(getAttachmentName("vol-005", testDriver, nodeName), nodeName, "vol-005"), nil
}
return false, nil, nil
},
},
}
for _, tc := range testCases {
@ -944,7 +958,14 @@ func TestAttacherDetach(t *testing.T) {
if err != nil {
t.Errorf("test case %s failed: %v", tc.name, err)
}
watchError := tc.watcherError
csiAttacher.waitSleepTime = 100 * time.Millisecond
go func() {
if watchError {
errStatus := apierrs.NewInternalError(fmt.Errorf("we got an error")).Status()
fakeWatcher.Error(&errStatus)
return
}
fakeWatcher.Delete(attachment)
}()
err = csiAttacher.Detach(volumeName, types.NodeName(nodeName))