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

filters: add basic pattern matching for label keys i.e --filter label=<pattern> #12295

Merged
Merged
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion pkg/util/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -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
}

Comment on lines +112 to +119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it need to handle only *?

Perhaps you could use filepath.Match?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think that would be much better. otherwise you might end up with more regex failures.

Copy link
Collaborator Author

@flouthoc flouthoc Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of it first but we want to match label keys where filepath.Match does not works for cases like. Filepath is very restricted to matching valid path. However my idea was to match for patterns.

This fails with filepath.Match and few more cases.

fmt.Println(filepath.Match("/home/c*", "/home/catch/foo"))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does Docker do it?

If Docker doesn't do it, could we just use a regex here?

Copy link
Collaborator Author

@flouthoc flouthoc Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think docker does it. This was a new feature request. Afaik people mostly use pipe and grep with docker for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree that a regex is better - it's definitely more complicated but attempting to treat labels as a path seems very fragile?

Besides, Podman does absolutely no standardization of label formats, so there's neither guarantee nor obligation for labels to include slashes as separators.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@giuseppe I think regex like regexp.MatchString("hello", key) would match all keys starting with hello so --filter label= becomes hard to use. But I'll try it out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My problem with regex is almost no human beings understand it and it has a lot more symbols.
People understand globing , well at least () a lot better then (.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker has a very small documentation but it only talks about * in examples https://docs.docker.com/engine/reference/commandline/images/#filtering . We could do a regex but lot of tests would need to be readjusted.

https://kliushnikov.medium.com/filtering-docker-images-5eb5aee358df

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm found a fix also confirmed with @giuseppe I'll amend the PR.

// MatchLabelFilters matches labels and returns true if they are valid
func MatchLabelFilters(filterValues []string, labels map[string]string) bool {
outer:
Expand All @@ -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
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/volume_ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ var _ = Describe("Podman volume ls", func() {
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() {
session := podmanTest.Podman([]string{"volume", "create", "myvol"})
session.WaitWithDefaultTimeout()
Expand Down