Skip to content

Add support for namespace label selectors in K8s policies#566

Merged
jmedved merged 3 commits intocontiv:masterfrom
brecode:master
Feb 2, 2018
Merged

Add support for namespace label selectors in K8s policies#566
jmedved merged 3 commits intocontiv:masterfrom
brecode:master

Conversation

@brecode
Copy link
Member

@brecode brecode commented Feb 2, 2018

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

@brecode brecode requested review from jmedved and milanlenco February 2, 2018 01:13
@jmedved jmedved requested a review from tiewei February 2, 2018 01:29
@jmedved jmedved changed the title plugins/policy: added support for namespace label selectors Add support for namespace label selectors in K8s policies Feb 2, 2018
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@coveralls
Copy link

coveralls commented Feb 2, 2018

Coverage Status

Coverage remained the same at 76.822% when pulling 8ad1e7e on brecode:master into 1977041 on contiv:master.

@brecode
Copy link
Member Author

brecode commented Feb 2, 2018

Commit 8ad1e7e
Fix formatting errors on plugins/policy/processor/matches_calculator.go & plugins/policy/processor/processor.go go files, to correct "make check_format" error from travis by removing unnecessary whitespaces. Solution is to run gofmt on the files producing the error.

Copy link
Contributor

@tiewei tiewei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some comments

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

matchLabels := namespaceLabelSelector.MatchLabel

found, namespaceSelectorPods := pc.getPodsByLabelSelector(matchLabels)
if !found {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here i would return empty string slice instead

if len(pods) == 0 {
return false, []string{}
}
return true, pods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not return false ?

// Check if matched namespaces include pod's namespace
for _, namespace := range namespaces {
if namespace == podNamespace {
namespaceExists = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would add a break here

@jmedved jmedved merged commit 324d2cb into contiv:master Feb 2, 2018
brecode added a commit to brecode/contiv-vpp that referenced this pull request Feb 3, 2018
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)
brecode added a commit to brecode/contiv-vpp that referenced this pull request Feb 3, 2018
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)
brecode added a commit to brecode/contiv-vpp that referenced this pull request Feb 3, 2018
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)
brecode added a commit to brecode/contiv-vpp that referenced this pull request Feb 3, 2018
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)
brecode added a commit to brecode/contiv-vpp that referenced this pull request Feb 3, 2018
 - 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.
brecode added a commit to brecode/contiv-vpp that referenced this pull request Feb 3, 2018
 - 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
milanlenco pushed a commit that referenced this pull request Feb 5, 2018
Policy Cache & Policy Processor fixes for #566
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants