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

crictl: add option to filter by container name in ps #404

Merged
merged 1 commit into from
Nov 8, 2018
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
5 changes: 3 additions & 2 deletions cmd/crictl/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
const (
// This labels are copied from kubelet directly, and may change
// without warning in the future.
kubePodNameLabel = "io.kubernetes.pod.name"
kubePodNamespaceLabel = "io.kubernetes.pod.namespace"
kubePodNameLabel = "io.kubernetes.pod.name"
kubePodNamespaceLabel = "io.kubernetes.pod.namespace"
kubeContainerNameLabel = "io.kubernetes.container.name"
)
30 changes: 20 additions & 10 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ var listContainersCommand = cli.Command{
Value: "",
Usage: "Filter by container id",
},
cli.StringFlag{
Name: "name",
Value: "",
Usage: "filter by container name regular expression pattern",
},
cli.StringFlag{
Name: "pod, p",
Value: "",
Expand Down Expand Up @@ -310,16 +315,17 @@ var listContainersCommand = cli.Command{
}

opts := listOptions{
id: context.String("id"),
podID: context.String("pod"),
state: context.String("state"),
verbose: context.Bool("verbose"),
quiet: context.Bool("quiet"),
output: context.String("output"),
all: context.Bool("all"),
latest: context.Bool("latest"),
last: context.Int("last"),
noTrunc: context.Bool("no-trunc"),
id: context.String("id"),
podID: context.String("pod"),
state: context.String("state"),
verbose: context.Bool("verbose"),
quiet: context.Bool("quiet"),
output: context.String("output"),
all: context.Bool("all"),
nameRegexp: context.String("name"),
latest: context.Bool("latest"),
last: context.Int("last"),
noTrunc: context.Bool("no-trunc"),
}
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
if err != nil {
Expand Down Expand Up @@ -617,6 +623,10 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
fmt.Fprintln(w, "CONTAINER ID\tIMAGE\tCREATED\tSTATE\tNAME\tATTEMPT\tPOD ID")
}
for _, c := range r.Containers {
// Filter by pod name/namespace regular expressions.
if !matchesRegex(opts.nameRegexp, c.Labels[kubeContainerNameLabel]) {
continue
}
if opts.quiet {
fmt.Printf("%s\n", c.Id)
continue
Expand Down
19 changes: 3 additions & 16 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"log"
"os"
"regexp"
"sort"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -227,7 +226,7 @@ var listPodCommand = cli.Command{
latest: context.Bool("latest"),
last: context.Int("last"),
noTrunc: context.Bool("no-trunc"),
podNameRegexp: context.String("name"),
nameRegexp: context.String("name"),
podNamespaceRegexp: context.String("namespace"),
}
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
Expand Down Expand Up @@ -383,18 +382,6 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID, output string, quiet b
return nil
}

func podMatchesRegex(pattern, target string) bool {
if pattern == "" {
return true
}
matched, err := regexp.MatchString(pattern, target)
if err != nil {
// Assume it's not a match if an error occurs.
return false
}
return matched
}

// ListPodSandboxes sends a ListPodSandboxRequest to the server, and parses
// the returned ListPodSandboxResponse.
func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
Expand Down Expand Up @@ -447,10 +434,10 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
}
for _, pod := range r.Items {
// Filter by pod name/namespace regular expressions.
if !podMatchesRegex(opts.podNameRegexp, pod.Labels[kubePodNameLabel]) {
if !matchesRegex(opts.nameRegexp, pod.Labels[kubePodNameLabel]) {
continue
}
if !podMatchesRegex(opts.podNamespaceRegexp, pod.Labels[kubePodNamespaceLabel]) {
if !matchesRegex(opts.podNamespaceRegexp, pod.Labels[kubePodNamespaceLabel]) {
continue
}

Expand Down
17 changes: 15 additions & 2 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"reflect"
"regexp"
"sort"
"strings"

Expand All @@ -48,8 +49,8 @@ type listOptions struct {
id string
// podID of container
podID string
// Regular expression pattern to match pod name
podNameRegexp string
// Regular expression pattern to match pod or container
nameRegexp string
// Regular expression pattern to match the pod namespace
podNamespaceRegexp string
// state of the sandbox
Expand Down Expand Up @@ -308,3 +309,15 @@ func getTruncatedID(id, prefix string) string {
}
return id
}

func matchesRegex(pattern, target string) bool {
if pattern == "" {
return true
}
matched, err := regexp.MatchString(pattern, target)
if err != nil {
// Assume it's not a match if an error occurs.
return false
}
return matched
}
12 changes: 6 additions & 6 deletions cmd/crictl/sandbox_test.go → cmd/crictl/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"testing"
)

func TestPodFilterByRegex(t *testing.T) {
func TestNameFilterByRegex(t *testing.T) {
testCases := []struct {
desc string
pattern string
podString string
isMatch bool
desc string
pattern string
name string
isMatch bool
}{
{
"exact name should match",
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestPodFilterByRegex(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
r := podMatchesRegex(tc.pattern, tc.podString)
r := matchesRegex(tc.pattern, tc.name)
if r != tc.isMatch {
t.Errorf("expected matched to be %v; actual result is %v", tc.isMatch, r)
}
Expand Down