From 2cc40ef9effb3301435d81f0b19c2e57a69c94d5 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 17 Oct 2016 12:25:30 -0700 Subject: [PATCH] Add client support for filtering pods Signed-off-by: Mrunal Patel --- cmd/client/sandbox.go | 61 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/cmd/client/sandbox.go b/cmd/client/sandbox.go index 2b5dcd97..96a53e42 100644 --- a/cmd/client/sandbox.go +++ b/cmd/client/sandbox.go @@ -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 }