Skip to content

Commit 9bc6652

Browse files
committed
combine endpoints based on cidr
1 parent f2a9f66 commit 9bc6652

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

pkg/policyendpoints/manager.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,37 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
167167
}
168168
}
169169

170-
return createPolicyEndpoints, updatePolicyEndpoints, deletePolicyEndpoints, nil
170+
return m.processPolicyEndpoints(createPolicyEndpoints), m.processPolicyEndpoints(updatePolicyEndpoints), deletePolicyEndpoints, nil
171+
}
172+
173+
func (m *policyEndpointsManager) processPolicyEndpoints(pes []policyinfo.PolicyEndpoint) []policyinfo.PolicyEndpoint {
174+
var newPEs []policyinfo.PolicyEndpoint
175+
for _, pe := range pes {
176+
pe.Spec.Ingress = combineRulesEndpoints(pe.Spec.Ingress)
177+
pe.Spec.Egress = combineRulesEndpoints(pe.Spec.Egress)
178+
newPEs = append(newPEs, pe)
179+
}
180+
m.logger.Info("manager processed policy endpoints to consolidate rules", "preLen", len(pes), "postLen", len(newPEs), "newPEs", newPEs)
181+
return newPEs
182+
}
183+
184+
// the controller should consolidate the ingress endpoints and put entries to one CIDR if they belong to a same cidr
185+
func combineRulesEndpoints(ingressEndpoints []policyinfo.EndpointInfo) []policyinfo.EndpointInfo {
186+
combinedMap := make(map[string]policyinfo.EndpointInfo)
187+
for _, iep := range ingressEndpoints {
188+
if _, ok := combinedMap[string(iep.CIDR)]; ok {
189+
tempIEP := combinedMap[string(iep.CIDR)]
190+
tempIEP.Ports = append(combinedMap[string(iep.CIDR)].Ports, iep.Ports...)
191+
tempIEP.Except = append(combinedMap[string(iep.CIDR)].Except, iep.Except...)
192+
combinedMap[string(iep.CIDR)] = tempIEP
193+
} else {
194+
combinedMap[string(iep.CIDR)] = iep
195+
}
196+
}
197+
if len(combinedMap) > 0 {
198+
return maps.Values(combinedMap)
199+
}
200+
return nil
171201
}
172202

173203
func (m *policyEndpointsManager) newPolicyEndpoint(policy *networking.NetworkPolicy,

0 commit comments

Comments
 (0)