Skip to content

Commit bc50b06

Browse files
authored
Merge pull request #550 from openSUSE/filter-fix
Fix pod and container name filter for JSON/YAML output
2 parents d37cf58 + d79440b commit bc50b06

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

cmd/crictl/container.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,6 @@ func ListContainers(runtimeClient pb.RuntimeServiceClient, imageClient pb.ImageS
859859
display.AddRow([]string{columnContainer, columnImage, columnCreated, columnState, columnName, columnAttempt, columnPodID})
860860
}
861861
for _, c := range r.Containers {
862-
// Filter by pod name/namespace regular expressions.
863-
if !matchesRegex(opts.nameRegexp, c.Metadata.Name) {
864-
continue
865-
}
866862
if match, err := matchesImage(imageClient, opts.image, c.GetImage().GetImage()); err != nil {
867863
return fmt.Errorf("failed to check image match %v", err)
868864
} else if !match {
@@ -941,8 +937,16 @@ func convertContainerState(state pb.ContainerState) string {
941937
}
942938

943939
func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.Container {
944-
sort.Sort(containerByCreated(containersList))
945-
n := len(containersList)
940+
filtered := []*pb.Container{}
941+
for _, c := range containersList {
942+
// Filter by pod name/namespace regular expressions.
943+
if matchesRegex(opts.nameRegexp, c.Metadata.Name) {
944+
filtered = append(filtered, c)
945+
}
946+
}
947+
948+
sort.Sort(containerByCreated(filtered))
949+
n := len(filtered)
946950
if opts.latest {
947951
n = 1
948952
}
@@ -954,7 +958,7 @@ func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.C
954958
return a
955959
}
956960
return b
957-
}(n, len(containersList))
961+
}(n, len(filtered))
958962

959-
return containersList[:n]
963+
return filtered[:n]
960964
}

cmd/crictl/sandbox.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
486486
case "json":
487487
return outputProtobufObjAsJSON(r)
488488
case "yaml":
489-
return outputProtobufObjAsJSON(r)
489+
return outputProtobufObjAsYAML(r)
490490
case "table", "":
491491
// continue; output will be generated after the switch block ends.
492492
default:
@@ -498,14 +498,6 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
498498
display.AddRow([]string{columnPodID, columnCreated, columnState, columnName, columnNamespace, columnAttempt})
499499
}
500500
for _, pod := range r.Items {
501-
// Filter by pod name/namespace regular expressions.
502-
if !matchesRegex(opts.nameRegexp, pod.Metadata.Name) {
503-
continue
504-
}
505-
if !matchesRegex(opts.podNamespaceRegexp, pod.Metadata.Namespace) {
506-
continue
507-
}
508-
509501
if opts.quiet {
510502
fmt.Printf("%s\n", pod.Id)
511503
continue
@@ -572,8 +564,17 @@ func convertPodState(state pb.PodSandboxState) string {
572564
}
573565

574566
func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.PodSandbox {
575-
sort.Sort(sandboxByCreated(sandboxesList))
576-
n := len(sandboxesList)
567+
filtered := []*pb.PodSandbox{}
568+
for _, p := range sandboxesList {
569+
// Filter by pod name/namespace regular expressions.
570+
if matchesRegex(opts.nameRegexp, p.Metadata.Name) &&
571+
matchesRegex(opts.podNamespaceRegexp, p.Metadata.Namespace) {
572+
filtered = append(filtered, p)
573+
}
574+
}
575+
576+
sort.Sort(sandboxByCreated(filtered))
577+
n := len(filtered)
577578
if opts.latest {
578579
n = 1
579580
}
@@ -585,7 +586,7 @@ func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.Po
585586
return a
586587
}
587588
return b
588-
}(n, len(sandboxesList))
589+
}(n, len(filtered))
589590

590-
return sandboxesList[:n]
591+
return filtered[:n]
591592
}

0 commit comments

Comments
 (0)