Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MatchExpression UnitTests #51

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions pkg/nsx/services/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ func (service *SecurityPolicyService) validateSelectorExpressions(matchLabelsCou
var err error = nil
var errorMsg string = ""
var totalExprCount = 0
var totoalCriteria = 0
var totalCriteria = 0

// Check total count of expressions from LabelSelectors in one group criteria
if matchExpressionsCount != 0 {
Expand All @@ -508,29 +508,29 @@ func (service *SecurityPolicyService) validateSelectorExpressions(matchLabelsCou
// Compute total expression counts of final produced criteria
if matchLabelsCount != 0 || matchExpressionsCount != 0 {
if opInValueCount != 0 {
totoalCriteria = opInValueCount
totalCriteria = opInValueCount
totalExprCount *= opInValueCount
} else {
// matchExpressions will be 'AND' with matchLabels(if present) to produce 1 criteria.
totoalCriteria = 1
totalCriteria = 1
}
}
return totoalCriteria, totalExprCount, err
return totalCriteria, totalExprCount, err
}

// Todo, refactor code when NSX support 'In' LabelSelector.
func (service *SecurityPolicyService) matchExpressionOpInExist(matchExpressions []metav1.LabelSelectorRequirement) (bool, int) {
var opeartorInIndex = -1
var operatorInIndex = -1
var isFound = false
for i := 0; i < len(matchExpressions); i++ {
// find Opetator IN
// find Operator IN
if matchExpressions[i].Operator == metav1.LabelSelectorOpIn {
opeartorInIndex = i
operatorInIndex = i
isFound = true
break
}
}
return isFound, opeartorInIndex
return isFound, operatorInIndex
}

// Todo, refactor code when NSX support 'In' LabelSelector.
Expand Down Expand Up @@ -589,26 +589,29 @@ func (service *SecurityPolicyService) updateMixedExpressionsMatchExpression(nsMa
policyExpression *[]*data.StructValue, clusterExpression *data.StructValue, tagValueExpression *data.StructValue, expressions *data.ListValue) error {
var err error = nil
var opInIdx = 0
var found bool = false
var opInMatchExpressions []metav1.LabelSelectorRequirement = nil
var memberType = ""

nsFound, opInIdx1 := service.matchExpressionOpInExist(nsMatchExpressions)
portFound, opInIdx2 := service.matchExpressionOpInExist(matchExpressions)

if nsFound && portFound {
errorMsg := "operator 'IN' is set in both Pod/VM selector and NamespaceSelector"
err = errors.New(errorMsg)
return err
}

if nsFound {
opInIdx = opInIdx1
memberType = "Segment"
opInMatchExpressions = nsMatchExpressions
found = true
} else if portFound {
opInIdx = opInIdx2
memberType = "SegmentPort"
opInMatchExpressions = matchExpressions
found = true
}

if !found {
if !nsFound && !portFound {
err = service.buildExpressionsMatchExpression(matchExpressions, "SegmentPort", expressions)
if err == nil {
err = service.buildExpressionsMatchExpression(nsMatchExpressions, "Segment", expressions)
Expand Down Expand Up @@ -636,14 +639,11 @@ func (service *SecurityPolicyService) updateMixedExpressionsMatchExpression(nsMa

if nsFound {
err = service.buildExpressionsMatchExpression(matchExpressions, "SegmentPort", expressions)
if err != nil {
break
}
} else {
err = service.buildExpressionsMatchExpression(nsMatchExpressions, "Segment", expressions)
if err != nil {
break
}
}
if err != nil {
break
}

service.addOperatorIfNeeded(expressions, "AND")
Expand Down Expand Up @@ -1058,8 +1058,7 @@ func (service *SecurityPolicyService) updatePeerExpressions(obj *v1alpha1.Securi
}

if opInValueCount > 0 && nsOpInValCount > 0 {
// Opeartor 'IN' is set in both Pod/VM selector and NamespaceSelector
errorMsg = "opeartor 'IN' is set in both Pod/VM selector and NamespaceSelector"
errorMsg = "operator 'IN' is set in both Pod/VM selector and NamespaceSelector"
err = errors.New(errorMsg)
log.Error(err, "validate operator 'IN' in label selector matchExpressions failed")
return 0, 0, err
Expand Down
Loading