Skip to content

Commit

Permalink
make linter happy;
Browse files Browse the repository at this point in the history
simplified complement pass connections calculation.
  • Loading branch information
tanyaveksler committed Jan 14, 2025
1 parent d78e3c4 commit 4ab3d96
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
7 changes: 1 addition & 6 deletions pkg/netpol/eval/internal/k8s/policy_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ func (pc *PolicyConnections) CollectANPConns(newAdminPolicyConns *PolicyConnecti

// ComplementPassConns complements pass connections to all connections (by adding the absent conections)
func (pc *PolicyConnections) ComplementPassConns() {
defaultPassConn := common.MakeConnectionSet(true)
defaultPassConn.Subtract(pc.AllowedConns)
defaultPassConn.Subtract(pc.DeniedConns)
// 'GetEquivalentCanonicalConnectionSet' below removes implying rules
// (we don't collect implying rules for default pass connections)
pc.PassConns.Union(defaultPassConn.GetEquivalentCanonicalConnectionSet(), false)
pc.PassConns.Union(common.MakeConnectionSet(true), false)
}

// CollectAllowedConnsFromNetpols updates allowed conns of current PolicyConnections object with allowed connections from
Expand Down
15 changes: 10 additions & 5 deletions pkg/netpol/internal/common/augmented_intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@ const (
//
// The logic of the update is as follows:
// - if 'collectStyle' is AlwaysCollectRules (comes from Intersection of connection sets) --> collect the rules in any case
// (Intersection of connection sets scenario)
// (Intersection of connection sets scenario, mainly for intersecion with pass connections)
// - if 'collectStyle' is CollectSameInclusionRules and the inclusion status persists ('sameInclusion' is true) --> collect the rules
// (Union of connection sets of multiple NPs scenario)
// - otherwise, if the inclusion status changes ('sameInclusion' is false) --> override the rules
// - otherwise, if the DominantLayer priortiy of the other rules is higher --> override the rules
// - otherwise, keep the current rules.
func (rules ImplyingXgressRulesType) update(other ImplyingXgressRulesType, sameInclusion bool, collectStyle CollectStyleType) ImplyingXgressRulesType {
func (rules ImplyingXgressRulesType) update(other ImplyingXgressRulesType, sameInclusion bool,
collectStyle CollectStyleType) ImplyingXgressRulesType {

Check failure on line 279 in pkg/netpol/internal/common/augmented_intervalset.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary leading newline (whitespace)

result := rules.Copy()
if other.Empty() {
return result
Expand All @@ -299,7 +301,8 @@ func (rules ImplyingXgressRulesType) update(other ImplyingXgressRulesType, sameI
return result
}

func (rules ImplyingRulesType) Update(other ImplyingRulesType, sameInclusion bool, collectStyle CollectStyleType) ImplyingRulesType {
func (rules ImplyingRulesType) Update(other ImplyingRulesType, sameInclusion bool,
collectStyle CollectStyleType) ImplyingRulesType {
result := ImplyingRulesType{}
result.Ingress = rules.Ingress.update(other.Ingress, sameInclusion, collectStyle)
result.Egress = rules.Egress.update(other.Egress, sameInclusion, collectStyle)
Expand All @@ -308,7 +311,8 @@ func (rules ImplyingRulesType) Update(other ImplyingRulesType, sameInclusion boo

// This function returns whether the current rules may be updated by the other rules.
// It follows the logic of Update() (see explanation above).
func (rules *ImplyingXgressRulesType) mayBeUpdatedBy(other ImplyingXgressRulesType, sameInclusion bool, collectStyle CollectStyleType) bool {
func (rules *ImplyingXgressRulesType) mayBeUpdatedBy(other ImplyingXgressRulesType, sameInclusion bool,
collectStyle CollectStyleType) bool {
if collectStyle == AlwaysCollectRules || (collectStyle == CollectSameInclusionRules && sameInclusion) {
// return true iff Union would change anything
for name := range other.Rules {
Expand All @@ -321,7 +325,8 @@ func (rules *ImplyingXgressRulesType) mayBeUpdatedBy(other ImplyingXgressRulesTy
return (!sameInclusion || rules.Empty() && !other.Empty()) || rules.DominantLayer < other.DominantLayer
}

func (rules ImplyingRulesType) mayBeUpdatedBy(other ImplyingRulesType, sameInclusion bool, collectStyle CollectStyleType) bool {
func (rules ImplyingRulesType) mayBeUpdatedBy(other ImplyingRulesType, sameInclusion bool,
collectStyle CollectStyleType) bool {
return rules.Ingress.mayBeUpdatedBy(other.Ingress, sameInclusion, collectStyle) ||
rules.Egress.mayBeUpdatedBy(other.Egress, sameInclusion, collectStyle)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/netpol/internal/common/portset.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (p *PortSet) AddPort(port intstr.IntOrString, implyingRules ImplyingRulesTy
p.NamedPorts[port.StrVal] = p.NamedPorts[port.StrVal].Update(implyingRules, false, NeverCollectRules)
delete(p.ExcludedNamedPorts, port.StrVal)
} else {
p.Ports.AddAugmentedInterval(NewAugmentedIntervalWithRules(int64(port.IntVal), int64(port.IntVal), true, implyingRules), NeverCollectRules)
p.Ports.AddAugmentedInterval(NewAugmentedIntervalWithRules(int64(port.IntVal), int64(port.IntVal),
true, implyingRules), NeverCollectRules)
}
}

Expand Down

0 comments on commit 4ab3d96

Please sign in to comment.