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

move prune filter parsing to common #1088

Merged
merged 1 commit into from
Jul 16, 2022
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
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
}