mirror of
https://github.com/containers/podman.git
synced 2025-06-22 01:48:54 +08:00
filter: add basic pattern matching for label keys
Following PR adds basic pattern matching to filter by labels for `keys`. Adds support for use-cases like `--filter label=some.prefix.com/key/*` where end-users want to match a pattern for keys as compared to exact value. Signed-off-by: Aditya Rajan <arajan@redhat.com>
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -94,6 +95,28 @@ func PrepareFilters(r *http.Request) (*map[string][]string, error) {
|
|||||||
return &filterMap, nil
|
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
|
// MatchLabelFilters matches labels and returns true if they are valid
|
||||||
func MatchLabelFilters(filterValues []string, labels map[string]string) bool {
|
func MatchLabelFilters(filterValues []string, labels map[string]string) bool {
|
||||||
outer:
|
outer:
|
||||||
@ -106,7 +129,7 @@ outer:
|
|||||||
filterValue = ""
|
filterValue = ""
|
||||||
}
|
}
|
||||||
for labelKey, labelValue := range labels {
|
for labelKey, labelValue := range labels {
|
||||||
if labelKey == filterKey && (filterValue == "" || labelValue == filterValue) {
|
if ((labelKey == filterKey) || matchPattern(filterKey, labelKey)) && (filterValue == "" || labelValue == filterValue) {
|
||||||
continue outer
|
continue outer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,17 @@ var _ = Describe("Podman volume ls", func() {
|
|||||||
Expect(len(session.OutputToStringArray())).To(Equal(2))
|
Expect(len(session.OutputToStringArray())).To(Equal(2))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman ls volume filter with a key pattern", func() {
|
||||||
|
session := podmanTest.Podman([]string{"volume", "create", "--label", "helloworld=world", "myvol2"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session).Should(Exit(0))
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"volume", "ls", "--filter", "label=hello*"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session).Should(Exit(0))
|
||||||
|
Expect(len(session.OutputToStringArray())).To(Equal(2))
|
||||||
|
})
|
||||||
|
|
||||||
It("podman ls volume with JSON format", func() {
|
It("podman ls volume with JSON format", func() {
|
||||||
session := podmanTest.Podman([]string{"volume", "create", "myvol"})
|
session := podmanTest.Podman([]string{"volume", "create", "myvol"})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
|
Reference in New Issue
Block a user