diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-11-18 18:55:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-18 18:55:05 +0100 |
commit | 61a82159dbb154b2044595b156dbdb9fac04bedd (patch) | |
tree | 55a9809a4eb587344f80e702eab7b4b5609995ac /pkg | |
parent | dcd498a6885f0293934214af0c6fc2d3c7717bd5 (diff) | |
parent | 4f427a89cbc0814c25dd4b562ddf4ff4e568a005 (diff) | |
download | podman-61a82159dbb154b2044595b156dbdb9fac04bedd.tar.gz podman-61a82159dbb154b2044595b156dbdb9fac04bedd.tar.bz2 podman-61a82159dbb154b2044595b156dbdb9fac04bedd.zip |
Merge pull request #8376 from Luap99/podman-filters
Align the podman ps --filter behavior with docker
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/api/handlers/compat/containers_prune.go | 14 | ||||
-rw-r--r-- | pkg/domain/infra/abi/containers.go | 12 | ||||
-rw-r--r-- | pkg/ps/ps.go | 16 | ||||
-rw-r--r-- | pkg/util/utils.go | 12 |
4 files changed, 30 insertions, 24 deletions
diff --git a/pkg/api/handlers/compat/containers_prune.go b/pkg/api/handlers/compat/containers_prune.go index 397feac9a..2cfeebcce 100644 --- a/pkg/api/handlers/compat/containers_prune.go +++ b/pkg/api/handlers/compat/containers_prune.go @@ -16,7 +16,6 @@ func PruneContainers(w http.ResponseWriter, r *http.Request) { var ( delContainers []string space int64 - filterFuncs []libpod.ContainerFilter ) runtime := r.Context().Value("runtime").(*libpod.Runtime) decoder := r.Context().Value("decoder").(*schema.Decoder) @@ -28,15 +27,14 @@ func PruneContainers(w http.ResponseWriter, r *http.Request) { utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) return } + filterFuncs := make([]libpod.ContainerFilter, 0, len(query.Filters)) for k, v := range query.Filters { - for _, val := range v { - generatedFunc, err := lpfilters.GenerateContainerFilterFuncs(k, val, runtime) - if err != nil { - utils.InternalServerError(w, err) - return - } - filterFuncs = append(filterFuncs, generatedFunc) + generatedFunc, err := lpfilters.GenerateContainerFilterFuncs(k, v, runtime) + if err != nil { + utils.InternalServerError(w, err) + return } + filterFuncs = append(filterFuncs, generatedFunc) } // Libpod response differs diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go index 855f9ece8..4b69ac74e 100644 --- a/pkg/domain/infra/abi/containers.go +++ b/pkg/domain/infra/abi/containers.go @@ -205,15 +205,13 @@ func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []strin } func (ic *ContainerEngine) ContainerPrune(ctx context.Context, options entities.ContainerPruneOptions) (*entities.ContainerPruneReport, error) { - var filterFuncs []libpod.ContainerFilter + filterFuncs := make([]libpod.ContainerFilter, 0, len(options.Filters)) for k, v := range options.Filters { - for _, val := range v { - generatedFunc, err := lpfilters.GenerateContainerFilterFuncs(k, val, ic.Libpod) - if err != nil { - return nil, err - } - filterFuncs = append(filterFuncs, generatedFunc) + generatedFunc, err := lpfilters.GenerateContainerFilterFuncs(k, v, ic.Libpod) + if err != nil { + return nil, err } + filterFuncs = append(filterFuncs, generatedFunc) } return ic.pruneContainersHelper(filterFuncs) } diff --git a/pkg/ps/ps.go b/pkg/ps/ps.go index 96b2d754f..3dd7eb0c6 100644 --- a/pkg/ps/ps.go +++ b/pkg/ps/ps.go @@ -21,19 +21,17 @@ import ( func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOptions) ([]entities.ListContainer, error) { var ( - filterFuncs []libpod.ContainerFilter - pss = []entities.ListContainer{} + pss = []entities.ListContainer{} ) + filterFuncs := make([]libpod.ContainerFilter, 0, len(options.Filters)) all := options.All || options.Last > 0 if len(options.Filters) > 0 { for k, v := range options.Filters { - for _, val := range v { - generatedFunc, err := lpfilters.GenerateContainerFilterFuncs(k, val, runtime) - if err != nil { - return nil, err - } - filterFuncs = append(filterFuncs, generatedFunc) + generatedFunc, err := lpfilters.GenerateContainerFilterFuncs(k, v, runtime) + if err != nil { + return nil, err } + filterFuncs = append(filterFuncs, generatedFunc) } } @@ -43,7 +41,7 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp all = true } if !all { - runningOnly, err := lpfilters.GenerateContainerFilterFuncs("status", define.ContainerStateRunning.String(), runtime) + runningOnly, err := lpfilters.GenerateContainerFilterFuncs("status", []string{define.ContainerStateRunning.String()}, runtime) if err != nil { return nil, err } diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 415fd169b..f6a084c00 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -6,6 +6,7 @@ import ( "os" "os/user" "path/filepath" + "regexp" "strconv" "strings" "sync" @@ -84,6 +85,17 @@ func StringInSlice(s string, sl []string) bool { return false } +// StringMatchRegexSlice determines if a given string matches one of the given regexes, returns bool +func StringMatchRegexSlice(s string, re []string) bool { + for _, r := range re { + m, err := regexp.MatchString(r, s) + if err == nil && m { + return true + } + } + return false +} + // ImageConfig is a wrapper around the OCIv1 Image Configuration struct exported // by containers/image, but containing additional fields that are not supported // by OCIv1 (but are by Docker v2) - notably OnBuild. |