-
Notifications
You must be signed in to change notification settings - Fork 114
/
match_label_selector.go
323 lines (272 loc) · 10.6 KB
/
match_label_selector.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* // Copyright (c) 2017 Cisco and/or its affiliates.
* //
* // 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 processor
import (
nsmodel "github.com/contiv/vpp/plugins/ksr/model/namespace"
podmodel "github.com/contiv/vpp/plugins/ksr/model/pod"
policymodel "github.com/contiv/vpp/plugins/ksr/model/policy"
"github.com/contiv/vpp/plugins/policy/utils"
)
const (
in = policymodel.Policy_LabelSelector_LabelExpression_IN
notIn = policymodel.Policy_LabelSelector_LabelExpression_NOT_IN
exists = policymodel.Policy_LabelSelector_LabelExpression_EXISTS
doesNotExist = policymodel.Policy_LabelSelector_LabelExpression_DOES_NOT_EXIST
)
// isPodLabelSelectorMatch finds if podLabelSelecor matches Pod's labels or not
func (pp *PolicyProcessor) isPodLabelSelectorMatch(
pod *podmodel.Pod,
matchPodLabels []*policymodel.Policy_Label,
matchPodExpressions []*policymodel.Policy_LabelSelector_LabelExpression,
policyNamespace string) bool {
// if we have both pod label selectors and expressions,
// results are AND'ed and both functions must return true. If only one label selector exists
// then the calling function must be true
if len(matchPodLabels) > 0 && len(matchPodExpressions) > 0 {
if pp.isPodLabelMatch(pod, matchPodLabels, policyNamespace) &&
pp.isPodExpressionMatch(pod, matchPodExpressions, policyNamespace) {
return true
}
return false
} else if len(matchPodLabels) == 0 && len(matchPodExpressions) > 0 {
if pp.isPodExpressionMatch(pod, matchPodExpressions, policyNamespace) {
return true
}
return false
} else if len(matchPodLabels) > 0 && len(matchPodExpressions) == 0 {
if pp.isPodLabelMatch(pod, matchPodLabels, policyNamespace) {
return true
}
return false
}
// Empty label selector (both podLabels and podExpressions)
// returns true as it matches all the resources
return true
}
// isPodLabelMatch finds if pod labels match a collection of pod label selectors.
func (pp *PolicyProcessor) isPodLabelMatch(pod *podmodel.Pod,
matchPodLabels []*policymodel.Policy_Label, policyNamespace string) bool {
podNamespace := pod.Namespace
podLabels := pod.Label
// create a map with current pod's labels
existMap := make(map[string]bool)
labelPrefix := policyNamespace + "/"
for _, podLabel := range podLabels {
labelExistsKey := podNamespace + podLabel.Key + "/" + podLabel.Value
existMap[labelExistsKey] = true
}
return isMatchLabel(matchPodLabels, existMap, labelPrefix)
}
// isPodExpressionMatch finds if pod labels match a collection of pod selector expressions.
func (pp *PolicyProcessor) isPodExpressionMatch(pod *podmodel.Pod,
matchPodExpressions []*policymodel.Policy_LabelSelector_LabelExpression, policyNamespace string) bool {
podNamespace := pod.Namespace
podLabels := pod.Label
// create a map with current pod's labels
existMap := make(map[string]bool)
labelPrefix := policyNamespace + "/"
for _, podLabel := range podLabels {
labelExistsKey := podNamespace + "/" + podLabel.Key + "/" + podLabel.Value
keyExistsKey := podNamespace + "/" + podLabel.Key
existMap[keyExistsKey] = true
existMap[labelExistsKey] = true
}
return isMatchExpression(matchPodExpressions, existMap, labelPrefix)
}
// isNsLabelSelectorMatch finds if namespace LabelSelector matches Pod's namespace labels.
func (pp *PolicyProcessor) isNsLabelSelectorMatch(
pod *podmodel.Pod,
matchNsLabels []*policymodel.Policy_Label,
matchNsExpressions []*policymodel.Policy_LabelSelector_LabelExpression) bool {
// if we have both namespace label selectors and expressions,
// results are AND'ed and both functions must return true. If only one label selector exists
// then the calling function must be true
if len(matchNsLabels) > 0 && len(matchNsExpressions) > 0 {
if !pp.isNsLabelMatch(pod, matchNsLabels) ||
!pp.isNsExpressionMatch(pod, matchNsExpressions) {
return false
}
return true
} else if len(matchNsLabels) == 0 && len(matchNsExpressions) > 0 {
if !pp.isNsExpressionMatch(pod, matchNsExpressions) {
return false
}
return true
} else if len(matchNsLabels) > 0 && len(matchNsExpressions) == 0 {
if !pp.isNsLabelMatch(pod, matchNsLabels) {
return false
}
return true
}
// Empty label selector (both podLabels and podExpressions)
// returns true as it matches all the resources
return true
}
// isNsLabelMatch finds if pod's namespace labels match a collection of namespace selector labels.
func (pp *PolicyProcessor) isNsLabelMatch(pod *podmodel.Pod,
matchNsLabels []*policymodel.Policy_Label) bool {
// Get Pod's namespace labels
podNamespaceID := nsmodel.ID(pod.Namespace)
_, nsData := pp.Cache.LookupNamespace(podNamespaceID)
nsLabels := nsData.Label
// create a map with current namespace's labels
existMap := make(map[string]bool)
labelPrefix := ""
for _, nsLabel := range nsLabels {
labelExistsKey := nsLabel.Key + "/" + nsLabel.Value
existMap[labelExistsKey] = true
}
return isMatchLabel(matchNsLabels, existMap, labelPrefix)
}
// isNsExpressionMatch finds if pod's namespace labels match a collection of namespace selector expressions.
func (pp *PolicyProcessor) isNsExpressionMatch(pod *podmodel.Pod,
matchNsExpressions []*policymodel.Policy_LabelSelector_LabelExpression) bool {
//Get Pod's namespace labels
podNamespaceID := nsmodel.ID(pod.Namespace)
_, nsData := pp.Cache.LookupNamespace(podNamespaceID)
nsLabels := nsData.Label
// create a map with current pod's labels
existMap := make(map[string]bool)
labelPrefix := ""
for _, nsLabel := range nsLabels {
labelExistsKey := nsLabel.Key + "/" + nsLabel.Value
keyExistsKey := nsLabel.Key
existMap[labelExistsKey] = true
existMap[keyExistsKey] = true
}
return isMatchExpression(matchNsExpressions, existMap, labelPrefix)
}
// isNsUpdateLabelSelectorMatch finds if pod's namespace labels match a collection of namespace selector labels,
// after an update in namespace labels
func (pp *PolicyProcessor) isNsUpdateLabelSelectorMatch(
ns *nsmodel.Namespace,
matchNsUpdateLabels []*policymodel.Policy_Label,
matchNsUpdateExpressions []*policymodel.Policy_LabelSelector_LabelExpression) bool {
// if we have both namespace label selectors and expressions,
// results are AND'ed and both functions must return true. If only one label selector exists
// then the calling function must be true
if len(matchNsUpdateLabels) > 0 && len(matchNsUpdateExpressions) > 0 {
if !pp.isNsUpdateLabelMatch(ns, matchNsUpdateLabels) ||
!pp.isNsUpdateExpressionMatch(ns, matchNsUpdateExpressions) {
return false
}
return true
} else if len(matchNsUpdateLabels) == 0 && len(matchNsUpdateExpressions) > 0 {
if !pp.isNsUpdateExpressionMatch(ns, matchNsUpdateExpressions) {
return false
}
return true
} else if len(matchNsUpdateLabels) > 0 && len(matchNsUpdateExpressions) == 0 {
if !pp.isNsUpdateLabelMatch(ns, matchNsUpdateLabels) {
return false
}
return true
}
// Empty label selector (both podLabels and podExpressions)
// returns true as it matches all the resources
return true
}
// isNsUpdateLabelMatch finds if namespace LabelSelector matches Pod's namespace labels,
// after an update in namespace labels
func (pp *PolicyProcessor) isNsUpdateLabelMatch(ns *nsmodel.Namespace,
matchNsUpdateLabels []*policymodel.Policy_Label) bool {
//Get Pod's namespace labels
_, nsData := pp.Cache.LookupNamespace(nsmodel.GetID(ns))
nsLabels := nsData.Label
// create a map with current namespace's labels
existMap := make(map[string]bool)
labelPrefix := ""
for _, nsLabel := range nsLabels {
labelExistsKey := nsLabel.Key + "/" + nsLabel.Value
existMap[labelExistsKey] = true
}
return isMatchLabel(matchNsUpdateLabels, existMap, labelPrefix)
}
// isNsUpdateExpressionMatch finds if pod's namespace labels match a collection of namespace selector expressions,
// after an update in namespace labels
func (pp *PolicyProcessor) isNsUpdateExpressionMatch(ns *nsmodel.Namespace,
matchNsUpdateExpressions []*policymodel.Policy_LabelSelector_LabelExpression) bool {
//Get Pod's namespace labels
_, nsData := pp.Cache.LookupNamespace(nsmodel.GetID(ns))
nsLabels := nsData.Label
// create a map with current pod's labels
existMap := make(map[string]bool)
labelPrefix := ""
for _, nsLabel := range nsLabels {
labelExistsKey := nsLabel.Key + "/" + nsLabel.Value
keyExistsKey := nsLabel.Key
existMap[labelExistsKey] = true
existMap[keyExistsKey] = true
}
return isMatchExpression(matchNsUpdateExpressions, existMap, labelPrefix)
}
// isMatchLabel checks if labels are matching a given map of existing labels that we compare against
func isMatchLabel(matchLabels []*policymodel.Policy_Label, labelExists map[string]bool, matchLabelPrefix string) bool {
isMatch := true
for _, matchLabel := range matchLabels {
labelExistsKey := matchLabelPrefix + matchLabel.Key + "/" + matchLabel.Value
if labelExists[labelExistsKey] != true {
isMatch = false
break
}
}
return isMatch
}
// isMatchExpression checks if expressions are matching a given map of existing expressions that we compare against
func isMatchExpression(matchExpressions []*policymodel.Policy_LabelSelector_LabelExpression,
existMap map[string]bool, expressionKeyPrefix string) bool {
isMatch := false
for _, matchExpression := range matchExpressions {
switch matchExpression.Operator {
case in:
labels := utils.ConstructLabels(matchExpression.Key, matchExpression.Value)
for _, label := range labels {
expressionKey := expressionKeyPrefix + label.Key + label.Value
if existMap[expressionKey] == true {
isMatch = true
break
} else {
isMatch = false
}
}
if !isMatch {
return false
}
case notIn:
labels := utils.ConstructLabels(matchExpression.Key, matchExpression.Value)
for _, label := range labels {
expressionKey := expressionKeyPrefix + label.Key + label.Value
if existMap[expressionKey] == true {
return false
}
}
isMatch = true
case exists:
expressionKey := expressionKeyPrefix + matchExpression.Key
if existMap[expressionKey] == false {
return false
}
isMatch = true
case doesNotExist:
expressionKey := expressionKeyPrefix + matchExpression.Key
if existMap[expressionKey] == true {
return false
}
isMatch = true
}
}
return isMatch
}