diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index dbd7ead72c..ab37bfca72 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -556,7 +556,8 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error { createdAt := time.Unix(0, c.CreatedAt) ctm := units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago" if !opts.verbose { - fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", c.Id, ctm, c.State, c.GetMetadata().GetName()) + truncatedID := strings.TrimPrefix(c.Id, "")[:truncatedIDLen] + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", truncatedID, ctm, c.State, c.GetMetadata().GetName()) continue } diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index f90236ca20..77ba3ec9bc 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -31,11 +31,6 @@ import ( pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" ) -const ( - // truncatedImageIDLen is the truncated length of imageID - truncatedImageIDLen = 13 -) - type imageByRef []*pb.Image func (a imageByRef) Len() int { return len(a) } @@ -151,7 +146,7 @@ var listImageCommand = cli.Command{ imageName, repoDigest := normalizeRepoDigest(image.RepoDigests) repoTagPairs := normalizeRepoTagPair(image.RepoTags, imageName) size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3) - trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen] + trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedIDLen] for _, repoTagPair := range repoTagPairs { if showDigest { fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], repoDigest, trunctedImage, size) diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index 709dbe54fa..43df4a0c2f 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -67,7 +67,7 @@ var runPodSandboxCommand = cli.Command{ // Test RuntimeServiceClient.RunPodSandbox err = RunPodSandbox(runtimeClient, podSandboxConfig) if err != nil { - return fmt.Errorf("Run pod sandbox failed: %v", err) + return fmt.Errorf("run pod sandbox failed: %v", err) } return nil }, @@ -154,6 +154,16 @@ var listPodSandboxCommand = cli.Command{ Value: "", Usage: "filter by pod sandbox id", }, + cli.StringFlag{ + Name: "name", + Value: "", + Usage: "filter by pod sandbox name", + }, + cli.StringFlag{ + Name: "namespace", + Value: "", + Usage: "filter by pod sandbox namespace", + }, cli.StringFlag{ Name: "state,s", Value: "", @@ -168,7 +178,7 @@ var listPodSandboxCommand = cli.Command{ Usage: "show verbose info for sandboxes", }, cli.BoolFlag{ - Name: "quiet", + Name: "quiet, q", Usage: "list only sandbox IDs", }, cli.StringFlag{ @@ -197,6 +207,12 @@ var listPodSandboxCommand = cli.Command{ } opts.labels[pair[0]] = pair[1] } + if context.String("name") != "" { + opts.labels["io.kubernetes.pod.name"] = context.String("name") + } + if context.String("namespace") != "" { + opts.labels["io.kubernetes.pod.namespace"] = context.String("namespace") + } err := ListPodSandboxes(runtimeClient, opts) if err != nil { @@ -357,7 +373,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) if !opts.verbose && !opts.quiet { - fmt.Fprintln(w, "SANDBOX ID\tNAME\tSTATE") + fmt.Fprintln(w, "SANDBOX ID\tSTATE\tNAME\tNAMESPACE") } for _, pod := range r.Items { if opts.quiet { @@ -365,7 +381,8 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { continue } if !opts.verbose { - fmt.Fprintf(w, "%s\t%s\t%s\n", pod.Id, pod.Metadata.Name, pod.State) + truncatedID := strings.TrimPrefix(pod.Id, "")[:truncatedIDLen] + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", truncatedID, pod.State, pod.Metadata.Name, pod.Metadata.Namespace) continue } diff --git a/cmd/crictl/util.go b/cmd/crictl/util.go index 1c7f9cf97b..1eb01d193a 100644 --- a/cmd/crictl/util.go +++ b/cmd/crictl/util.go @@ -30,6 +30,11 @@ import ( pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" ) +const ( + // truncatedImageIDLen is the truncated length of imageID + truncatedIDLen = 13 +) + var runtimeClient pb.RuntimeServiceClient var imageClient pb.ImageServiceClient var conn *grpc.ClientConn