Skip to content

Commit

Permalink
Merge pull request #1088 from cdoern/prune
Browse files Browse the repository at this point in the history
move prune filter parsing to common
  • Loading branch information
openshift-ci[bot] authored Jul 16, 2022
2 parents 2ac10fa + ee08c87 commit fc74878
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 @@ -159,7 +159,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 @@ -111,11 +112,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 fc74878

Please sign in to comment.