Merge pull request #1434 from crosbymichael/kill-all

Add KillOpts for killing all processes
This commit is contained in:
Kenfe-Mickaël Laventure 2017-08-28 11:07:12 -07:00 committed by GitHub
commit 31b5bb9107
5 changed files with 97 additions and 4 deletions

View File

@ -21,7 +21,7 @@ type resizer interface {
} }
type killer interface { type killer interface {
Kill(gocontext.Context, syscall.Signal) error Kill(gocontext.Context, syscall.Signal, ...containerd.KillOpts) error
} }
func withEnv(context *cli.Context) containerd.SpecOpts { func withEnv(context *cli.Context) containerd.SpecOpts {

View File

@ -647,3 +647,68 @@ func TestContainerUserID(t *testing.T) {
t.Errorf("expected uid:gid to be 3:4, but received %q", output) t.Errorf("expected uid:gid to be 3:4, but received %q", output)
} }
} }
func TestContainerKillAll(t *testing.T) {
t.Parallel()
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()
image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id,
withNewSnapshot(id, image),
WithNewSpec(withImageConfig(image),
withProcessArgs("sh", "-c", "top"),
WithHostNamespace(specs.PIDNamespace),
),
)
if err != nil {
t.Error(err)
return
}
defer container.Delete(ctx, WithSnapshotCleanup)
stdout := bytes.NewBuffer(nil)
task, err := container.NewTask(ctx, NewIO(bytes.NewBuffer(nil), stdout, bytes.NewBuffer(nil)))
if err != nil {
t.Error(err)
return
}
defer task.Delete(ctx)
statusC, err := task.Wait(ctx)
if err != nil {
t.Error(err)
return
}
if err := task.Start(ctx); err != nil {
t.Error(err)
return
}
if err := task.Kill(ctx, syscall.SIGKILL, WithKillAll); err != nil {
t.Error(err)
}
<-statusC
if _, err := task.Delete(ctx); err != nil {
t.Error(err)
return
}
}

View File

@ -23,7 +23,7 @@ type Process interface {
// Delete removes the process and any resources allocated returning the exit status // Delete removes the process and any resources allocated returning the exit status
Delete(context.Context, ...ProcessDeleteOpts) (*ExitStatus, error) Delete(context.Context, ...ProcessDeleteOpts) (*ExitStatus, error)
// Kill sends the provided signal to the process // Kill sends the provided signal to the process
Kill(context.Context, syscall.Signal) error Kill(context.Context, syscall.Signal, ...KillOpts) error
// Wait asynchronously waits for the process to exit, and sends the exit code to the returned channel // Wait asynchronously waits for the process to exit, and sends the exit code to the returned channel
Wait(context.Context) (<-chan ExitStatus, error) Wait(context.Context) (<-chan ExitStatus, error)
// CloseIO allows various pipes to be closed on the process // CloseIO allows various pipes to be closed on the process
@ -104,11 +104,18 @@ func (p *process) Start(ctx context.Context) error {
return nil return nil
} }
func (p *process) Kill(ctx context.Context, s syscall.Signal) error { func (p *process) Kill(ctx context.Context, s syscall.Signal, opts ...KillOpts) error {
var i KillInfo
for _, o := range opts {
if err := o(ctx, p, &i); err != nil {
return err
}
}
_, err := p.task.client.TaskService().Kill(ctx, &tasks.KillRequest{ _, err := p.task.client.TaskService().Kill(ctx, &tasks.KillRequest{
Signal: uint32(s), Signal: uint32(s),
ContainerID: p.task.id, ContainerID: p.task.id,
ExecID: p.id, ExecID: p.id,
All: i.All,
}) })
return errdefs.FromGRPC(err) return errdefs.FromGRPC(err)
} }

View File

@ -163,10 +163,17 @@ func (t *task) Start(ctx context.Context) error {
return errdefs.FromGRPC(err) return errdefs.FromGRPC(err)
} }
func (t *task) Kill(ctx context.Context, s syscall.Signal) error { func (t *task) Kill(ctx context.Context, s syscall.Signal, opts ...KillOpts) error {
var i KillInfo
for _, o := range opts {
if err := o(ctx, t, &i); err != nil {
return err
}
}
_, err := t.client.TaskService().Kill(ctx, &tasks.KillRequest{ _, err := t.client.TaskService().Kill(ctx, &tasks.KillRequest{
Signal: uint32(s), Signal: uint32(s),
ContainerID: t.id, ContainerID: t.id,
All: i.All,
}) })
if err != nil { if err != nil {
return errdefs.FromGRPC(err) return errdefs.FromGRPC(err)

View File

@ -51,3 +51,17 @@ func WithProcessKill(ctx context.Context, p Process) error {
<-s <-s
return nil return nil
} }
type KillInfo struct {
// All kills all processes inside the task
// only valid on tasks, ignored on processes
All bool
}
type KillOpts func(context.Context, Process, *KillInfo) error
// WithKillAll kills all processes for a task
func WithKillAll(ctx context.Context, p Process, i *KillInfo) error {
i.All = true
return nil
}