Skip to content

Commit 2c29c5c

Browse files
authored
Merge pull request #217 from Random-Liu/fix-sandbox-container-list
Fix container/sandbox list.
2 parents 0cc0c63 + cd98da1 commit 2c29c5c

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 {
@@ -591,7 +591,6 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
591591
return err
592592
}
593593
r.Containers = getContainersList(r.GetContainers(), opts)
594-
sort.Sort(containerByCreated(r.Containers))
595594

596595
switch opts.output {
597596
case "json":
@@ -655,6 +654,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
655654
}
656655

657656
func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.Container {
657+
sort.Sort(containerByCreated(containersList))
658658
n := len(containersList)
659659
if opts.latest {
660660
n = 1
@@ -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
cli.BoolFlag{
196198
Name: "no-trunc",
197199
Usage: "Show output without truncating the ID",
@@ -209,6 +211,8 @@ var listPodSandboxCommand = cli.Command{
209211
verbose: context.Bool("verbose"),
210212
quiet: context.Bool("quiet"),
211213
output: context.String("output"),
214+
latest: context.Bool("latest"),
215+
last: context.Int("last"),
212216
noTrunc: context.Bool("no-trunc"),
213217
}
214218
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
@@ -401,7 +405,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
401405
if err != nil {
402406
return err
403407
}
404-
sort.Sort(sandboxBySort(r.Items))
408+
r.Items = getSandboxesList(r.GetItems(), opts)
405409

406410
switch opts.output {
407411
case "json":
@@ -467,3 +471,22 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
467471
w.Flush()
468472
return nil
469473
}
474+
475+
func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.PodSandbox {
476+
sort.Sort(sandboxByCreated(sandboxesList))
477+
n := len(sandboxesList)
478+
if opts.latest {
479+
n = 1
480+
}
481+
if opts.last > 0 {
482+
n = opts.last
483+
}
484+
n = func(a, b int) int {
485+
if a < b {
486+
return a
487+
}
488+
return b
489+
}(n, len(sandboxesList))
490+
491+
return sandboxesList[:n]
492+
}

0 commit comments

Comments
 (0)