Skip to content

Commit 9c6a3c8

Browse files
authored
Merge pull request #1632 from saschagrunert/namespace-filter
crictl ps, inspect: allow pod namespace filtering
2 parents 918d10d + e60d59b commit 9c6a3c8

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

cmd/crictl/container.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ var containerStatusCommand = &cli.Command{
506506
Name: "name",
507507
Usage: "Filter by container name regular expression pattern",
508508
},
509+
&cli.StringFlag{
510+
Name: "namespace",
511+
Usage: "Filter by pod namespace regular expression pattern",
512+
},
509513
&cli.StringFlag{
510514
Name: "pod",
511515
Aliases: []string{"p"},
@@ -545,12 +549,13 @@ var containerStatusCommand = &cli.Command{
545549

546550
if len(ids) == 0 {
547551
opts := &listOptions{
548-
nameRegexp: c.String("name"),
549-
podID: c.String("pod"),
550-
image: c.String("image"),
551-
state: c.String("state"),
552-
latest: c.Bool("latest"),
553-
last: c.Int("last"),
552+
nameRegexp: c.String("name"),
553+
podID: c.String("pod"),
554+
podNamespaceRegexp: c.String("namespace"),
555+
image: c.String("image"),
556+
state: c.String("state"),
557+
latest: c.Bool("latest"),
558+
last: c.Int("last"),
554559
}
555560
opts.labels, err = parseLabelStringSlice(c.StringSlice("label"))
556561
if err != nil {
@@ -656,6 +661,10 @@ var listContainersCommand = &cli.Command{
656661
Aliases: []string{"r"},
657662
Usage: "Show image path instead of image id",
658663
},
664+
&cli.StringFlag{
665+
Name: "namespace",
666+
Usage: "Filter by pod namespace regular expression pattern",
667+
},
659668
},
660669
Action: func(c *cli.Context) error {
661670
if c.NArg() != 0 {
@@ -673,19 +682,20 @@ var listContainersCommand = &cli.Command{
673682
}
674683

675684
opts := &listOptions{
676-
id: c.String("id"),
677-
podID: c.String("pod"),
678-
state: c.String("state"),
679-
verbose: c.Bool("verbose"),
680-
quiet: c.Bool("quiet"),
681-
output: c.String("output"),
682-
all: c.Bool("all"),
683-
nameRegexp: c.String("name"),
684-
latest: c.Bool("latest"),
685-
last: c.Int("last"),
686-
noTrunc: c.Bool("no-trunc"),
687-
image: c.String("image"),
688-
resolveImagePath: c.Bool("resolve-image-path"),
685+
id: c.String("id"),
686+
podID: c.String("pod"),
687+
podNamespaceRegexp: c.String("namespace"),
688+
state: c.String("state"),
689+
verbose: c.Bool("verbose"),
690+
quiet: c.Bool("quiet"),
691+
output: c.String("output"),
692+
all: c.Bool("all"),
693+
nameRegexp: c.String("name"),
694+
latest: c.Bool("latest"),
695+
last: c.Int("last"),
696+
noTrunc: c.Bool("no-trunc"),
697+
image: c.String("image"),
698+
resolveImagePath: c.Bool("resolve-image-path"),
689699
}
690700
opts.labels, err = parseLabelStringSlice(c.StringSlice("label"))
691701
if err != nil {
@@ -1299,19 +1309,29 @@ func convertContainerState(state pb.ContainerState) string {
12991309
}
13001310
}
13011311

1302-
func getPodNameFromLabels(label map[string]string) string {
1303-
podName, ok := label[types.KubernetesPodNameLabel]
1312+
func getPodNameFromLabels(labels map[string]string) string {
1313+
return getFromLabels(labels, types.KubernetesPodNameLabel)
1314+
}
1315+
1316+
func getPodNamespaceFromLabels(labels map[string]string) string {
1317+
return getFromLabels(labels, types.KubernetesPodNamespaceLabel)
1318+
}
1319+
1320+
func getFromLabels(labels map[string]string, label string) string {
1321+
value, ok := labels[label]
13041322
if ok {
1305-
return podName
1323+
return value
13061324
}
13071325
return "unknown"
13081326
}
13091327

13101328
func getContainersList(containersList []*pb.Container, opts *listOptions) []*pb.Container {
13111329
filtered := []*pb.Container{}
13121330
for _, c := range containersList {
1331+
podNamespace := getPodNamespaceFromLabels(c.Labels)
13131332
// Filter by pod name/namespace regular expressions.
1314-
if matchesRegex(opts.nameRegexp, c.Metadata.Name) {
1333+
if matchesRegex(opts.nameRegexp, c.Metadata.Name) &&
1334+
matchesRegex(opts.podNamespaceRegexp, podNamespace) {
13151335
filtered = append(filtered, c)
13161336
}
13171337
}

0 commit comments

Comments
 (0)