Skip to content

Commit

Permalink
sort the list result for ps,sandboxes,images
Browse files Browse the repository at this point in the history
fix #168
Signed-off-by: yanxuean <yan.xuean@zte.com.cn>
  • Loading branch information
yanxuean committed Nov 2, 2017
1 parent 8e989f8 commit b16738a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
Expand All @@ -31,6 +32,14 @@ import (
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)

type containerByCreated []*pb.Container

func (a containerByCreated) Len() int { return len(a) }
func (a containerByCreated) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a containerByCreated) Less(i, j int) bool {
return a[i].CreatedAt < a[j].CreatedAt
}

type createOptions struct {
// configPath is path to the config for container
configPath string
Expand Down Expand Up @@ -524,6 +533,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
if err != nil {
return err
}
sort.Sort(containerByCreated(r.Containers))

switch opts.output {
case "json":
Expand Down
16 changes: 16 additions & 0 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"

Expand All @@ -35,6 +36,20 @@ const (
truncatedImageIDLen = 13
)

type imageByRef []*pb.Image

func (a imageByRef) Len() int { return len(a) }
func (a imageByRef) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a imageByRef) Less(i, j int) bool {
if len(a[i].RepoTags) > 0 && len(a[j].RepoTags) > 0 {
return a[i].RepoTags[0] < a[j].RepoTags[0]
}
if len(a[i].RepoDigests) > 0 && len(a[j].RepoDigests) > 0 {
return a[i].RepoDigests[0] < a[j].RepoDigests[0]
}
return a[i].Id < a[j].Id
}

var pullImageCommand = cli.Command{
Name: "pull",
Usage: "Pull an image from a registry",
Expand Down Expand Up @@ -105,6 +120,7 @@ var listImageCommand = cli.Command{
if err != nil {
return fmt.Errorf("listing images failed: %v", err)
}
sort.Sort(imageByRef(r.Images))

switch context.String("output") {
case "json":
Expand Down
17 changes: 16 additions & 1 deletion cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
Expand All @@ -30,6 +31,20 @@ import (
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)

type sandboxBySort []*pb.PodSandbox

func (a sandboxBySort) Len() int { return len(a) }
func (a sandboxBySort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sandboxBySort) Less(i, j int) bool {
if a[i].Metadata.Namespace != a[j].Metadata.Namespace {
return a[i].Metadata.Namespace < a[j].Metadata.Namespace
}
if a[i].Metadata.Name != a[j].Metadata.Name {
return a[i].Metadata.Name < a[j].Metadata.Name
}
return a[i].CreatedAt < a[j].CreatedAt
}

var runPodSandboxCommand = cli.Command{
Name: "runs",
Usage: "Run a new sandbox",
Expand Down Expand Up @@ -331,11 +346,11 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
if err != nil {
return err
}
sort.Sort(sandboxBySort(r.Items))

switch opts.output {
case "json":
return outputJSON(r.Items)

case "yaml":
return outputYAML(r.Items)
}
Expand Down

0 comments on commit b16738a

Please sign in to comment.