Skip to content

Policy Cache & Policy Processor fixes for #566#570

Merged
milanlenco merged 3 commits intocontiv:masterfrom
brecode:master
Feb 5, 2018
Merged

Policy Cache & Policy Processor fixes for #566#570
milanlenco merged 3 commits intocontiv:masterfrom
brecode:master

Conversation

@brecode
Copy link
Member

@brecode brecode commented Feb 3, 2018

This PR addresses comment suggestions of #566.

  • Refactor code for match_expression and match_label go files to return only []string
    instead of (bool, []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)
@coveralls
Copy link

coveralls commented Feb 3, 2018

Coverage Status

Coverage remained the same at 76.822% when pulling e942add on brecode:master into 0ae2899 on contiv:master.

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.

Good to see improvements here, would love to see the changes for the Intersect related improvements and, i think currently getPodsByLabelSelector and getPodsByNSLabelSelector are potentially broken due to empty labels passed

newPodSet := pc.configuredPods.LookupPodsByNSLabelSelector(newNSLabelSelector)
current := utils.Intersect(prevPodSet, newPodSet)
if len(current) == 0 {
return []string{}
Copy link
Contributor

Choose a reason for hiding this comment

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

you can just break here, but it's not big deal

}

return nil
return []podmodel.ID{}
Copy link
Contributor

Choose a reason for hiding this comment

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

L107 - L140 can be as simple as

mlPods := pc.getPodsByNSLabelSelector(policyNamespace, matchLabels)
mePods := pc.getMatchExpressionPods(policyNamespace, matchExpressions)
return utils.UnstringPodID(utils.Intersect(mlPods, mePods))

The empty case should be handled on the methods getPodsByNSLabelSelector and getMatchExpressionPods, they should return empty slice when no labels provided,
Intersectand UnstringPodID are smart enough to be if any of slice is empty, return empty

newPodSet := []string{}

// 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) []string {
Copy link
Contributor

Choose a reason for hiding this comment

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

you should check if labels is empty here, otherwise you may will get error at labes[0]

Copy link
Member Author

Choose a reason for hiding this comment

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

done

// GetPodsByNSLabelSelector returns the pods that match a collection of Label Selectors
func (pc *PolicyCache) getPodsByLabelSelector(labels []*policymodel.Policy_Label) (bool, []string) {
// getPodsByLabelSelector returns the pods that match a collection of Label Selectors
func (pc *PolicyCache) getPodsByLabelSelector(labels []*policymodel.Policy_Label) []string {
Copy link
Contributor

Choose a reason for hiding this comment

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

you should check if labels is empty, or there will be error

Copy link
Member Author

Choose a reason for hiding this comment

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

done

newNamespaceSet := pc.configuredNamespaces.LookupNamespacesByLabelSelector(newNSLabelSelector)
current = utils.Intersect(prevNamespaceSet, newNamespaceSet)
if len(current) == 0 {
return []string{}
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 break here,

return false, nil
}
return true, pods
return pods
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, I would say update utils.Intersect to make it smarter on either of them is empty case, like

func Intersect(a []string, b []string) []string {
   if len(a) == 0 || len(b) == 0 {
          return []string{}
   }
    ...
}

or further improve it to take a slice variables

// Intersect at least need two lists
func Intersect(a []string, b []string, s ...[]string) []string {
	if len(a) == 0 || len(b) == 0 {
		return []string{}
	}
	set := make([]string, 0)
	hash := make(map[string]bool)
	for _, el := range a {
		hash[el] = true
	}
	for _, el := range b {
		if _, found := hash[el]; found {
			set = append(set, el)
		}
	}
	if len(s) == 0 {
		return set
	}
	return Intersect(set, s[0], s[1:]...)
}

In this way, a lot places can be just

return utils.Intersect(inPodSet, notInPodSet, existsPodSet, notExistPodSet)

@@ -97,17 +97,14 @@ func (pc *PolicyCache) getMatchExpressionPods(namespace string, expressions []*p
notExistPodSet = utils.RemoveDuplicates(inPodSet)

inMatcher := utils.Intersect(inPodSet, notInPodSet)
Copy link
Contributor

Choose a reason for hiding this comment

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

aren't L95-L97 bugs ?

notInPodSet = utils.RemoveDuplicates(notInPodSet)
existsPodSet = utils.RemoveDuplicates(existsPodSet)
notExistPodSet = utils.RemoveDuplicates(notExistPodSet)

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.
 - 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

// Intersect returns the common elements of two slices
func Intersect(a []string, b []string) []string {
// Intersect returns the common elements of or more slices
Copy link
Contributor

Choose a reason for hiding this comment

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

a typo: two or more slices

// Intersect returns the common elements of two slices
func Intersect(a []string, b []string) []string {
// Intersect returns the common elements of or more slices
// Intersect needs at least two lists
Copy link
Contributor

Choose a reason for hiding this comment

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

if corrected above line, this line is not needed

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.

Just need a fix on godoc of Intersect, rest are good

@milanlenco milanlenco merged commit f104b8b into contiv:master Feb 5, 2018
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