diff --git a/cmd/ctr/commands/tasks/kill.go b/cmd/ctr/commands/tasks/kill.go index 5b1a5f427..4d2a80adf 100644 --- a/cmd/ctr/commands/tasks/kill.go +++ b/cmd/ctr/commands/tasks/kill.go @@ -1,13 +1,12 @@ package tasks import ( + "github.com/containerd/containerd" "github.com/containerd/containerd/cmd/ctr/commands" "github.com/pkg/errors" "github.com/urfave/cli" ) -// TODO:(jessvalarezo) the pid flag is not used here -// update to be able to signal given pid var killCommand = cli.Command{ Name: "kill", Usage: "signal a container (default: SIGTERM)", @@ -18,10 +17,9 @@ var killCommand = cli.Command{ Value: "SIGTERM", Usage: "signal to send to the container", }, - cli.IntFlag{ - Name: "pid", - Usage: "pid to kill", - Value: 0, + cli.StringFlag{ + Name: "exec-id", + Usage: "process ID to kill", }, cli.BoolFlag{ Name: "all, a", @@ -38,17 +36,24 @@ var killCommand = cli.Command{ return err } var ( - pid = context.Int("pid") - all = context.Bool("all") + all = context.Bool("all") + execID = context.String("exec-id") + opts []containerd.KillOpts ) - if pid > 0 && all { - return errors.New("enter a pid or all; not both") + if all && execID != "" { + return errors.New("specify an exec-id or all; not both") } client, ctx, cancel, err := commands.NewClient(context) if err != nil { return err } defer cancel() + if all { + opts = append(opts, containerd.WithKillAll) + } + if execID != "" { + opts = append(opts, containerd.WithKillExecID(execID)) + } container, err := client.LoadContainer(ctx, id) if err != nil { return err @@ -57,6 +62,6 @@ var killCommand = cli.Command{ if err != nil { return err } - return task.Kill(ctx, signal) + return task.Kill(ctx, signal, opts...) }, } diff --git a/process.go b/process.go index e51367aaa..ebefb6973 100644 --- a/process.go +++ b/process.go @@ -104,7 +104,7 @@ func (p *process) Start(ctx context.Context) 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 { + if err := o(ctx, &i); err != nil { return err } } diff --git a/task.go b/task.go index 7ae1bf622..a4e5fe4d1 100644 --- a/task.go +++ b/task.go @@ -175,13 +175,14 @@ func (t *task) Start(ctx context.Context) 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 { + if err := o(ctx, &i); err != nil { return err } } _, err := t.client.TaskService().Kill(ctx, &tasks.KillRequest{ Signal: uint32(s), ContainerID: t.id, + ExecID: i.ExecID, All: i.All, }) if err != nil { diff --git a/task_opts.go b/task_opts.go index d8569f572..a387adb6e 100644 --- a/task_opts.go +++ b/task_opts.go @@ -65,13 +65,23 @@ type KillInfo struct { // All kills all processes inside the task // only valid on tasks, ignored on processes All bool + // ExecID is the ID of a process to kill + ExecID string } // KillOpts allows options to be set for the killing of a process -type KillOpts func(context.Context, Process, *KillInfo) error +type KillOpts func(context.Context, *KillInfo) error // WithKillAll kills all processes for a task -func WithKillAll(ctx context.Context, p Process, i *KillInfo) error { +func WithKillAll(ctx context.Context, i *KillInfo) error { i.All = true return nil } + +// WithKillExecID specifies the process ID +func WithKillExecID(execID string) KillOpts { + return func(ctx context.Context, i *KillInfo) error { + i.ExecID = execID + return nil + } +}