Skip to content

Commit

Permalink
Merge pull request #217 from Random-Liu/fix-sandbox-container-list
Browse files Browse the repository at this point in the history
Fix container/sandbox list.
  • Loading branch information
Random-Liu authored Dec 14, 2017
2 parents 0cc0c63 + cd98da1 commit 2c29c5c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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
return a[i].CreatedAt > a[j].CreatedAt
}

type createOptions struct {
Expand Down Expand Up @@ -591,7 +591,6 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
return err
}
r.Containers = getContainersList(r.GetContainers(), opts)
sort.Sort(containerByCreated(r.Containers))

switch opts.output {
case "json":
Expand Down Expand Up @@ -655,6 +654,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
}

func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.Container {
sort.Sort(containerByCreated(containersList))
n := len(containersList)
if opts.latest {
n = 1
Expand All @@ -669,5 +669,5 @@ func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.C
return b
}(n, len(containersList))

return containersList[len(containersList)-n:]
return containersList[:n]
}
51 changes: 37 additions & 14 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ import (
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)

type sandboxBySort []*pb.PodSandbox
type sandboxByCreated []*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
func (a sandboxByCreated) Len() int { return len(a) }
func (a sandboxByCreated) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sandboxByCreated) Less(i, j int) bool {
return a[i].CreatedAt > a[j].CreatedAt
}

var runPodSandboxCommand = cli.Command{
Expand Down Expand Up @@ -172,12 +166,12 @@ var listPodSandboxCommand = cli.Command{
Usage: "filter by pod sandbox namespace",
},
cli.StringFlag{
Name: "state,s",
Name: "state, s",
Value: "",
Usage: "filter by pod sandbox state",
},
cli.StringSliceFlag{
Name: "label,l",
Name: "label",
Usage: "filter by key=value label",
},
cli.BoolFlag{
Expand All @@ -192,6 +186,14 @@ var listPodSandboxCommand = cli.Command{
Name: "output, o",
Usage: "Output format, One of: json|yaml|table",
},
cli.BoolFlag{
Name: "latest, l",
Usage: "Show recently created sandboxes",
},
cli.IntFlag{
Name: "last, n",
Usage: "Show last n recently created sandboxes",
},
cli.BoolFlag{
Name: "no-trunc",
Usage: "Show output without truncating the ID",
Expand All @@ -209,6 +211,8 @@ var listPodSandboxCommand = cli.Command{
verbose: context.Bool("verbose"),
quiet: context.Bool("quiet"),
output: context.String("output"),
latest: context.Bool("latest"),
last: context.Int("last"),
noTrunc: context.Bool("no-trunc"),
}
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
Expand Down Expand Up @@ -401,7 +405,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
if err != nil {
return err
}
sort.Sort(sandboxBySort(r.Items))
r.Items = getSandboxesList(r.GetItems(), opts)

switch opts.output {
case "json":
Expand Down Expand Up @@ -467,3 +471,22 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
w.Flush()
return nil
}

func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.PodSandbox {
sort.Sort(sandboxByCreated(sandboxesList))
n := len(sandboxesList)
if opts.latest {
n = 1
}
if opts.last > 0 {
n = opts.last
}
n = func(a, b int) int {
if a < b {
return a
}
return b
}(n, len(sandboxesList))

return sandboxesList[:n]
}

0 comments on commit 2c29c5c

Please sign in to comment.