...
1
2 package match
3
4 import (
5 "strings"
6
7 "k8s.io/apimachinery/pkg/runtime/schema"
8 "sigs.k8s.io/controller-runtime/pkg/client"
9 )
10
11
12
13
14
15 type Matcher interface {
16 Match(o client.Object) bool
17 }
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 func Object(o client.Object, m Matcher) bool {
37 if m == nil || o == nil {
38 return false
39 }
40 return m.Match(o)
41 }
42
43
44 type Everything struct{}
45
46 func (e Everything) Match(_ client.Object) bool {
47 return true
48 }
49
50
51
52 type All []Matcher
53
54 func (a All) Match(o client.Object) bool {
55 if len(a) == 0 {
56 return false
57 }
58 for _, m := range a {
59 if m != nil && !m.Match(o) {
60 return false
61 }
62 }
63 return true
64 }
65
66
67
68 type Any []Matcher
69
70 func (a Any) Match(o client.Object) bool {
71 for _, m := range a {
72 if m != nil && m.Match(o) {
73 return true
74 }
75 }
76 return false
77 }
78
79
80
81 type AnyInMetadata map[string]string
82
83 func (a AnyInMetadata) Match(o client.Object) bool {
84 labels, annos := o.GetLabels(), o.GetAnnotations()
85 for k, v := range a {
86 if strings.EqualFold(labels[k], v) || strings.EqualFold(annos[k], v) {
87 return true
88 }
89 }
90 return false
91 }
92
93
94
95 type Labels map[string]string
96
97 func (l Labels) Match(o client.Object) bool {
98 if len(l) == 0 {
99 return false
100 }
101 labels := o.GetLabels()
102 for k, v := range l {
103 if !strings.EqualFold(labels[k], v) {
104 return false
105 }
106 }
107 return true
108 }
109
110
111
112 type Annotations map[string]string
113
114 func (a Annotations) Match(o client.Object) bool {
115 if len(a) == 0 {
116 return false
117 }
118 annos := o.GetAnnotations()
119 for k, v := range a {
120 if !strings.EqualFold(annos[k], v) {
121 return false
122 }
123 }
124 return true
125 }
126
127
128 type Kind struct{ Kind string }
129
130 func (k Kind) Match(o client.Object) bool {
131 return o.GetObjectKind().GroupVersionKind().Kind == k.Kind
132 }
133
134
135 type GroupVersion schema.GroupVersion
136
137 func (gv GroupVersion) Match(o client.Object) bool {
138 actual := o.GetObjectKind().GroupVersionKind().GroupVersion()
139 return gv.Group == actual.Group && gv.Version == actual.Version
140 }
141
142
143 type Name struct{ Name string }
144
145 func (n Name) Match(o client.Object) bool {
146 return o.GetName() == n.Name
147 }
148
149
150 type Namespace struct{ Namespace string }
151
152 func (n Namespace) Match(o client.Object) bool {
153 return o.GetNamespace() == n.Namespace
154 }
155
View as plain text