Skip to content

Commit 9e9a666

Browse files
authoredFeb 3, 2024
combine endpoints based on cidr (#80)
2 parents f2a9f66 + 338d6fe commit 9e9a666

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed
 

‎pkg/policyendpoints/manager.go

+31-1
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 and egress 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,

‎pkg/policyendpoints/manager_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
networking "k8s.io/api/networking/v1"
1010
"k8s.io/apimachinery/pkg/api/equality"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1213

1314
policyinfo "github.com/aws/amazon-network-policy-controller-k8s/api/v1alpha1"
1415
)
@@ -494,3 +495,61 @@ func Test_policyEndpointsManager_computePolicyEndpoints(t *testing.T) {
494495
})
495496
}
496497
}
498+
499+
func Test_processPolicyEndpoints(t *testing.T) {
500+
m := &policyEndpointsManager{
501+
logger: zap.New(),
502+
}
503+
504+
p80 := int32(80)
505+
p8080 := int32(8080)
506+
pTCP := corev1.ProtocolTCP
507+
pUDP := corev1.ProtocolUDP
508+
509+
pes := m.processPolicyEndpoints([]policyinfo.PolicyEndpoint{
510+
{
511+
Spec: policyinfo.PolicyEndpointSpec{
512+
Ingress: []policyinfo.EndpointInfo{
513+
{
514+
CIDR: "1.2.3.4",
515+
Ports: []policyinfo.Port{
516+
{Port: &p80, Protocol: &pTCP},
517+
},
518+
},
519+
{
520+
CIDR: "1.2.3.4",
521+
Ports: []policyinfo.Port{
522+
{Port: &p8080, Protocol: &pTCP},
523+
},
524+
},
525+
{
526+
CIDR: "1.2.3.4",
527+
Ports: []policyinfo.Port{
528+
{Protocol: &pUDP},
529+
},
530+
},
531+
},
532+
Egress: []policyinfo.EndpointInfo{
533+
{
534+
CIDR: "1.2.3.5",
535+
Ports: []policyinfo.Port{
536+
{Port: &p80, Protocol: &pTCP},
537+
},
538+
},
539+
{
540+
CIDR: "1.2.3.5",
541+
Ports: []policyinfo.Port{
542+
{Port: &p8080, Protocol: &pTCP},
543+
},
544+
},
545+
},
546+
},
547+
},
548+
})
549+
assert.Equal(t, 1, len(pes[0].Spec.Ingress))
550+
assert.Equal(t, 1, len(pes[0].Spec.Egress))
551+
assert.Equal(t, "1.2.3.4", string(pes[0].Spec.Ingress[0].CIDR))
552+
assert.Equal(t, "1.2.3.5", string(pes[0].Spec.Egress[0].CIDR))
553+
assert.Equal(t, 3, len(pes[0].Spec.Ingress[0].Ports))
554+
assert.Equal(t, 2, len(pes[0].Spec.Egress[0].Ports))
555+
}

0 commit comments

Comments
 (0)
Failed to load comments.