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

policy: Optimize getNets() #26345

Merged
merged 1 commit into from
Jun 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/policy/distillery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var (
ep2 = testutils.NewTestEndpoint()
)

func localIdentity(n uint32) identity.NumericIdentity {
return identity.NumericIdentity(n) | identity.LocalIdentityFlag

}
func (s *DistilleryTestSuite) TestCacheManagement(c *C) {
repo := NewPolicyRepository(nil, nil, nil, nil)
cache := repo.policyCache
Expand Down Expand Up @@ -1205,7 +1209,7 @@ var (
owners: map[MapStateOwner]struct{}{},
}

worldIPIdentity = identity.NumericIdentity(16324)
worldIPIdentity = localIdentity(16324)
worldCIDR = api.CIDR("192.0.2.3/32")
lblWorldIP = labels.ParseSelectLabelArray(fmt.Sprintf("%s:%s", labels.LabelSourceCIDR, worldCIDR))
ruleL3AllowWorldIP = api.NewRule().WithIngressRules([]api.IngressRule{{
Expand All @@ -1218,7 +1222,7 @@ var (
},
}}).WithEndpointSelector(api.WildcardEndpointSelector)

worldSubnetIdentity = identity.NumericIdentity(16325)
worldSubnetIdentity = localIdentity(16325)
worldSubnet = api.CIDR("192.0.2.0/24")
worldSubnetRule = api.CIDRRule{
Cidr: worldSubnet,
Expand Down
10 changes: 5 additions & 5 deletions pkg/policy/mapstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ func (e *MapStateEntry) getNets(identities Identities, ident uint32) []*net.IPNe
}
return e.cachedNets
}
if identities == nil {
// CIDR identities have a local scope, so we can skip the rest if id is not of local scope.
if !id.HasLocalScope() || identities == nil {
return nil
}
lbls := identities.GetLabels(id)
nets := make([]*net.IPNet, 0, 1)
var (
maskSize int
mostSpecificCidr *net.IPNet
Expand All @@ -219,10 +219,10 @@ func (e *MapStateEntry) getNets(identities Identities, ident uint32) []*net.IPNe
}
}
if mostSpecificCidr != nil {
nets = append(nets, mostSpecificCidr)
e.cachedNets = []*net.IPNet{mostSpecificCidr}
return e.cachedNets
}
e.cachedNets = nets
return nets
return nil
}

// AddDependent adds 'key' to the set of dependent keys.
Expand Down