From d64e3a68dcf42a4c461db70d97786b2251eab2fc Mon Sep 17 00:00:00 2001 From: yanxuean Date: Wed, 1 Nov 2017 16:21:48 +0800 Subject: [PATCH] sort the list result for ps,sandboxes,images fix #168 Signed-off-by: yanxuean --- cmd/crictl/container.go | 10 ++++++++++ cmd/crictl/image.go | 16 ++++++++++++++++ cmd/crictl/sandbox.go | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 7d26ae6b08..c40305c8d1 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -20,6 +20,7 @@ import ( "fmt" "log" "os" + "sort" "strings" "text/tabwriter" "time" @@ -31,6 +32,14 @@ import ( pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" ) +type containerByName []*pb.Container + +func (a containerByName) Len() int { return len(a) } +func (a containerByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a containerByName) 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 @@ -524,6 +533,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error { if err != nil { return err } + sort.Sort(containerByName(r.Containers)) switch opts.output { case "json": diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index d34be31581..126f00b61d 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "os" + "sort" "strings" "text/tabwriter" @@ -35,6 +36,20 @@ const ( truncatedImageIDLen = 13 ) +type imageBySort []*pb.Image + +func (a imageBySort) Len() int { return len(a) } +func (a imageBySort) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a imageBySort) 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", @@ -105,6 +120,7 @@ var listImageCommand = cli.Command{ if err != nil { return fmt.Errorf("listing images failed: %v", err) } + sort.Sort(imageBySort(r.Images)) switch context.String("output") { case "json": diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index 84112cd075..ab19344534 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -20,6 +20,7 @@ import ( "fmt" "log" "os" + "sort" "strings" "text/tabwriter" "time" @@ -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", @@ -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) }