Kill container directly if timeout is 0.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2017-06-13 18:44:46 +00:00
parent 479e8c3045
commit d381cfa831
2 changed files with 29 additions and 14 deletions

View File

@ -65,24 +65,28 @@ func (c *criContainerdService) StopContainer(ctx context.Context, r *runtime.Sto
return &runtime.StopContainerResponse{}, nil return &runtime.StopContainerResponse{}, nil
} }
// TODO(random-liu): [P1] Get stop signal from image config. if r.GetTimeout() > 0 {
stopSignal := unix.SIGTERM // TODO(random-liu): [P1] Get stop signal from image config.
glog.V(2).Infof("Stop container %q with signal %v", id, stopSignal) stopSignal := unix.SIGTERM
_, err = c.containerService.Kill(ctx, &execution.KillRequest{ID: id, Signal: uint32(stopSignal)}) glog.V(2).Infof("Stop container %q with signal %v", id, stopSignal)
if err != nil { _, err = c.containerService.Kill(ctx, &execution.KillRequest{ID: id, Signal: uint32(stopSignal)})
if isContainerdContainerNotExistError(err) { if err != nil {
if isContainerdContainerNotExistError(err) {
return &runtime.StopContainerResponse{}, nil
}
return nil, fmt.Errorf("failed to stop container %q: %v", id, err)
}
err = c.waitContainerStop(ctx, id, time.Duration(r.GetTimeout())*time.Second)
if err == nil {
return &runtime.StopContainerResponse{}, nil return &runtime.StopContainerResponse{}, nil
} }
return nil, fmt.Errorf("failed to stop container %q: %v", id, err) glog.Errorf("Stop container %q timed out: %v", id, err)
} }
err = c.waitContainerStop(ctx, id, time.Duration(r.GetTimeout())*time.Second)
if err == nil {
return &runtime.StopContainerResponse{}, nil
}
glog.Errorf("Stop container %q timed out: %v", id, err)
glog.V(2).Infof("Delete container from containerd %q", id) 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}) _, err = c.containerService.Delete(ctx, &execution.DeleteRequest{ID: id})
if err != nil { if err != nil {
if isContainerdContainerNotExistError(err) { if isContainerdContainerNotExistError(err) {

View File

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