Add WithProcessKill DeleteOpt

Add an option that allows users for force kill and delete a process/task
when calling `Delete`

Fixes #1274

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-08-07 16:10:43 -04:00
parent d4be9822ed
commit d8c075aadc
4 changed files with 181 additions and 3 deletions

View File

@ -1132,3 +1132,133 @@ func TestWaitStoppedProcess(t *testing.T) {
}
<-finished
}
func TestTaskForceDelete(t *testing.T) {
client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
var (
image Image
ctx, cancel = testContext()
id = t.Name()
)
defer cancel()
if runtime.GOOS != "windows" {
image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Error(err)
return
}
}
spec, err := generateSpec(withImageConfig(ctx, image), withProcessArgs("sleep", "30"))
if err != nil {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, WithSpec(spec), withNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
}
defer container.Delete(ctx, WithSnapshotCleanup)
task, err := container.NewTask(ctx, Stdio)
if err != nil {
t.Error(err)
return
}
if err := task.Start(ctx); err != nil {
t.Error(err)
return
}
if _, err := task.Delete(ctx); err == nil {
t.Error("task.Delete of a running task should create an error")
}
if _, err := task.Delete(ctx, WithProcessKill); err != nil {
t.Error(err)
return
}
}
func TestProcessForceDelete(t *testing.T) {
client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
var (
image Image
ctx, cancel = testContext()
id = t.Name()
)
defer cancel()
if runtime.GOOS != "windows" {
image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Error(err)
return
}
}
spec, err := generateSpec(withImageConfig(ctx, image), withProcessArgs("sleep", "30"))
if err != nil {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, WithSpec(spec), withNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
}
defer container.Delete(ctx, WithSnapshotCleanup)
task, err := container.NewTask(ctx, Stdio)
if err != nil {
t.Error(err)
return
}
defer task.Delete(ctx)
statusC := make(chan uint32, 1)
go func() {
status, err := task.Wait(ctx)
if err != nil {
t.Error(err)
}
statusC <- status
}()
// task must be started on windows
if err := task.Start(ctx); err != nil {
t.Error(err)
return
}
processSpec := spec.Process
withExecArgs(processSpec, "sleep", "20")
execID := t.Name() + "_exec"
process, err := task.Exec(ctx, execID, processSpec, empty())
if err != nil {
t.Error(err)
return
}
if err := process.Start(ctx); err != nil {
t.Error(err)
return
}
if _, err := process.Delete(ctx); err == nil {
t.Error("process.Delete should return an error when process is running")
}
if _, err := process.Delete(ctx, WithProcessKill); err != nil {
t.Error(err)
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
t.Error(err)
return
}
<-statusC
}

View File

@ -11,6 +11,7 @@ import (
"github.com/containerd/containerd/runtime"
"github.com/containerd/containerd/typeurl"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
// Process represents a system process
@ -20,7 +21,7 @@ type Process interface {
// Start starts the process executing the user's defined binary
Start(context.Context) error
// Delete removes the process and any resources allocated returning the exit status
Delete(context.Context) (uint32, error)
Delete(context.Context, ...ProcessDeleteOpts) (uint32, error)
// Kill sends the provided signal to the process
Kill(context.Context, syscall.Signal) error
// Wait blocks until the process has exited returning the exit status
@ -141,7 +142,19 @@ func (p *process) Resize(ctx context.Context, w, h uint32) error {
return err
}
func (p *process) Delete(ctx context.Context) (uint32, error) {
func (p *process) Delete(ctx context.Context, opts ...ProcessDeleteOpts) (uint32, error) {
for _, o := range opts {
if err := o(ctx, p); err != nil {
return UnknownExitStatus, err
}
}
status, err := p.Status(ctx)
if err != nil {
return UnknownExitStatus, err
}
if status.Status != Stopped {
return UnknownExitStatus, errors.Wrapf(errdefs.ErrFailedPrecondition, "process must be stopped before deletion")
}
if p.io != nil {
p.io.Wait()
p.io.Close()

18
task.go
View File

@ -54,6 +54,8 @@ const (
// Pausing indicates that the process is currently switching from a
// running state into a paused state
Pausing ProcessStatus = "pausing"
// Unknown indicates that we could not determine the status from the runtime
Unknown ProcessStatus = "unknown"
)
// IOCloseInfo allows specific io pipes to be closed on a process
@ -236,7 +238,21 @@ func (t *task) Wait(ctx context.Context) (uint32, error) {
// Delete deletes the task and its runtime state
// it returns the exit status of the task and any errors that were encountered
// during cleanup
func (t *task) Delete(ctx context.Context) (uint32, error) {
func (t *task) Delete(ctx context.Context, opts ...ProcessDeleteOpts) (uint32, error) {
for _, o := range opts {
if err := o(ctx, t); err != nil {
return UnknownExitStatus, err
}
}
status, err := t.Status(ctx)
if err != nil && errdefs.IsNotFound(err) {
return UnknownExitStatus, err
}
switch status.Status {
case Stopped, Unknown, "":
default:
return UnknownExitStatus, errors.Wrapf(errdefs.ErrFailedPrecondition, "task must be stopped before deletion: %s", status.Status)
}
if t.io != nil {
t.io.Cancel()
t.io.Wait()

View File

@ -2,6 +2,7 @@ package containerd
import (
"context"
"syscall"
"github.com/containerd/containerd/linux/runcopts"
"github.com/containerd/containerd/mount"
@ -25,3 +26,21 @@ func WithExit(r *CheckpointTaskInfo) error {
}
return nil
}
// ProcessDeleteOpts allows the caller to set options for the deletion of a task
type ProcessDeleteOpts func(context.Context, Process) error
// WithProcessKill will forcefully kill and delete a process
func WithProcessKill(ctx context.Context, p Process) error {
s := make(chan struct{}, 1)
// ignore errors to wait and kill as we are forcefully killing
// the process and don't care about the exit status
go func() {
p.Wait(ctx)
close(s)
}()
p.Kill(ctx, syscall.SIGKILL)
// wait for the process to fully stop before letting the rest of the deletion complete
<-s
return nil
}