in controllers/policyendpoints_controller.go [428:490]
func (r *PolicyEndpointsReconciler) deriveIngressAndEgressFirewallRules(ctx context.Context,
podIdentifier string, resourceNamespace string, resourceName string, isDeleteFlow bool) ([]ebpf.EbpfFirewallRules, []ebpf.EbpfFirewallRules, bool, bool, error) {
var ingressRules, egressRules []ebpf.EbpfFirewallRules
isIngressIsolated, isEgressIsolated := false, false
currentPE := &policyk8sawsv1.PolicyEndpoint{}
if policyEndpointList, ok := r.podIdentifierToPolicyEndpointMap.Load(podIdentifier); ok {
r.log.Info("Total number of PolicyEndpoint resources for", "podIdentifier ", podIdentifier, " are ", len(policyEndpointList.([]string)))
for _, policyEndpointResource := range policyEndpointList.([]string) {
peNamespacedName := types.NamespacedName{
Name: policyEndpointResource,
Namespace: resourceNamespace,
}
if isDeleteFlow {
deletedPEParentNPName := utils.GetParentNPNameFromPEName(resourceName)
currentPEParentNPName := utils.GetParentNPNameFromPEName(policyEndpointResource)
if deletedPEParentNPName == currentPEParentNPName {
r.log.Info("PE belongs to same NP. Ignore and move on since it's a delete flow",
"deletedPE", resourceName, "currentPE", policyEndpointResource)
continue
}
}
if err := r.k8sClient.Get(ctx, peNamespacedName, currentPE); err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, nil, isIngressIsolated, isEgressIsolated, err
}
r.log.Info("Deriving Firewall rules for PolicyEndpoint:", "Name: ", currentPE.Name)
for _, endPointInfo := range currentPE.Spec.Ingress {
ingressRules = append(ingressRules,
ebpf.EbpfFirewallRules{
IPCidr: endPointInfo.CIDR,
Except: endPointInfo.Except,
L4Info: endPointInfo.Ports,
})
}
for _, endPointInfo := range currentPE.Spec.Egress {
egressRules = append(egressRules,
ebpf.EbpfFirewallRules{
IPCidr: endPointInfo.CIDR,
Except: endPointInfo.Except,
L4Info: endPointInfo.Ports,
})
}
r.log.Info("Total no.of - ", "ingressRules", len(ingressRules), "egressRules", len(egressRules))
ingressIsolated, egressIsolated := r.deriveDefaultPodIsolation(ctx, currentPE, len(ingressRules), len(egressRules))
isIngressIsolated = isIngressIsolated || ingressIsolated
isEgressIsolated = isEgressIsolated || egressIsolated
}
}
if len(ingressRules) > 0 {
isIngressIsolated = false
}
if len(egressRules) > 0 {
isEgressIsolated = false
}
return ingressRules, egressRules, isIngressIsolated, isEgressIsolated, nil
}