Skip to content

Commit

Permalink
move prune filter parsing to common
Browse files Browse the repository at this point in the history
there was some eerily similar code in c/common and in podman for
creating filter functions for various types. Move some of it here
and add support for the label!= filter in libimage and libnetwork that basically creates the inverse
function of label=

after this merges, will file a PR with the fix for containers within podman as well

there is already a label!= test in libimage/filters_test. Libimage somehow lets this syntax slide
even though it does not actually imact anything in podman

see containers/podman#14182

Signed-off-by: Charlie Doern <[email protected]>
  • Loading branch information
cdoern committed Jul 14, 2022
1 parent dd1c331 commit f08f316
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
13 changes: 9 additions & 4 deletions libimage/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ func (r *Runtime) compileImageFilters(ctx context.Context, options *ListImagesOp
filter = filterIntermediate(ctx, intermediate, t)

case "label":
filter = filterLabel(ctx, value)

filter = filterLabel(ctx, value, false)
case "label!":
filter = filterLabel(ctx, value, true)
case "readonly":
readOnly, err := r.bool(duplicate, key, value)
if err != nil {
Expand Down Expand Up @@ -309,13 +310,17 @@ func filterReferences(value string) filterFunc {
}

// filterLabel creates a label for matching the specified value.
func filterLabel(ctx context.Context, value string) filterFunc {
func filterLabel(ctx context.Context, value string, inverse bool) filterFunc {
return func(img *Image) (bool, error) {
labels, err := img.Labels(ctx)
if err != nil {
return false, err
}
return filtersPkg.MatchLabelFilters([]string{value}, labels), nil
val := filtersPkg.MatchLabelFilters([]string{value}, labels)
if inverse {
return !val, nil
}
return val, nil
}
}

Expand Down
5 changes: 4 additions & 1 deletion libnetwork/util/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func createPruneFilterFuncs(key string, filterValues []string) (types.FilterFunc
return func(net types.Network) bool {
return filters.MatchLabelFilters(filterValues, net.Labels)
}, nil

case "label!":
return func(net types.Network) bool {
return !filters.MatchLabelFilters(filterValues, net.Labels)
}, nil
case "until":
until, err := filters.ComputeUntilTimestamp(filterValues)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -112,11 +113,24 @@ outer:
filterValue = ""
}
for labelKey, labelValue := range labels {
if labelKey == filterKey && (filterValue == "" || labelValue == filterValue) {
continue outer
if filterValue == "" || labelValue == filterValue {
if labelKey == filterKey || matchPattern(filterKey, labelKey) {
continue outer
}
}
}
return false
}
return true
}

func matchPattern(pattern string, value string) bool {
if strings.Contains(pattern, "*") {
filter := fmt.Sprintf("*%s*", pattern)
filter = strings.ReplaceAll(filter, string(filepath.Separator), "|")
newName := strings.ReplaceAll(value, string(filepath.Separator), "|")
match, _ := filepath.Match(filter, newName)
return match
}
return false
}

0 comments on commit f08f316

Please sign in to comment.