-
Notifications
You must be signed in to change notification settings - Fork 216
/
clustermatcher.go
66 lines (55 loc) · 1.81 KB
/
clustermatcher.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
package matcher
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type criteria func(clusterName, clusterGroup string, clusterGroupLabels, clusterLabels map[string]string) bool
type ClusterMatcher struct {
criteria []criteria
}
func toSelector(labels *metav1.LabelSelector) (labels.Selector, error) {
return metav1.LabelSelectorAsSelector(labels)
}
func NewClusterMatcher(clusterName, clusterGroup string, clusterGroupSelector *metav1.LabelSelector, clusterSelector *metav1.LabelSelector) (*ClusterMatcher, error) {
t := &ClusterMatcher{}
if clusterName != "" {
t.criteria = append(t.criteria, func(clusterNameTest, _ string, _, _ map[string]string) bool {
return clusterName == clusterNameTest
})
}
if clusterGroup != "" {
t.criteria = append(t.criteria, func(_, clusterGroupTest string, _, _ map[string]string) bool {
return clusterGroup == clusterGroupTest
})
}
if clusterGroupSelector != nil {
selector, err := toSelector(clusterGroupSelector)
if err != nil {
return nil, err
}
t.criteria = append(t.criteria, func(_, _ string, clusterGroupLabels, _ map[string]string) bool {
return selector.Matches(labels.Set(clusterGroupLabels))
})
}
if clusterSelector != nil {
selector, err := toSelector(clusterSelector)
if err != nil {
return nil, err
}
t.criteria = append(t.criteria, func(_, _ string, _, clusterLabels map[string]string) bool {
return selector.Matches(labels.Set(clusterLabels))
})
}
return t, nil
}
func (t *ClusterMatcher) Match(clusterName, clusterGroup string, clusterGroupLabels, clusterLabels map[string]string) bool {
if len(t.criteria) == 0 {
return false
}
for _, criteria := range t.criteria {
if !criteria(clusterName, clusterGroup, clusterGroupLabels, clusterLabels) {
return false
}
}
return true
}