Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pod and container name filter for JSON/YAML output #550

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
}