Skip to content

Commit

Permalink
combine endpoints based on cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
haouc committed Feb 2, 2024
1 parent f2a9f66 commit 9bc6652
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pkg/policyendpoints/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,37 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
}
}

return createPolicyEndpoints, updatePolicyEndpoints, deletePolicyEndpoints, nil
return m.processPolicyEndpoints(createPolicyEndpoints), m.processPolicyEndpoints(updatePolicyEndpoints), deletePolicyEndpoints, nil
}

func (m *policyEndpointsManager) processPolicyEndpoints(pes []policyinfo.PolicyEndpoint) []policyinfo.PolicyEndpoint {
var newPEs []policyinfo.PolicyEndpoint
for _, pe := range pes {
pe.Spec.Ingress = combineRulesEndpoints(pe.Spec.Ingress)
pe.Spec.Egress = combineRulesEndpoints(pe.Spec.Egress)
newPEs = append(newPEs, pe)
}
m.logger.Info("manager processed policy endpoints to consolidate rules", "preLen", len(pes), "postLen", len(newPEs), "newPEs", newPEs)
return newPEs
}

// the controller should consolidate the ingress endpoints and put entries to one CIDR if they belong to a same cidr
func combineRulesEndpoints(ingressEndpoints []policyinfo.EndpointInfo) []policyinfo.EndpointInfo {
combinedMap := make(map[string]policyinfo.EndpointInfo)
for _, iep := range ingressEndpoints {
if _, ok := combinedMap[string(iep.CIDR)]; ok {
tempIEP := combinedMap[string(iep.CIDR)]
tempIEP.Ports = append(combinedMap[string(iep.CIDR)].Ports, iep.Ports...)
tempIEP.Except = append(combinedMap[string(iep.CIDR)].Except, iep.Except...)
combinedMap[string(iep.CIDR)] = tempIEP
} else {
combinedMap[string(iep.CIDR)] = iep
}
}
if len(combinedMap) > 0 {
return maps.Values(combinedMap)
}
return nil
}

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

0 comments on commit 9bc6652

Please sign in to comment.