ctr: update task kill to take exec-id
Signed-off-by: Jess Valarezo <valarezo.jessica@gmail.com>
This commit is contained in:
parent
17093c2f6a
commit
1966f9f1b7
@ -1,13 +1,12 @@
|
|||||||
package tasks
|
package tasks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/containerd/containerd"
|
||||||
"github.com/containerd/containerd/cmd/ctr/commands"
|
"github.com/containerd/containerd/cmd/ctr/commands"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/urfave/cli"
|
"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{
|
var killCommand = cli.Command{
|
||||||
Name: "kill",
|
Name: "kill",
|
||||||
Usage: "signal a container (default: SIGTERM)",
|
Usage: "signal a container (default: SIGTERM)",
|
||||||
@ -18,10 +17,9 @@ var killCommand = cli.Command{
|
|||||||
Value: "SIGTERM",
|
Value: "SIGTERM",
|
||||||
Usage: "signal to send to the container",
|
Usage: "signal to send to the container",
|
||||||
},
|
},
|
||||||
cli.IntFlag{
|
cli.StringFlag{
|
||||||
Name: "pid",
|
Name: "exec-id",
|
||||||
Usage: "pid to kill",
|
Usage: "process ID to kill",
|
||||||
Value: 0,
|
|
||||||
},
|
},
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "all, a",
|
Name: "all, a",
|
||||||
@ -38,17 +36,24 @@ var killCommand = cli.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var (
|
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 {
|
if all && execID != "" {
|
||||||
return errors.New("enter a pid or all; not both")
|
return errors.New("specify an exec-id or all; not both")
|
||||||
}
|
}
|
||||||
client, ctx, cancel, err := commands.NewClient(context)
|
client, ctx, cancel, err := commands.NewClient(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
if all {
|
||||||
|
opts = append(opts, containerd.WithKillAll)
|
||||||
|
}
|
||||||
|
if execID != "" {
|
||||||
|
opts = append(opts, containerd.WithKillExecID(execID))
|
||||||
|
}
|
||||||
container, err := client.LoadContainer(ctx, id)
|
container, err := client.LoadContainer(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -57,6 +62,6 @@ var killCommand = cli.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return task.Kill(ctx, signal)
|
return task.Kill(ctx, signal, opts...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
func (p *process) Kill(ctx context.Context, s syscall.Signal, opts ...KillOpts) error {
|
||||||
var i KillInfo
|
var i KillInfo
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
if err := o(ctx, p, &i); err != nil {
|
if err := o(ctx, &i); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
3
task.go
3
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 {
|
func (t *task) Kill(ctx context.Context, s syscall.Signal, opts ...KillOpts) error {
|
||||||
var i KillInfo
|
var i KillInfo
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
if err := o(ctx, t, &i); err != nil {
|
if err := o(ctx, &i); err != nil {
|
||||||
return err
|
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,
|
||||||
|
ExecID: i.ExecID,
|
||||||
All: i.All,
|
All: i.All,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
14
task_opts.go
14
task_opts.go
@ -65,13 +65,23 @@ type KillInfo struct {
|
|||||||
// All kills all processes inside the task
|
// All kills all processes inside the task
|
||||||
// only valid on tasks, ignored on processes
|
// only valid on tasks, ignored on processes
|
||||||
All bool
|
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
|
// 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
|
// 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
|
i.All = true
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user