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 3, 2017
1 parent 4e3c997 commit ee19f89
Show file tree
Hide file tree
Showing 4 changed files with 30 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
26 changes: 22 additions & 4 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/urfave/cli"
"golang.org/x/net/context"
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
"k8s.io/kubernetes/pkg/kubelet/types"
)

type sandboxBySort []*pb.PodSandbox
Expand Down Expand Up @@ -67,7 +68,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 +155,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 +179,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 +208,12 @@ var listPodSandboxCommand = cli.Command{
}
opts.labels[pair[0]] = pair[1]
}
if context.String("name") != "" {
opts.labels[types.KubernetesPodNameLabel] = context.String("name")
}
if context.String("namespace") != "" {
opts.labels[types.KubernetesPodNamespaceLabel] = context.String("namespace")
}

err := ListPodSandboxes(runtimeClient, opts)
if err != nil {
Expand Down Expand Up @@ -357,15 +374,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\tNAME\tNAMESPACE\tSTATE")
}
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.Metadata.Name, pod.Metadata.Namespace, pod.State)
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 ee19f89

Please sign in to comment.