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

Conversation

flouthoc
Copy link
Collaborator

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.

Closes: #12199

@flouthoc flouthoc force-pushed the filter-label-pattern branch from ca61170 to 877fe45 Compare November 15, 2021 11:31
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 <[email protected]>
@flouthoc flouthoc force-pushed the filter-label-pattern branch from 877fe45 to c050f05 Compare November 15, 2021 12:20
Comment on lines +112 to +119
func matchPattern(pattern string, value string) bool {
if strings.Contains(pattern, "*") {
result, _ := regexp.MatchString(wildCardToRegexp(pattern), value)
return result
}
return false
}

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.

@flouthoc flouthoc requested review from giuseppe and rhatdan November 15, 2021 15:43
@rhatdan
Copy link
Member

rhatdan commented Nov 18, 2021

/lgtm
/approve

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Nov 18, 2021
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Nov 18, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: flouthoc, rhatdan

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 18, 2021
@openshift-merge-robot openshift-merge-robot merged commit 3242931 into containers:main Nov 18, 2021
@flouthoc
Copy link
Collaborator Author

@rhatdan I wasn't able to amend the PR yet. I still wanted to make the change but no issues i am creating a follow up PR.

@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Sep 22, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFE] Support for wildcard in volume ls --filter label=key
5 participants