|
1 | 1 | # NetworkPolicy |
| 2 | +In `Ironcore`, NetworkPolicies are implemented based on the standard Kubernetes `NetworkPolicy` approach, which is enforced by the underlying `Ironcore's` network plugin <a href="https://github.com/ironcore-dev/ironcore-net/blob/main/apinetlet/controllers/networkpolicy_controller.go"> ironcore-net </a> and other components. These policies use label selectors to define the source and destination of allowed traffic within the same network and can specify rules for both ingress (incoming) and egress (outgoing) traffic. |
| 3 | + |
| 4 | +In the `Ironcore` ecosystem, the `NetworkPolicy` has the following characteristics: |
| 5 | + |
| 6 | +- NetworkPolicy is applied exclusively to NetworkInterfaces selected using label selectors. |
| 7 | + |
| 8 | +- These NetworkInterfaces must belong to the same network. |
| 9 | + |
| 10 | +- The policy governs traffic to and from other `NetworkInterfaces`, `LoadBalancers`, etc., based on the rules defined in the NetworkPolicy. |
| 11 | + |
| 12 | +# Example NetworkPolicy Resource |
| 13 | +An example of how to define a `NetworkPolicy` resource in `Ironcore` |
| 14 | + |
| 15 | +``` |
| 16 | +apiVersion: networking.ironcore.dev/v1alpha1 |
| 17 | +kind: NetworkPolicy |
| 18 | +metadata: |
| 19 | + namespace: default |
| 20 | + name: my-network-policy |
| 21 | +spec: |
| 22 | + networkRef: |
| 23 | + name: my-network |
| 24 | + networkInterfaceSelector: |
| 25 | + matchLabels: |
| 26 | + app: db |
| 27 | + policyTypes: |
| 28 | + - Ingress |
| 29 | + - Egress |
| 30 | + ingress: |
| 31 | + - from: |
| 32 | + - ipBlock: |
| 33 | + cidr: 172.17.0.0/16 |
| 34 | + - objectSelector: |
| 35 | + kind: NetworkInterface |
| 36 | + matchLabels: |
| 37 | + app: web |
| 38 | + - objectSelector: |
| 39 | + kind: LoadBalancer |
| 40 | + matchLabels: |
| 41 | + app: web |
| 42 | + # Ports always have to be specified. Only traffic matching the ports |
| 43 | + # will be allowed. |
| 44 | + ports: |
| 45 | + - protocol: TCP |
| 46 | + port: 5432 |
| 47 | + egress: |
| 48 | + - to: |
| 49 | + - ipBlock: |
| 50 | + cidr: 10.0.0.0/24 |
| 51 | + ports: |
| 52 | + - protocol: TCP |
| 53 | + port: 8080 |
| 54 | +``` |
| 55 | +https://github.com/ironcore-dev/ironcore/tree/main/config/samples/e2e/network-policy |
| 56 | + |
| 57 | +(`Note`: Refer to <a href="https://github.com/ironcore-dev/ironcore/tree/main/config/samples/e2e/network-policy">E2E Examples</a> for more detailed example on networkpolicy to understant e2e flow) |
| 58 | + |
| 59 | +# Key Fields |
| 60 | + |
| 61 | +- `networkRef`(`string`): NetworkRef is the Network to regulate using this NetworkPolicy. |
| 62 | + |
| 63 | +- `networkInterfaceSelector`(`labelSelector`): NetworkInterfaceSelector defines the target `NetworkInterfaces` for which this `NetworkPolicy` should be applied. |
| 64 | + |
| 65 | +- `policyTypes`(`list`): There are two supported policyTypes `Ingress` and `Egress`. |
| 66 | + |
| 67 | +- `ingress`(`list`): An Ingress section in a `NetworkPolicy` defines a list of `NetworkPolicyIngressRules` that specify which incoming traffic is allowed. Each `NetworkPolicy` can have multiple ingress rules, and each rule allows traffic that satisfies both the from and ports criteria. |
| 68 | + |
| 69 | + For example, a `NetworkPolicy` with a single ingress rule may permit traffic on a specific port and only from one of the following sources: |
| 70 | + - An IP range, defined using an ipBlock. |
| 71 | + - A set of resources identified by an objectSelector. |
| 72 | + |
| 73 | +- `egress`(`list`): egress defines the list of `NetworkPolicyEgressRules`. Each NetworkPolicy may include a list of allowed egress rules. Each rule allows traffic that matches both `to` and `ports` sections. The example policy contains a single rule, which matches traffic on a single port to any destination in 10.0.0.0/24. |
| 74 | + |
| 75 | +# Reconciliation Process: |
| 76 | +The `NetworkPolicyReconciler` in the Ironcore project is responsible for managing the lifecycle of `NetworkPolicy` resources. Its primary function is to ensure that the rules specified by the user in the NetworkPolicy resource are enforced and applied on the target `NetworkInterface`. |
| 77 | + |
| 78 | +The <a href="https://github.com/ironcore-dev/ironcore-net/blob/main/apinetlet/controllers/networkpolicy_controller.go"> apinetlet </a> component in `ironcore-net` plugin is responsible for translating the policy rules into another APInet type resource `NetworkPolicyRule`. Finally, the <a href="https://github.com/ironcore-dev/ironcore-net/blob/main/metalnetlet/controllers/networkinterface_controller.go"> metalnetlet </a> component in `ironcore-net` and other components propagates these rules for enforcement at `dpservice` level in the Ironcore infrastucture. |
| 79 | + |
| 80 | +The reconciliation process involves several key steps: |
| 81 | + |
| 82 | +- **Fetching the NetworkPolicy Resource**: The reconciler retrieves the NetworkPolicy resource specified in the reconciliation request. If the resource is not found, it may have been deleted, and the reconciler will handle this scenario appropriately. |
| 83 | + |
| 84 | +- **Validating the NetworkPolicy**: The retrieved NetworkPolicy is validated to ensure it confirms the expected specifications. This includes checking fields such as NetworkRef, NetworkInterfaceSelector, Ingress, Egress, and PolicyTypes to ensure they are correctly defined. |
| 85 | + |
| 86 | +- **Fetching Associated Network Interfaces**: Using the NetworkInterfaceSelector, the reconciler identifies the network interfaces that are subject to the policy. |
| 87 | + |
| 88 | +- **Applying Policy Rules**: The reconciler translates the ingress and egress rules defined in the NetworkPolicy into configurations that can be enforced by the underlying network infrastructure. This involves interacting with other components responsible for NetworkPolicy or Firewall rule enforcement. |
| 89 | + |
| 90 | +- **Handling Errors and Reconciliation Loops**: If errors occur during any of the above steps, the reconciler will log the issues and may retry the reconciliation. |
| 91 | + |
0 commit comments