diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 7ee86f8e9f..0b0c45e05a 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -859,10 +859,6 @@ func ListContainers(runtimeClient pb.RuntimeServiceClient, imageClient pb.ImageS display.AddRow([]string{columnContainer, columnImage, columnCreated, columnState, columnName, columnAttempt, columnPodID}) } for _, c := range r.Containers { - // Filter by pod name/namespace regular expressions. - if !matchesRegex(opts.nameRegexp, c.Metadata.Name) { - continue - } if match, err := matchesImage(imageClient, opts.image, c.GetImage().GetImage()); err != nil { return fmt.Errorf("failed to check image match %v", err) } else if !match { @@ -941,8 +937,16 @@ func convertContainerState(state pb.ContainerState) string { } func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.Container { - sort.Sort(containerByCreated(containersList)) - n := len(containersList) + filtered := []*pb.Container{} + for _, c := range containersList { + // Filter by pod name/namespace regular expressions. + if matchesRegex(opts.nameRegexp, c.Metadata.Name) { + filtered = append(filtered, c) + } + } + + sort.Sort(containerByCreated(filtered)) + n := len(filtered) if opts.latest { n = 1 } @@ -954,7 +958,7 @@ func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.C return a } return b - }(n, len(containersList)) + }(n, len(filtered)) - return containersList[:n] + return filtered[:n] } diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index 3d655df951..60adf1756d 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -486,7 +486,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { case "json": return outputProtobufObjAsJSON(r) case "yaml": - return outputProtobufObjAsJSON(r) + return outputProtobufObjAsYAML(r) case "table", "": // continue; output will be generated after the switch block ends. default: @@ -498,14 +498,6 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { display.AddRow([]string{columnPodID, columnCreated, columnState, columnName, columnNamespace, columnAttempt}) } for _, pod := range r.Items { - // Filter by pod name/namespace regular expressions. - if !matchesRegex(opts.nameRegexp, pod.Metadata.Name) { - continue - } - if !matchesRegex(opts.podNamespaceRegexp, pod.Metadata.Namespace) { - continue - } - if opts.quiet { fmt.Printf("%s\n", pod.Id) continue @@ -572,8 +564,17 @@ func convertPodState(state pb.PodSandboxState) string { } func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.PodSandbox { - sort.Sort(sandboxByCreated(sandboxesList)) - n := len(sandboxesList) + filtered := []*pb.PodSandbox{} + for _, p := range sandboxesList { + // Filter by pod name/namespace regular expressions. + if matchesRegex(opts.nameRegexp, p.Metadata.Name) && + matchesRegex(opts.podNamespaceRegexp, p.Metadata.Namespace) { + filtered = append(filtered, p) + } + } + + sort.Sort(sandboxByCreated(filtered)) + n := len(filtered) if opts.latest { n = 1 } @@ -585,7 +586,7 @@ func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.Po return a } return b - }(n, len(sandboxesList)) + }(n, len(filtered)) - return sandboxesList[:n] + return filtered[:n] }