Skip to content

Commit

Permalink
Merge pull request #550 from openSUSE/filter-fix
Browse files Browse the repository at this point in the history
Fix pod and container name filter for JSON/YAML output
  • Loading branch information
k8s-ci-robot authored Oct 28, 2019
2 parents d37cf58 + d79440b commit bc50b06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
20 changes: 12 additions & 8 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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]
}
27 changes: 14 additions & 13 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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]
}

0 comments on commit bc50b06

Please sign in to comment.