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

View File

@ -898,6 +898,7 @@ func TestAttacherDetach(t *testing.T) {
volID string volID string
attachID string attachID string
shouldFail bool shouldFail bool
watcherError bool
reactor func(action core.Action) (handled bool, ret runtime.Object, err error) 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", volID: "vol-001", attachID: getAttachmentName("vol-001", testDriver, nodeName)},
@ -916,6 +917,19 @@ func TestAttacherDetach(t *testing.T) {
return false, nil, nil 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 { for _, tc := range testCases {
@ -944,7 +958,14 @@ func TestAttacherDetach(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("test case %s failed: %v", tc.name, err) t.Errorf("test case %s failed: %v", tc.name, err)
} }
watchError := tc.watcherError
csiAttacher.waitSleepTime = 100 * time.Millisecond
go func() { go func() {
if watchError {
errStatus := apierrs.NewInternalError(fmt.Errorf("we got an error")).Status()
fakeWatcher.Error(&errStatus)
return
}
fakeWatcher.Delete(attachment) fakeWatcher.Delete(attachment)
}() }()
err = csiAttacher.Detach(volumeName, types.NodeName(nodeName)) err = csiAttacher.Detach(volumeName, types.NodeName(nodeName))