Skip to content

Commit

Permalink
ps, images and sandboxes print table name when there are no items
Browse files Browse the repository at this point in the history
Signed-off-by: Yanqiang Miao <[email protected]>
  • Loading branch information
Yanqiang Miao committed Nov 4, 2017
1 parent 4e3c997 commit 59108b5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 1 addition & 6 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 21 additions & 4 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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: "",
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -357,15 +373,16 @@ 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 {
fmt.Printf("%s\n", pod.Id)
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
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 59108b5

Please sign in to comment.