Sub topic 18
Limits of the core API and plugin-specific extensions
This chapter assumes you can write ingress and egress rules, know that a policy only takes effect when the network plugin enforces it, and have built a default-deny baseline. Here you will learn what the core NetworkPolicy API cannot express and where plugin-specific policies fill the gap.
What the core API cannot do
The networking.k8s.io/v1 NetworkPolicy is deliberately small. It works at layers 3 and 4 and only allows traffic. These are the gaps you will hit most often.
| You want to... | Core API | Why it matters |
|---|---|---|
| Write an explicit deny rule | Not supported | Every rule is an allow. Denial only comes from isolation plus the absence of an allow. |
| Filter by hostname or DNS name | Not supported | `ipBlock` takes CIDRs only. Cloud service IPs change, so CIDRs go stale. |
| Filter by HTTP method, path or TLS identity | Not supported | Rules stop at protocol and port. |
| Set rule priority or ordering | Not supported | Policies are additive, so no policy can override another. |
| Apply one policy to the whole cluster | Not supported | Every policy lives in one namespace and selects pods there. |
| Match a Service by name | Not supported | Rules match pods and IPs. Policy is applied to the pod behind a Service. |
| Log or audit allowed and denied traffic | Not supported | There is no logging field, so you rely on the plugin's tools. |
| Control traffic to or from the node | Not defined | Behavior for host traffic depends on the plugin. |
Two details are easy to miss. Newer API versions support endPort for port ranges, but only if your cluster and plugin support it. Also, a plugin that does not implement a field may ignore it silently. You get a policy object that the API server accepts and nothing enforces.
Plugin-specific extensions
Several network plugins add their own custom resources. They are not portable, so a policy written for one plugin does nothing on another.
- Calico offers
GlobalNetworkPolicyandNetworkPolicyin theprojectcalico.orgAPI group. They support ordered rules, explicitDenyandLogactions, and cluster-wide scope. - Cilium offers
CiliumNetworkPolicyandCiliumClusterwideNetworkPolicy. They add DNS-name rules (toFQDNs) and layer 7 rules for HTTP. - Some managed platforms add their own resources for similar features.
The Kubernetes project also develops AdminNetworkPolicy and BaselineAdminNetworkPolicy. These cluster-scoped resources add priority-ordered allow and deny rules for cluster administrators. Check that your plugin supports them before you rely on them.
Choosing between them
Start with the core API wherever it is enough, because it is portable. Add plugin resources only for the specific gaps, and keep them in a clearly named location in your repository.
Exercise
Your team must block egress to one malicious IP while still allowing all other internet traffic from a namespace. Can you do this with core NetworkPolicy alone? Explain.
Show answer
Not with an explicit deny rule, because the core API has none. You can approximate it by allowing egress to 0.0.0.0/0 with that IP in the except list of an ipBlock. For clean deny semantics, or cluster-wide rules, use a plugin resource such as Calico GlobalNetworkPolicy or Cilium's clusterwide policy.