Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix container/sandbox list. #217

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add shortname for namespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last is using the shortname n.

},
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",
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems two options are same. Merge last to latest and defaulting latest to one?

Copy link
Contributor Author

@Random-Liu Random-Liu Dec 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially just want to keep the same UX with docker, but of course we could make change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feiskyer I tried to merge them into one flag. But I find that it's hard to set default for -l, because once you define it as an int flag, you have to specify a value when using it.

Given that I think it's fine to keep 2 flags as what docker does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, didn't notice this problem. let's keep both

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]
}