Add client support for filtering pods
Signed-off-by: Mrunal Patel <mpatel@redhat.com>
This commit is contained in:
parent
5e7d96bd6a
commit
2cc40ef9ef
1 changed files with 57 additions and 4 deletions
|
@ -2,6 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
@ -138,6 +140,20 @@ var listPodSandboxCommand = cli.Command{
|
||||||
Name: "list",
|
Name: "list",
|
||||||
Usage: "list pod sandboxes",
|
Usage: "list pod sandboxes",
|
||||||
Flags: []cli.Flag{
|
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{
|
cli.BoolFlag{
|
||||||
Name: "quiet",
|
Name: "quiet",
|
||||||
Usage: "list only pod IDs",
|
Usage: "list only pod IDs",
|
||||||
|
@ -152,7 +168,22 @@ var listPodSandboxCommand = cli.Command{
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
client := pb.NewRuntimeServiceClient(conn)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("listing pod sandboxes failed: %v", err)
|
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
|
// ListPodSandboxes sends a ListPodSandboxRequest to the server, and parses
|
||||||
// the returned ListPodSandboxResponse.
|
// the returned ListPodSandboxResponse.
|
||||||
func ListPodSandboxes(client pb.RuntimeServiceClient, quiet bool) error {
|
func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
|
||||||
r, err := client.ListPodSandbox(context.Background(), &pb.ListPodSandboxRequest{})
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, pod := range r.Items {
|
for _, pod := range r.Items {
|
||||||
if quiet {
|
if opts.quiet {
|
||||||
fmt.Println(*pod.Id)
|
fmt.Println(*pod.Id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue