Add client support for filtering pods

Signed-off-by: Mrunal Patel <mpatel@redhat.com>
This commit is contained in:
Mrunal Patel 2016-10-17 12:25:30 -07:00
parent 5e7d96bd6a
commit 2cc40ef9ef

View file

@ -2,6 +2,8 @@ package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/urfave/cli"
@ -138,6 +140,20 @@ var listPodSandboxCommand = cli.Command{
Name: "list",
Usage: "list pod sandboxes",
Flags: []cli.Flag{
cli.StringFlag{
Name: "id",
Value: "",
Usage: "filter by pod sandbox id",
},
cli.StringFlag{
Name: "state",
Value: "",
Usage: "filter by pod sandbox state",
},
cli.StringSliceFlag{
Name: "label",
Usage: "filter by key=value label",
},
cli.BoolFlag{
Name: "quiet",
Usage: "list only pod IDs",
@ -152,7 +168,22 @@ var listPodSandboxCommand = cli.Command{
defer conn.Close()
client := pb.NewRuntimeServiceClient(conn)
err = ListPodSandboxes(client, context.Bool("quiet"))
opts := listOptions{
id: context.String("id"),
state: context.String("state"),
quiet: context.Bool("quiet"),
labels: make(map[string]string),
}
for _, l := range context.StringSlice("label") {
pair := strings.Split(l, "=")
if len(pair) != 2 {
return fmt.Errorf("incorrectly specified label: %v", l)
}
opts.labels[pair[0]] = pair[1]
}
err = ListPodSandboxes(client, opts)
if err != nil {
return fmt.Errorf("listing pod sandboxes failed: %v", err)
}
@ -264,13 +295,35 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID string) error {
// ListPodSandboxes sends a ListPodSandboxRequest to the server, and parses
// the returned ListPodSandboxResponse.
func ListPodSandboxes(client pb.RuntimeServiceClient, quiet bool) error {
r, err := client.ListPodSandbox(context.Background(), &pb.ListPodSandboxRequest{})
func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
filter := &pb.PodSandboxFilter{}
if opts.id != "" {
filter.Id = &opts.id
}
if opts.state != "" {
st := pb.PodSandBoxState_NOTREADY
switch opts.state {
case "ready":
st = pb.PodSandBoxState_READY
filter.State = &st
case "notready":
st = pb.PodSandBoxState_NOTREADY
filter.State = &st
default:
log.Fatalf("--state should be ready or notready")
}
}
if opts.labels != nil {
filter.LabelSelector = opts.labels
}
r, err := client.ListPodSandbox(context.Background(), &pb.ListPodSandboxRequest{
Filter: filter,
})
if err != nil {
return err
}
for _, pod := range r.Items {
if quiet {
if opts.quiet {
fmt.Println(*pod.Id)
continue
}