Merge pull request #75 from Random-Liu/kill-with-0-timeout

Kill container directly if timeout is 0.
This commit is contained in:
Lantao Liu 2017-06-13 15:41:01 -07:00 committed by GitHub
commit 7050011faa
2 changed files with 29 additions and 14 deletions

View File

@ -65,6 +65,7 @@ func (c *criContainerdService) StopContainer(ctx context.Context, r *runtime.Sto
return &runtime.StopContainerResponse{}, nil
}
if r.GetTimeout() > 0 {
// TODO(random-liu): [P1] Get stop signal from image config.
stopSignal := unix.SIGTERM
glog.V(2).Infof("Stop container %q with signal %v", id, stopSignal)
@ -81,8 +82,11 @@ func (c *criContainerdService) StopContainer(ctx context.Context, r *runtime.Sto
return &runtime.StopContainerResponse{}, nil
}
glog.Errorf("Stop container %q timed out: %v", id, err)
}
glog.V(2).Infof("Delete container from containerd %q", id)
// Delete sends SIGKILL to the container in the containerd version we are using.
// TODO(random-liu): Replace with `Kill` to avoid race soon.
_, err = c.containerService.Delete(ctx, &execution.DeleteRequest{ID: id})
if err != nil {
if isContainerdContainerNotExistError(err) {

View File

@ -109,6 +109,7 @@ func TestStopContainer(t *testing.T) {
killErr error
deleteErr error
discardEvents int
noTimeout bool
expectErr bool
expectCalls []string
}{
@ -155,6 +156,12 @@ func TestStopContainer(t *testing.T) {
expectErr: true,
expectCalls: []string{"kill"},
},
"should directly kill container if timeout is 0": {
metadata: &testMetadata,
containerdContainer: &testContainer,
noTimeout: true,
expectCalls: []string{"delete", "delete"},
},
"should return error if delete failed": {
metadata: &testMetadata,
containerdContainer: &testContainer,
@ -201,11 +208,15 @@ func TestStopContainer(t *testing.T) {
}
}(eventClient, test.discardEvents)
fake.ClearCalls()
timeout := int64(1)
if test.noTimeout {
timeout = 0
}
// 1 second timeout should be enough for the unit test.
// TODO(random-liu): Use fake clock for this test.
resp, err := c.StopContainer(context.Background(), &runtime.StopContainerRequest{
ContainerId: testID,
Timeout: 1,
Timeout: timeout,
})
if test.expectErr {
assert.Error(t, err)