ctr: update task kill to take exec-id

Signed-off-by: Jess Valarezo <valarezo.jessica@gmail.com>
This commit is contained in:
Jess Valarezo 2017-11-13 10:47:15 -08:00
parent 17093c2f6a
commit 1966f9f1b7
4 changed files with 31 additions and 15 deletions

View File

@ -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...)
},
}

View File

@ -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
}
}

View File

@ -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 {

View File

@ -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
}
}