Merge pull request #79005 from cwdsuzhou/June/remove_recursion_detach

Remove recursion in waitForVolumeDetachmentInternal
This commit is contained in:
Kubernetes Prow Robot 2019-09-10 14:29:01 -07:00 committed by GitHub
commit 494d4cb841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 16 deletions

View File

@ -480,14 +480,9 @@ func (c *csiAttacher) waitForVolumeDetachmentInternal(volumeHandle, attachID str
if err != nil { if err != nil {
return errors.New(log("watch error:%v for volume %v", err, volumeHandle)) return errors.New(log("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:
@ -511,10 +506,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

@ -894,11 +894,12 @@ func TestAttacherDetach(t *testing.T) {
nodeName := "test-node" nodeName := "test-node"
testCases := []struct { testCases := []struct {
name string name string
volID string volID string
attachID string attachID string
shouldFail bool shouldFail bool
reactor func(action core.Action) (handled bool, ret runtime.Object, err error) 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", volID: "vol-001", attachID: getAttachmentName("vol-001", testDriver, nodeName)},
{name: "normal test 2", volID: "vol-002", attachID: getAttachmentName("vol-002", 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 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))