Skip to content

Commit ef9890a

Browse files
committed
Fix container/sandbox list.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 23a53d2 commit ef9890a

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

cmd/crictl/container.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type containerByCreated []*pb.Container
3838
func (a containerByCreated) Len() int { return len(a) }
3939
func (a containerByCreated) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
4040
func (a containerByCreated) Less(i, j int) bool {
41-
return a[i].CreatedAt < a[j].CreatedAt
41+
return a[i].CreatedAt > a[j].CreatedAt
4242
}
4343

4444
type createOptions struct {
@@ -590,8 +590,8 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
590590
if err != nil {
591591
return err
592592
}
593+
sort.Sort(containerByCreated(r.GetContainers()))
593594
r.Containers = getContainersList(r.GetContainers(), opts)
594-
sort.Sort(containerByCreated(r.Containers))
595595

596596
switch opts.output {
597597
case "json":
@@ -669,5 +669,5 @@ func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.C
669669
return b
670670
}(n, len(containersList))
671671

672-
return containersList[len(containersList)-n:]
672+
return containersList[:n]
673673
}

cmd/crictl/sandbox.go

+37-14
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,12 @@ import (
3434
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
3535
)
3636

37-
type sandboxBySort []*pb.PodSandbox
37+
type sandboxByCreated []*pb.PodSandbox
3838

39-
func (a sandboxBySort) Len() int { return len(a) }
40-
func (a sandboxBySort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
41-
func (a sandboxBySort) Less(i, j int) bool {
42-
if a[i].Metadata.Namespace != a[j].Metadata.Namespace {
43-
return a[i].Metadata.Namespace < a[j].Metadata.Namespace
44-
}
45-
if a[i].Metadata.Name != a[j].Metadata.Name {
46-
return a[i].Metadata.Name < a[j].Metadata.Name
47-
}
48-
return a[i].CreatedAt < a[j].CreatedAt
39+
func (a sandboxByCreated) Len() int { return len(a) }
40+
func (a sandboxByCreated) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
41+
func (a sandboxByCreated) Less(i, j int) bool {
42+
return a[i].CreatedAt > a[j].CreatedAt
4943
}
5044

5145
var runPodSandboxCommand = cli.Command{
@@ -172,12 +166,12 @@ var listPodSandboxCommand = cli.Command{
172166
Usage: "filter by pod sandbox namespace",
173167
},
174168
cli.StringFlag{
175-
Name: "state,s",
169+
Name: "state, s",
176170
Value: "",
177171
Usage: "filter by pod sandbox state",
178172
},
179173
cli.StringSliceFlag{
180-
Name: "label,l",
174+
Name: "label",
181175
Usage: "filter by key=value label",
182176
},
183177
cli.BoolFlag{
@@ -192,6 +186,14 @@ var listPodSandboxCommand = cli.Command{
192186
Name: "output, o",
193187
Usage: "Output format, One of: json|yaml|table",
194188
},
189+
cli.BoolFlag{
190+
Name: "latest, l",
191+
Usage: "Show recently created sandboxes",
192+
},
193+
cli.IntFlag{
194+
Name: "last, n",
195+
Usage: "Show last n recently created sandboxes",
196+
},
195197
},
196198
Action: func(context *cli.Context) error {
197199
var err error
@@ -205,6 +207,8 @@ var listPodSandboxCommand = cli.Command{
205207
verbose: context.Bool("verbose"),
206208
quiet: context.Bool("quiet"),
207209
output: context.String("output"),
210+
latest: context.Bool("latest"),
211+
last: context.Int("last"),
208212
}
209213
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
210214
if err != nil {
@@ -396,7 +400,8 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
396400
if err != nil {
397401
return err
398402
}
399-
sort.Sort(sandboxBySort(r.Items))
403+
sort.Sort(sandboxByCreated(r.GetItems()))
404+
r.Items = getSandboxesList(r.GetItems(), opts)
400405

401406
switch opts.output {
402407
case "json":
@@ -459,3 +464,21 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
459464
w.Flush()
460465
return nil
461466
}
467+
468+
func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.PodSandbox {
469+
n := len(sandboxesList)
470+
if opts.latest {
471+
n = 1
472+
}
473+
if opts.last > 0 {
474+
n = opts.last
475+
}
476+
n = func(a, b int) int {
477+
if a < b {
478+
return a
479+
}
480+
return b
481+
}(n, len(sandboxesList))
482+
483+
return sandboxesList[:n]
484+
}

0 commit comments

Comments
 (0)