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 libnetwork only. Libimage already supports it.

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

see containers/podman#14182

Signed-off-by: Charlie Doern <[email protected]>
  • Loading branch information
cdoern committed Jul 15, 2022
1 parent dd1c331 commit ee08c87
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
1 change: 0 additions & 1 deletion libimage/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func (r *Runtime) compileImageFilters(ctx context.Context, options *ListImagesOp

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

case "readonly":
readOnly, err := r.bool(duplicate, key, value)
if err != 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
47 changes: 47 additions & 0 deletions libnetwork/util/filters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package util

import "testing"

func TestGenerateFilterFunc(t *testing.T) {
testValues := []string{
"",
"test",
"",
}
type args struct {
keys []string
labels []string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Match when all filters the same as labels",
args: args{
keys: []string{"label", "label", "label"},
labels: testValues,
},
want: true,
},
{
name: "Match with inverse",
args: args{
keys: []string{"label", "label", "label!"},
labels: testValues,
},
want: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
for _, entry := range tt.args.keys {
if _, err := createFilterFuncs(entry, tt.args.labels); err != nil {
t.Errorf("createPruneFilterFuncs() failed on %s with entry %s: %s", tt.name, entry, err.Error())
}
}
})
}
}
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 ee08c87

Please sign in to comment.