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

Allow filtering containers by command #24791

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 53 additions & 1 deletion cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ func setupImageEngine(cmd *cobra.Command) (entities.ImageEngine, error) {
}

func getContainers(cmd *cobra.Command, toComplete string, cType completeType, statuses ...string) ([]string, cobra.ShellCompDirective) {
var listContainers []entities.ListContainer
suggestions := []string{}
listOpts := entities.ContainerListOptions{
Filters: make(map[string][]string),
@@ -109,7 +110,20 @@ func getContainers(cmd *cobra.Command, toComplete string, cType completeType, st
return nil, cobra.ShellCompDirectiveNoFileComp
}

for _, c := range containers {
listContainers = append(listContainers, containers...)

// Add containers from the external storage into complete list
if ok, _ := cmd.Flags().GetBool("external"); ok {
externalContainers, err := engine.ContainerListExternal(registry.Context())
if err != nil {
cobra.CompErrorln(err.Error())
return nil, cobra.ShellCompDirectiveNoFileComp
}

listContainers = append(listContainers, externalContainers...)
}

for _, c := range listContainers {
// include ids in suggestions if cType == completeIDs or
// more then 2 chars are typed and cType == completeDefault
if ((len(toComplete) > 1 && cType == completeDefault) ||
@@ -341,6 +355,43 @@ func getArtifacts(cmd *cobra.Command, toComplete string) ([]string, cobra.ShellC
return suggestions, cobra.ShellCompDirectiveNoFileComp
}

func getCommands(cmd *cobra.Command, toComplete string) ([]string, cobra.ShellCompDirective) {
suggestions := []string{}
lsOpts := entities.ContainerListOptions{}

engine, err := setupContainerEngine(cmd)
if err != nil {
cobra.CompErrorln(err.Error())
return nil, cobra.ShellCompDirectiveNoFileComp
}

containers, err := engine.ContainerList(registry.Context(), lsOpts)
if err != nil {
cobra.CompErrorln(err.Error())
return nil, cobra.ShellCompDirectiveNoFileComp
}

externalContainers, err := engine.ContainerListExternal(registry.Context())
if err != nil {
cobra.CompErrorln(err.Error())
return nil, cobra.ShellCompDirectiveNoFileComp
}
containers = append(containers, externalContainers...)

for _, container := range containers {
// taking of the first element of commands list is done intentionally
// to exclude command arguments from suggestions (e.g. exclude arguments "-g daemon"
// from "nginx -g daemon" output)
if len(container.Command) > 0 {
if strings.HasPrefix(container.Command[0], toComplete) {
suggestions = append(suggestions, container.Command[0])
}
}
}

return suggestions, cobra.ShellCompDirectiveNoFileComp
}

func fdIsNotDir(f *os.File) bool {
stat, err := f.Stat()
if err != nil {
@@ -1703,6 +1754,7 @@ func AutocompletePsFilters(cmd *cobra.Command, args []string, toComplete string)
kv := keyValueCompletion{
"ancestor=": func(s string) ([]string, cobra.ShellCompDirective) { return getImages(cmd, s) },
"before=": func(s string) ([]string, cobra.ShellCompDirective) { return getContainers(cmd, s, completeDefault) },
"command=": func(s string) ([]string, cobra.ShellCompDirective) { return getCommands(cmd, s) },
"exited=": nil,
"health=": func(_ string) ([]string, cobra.ShellCompDirective) {
return []string{define.HealthCheckHealthy,
1 change: 1 addition & 0 deletions docs/source/markdown/podman-pause.1.md.in
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] Containers created before the given duration or time. |
| command | [Command] the command the container is executing, only argv[0] is taken |

@@option latest

2 changes: 1 addition & 1 deletion docs/source/markdown/podman-ps.1.md
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] container created before the given duration or time. |

| command | [Command] the command the container is executing, only argv[0] is taken |

#### **--format**=*format*

1 change: 1 addition & 0 deletions docs/source/markdown/podman-restart.1.md.in
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] Containers created before the given duration or time. |
| command | [Command] the command the container is executing, only argv[0] is taken |

@@option latest

1 change: 1 addition & 0 deletions docs/source/markdown/podman-rm.1.md.in
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] Containers created before the given duration or time. |
| command | [Command] the command the container is executing, only argv[0] is taken |

#### **--force**, **-f**

1 change: 1 addition & 0 deletions docs/source/markdown/podman-start.1.md.in
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] Containers created before the given duration or time. |
| command | [Command] the command the container is executing, only argv[0] is taken |

@@option interactive

1 change: 1 addition & 0 deletions docs/source/markdown/podman-stop.1.md.in
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] Containers created before the given duration or time. |
| command | [Command] the command the container is executing, only argv[0] is taken |

@@option ignore

1 change: 1 addition & 0 deletions docs/source/markdown/podman-unpause.1.md.in
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] Containers created before the given duration or time. |
| command | [Command] the command the container is executing, only argv[0] is taken |

@@option latest

16 changes: 13 additions & 3 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
@@ -1246,11 +1246,21 @@ func (r *Runtime) GetContainers(loadState bool, filters ...ContainerFilter) ([]*
return nil, err
}

ctrsFiltered := make([]*Container, 0, len(ctrs))
ctrsFiltered := applyContainersFilters(ctrs, filters...)

for _, ctr := range ctrs {
return ctrsFiltered, nil
}

// Applies container filters on bunch of containers
func applyContainersFilters(containers []*Container, filters ...ContainerFilter) []*Container {
ctrsFiltered := make([]*Container, 0, len(containers))

for _, ctr := range containers {
include := true
for _, filter := range filters {
if filter == nil {
continue
}
include = include && filter(ctr)
}

@@ -1259,7 +1269,7 @@ func (r *Runtime) GetContainers(loadState bool, filters ...ContainerFilter) ([]*
}
}

return ctrsFiltered, nil
return ctrsFiltered
}

// GetAllContainers is a helper function for GetContainers
5 changes: 5 additions & 0 deletions pkg/domain/entities/container_ps.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,11 @@ import (
"github.com/containers/podman/v5/pkg/domain/entities/types"
)

// ExternalContainerFilter is a function to determine whether a container list is included
// in command output. Container lists to be outputted are tested using the function.
// A true return will include the container list, a false return will exclude it.
type ExternalContainerFilter func(*ListContainer) bool

// ListContainer describes a container suitable for listing
type ListContainer = types.ListContainer

Loading