forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
100 lines (87 loc) · 2.99 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
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconciler
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// AnnotationFilterFunc creates a FilterFunc only accepting objects with given annotation key and value
func AnnotationFilterFunc(key string, value string, allowUnset bool) func(interface{}) bool {
return func(obj interface{}) bool {
if mo, ok := obj.(metav1.Object); ok {
return mapHasOrDefault(mo.GetAnnotations(), key, value, allowUnset)
}
return false
}
}
// LabelExistsFilterFunc creates a FilterFunc only accepting objects which have a given label.
func LabelExistsFilterFunc(label string) func(obj interface{}) bool {
return func(obj interface{}) bool {
if mo, ok := obj.(metav1.Object); ok {
labels := mo.GetLabels()
_, ok := labels[label]
return ok
}
return false
}
}
// LabelFilterFunc creates a FilterFunc only accepting objects where a label is set to a specific value.
func LabelFilterFunc(label string, value string, allowUnset bool) func(interface{}) bool {
return func(obj interface{}) bool {
if mo, ok := obj.(metav1.Object); ok {
return mapHasOrDefault(mo.GetLabels(), label, value, allowUnset)
}
return false
}
}
// NameFilterFunc creates a FilterFunc only accepting objects with the given name.
func NameFilterFunc(name string) func(interface{}) bool {
return func(obj interface{}) bool {
if mo, ok := obj.(metav1.Object); ok {
return mo.GetName() == name
}
return false
}
}
// NamespaceFilterFunc creates a FilterFunc only accepting objects in the given namespace.
func NamespaceFilterFunc(namespace string) func(interface{}) bool {
return func(obj interface{}) bool {
if mo, ok := obj.(metav1.Object); ok {
return mo.GetNamespace() == namespace
}
return false
}
}
// Not inverts the result of the predicate.
func Not(f func(interface{}) bool) func(interface{}) bool {
return func(obj interface{}) bool {
return !f(obj)
}
}
// ChainFilterFuncs creates a FilterFunc which performs an AND of the passed FilterFuncs.
func ChainFilterFuncs(funcs ...func(interface{}) bool) func(interface{}) bool {
return func(obj interface{}) bool {
for _, f := range funcs {
if !f(obj) {
return false
}
}
return true
}
}
// mapHasOrDefault returns true if the map has the key and its value is equal to value.
// If the key is not found, it returns defaultValue.
func mapHasOrDefault(m map[string]string, key string, value string, defaultValue bool) bool {
val, ok := m[key]
if !ok {
return defaultValue
}
return val == value
}