Skip to content

Commit 04f3427

Browse files
committed
crictl: add option to filter by container name in ps
Signed-off-by: Antonio Murdaca <[email protected]>
1 parent c616b23 commit 04f3427

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

cmd/crictl/constants.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
const (
2020
// This labels are copied from kubelet directly, and may change
2121
// without warning in the future.
22-
kubePodNameLabel = "io.kubernetes.pod.name"
23-
kubePodNamespaceLabel = "io.kubernetes.pod.namespace"
22+
kubePodNameLabel = "io.kubernetes.pod.name"
23+
kubePodNamespaceLabel = "io.kubernetes.pod.namespace"
24+
kubeContainerNameLabel = "io.kubernetes.container.name"
2425
)

cmd/crictl/container.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ var listContainersCommand = cli.Command{
264264
Value: "",
265265
Usage: "Filter by container id",
266266
},
267+
cli.StringFlag{
268+
Name: "name",
269+
Value: "",
270+
Usage: "filter by container name regular expression pattern",
271+
},
267272
cli.StringFlag{
268273
Name: "pod, p",
269274
Value: "",
@@ -310,16 +315,17 @@ var listContainersCommand = cli.Command{
310315
}
311316

312317
opts := listOptions{
313-
id: context.String("id"),
314-
podID: context.String("pod"),
315-
state: context.String("state"),
316-
verbose: context.Bool("verbose"),
317-
quiet: context.Bool("quiet"),
318-
output: context.String("output"),
319-
all: context.Bool("all"),
320-
latest: context.Bool("latest"),
321-
last: context.Int("last"),
322-
noTrunc: context.Bool("no-trunc"),
318+
id: context.String("id"),
319+
podID: context.String("pod"),
320+
state: context.String("state"),
321+
verbose: context.Bool("verbose"),
322+
quiet: context.Bool("quiet"),
323+
output: context.String("output"),
324+
all: context.Bool("all"),
325+
nameRegexp: context.String("name"),
326+
latest: context.Bool("latest"),
327+
last: context.Int("last"),
328+
noTrunc: context.Bool("no-trunc"),
323329
}
324330
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
325331
if err != nil {
@@ -617,6 +623,10 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
617623
fmt.Fprintln(w, "CONTAINER ID\tIMAGE\tCREATED\tSTATE\tNAME\tATTEMPT\tPOD ID")
618624
}
619625
for _, c := range r.Containers {
626+
// Filter by pod name/namespace regular expressions.
627+
if !matchesRegex(opts.nameRegexp, c.Labels[kubeContainerNameLabel]) {
628+
continue
629+
}
620630
if opts.quiet {
621631
fmt.Printf("%s\n", c.Id)
622632
continue

cmd/crictl/sandbox.go

+3-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"log"
2323
"os"
24-
"regexp"
2524
"sort"
2625
"strings"
2726
"text/tabwriter"
@@ -227,7 +226,7 @@ var listPodCommand = cli.Command{
227226
latest: context.Bool("latest"),
228227
last: context.Int("last"),
229228
noTrunc: context.Bool("no-trunc"),
230-
podNameRegexp: context.String("name"),
229+
nameRegexp: context.String("name"),
231230
podNamespaceRegexp: context.String("namespace"),
232231
}
233232
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
@@ -383,18 +382,6 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID, output string, quiet b
383382
return nil
384383
}
385384

386-
func podMatchesRegex(pattern, target string) bool {
387-
if pattern == "" {
388-
return true
389-
}
390-
matched, err := regexp.MatchString(pattern, target)
391-
if err != nil {
392-
// Assume it's not a match if an error occurs.
393-
return false
394-
}
395-
return matched
396-
}
397-
398385
// ListPodSandboxes sends a ListPodSandboxRequest to the server, and parses
399386
// the returned ListPodSandboxResponse.
400387
func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
@@ -447,10 +434,10 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
447434
}
448435
for _, pod := range r.Items {
449436
// Filter by pod name/namespace regular expressions.
450-
if !podMatchesRegex(opts.podNameRegexp, pod.Labels[kubePodNameLabel]) {
437+
if !matchesRegex(opts.nameRegexp, pod.Labels[kubePodNameLabel]) {
451438
continue
452439
}
453-
if !podMatchesRegex(opts.podNamespaceRegexp, pod.Labels[kubePodNamespaceLabel]) {
440+
if !matchesRegex(opts.podNamespaceRegexp, pod.Labels[kubePodNamespaceLabel]) {
454441
continue
455442
}
456443

cmd/crictl/util.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"reflect"
25+
"regexp"
2526
"sort"
2627
"strings"
2728

@@ -48,8 +49,8 @@ type listOptions struct {
4849
id string
4950
// podID of container
5051
podID string
51-
// Regular expression pattern to match pod name
52-
podNameRegexp string
52+
// Regular expression pattern to match pod or container
53+
nameRegexp string
5354
// Regular expression pattern to match the pod namespace
5455
podNamespaceRegexp string
5556
// state of the sandbox
@@ -308,3 +309,15 @@ func getTruncatedID(id, prefix string) string {
308309
}
309310
return id
310311
}
312+
313+
func matchesRegex(pattern, target string) bool {
314+
if pattern == "" {
315+
return true
316+
}
317+
matched, err := regexp.MatchString(pattern, target)
318+
if err != nil {
319+
// Assume it's not a match if an error occurs.
320+
return false
321+
}
322+
return matched
323+
}

cmd/crictl/sandbox_test.go cmd/crictl/util_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
"testing"
2121
)
2222

23-
func TestPodFilterByRegex(t *testing.T) {
23+
func TestNameFilterByRegex(t *testing.T) {
2424
testCases := []struct {
25-
desc string
26-
pattern string
27-
podString string
28-
isMatch bool
25+
desc string
26+
pattern string
27+
name string
28+
isMatch bool
2929
}{
3030
{
3131
"exact name should match",
@@ -60,7 +60,7 @@ func TestPodFilterByRegex(t *testing.T) {
6060
}
6161
for _, tc := range testCases {
6262
t.Run(tc.desc, func(t *testing.T) {
63-
r := podMatchesRegex(tc.pattern, tc.podString)
63+
r := matchesRegex(tc.pattern, tc.name)
6464
if r != tc.isMatch {
6565
t.Errorf("expected matched to be %v; actual result is %v", tc.isMatch, r)
6666
}

0 commit comments

Comments
 (0)