Container list and Task List printed separately.
Fix for #914 Signed-off-by: Kunal Kushwaha <kushwaha_kunal_v7@lab.ntt.co.jp>
This commit is contained in:
parent
80656bf8ca
commit
a0f73ae229
178
cmd/ctr/list.go
178
cmd/ctr/list.go
@ -6,47 +6,94 @@ import (
|
|||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
"github.com/containerd/containerd"
|
"github.com/containerd/containerd"
|
||||||
|
"github.com/containerd/containerd/api/services/execution"
|
||||||
|
tasktypes "github.com/containerd/containerd/api/types/task"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
var containersCommand = cli.Command{
|
var taskListCommand = cli.Command{
|
||||||
Name: "containers",
|
Name: "tasks",
|
||||||
}
|
Usage: "manage tasks",
|
||||||
|
Aliases: []string{"t"},
|
||||||
var listCommand = cli.Command{
|
Subcommands: []cli.Command{
|
||||||
Name: "list",
|
{
|
||||||
Aliases: []string{"ls"},
|
Name: "list",
|
||||||
Usage: "list containers",
|
Usage: "list tasks",
|
||||||
Flags: []cli.Flag{
|
Aliases: []string{"ls"},
|
||||||
cli.BoolFlag{
|
Flags: []cli.Flag{
|
||||||
Name: "quiet, q",
|
cli.BoolFlag{
|
||||||
Usage: "print only the container id",
|
Name: "quiet, q",
|
||||||
|
Usage: "print only the task id & pid",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: taskListFn,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(context *cli.Context) error {
|
}
|
||||||
var (
|
|
||||||
quiet = context.Bool("quiet")
|
|
||||||
ctx, cancel = appContext(context)
|
|
||||||
)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
client, err := newClient(context)
|
var containerListCommand = cli.Command{
|
||||||
if err != nil {
|
Name: "containers",
|
||||||
return err
|
Usage: "manage containers (metadata)",
|
||||||
}
|
Aliases: []string{"c"},
|
||||||
containers, err := client.Containers(ctx)
|
Subcommands: []cli.Command{
|
||||||
if err != nil {
|
{
|
||||||
return err
|
Name: "list",
|
||||||
}
|
Usage: "list tasks",
|
||||||
if quiet {
|
Aliases: []string{"ls"},
|
||||||
for _, c := range containers {
|
Flags: []cli.Flag{
|
||||||
fmt.Println(c.ID())
|
cli.BoolFlag{
|
||||||
|
Name: "quiet, q",
|
||||||
|
Usage: "print only the container id",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: containerListFn,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func taskListFn(context *cli.Context) error {
|
||||||
|
var (
|
||||||
|
quiet = context.Bool("quiet")
|
||||||
|
ctx, cancel = appContext(context)
|
||||||
|
)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
client, err := newClient(context)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
containers, err := client.Containers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks := client.TaskService()
|
||||||
|
|
||||||
|
tasksResponse, err := tasks.List(ctx, &execution.ListRequest{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join with tasks to get status.
|
||||||
|
tasksByContainerID := map[string]*tasktypes.Task{}
|
||||||
|
for _, task := range tasksResponse.Tasks {
|
||||||
|
tasksByContainerID[task.ContainerID] = task
|
||||||
|
}
|
||||||
|
|
||||||
|
if quiet {
|
||||||
|
for _, c := range containers {
|
||||||
|
task, ok := tasksByContainerID[c.ID()]
|
||||||
|
if ok {
|
||||||
|
fmt.Printf("%s\t%d\n", c.ID(), task.Pid)
|
||||||
|
} else {
|
||||||
|
//Since task is not running, PID is printed 0
|
||||||
|
fmt.Printf("%s\t%d\n", c.ID(), 0)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
||||||
fmt.Fprintln(w, "ID\tIMAGE\tPID\tSTATUS")
|
fmt.Fprintln(w, "TASK-ID\tIMAGE\tPID\tSTATUS")
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
var imageName string
|
var imageName string
|
||||||
if image, err := c.Image(ctx); err != nil {
|
if image, err := c.Image(ctx); err != nil {
|
||||||
@ -61,21 +108,15 @@ var listCommand = cli.Command{
|
|||||||
status string
|
status string
|
||||||
pid uint32
|
pid uint32
|
||||||
)
|
)
|
||||||
task, err := c.Task(ctx, nil)
|
task, ok := tasksByContainerID[c.ID()]
|
||||||
if err == nil {
|
if ok {
|
||||||
s, err := task.Status(ctx)
|
status = task.Status.String()
|
||||||
if err != nil {
|
pid = task.Pid
|
||||||
return err
|
|
||||||
}
|
|
||||||
status = string(s)
|
|
||||||
pid = task.Pid()
|
|
||||||
} else {
|
} else {
|
||||||
if err != containerd.ErrNoRunningTask {
|
status = "STOPPED" // TODO(stevvooe): Is this assumption correct?
|
||||||
return err
|
|
||||||
}
|
|
||||||
status = string(containerd.Stopped)
|
|
||||||
pid = 0
|
pid = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%d\t%s\n",
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%d\t%s\n",
|
||||||
c.ID(),
|
c.ID(),
|
||||||
imageName,
|
imageName,
|
||||||
@ -85,7 +126,54 @@ var listCommand = cli.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return w.Flush()
|
return w.Flush()
|
||||||
},
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func containerListFn(context *cli.Context) error {
|
||||||
|
var (
|
||||||
|
quiet = context.Bool("quiet")
|
||||||
|
ctx, cancel = appContext(context)
|
||||||
|
)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
client, err := newClient(context)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
containers, err := client.Containers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if quiet {
|
||||||
|
for _, c := range containers {
|
||||||
|
fmt.Printf("%s\n", c.ID())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cl := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
||||||
|
fmt.Fprintln(cl, "ID\tIMAGE\tRUNTIME\tSIZE")
|
||||||
|
for _, c := range containers {
|
||||||
|
var imageName string
|
||||||
|
if image, err := c.Image(ctx); err != nil {
|
||||||
|
if err != containerd.ErrNoImage {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
imageName = "-"
|
||||||
|
} else {
|
||||||
|
imageName = image.Name()
|
||||||
|
}
|
||||||
|
proto := c.Proto()
|
||||||
|
if _, err := fmt.Fprintf(cl, "%s\t%s\t%s\t%d\n",
|
||||||
|
c.ID(),
|
||||||
|
imageName,
|
||||||
|
proto.Runtime,
|
||||||
|
proto.Size(),
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cl.Flush()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,8 @@ containerd CLI
|
|||||||
deleteCommand,
|
deleteCommand,
|
||||||
namespacesCommand,
|
namespacesCommand,
|
||||||
eventsCommand,
|
eventsCommand,
|
||||||
listCommand,
|
containerListCommand,
|
||||||
|
taskListCommand,
|
||||||
infoCommand,
|
infoCommand,
|
||||||
killCommand,
|
killCommand,
|
||||||
pprofCommand,
|
pprofCommand,
|
||||||
|
Loading…
Reference in New Issue
Block a user