Skip to content

Commit 021d0f2

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

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
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

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ var listPodCommand = cli.Command{
227227
latest: context.Bool("latest"),
228228
last: context.Int("last"),
229229
noTrunc: context.Bool("no-trunc"),
230-
podNameRegexp: context.String("name"),
230+
nameRegexp: context.String("name"),
231231
podNamespaceRegexp: context.String("namespace"),
232232
}
233233
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
@@ -383,7 +383,7 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID, output string, quiet b
383383
return nil
384384
}
385385

386-
func podMatchesRegex(pattern, target string) bool {
386+
func matchesRegex(pattern, target string) bool {
387387
if pattern == "" {
388388
return true
389389
}
@@ -447,10 +447,10 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
447447
}
448448
for _, pod := range r.Items {
449449
// Filter by pod name/namespace regular expressions.
450-
if !podMatchesRegex(opts.podNameRegexp, pod.Labels[kubePodNameLabel]) {
450+
if !matchesRegex(opts.nameRegexp, pod.Labels[kubePodNameLabel]) {
451451
continue
452452
}
453-
if !podMatchesRegex(opts.podNamespaceRegexp, pod.Labels[kubePodNamespaceLabel]) {
453+
if !matchesRegex(opts.podNamespaceRegexp, pod.Labels[kubePodNamespaceLabel]) {
454454
continue
455455
}
456456

cmd/crictl/util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ type listOptions struct {
4848
id string
4949
// podID of container
5050
podID string
51-
// Regular expression pattern to match pod name
52-
podNameRegexp string
51+
// Regular expression pattern to match pod or container
52+
nameRegexp string
5353
// Regular expression pattern to match the pod namespace
5454
podNamespaceRegexp string
5555
// state of the sandbox

0 commit comments

Comments
 (0)