-
Notifications
You must be signed in to change notification settings - Fork 53
/
selection.go
74 lines (70 loc) · 1.68 KB
/
selection.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
package storage
import corev1 "github.com/rancher/opni/pkg/apis/core/v1"
type SelectorPredicate[T corev1.IdLabelReader] func(T) bool
func NewSelectorPredicate[T corev1.IdLabelReader](s *corev1.ClusterSelector) SelectorPredicate[T] {
emptyLabelSelector := s.LabelSelector.IsEmpty()
if emptyLabelSelector && len(s.ClusterIDs) == 0 {
switch {
case s.MatchOptions&corev1.MatchOptions_EmptySelectorMatchesNone != 0:
return func(T) bool { return false }
default:
return func(T) bool { return true }
}
}
idSet := map[string]struct{}{}
for _, id := range s.ClusterIDs {
idSet[id] = struct{}{}
}
return func(c T) bool {
id := c.GetId()
if _, ok := idSet[id]; ok {
return true
}
if emptyLabelSelector {
return false
}
return labelSelectorMatches(s.LabelSelector, c.GetLabels())
}
}
func labelSelectorMatches(selector *corev1.LabelSelector, labels map[string]string) bool {
for key, value := range selector.MatchLabels {
if labels[key] != value {
return false
}
}
for _, req := range selector.MatchExpressions {
switch corev1.LabelSelectorOperator(req.Operator) {
case corev1.LabelSelectorOpIn:
ok := false
for _, value := range req.Values {
if labels[req.Key] == value {
ok = true
break
}
}
if !ok {
return false
}
case corev1.LabelSelectorOpNotIn:
v, ok := labels[req.Key]
if !ok {
return false
}
for _, value := range req.Values {
if v == value {
return false
}
}
return true
case corev1.LabelSelectorOpExists:
if _, ok := labels[req.Key]; !ok {
return false
}
case corev1.LabelSelectorOpDoesNotExist:
if _, ok := labels[req.Key]; ok {
return false
}
}
}
return true
}