-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
filter.go
231 lines (200 loc) · 6.15 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package filter
import (
"fmt"
"strings"
"github.com/gobwas/glob"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
)
// Filter holds filter configuration
type Filter struct {
ExcludeAll bool // highest priority
// include filters take precedence
includeNames sets.String
includeGlobs []glob.Glob
rawIncludeGlobs []string
excludeNames sets.String
excludeGlobs []glob.Glob
rawExcludeGlobs []string
}
// NewFilter returns a new initialized Filter
func NewFilter() Filter {
return Filter{
ExcludeAll: false,
includeNames: sets.NewString(),
excludeNames: sets.NewString(),
}
}
// AppendIncludeNames appends explicit names to the include filter
func (f *Filter) AppendIncludeNames(names ...string) { f.includeNames.Insert(names...) }
// AppendExcludeGlobs sets globs for exclusion rules
func (f *Filter) AppendExcludeGlobs(globExprs ...string) error {
for _, expr := range globExprs {
compiledExpr, err := glob.Compile(expr)
if err != nil {
return errors.Wrapf(err, "parsing glob filter %q", expr)
}
f.excludeGlobs = append(f.excludeGlobs, compiledExpr)
f.rawExcludeGlobs = append(f.rawExcludeGlobs, expr)
}
return nil // exclude filter doesn't have to match anything, so we don't validate it
}
// AppendExcludeNames appends explicit names to the exclude filter
func (f *Filter) AppendExcludeNames(names ...string) { f.excludeNames.Insert(names...) }
func (*Filter) matchGlobs(name string, exprs []glob.Glob) bool {
for _, compiledExpr := range exprs {
if compiledExpr.Match(name) {
return true
}
}
return false
}
// hasIncludeRules returns true if the user has supplied inclusion globs or names
func (f *Filter) hasIncludeRules() bool {
return len(f.includeGlobs) != 0
}
func (f *Filter) describeIncludeRules() string {
rules := append(f.includeNames.List(), f.rawIncludeGlobs...)
return strings.Join(rules, ",")
}
// hasExcludeRules returns true if the user has supplied exclusion globs or names
func (f *Filter) hasExcludeRules() bool {
return len(f.excludeGlobs) != 0
}
func (f *Filter) describeExcludeRules() string {
rules := append(f.excludeNames.List(), f.rawExcludeGlobs...)
return strings.Join(rules, ",")
}
// Match given name against the filter and returns
// true or false if it has to be included or excluded
func (f *Filter) Match(name string) bool {
if f.ExcludeAll {
return false // force exclude
}
// Name overwrites
if f.includeNames.Has(name) && !f.excludeNames.Has(name) {
return true
}
if f.excludeNames.Has(name) {
return false
}
hasIncludeRules := f.hasIncludeRules()
hasExcludeRules := f.hasExcludeRules()
if !hasIncludeRules && !hasExcludeRules {
return true
}
// Exclusion override takes precedence
if f.excludeNames.Has(name) {
return false
}
if hasIncludeRules {
if f.matchGlobs(name, f.includeGlobs) {
if hasExcludeRules {
// exclusion takes precedence
if f.matchGlobs(name, f.excludeGlobs) {
return false
}
}
return true
}
// if there are include rules and it doesn't match then it must be excluded regardless of the exclude rules
return false
}
// With only exclusion rules, everything that is not excluded is included
if hasExcludeRules {
// Overwrites by name take precedence
if f.excludeNames.Has(name) {
return false
}
if f.matchGlobs(name, f.excludeGlobs) {
return false
}
return true
}
return false
}
// doMatchAll all names against the filter and return two sets of names - included and excluded
func (f *Filter) doMatchAll(names []string) (sets.String, sets.String) {
included, excluded := sets.NewString(), sets.NewString()
if f.ExcludeAll {
for _, n := range names {
excluded.Insert(n)
}
return included, excluded
}
for _, n := range names {
if f.Match(n) {
included.Insert(n)
} else {
excluded.Insert(n)
}
}
return included, excluded
}
// doAppendIncludeGlobs sets globs for inclusion rules
func (f *Filter) doAppendIncludeGlobs(names []string, resource string, globExprs ...string) error {
for _, expr := range globExprs {
compiledExpr, err := glob.Compile(expr)
if err != nil {
return errors.Wrapf(err, "parsing glob filter %q", expr)
}
f.includeGlobs = append(f.includeGlobs, compiledExpr)
f.rawIncludeGlobs = append(f.rawIncludeGlobs, expr)
}
return f.includeGlobsMatchAnything(names, resource)
}
func (f *Filter) doSetExcludeExistingFilter(names []string, resource string) error {
uniqueNames := sets.NewString(names...).List()
f.excludeNames.Insert(uniqueNames...)
for _, n := range uniqueNames {
isAlsoIncluded := f.includeNames.Has(n) || f.matchGlobs(n, f.includeGlobs)
if isAlsoIncluded {
return fmt.Errorf("existing %s %q should be excluded, but matches include filter: %s", resource, n, f.describeIncludeRules())
}
}
if len(uniqueNames) != 0 {
logger.Info("%d existing %s(s) (%s) will be excluded", len(uniqueNames), resource, strings.Join(uniqueNames, ","))
}
return nil
}
func (f *Filter) includeGlobsMatchAnything(names []string, resource string) error {
if len(f.includeGlobs) == 0 {
return nil
}
for _, n := range names {
if f.matchGlobs(n, f.includeGlobs) {
return nil
}
}
return fmt.Errorf("no %ss match include glob filter specification: %q", resource, strings.Join(f.rawIncludeGlobs, ","))
}
func (f *Filter) doLogInfo(resource string, included, excluded sets.String) {
logMsg := func(subset sets.String, status string) {
count := subset.Len()
list := strings.Join(subset.List(), ", ")
subjectFmt := "%d %ss (%s) were %s"
if count == 1 {
subjectFmt = "%d %s (%s) was %s"
}
logger.Info(subjectFmt, count, resource, list, status+" (based on the include/exclude rules)")
}
if f.hasIncludeRules() {
logger.Info("combined include rules: %s", f.describeIncludeRules())
if included.Len() == 0 {
logger.Info("no %ss present in the current set were included by the filter", resource)
}
}
if included.Len() > 0 {
logMsg(included, "included")
}
if f.hasExcludeRules() {
logger.Info("combined exclude rules: %s", f.describeExcludeRules())
if excluded.Len() == 0 {
logger.Info("no %ss present in the current set were excluded by the filter", resource)
}
}
if excluded.Len() > 0 {
logMsg(excluded, "excluded")
}
}