Add support for namespace label selectors in K8s policies#566
Add support for namespace label selectors in K8s policies#566jmedved merged 3 commits intocontiv:masterfrom
Conversation
| namespaceLabelSelector *policymodel.Policy_LabelSelector) (pods []podmodel.ID) { | ||
| // An empty namespace selector matches all namespaces. | ||
| if len(namespaceLabelSelector.MatchExpression) == 0 && len(namespaceLabelSelector.MatchLabel) == 0 { | ||
| allPods := pc.configuredPods.ListAll() |
There was a problem hiding this comment.
check that namespaceLabelSelector is not nil first; if it is (possible if no label selectors are present in the policy), the check for MatchExpression will panic.
…go & plugins/policy/processor/processor.go
|
Commit 8ad1e7e |
| namespaceLabelSelector *policymodel.Policy_LabelSelector) (pods []podmodel.ID) { | ||
| // An empty namespace selector matches all namespaces. | ||
| if len(namespaceLabelSelector.MatchExpression) == 0 && len(namespaceLabelSelector.MatchLabel) == 0 { | ||
| allPods := pc.configuredPods.ListAll() |
| matchLabels := namespaceLabelSelector.MatchLabel | ||
|
|
||
| found, namespaceSelectorPods := pc.getPodsByLabelSelector(matchLabels) | ||
| if !found { |
There was a problem hiding this comment.
I think this is not required, if not found it namespaceSelectorPods is empty slice anyway
|
|
||
| // GetPodsByNSLabelSelector returns the pods that match a collection of Label Selectors in the same namespace | ||
| func (pc *PolicyCache) getPodsByNSLabelSelector(namespace string, labels []*policymodel.Policy_Label) (bool, []string) { | ||
| newPodSet := []string{} |
There was a problem hiding this comment.
this change doesn't look necessary, but it's fine
| tmp := utils.Intersect(prevPodSet, newPodSet) | ||
| tmp := utils.Intersect(prevNamespaceSet, newNamespaceSet) | ||
| if len(tmp) == 0 { | ||
| return false, nil |
There was a problem hiding this comment.
Here i would return empty string slice instead
| if len(pods) == 0 { | ||
| return false, []string{} | ||
| } | ||
| return true, pods |
There was a problem hiding this comment.
I noticed this file having a lot return Bool, Slice usage, is it really necessary ? an empty slice should be enough for later range operations, the only usage difference when return false is the 2nd return value is a clear empty slice, while at that time the pods is already ensured to be a empty slice, so whats the point of doing it here ?
| prevPodSet = newPodSet | ||
| newPodSet = tmp | ||
| prevNamespaceSet = newNamespaceSet | ||
| newNamespaceSet = tmp |
There was a problem hiding this comment.
this for loop is trying to get the intersect of all labels selector's result, it's a little hard to understand
current = pre = setA
for i := 1; i < len(labels); i++ {
pre = current
setB = query()
current = utils.Intersect(pre, setB)
}
for _, ns : = range current
pods = append(pods, xxx)
}
In this way you don't have to treat the 1 label case separately
| // Get all namespaces that match namespace label selector | ||
| namespaces := pp.Cache.LookupNamespacesByLabelSelector(label) | ||
| if len(namespaces) == 0 { | ||
| return isMatch |
| // Check if matched namespaces include pod's namespace | ||
| for _, namespace := range namespaces { | ||
| if namespace == podNamespace { | ||
| namespaceExists = true |
This PR addresses comment suggestions of contiv#566. - Refactor code for match expression and match label files to return only []string - Simplified getPodsByNSLabelSelector & getPodsByLabelSelector in match_label.go to be easily readable - Added missing comments for function description and more robust comments in code - Fixed two bugs in processor.go (missing "!" and worng function call)
This PR addresses comment suggestions of contiv#566. - Refactor code for match expression and match label files to return only []string - Simplified getPodsByNSLabelSelector & getPodsByLabelSelector in match_label.go to be easily readable - Added missing comments for function description and more robust comments in code - Fixed two bugs in processor.go (missing "!" and worng function call)
This PR addresses comment suggestions of contiv#566. - Refactor code for match expression and match label files to return only []string - Simplified getPodsByNSLabelSelector & getPodsByLabelSelector in match_label.go to be easily readable - Added missing comments for function description and more robust comments in code - Fixed two bugs in processor.go (missing "!" and worng function call)
This PR addresses comment suggestions of contiv#566. - Refactor code for match expression and match label files to return only []string - Simplified getPodsByNSLabelSelector & getPodsByLabelSelector in match_label.go to be easily readable - Added missing comments for function description and more robust comments in code - Fixed two bugs in processor.go (missing "!" and worng function call)
- Changed Interesct function to calculate more than two slices. Added an additional check for empty slices. - Added an additional check for empty labels in getPodsByNSLabelSelector and getMatchExpressionPods on file match_label.on. If empty labels empty slice is returned - Simplified cache_implementation code by removing if cases when no labels provided. The if cases are now handled in getPodsByNSLabelSelector and getMatchExpressionPods.
- Changed Interesct function to calculate more than two slices. Added an additional check for empty slices. - Added an additional check for empty labels in getPodsByNSLabelSelector and getMatchExpressionPods on file match_label.on. If empty labels empty slice is returned - Simplified cache_implementation code by removing if cases when no labels provided. The if cases are now handled in getPodsByNSLabelSelector and getMatchExpressionPods. - Removed unecessary function call on duplicates to slices on match_expression.go The duplicate removal is handled by Intersect function
Policy Cache & Policy Processor fixes for #566
This PR makes a few changes in the policy plugin, adding support for namespace label selectors, both for Ingress and Egress Rules. Also fixes bugs related to empty podSelectors and empty namespaceLabelSelectors. Fixes should be also part of #560 and #565