Skip to content

Commit 0d2e1dc

Browse files
authored
refactor: [NPM] General translation logic (mainly clean-up codes and correct bugs) (#1105)
* Delete unneeded codes for podSelector and update UTs * Delete unneeded codes for nameSpaceSelector and UTs * Delete unneeded codes in parseSelector * Use nil slices instead of zero slices for TranslatedIPSet from namespaceSelector * Use Variadic functions in NewTranslatedIPSet to use nil slice instead of empty slice and update UTs accordingly * Use right settype for all-namespaces * no export for flattenNameSpaceSelector function in parseSelector (vamsi's comments)
1 parent 6835b86 commit 0d2e1dc

File tree

5 files changed

+79
-1528
lines changed

5 files changed

+79
-1528
lines changed

npm/pkg/controlplane/translation/parseSelector.go

Lines changed: 3 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,9 @@ import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
)
99

10-
// ParseLabel takes a Azure-NPM processed label then returns if it's referring to complement set,
11-
// and if so, returns the original set as well.
12-
func ParseLabel(label string) (string, bool) {
13-
// The input label is guaranteed to have a non-zero length validated by k8s.
14-
// For label definition, see below parseSelector() function.
15-
if label[0:1] == util.IptablesNotFlag {
16-
return label[1:], true
17-
}
18-
return label, false
19-
}
20-
21-
// GetOperatorAndLabel returns the operator associated with the label and the label without operator.
22-
func GetOperatorAndLabel(labelWithOp string) (op, label string) {
23-
// TODO(jungukcho): check whether this is possible
24-
if labelWithOp == "" {
25-
return op, label
26-
}
27-
28-
// in case "!"" Operaror do not exist
29-
if string(labelWithOp[0]) != util.IptablesNotFlag {
30-
label = labelWithOp
31-
return op, label
32-
}
33-
34-
// in case "!"" Operaror exists
35-
op, label = util.IptablesNotFlag, labelWithOp[1:]
36-
return op, label
37-
}
38-
39-
// GetOperatorsAndLabels returns the operators along with the associated labels.
40-
func GetOperatorsAndLabels(labelsWithOps []string) (ops, labelsWithoutOps []string) {
41-
ops = make([]string, len(labelsWithOps))
42-
labelsWithoutOps = make([]string, len(labelsWithOps))
43-
44-
for i, labelWithOp := range labelsWithOps {
45-
op, labelWithoutOp := GetOperatorAndLabel(labelWithOp)
46-
ops[i] = op
47-
labelsWithoutOps[i] = labelWithoutOp
48-
}
49-
return ops, labelsWithoutOps
50-
}
51-
52-
// getSetNameForMultiValueSelector takes in label with multiple values without operator
53-
// and returns a new 2nd level ipset name
54-
func getSetNameForMultiValueSelector(key string, vals []string) string {
55-
newIPSet := key
56-
for _, val := range vals {
57-
newIPSet = util.GetIpSetFromLabelKV(newIPSet, val)
58-
}
59-
return newIPSet
60-
}
61-
62-
// FlattenNameSpaceSelector will help flatten multiple nameSpace selector match Expressions values
10+
// flattenNameSpaceSelector will help flatten multiple nameSpace selector match Expressions values
6311
// into multiple label selectors helping with the OR condition.
64-
func FlattenNameSpaceSelector(nsSelector *metav1.LabelSelector) []metav1.LabelSelector {
12+
func flattenNameSpaceSelector(nsSelector *metav1.LabelSelector) []metav1.LabelSelector {
6513
/*
6614
This function helps to create multiple labelSelectors when given a single multivalue nsSelector
6715
Take below example: this nsSelector has 2 values in a matchSelector.
@@ -179,66 +127,6 @@ func zipMatchExprs(baseSelectors []metav1.LabelSelector, matchExpr metav1.LabelS
179127
return zippedLabelSelectors
180128
}
181129

182-
// parseSelector takes a LabelSelector and returns a slice of processed labels, Lists with members as values.
183-
// this function returns a slice of all the label ipsets excluding multivalue matchExprs
184-
// and a map of labelKeys and labelIpsetname for multivalue match exprs
185-
// higher level functions will need to compute what sets or ipsets should be
186-
// used from this map
187-
func parseSelector(selector *metav1.LabelSelector) (labels []string, vals map[string][]string) {
188-
// TODO(jungukcho): check return values
189-
// labels []string and []string{}
190-
if selector == nil {
191-
return labels, vals
192-
}
193-
194-
labels = []string{}
195-
vals = make(map[string][]string)
196-
if len(selector.MatchLabels) == 0 && len(selector.MatchExpressions) == 0 {
197-
labels = append(labels, "")
198-
return labels, vals
199-
}
200-
201-
sortedKeys, sortedVals := util.SortMap(&selector.MatchLabels)
202-
for i := range sortedKeys {
203-
labels = append(labels, sortedKeys[i]+":"+sortedVals[i])
204-
}
205-
206-
for _, req := range selector.MatchExpressions {
207-
var k string
208-
switch op := req.Operator; op {
209-
case metav1.LabelSelectorOpIn:
210-
k = req.Key
211-
if len(req.Values) == 1 {
212-
labels = append(labels, k+":"+req.Values[0])
213-
} else {
214-
// We are not adding the k:v to labels for multiple values, because, labels are used
215-
// to construct partial IptEntries and if these below labels are added then we are inducing
216-
// AND condition on values of a match expression instead of OR
217-
vals[k] = append(vals[k], req.Values...)
218-
}
219-
case metav1.LabelSelectorOpNotIn:
220-
k = util.IptablesNotFlag + req.Key
221-
if len(req.Values) == 1 {
222-
labels = append(labels, k+":"+req.Values[0])
223-
} else {
224-
vals[k] = append(vals[k], req.Values...)
225-
}
226-
// Exists matches pods with req.Key as key
227-
case metav1.LabelSelectorOpExists:
228-
k = req.Key
229-
labels = append(labels, k)
230-
// DoesNotExist matches pods without req.Key as key
231-
case metav1.LabelSelectorOpDoesNotExist:
232-
k = util.IptablesNotFlag + req.Key
233-
labels = append(labels, k)
234-
default:
235-
log.Errorf("Invalid operator [%s] for selector [%v] requirement", op, *selector)
236-
}
237-
}
238-
239-
return labels, vals
240-
}
241-
242130
// labelSelector has parsed matchLabels and MatchExpressions information.
243131
type labelSelector struct {
244132
// include is a flag to indicate whether Op exists or not.
@@ -297,7 +185,7 @@ func (ps *parsedSelectors) addSelector(include bool, setType ipsets.SetType, set
297185
// parseNSSelector parses namespaceSelector and returns slice of labelSelector object
298186
// which includes operator, setType, ipset name and always nil members slice.
299187
// Member slices is always nil since parseNSSelector function is called
300-
// after FlattenNameSpaceSelector function is called, which guarantees
188+
// after flattenNameSpaceSelector function is called, which guarantees
301189
// there is no matchExpression with multiple values.
302190
// TODO: good to remove this dependency later if possible.
303191
func parseNSSelector(selector *metav1.LabelSelector) []labelSelector {

0 commit comments

Comments
 (0)