Skip to content

Commit

Permalink
Merge pull request #1632 from saschagrunert/namespace-filter
Browse files Browse the repository at this point in the history
crictl ps, inspect: allow pod namespace filtering
  • Loading branch information
k8s-ci-robot authored Oct 14, 2024
2 parents 918d10d + e60d59b commit 9c6a3c8
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ var containerStatusCommand = &cli.Command{
Name: "name",
Usage: "Filter by container name regular expression pattern",
},
&cli.StringFlag{
Name: "namespace",
Usage: "Filter by pod namespace regular expression pattern",
},
&cli.StringFlag{
Name: "pod",
Aliases: []string{"p"},
Expand Down Expand Up @@ -545,12 +549,13 @@ var containerStatusCommand = &cli.Command{

if len(ids) == 0 {
opts := &listOptions{
nameRegexp: c.String("name"),
podID: c.String("pod"),
image: c.String("image"),
state: c.String("state"),
latest: c.Bool("latest"),
last: c.Int("last"),
nameRegexp: c.String("name"),
podID: c.String("pod"),
podNamespaceRegexp: c.String("namespace"),
image: c.String("image"),
state: c.String("state"),
latest: c.Bool("latest"),
last: c.Int("last"),
}
opts.labels, err = parseLabelStringSlice(c.StringSlice("label"))
if err != nil {
Expand Down Expand Up @@ -656,6 +661,10 @@ var listContainersCommand = &cli.Command{
Aliases: []string{"r"},
Usage: "Show image path instead of image id",
},
&cli.StringFlag{
Name: "namespace",
Usage: "Filter by pod namespace regular expression pattern",
},
},
Action: func(c *cli.Context) error {
if c.NArg() != 0 {
Expand All @@ -673,19 +682,20 @@ var listContainersCommand = &cli.Command{
}

opts := &listOptions{
id: c.String("id"),
podID: c.String("pod"),
state: c.String("state"),
verbose: c.Bool("verbose"),
quiet: c.Bool("quiet"),
output: c.String("output"),
all: c.Bool("all"),
nameRegexp: c.String("name"),
latest: c.Bool("latest"),
last: c.Int("last"),
noTrunc: c.Bool("no-trunc"),
image: c.String("image"),
resolveImagePath: c.Bool("resolve-image-path"),
id: c.String("id"),
podID: c.String("pod"),
podNamespaceRegexp: c.String("namespace"),
state: c.String("state"),
verbose: c.Bool("verbose"),
quiet: c.Bool("quiet"),
output: c.String("output"),
all: c.Bool("all"),
nameRegexp: c.String("name"),
latest: c.Bool("latest"),
last: c.Int("last"),
noTrunc: c.Bool("no-trunc"),
image: c.String("image"),
resolveImagePath: c.Bool("resolve-image-path"),
}
opts.labels, err = parseLabelStringSlice(c.StringSlice("label"))
if err != nil {
Expand Down Expand Up @@ -1299,19 +1309,29 @@ func convertContainerState(state pb.ContainerState) string {
}
}

func getPodNameFromLabels(label map[string]string) string {
podName, ok := label[types.KubernetesPodNameLabel]
func getPodNameFromLabels(labels map[string]string) string {
return getFromLabels(labels, types.KubernetesPodNameLabel)
}

func getPodNamespaceFromLabels(labels map[string]string) string {
return getFromLabels(labels, types.KubernetesPodNamespaceLabel)
}

func getFromLabels(labels map[string]string, label string) string {
value, ok := labels[label]
if ok {
return podName
return value
}
return "unknown"
}

func getContainersList(containersList []*pb.Container, opts *listOptions) []*pb.Container {
filtered := []*pb.Container{}
for _, c := range containersList {
podNamespace := getPodNamespaceFromLabels(c.Labels)
// Filter by pod name/namespace regular expressions.
if matchesRegex(opts.nameRegexp, c.Metadata.Name) {
if matchesRegex(opts.nameRegexp, c.Metadata.Name) &&
matchesRegex(opts.podNamespaceRegexp, podNamespace) {
filtered = append(filtered, c)
}
}
Expand Down

0 comments on commit 9c6a3c8

Please sign in to comment.