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 ps, inspect: allow pod namespace filtering #1632

Merged
merged 1 commit into from
Oct 14, 2024
Merged
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
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
Loading