aboutsummaryrefslogtreecommitdiff
path: root/pkg/util
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-11-18 22:03:13 +0100
committerGitHub <noreply@github.com>2021-11-18 22:03:13 +0100
commit3242931c6267274aa696291cc693998884447389 (patch)
treea9f62de4be50d1863d30fa686eb46e7d6baf5304 /pkg/util
parent82a050a58f2ef2cf940c2ef49e1f8c4b17ab8beb (diff)
parentc050f05ccff6416ccd41d271f0051b395cbe5a1f (diff)
downloadpodman-3242931c6267274aa696291cc693998884447389.tar.gz
podman-3242931c6267274aa696291cc693998884447389.tar.bz2
podman-3242931c6267274aa696291cc693998884447389.zip
Merge pull request #12295 from flouthoc/filter-label-pattern
filters: add basic pattern matching for label keys i.e `--filter label=<pattern>`
Diffstat (limited to 'pkg/util')
-rw-r--r--pkg/util/filters.go25
1 files changed, 24 insertions, 1 deletions
diff --git a/pkg/util/filters.go b/pkg/util/filters.go
index e252c1ddf..5af868873 100644
--- a/pkg/util/filters.go
+++ b/pkg/util/filters.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
+ "regexp"
"strings"
"time"
@@ -94,6 +95,28 @@ func PrepareFilters(r *http.Request) (*map[string][]string, error) {
return &filterMap, nil
}
+func wildCardToRegexp(pattern string) string {
+ var result strings.Builder
+ for i, literal := range strings.Split(pattern, "*") {
+ // Replace * with .*
+ if i > 0 {
+ result.WriteString(".*")
+ }
+ // Quote any regular expression meta characters in the
+ // literal text.
+ result.WriteString(regexp.QuoteMeta(literal))
+ }
+ return result.String()
+}
+
+func matchPattern(pattern string, value string) bool {
+ if strings.Contains(pattern, "*") {
+ result, _ := regexp.MatchString(wildCardToRegexp(pattern), value)
+ return result
+ }
+ return false
+}
+
// MatchLabelFilters matches labels and returns true if they are valid
func MatchLabelFilters(filterValues []string, labels map[string]string) bool {
outer:
@@ -106,7 +129,7 @@ outer:
filterValue = ""
}
for labelKey, labelValue := range labels {
- if labelKey == filterKey && (filterValue == "" || labelValue == filterValue) {
+ if ((labelKey == filterKey) || matchPattern(filterKey, labelKey)) && (filterValue == "" || labelValue == filterValue) {
continue outer
}
}