How-to guide / Platform capabilities
Enable generated network policy
These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.
Enable generated network policy when you want Kix to restrict workloads using the Service relationships already present in the deployment graph. Kix creates default-deny policies and the allowances each workload needs.
This guide assumes the cluster has a package that provides the
network-policy-enforcer role. Follow Install Cilium / network policy
support
first if the cluster does not have one. On GKE, the gke and gke-autopilot
flavors already provide an enforcer. On EKS, enable network policy in the
Amazon VPC CNI add-on and set cluster.flavor.eks.networkPolicy = "vpc-cni".
On both clouds, Kix emits standard NetworkPolicy objects and may require IP
ranges under cluster.network (see Standard
NetworkPolicy).
Use a tracked Service endpoint
Section titled “Use a tracked Service endpoint”In the calling package, use an address from the dependency’s out API:
15 build =16 {17 self,18 scope,19 backend,20 ...21 }:22 {23 deployment = scope.mkDeployment {24 name = scope.instanceName;25 spec = {26 replicas = 1;27 selector.matchLabels = scope.selectorLabels;28 template.spec.containers = [29 {30 name = "client";31 image = "docker.io/library/nginx:1.27-alpine";32 env = [33 {34 name = "BACKEND_URL";35 value = backend.out.url { };36 }37 ];38 }39 ];40 };41 };42
43 root = self.deployment;44 };backend.out.url { } supplies the application with the Service URL and gives
Kix the destination, port, and protocol needed for policy generation. Keep the
endpoint connected to the workload that makes the request.
Not every out field provides enough information for policy generation.
out.fqdn, out.url, and address outputs declared by a package identify the
Service being reached, so Kix can derive an egress rule. Structural values such
as out.name and out.selector create a dependency edge but no network-policy
rule. Using one as an address can therefore pass kix check and still produce
a blocked connection at runtime.
When a workload references a Service without using one of its addresses, Kix
emits a warning that workload references Service '<namespace>/<name>' without using its address. Kix has not generated a network policy for this connection,
along with a suggestion to use out.fqdn or out.url. This is a warning, not
a check failure. If the reference is intentionally structural, such as a label
or display value, mark it with facts.refOnly to suppress the warning.
Enable policy generation
Section titled “Enable policy generation”Enable network policy in the cluster definition:
28 networkPolicy = {29 enable = true;30 defaultDeny = true;31 directions = "both";32 };defaultDeny = true creates a policy for each managed workload namespace.
directions = "both" denies ingress and egress unless another generated rule
allows it.
Wire the dependency
Section titled “Wire the dependency”Add the destination and caller instances. For a cross-namespace connection, wire the dependency explicitly:
60 instances.services.backend = {61 package = packages.echo-server;62 config.message = "Hello across a generated network policy!";63 };64
65 instances.apps.client = {66 package = clientPackage;67 deps.backend = ref.services.backend;68 };The client runs in apps; the backend Service and its pods run in services.
Check the generated policies
Section titled “Check the generated policies”Evaluate the cluster:
❱ kix check how-to-platform-network-policy
TOOL RESULT DETAILS
eval pass 48 manifests evaluated, 3 warnings
scorecard pass 0 errors, 26 warnings, 5 info
Warnings from the Kix library, 3:
kube-system/cilium (experimental/cilium): DaemonSet/cilium uses hostNetwork, so network policy does not filter its traffic.
Object: DaemonSet cilium
Fix: Restrict access to its ports on the node's host network.
kube-system/cilium (experimental/cilium): DaemonSet/cilium-envoy uses hostNetwork, so network policy does not filter its traffic.
Object: DaemonSet cilium-envoy
Fix: Restrict access to its ports on the node's host network.
kube-system/cilium (experimental/cilium): Deployment/cilium-operator uses hostNetwork, so network policy does not filter its traffic.
Object: Deployment cilium-operator
Fix: Restrict access to its ports on the node's host network.
architecture.annotationDomainOwnership (warning), 1:
kube-system/cilium: 'cilium' uses annotations under domain 'prometheus.io' (Service/cilium-envoy) but no package in this cluster declares that it handles them. Add the operator package that handles these annotations. If something outside Kix consumes this domain, declare it in `scorecard.knownAnnotationDomains`.
architecture.consistentPortNames (warning), 1:
kube-system/cilium: port name "health" means more than one number in this instance: DaemonSet/cilium container cilium-agent = 9879, Deployment/cilium-operator container cilium-operator = 9234. A consumer that resolves the name without naming the workload picks one of them by accident. Give each component a name of its own.
architecture.selectorCoherence (warning), 1:
kube-system/cilium/cilium-envoy: Service 'cilium-envoy' has selector labels that Kix cannot trace to a workload. Use `kix.service.fromWorkload` to derive the selector and track the dependency on the workload.
governance.hasOwner (warning), 1:
apps/client: package 'client' has no meta.owner
reliability.boundedResources (warning), 9:
apps/client/client: container 'client' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium: container 'cilium-agent' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium: container 'config' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium: container 'mount-cgroup' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium: container 'apply-sysctl-overwrites' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium: container 'mount-bpf-fs' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium: container 'clean-cilium-state' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium-envoy: container 'cilium-envoy' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
kube-system/cilium/cilium-operator: container 'cilium-operator' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
reliability.hasProbes (warning), 2:
apps/client/client: container 'client' has no livenessProbe
apps/client/client: container 'client' has no readinessProbe
security.nonRoot (warning), 1:
apps/client/client: container 'client' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it
security.readOnlyRootfs (warning), 10:
apps/client/client: container 'client' does not set readOnlyRootFilesystem
kube-system/cilium/cilium: container 'cilium-agent' does not set readOnlyRootFilesystem
kube-system/cilium/cilium: container 'config' does not set readOnlyRootFilesystem
kube-system/cilium/cilium: container 'mount-cgroup' does not set readOnlyRootFilesystem
kube-system/cilium/cilium: container 'apply-sysctl-overwrites' does not set readOnlyRootFilesystem
kube-system/cilium/cilium: container 'mount-bpf-fs' does not set readOnlyRootFilesystem
kube-system/cilium/cilium: container 'clean-cilium-state' does not set readOnlyRootFilesystem
kube-system/cilium/cilium: container 'install-cni-binaries' does not set readOnlyRootFilesystem
kube-system/cilium/cilium-envoy: container 'cilium-envoy' does not set readOnlyRootFilesystem
kube-system/cilium/cilium-operator: container 'cilium-operator' does not set readOnlyRootFilesystem
5 info finding(s) not listed; run with -v to list them. Inspect the policies produced by the build:
❱ kix build how-to-platform-network-policy --output json Show outputHide output · 148 lines
[
{
"apiVersion": "cilium.io/v2",
"kind": "CiliumNetworkPolicy",
"metadata": {
"name": "apps-default-deny",
"namespace": "apps"
},
"spec": {
"egress": [
{}
],
"endpointSelector": {},
"ingress": [
{}
]
}
},
{
"apiVersion": "cilium.io/v2",
"kind": "CiliumNetworkPolicy",
"metadata": {
"name": "client-deployment-client-egress",
"namespace": "apps"
},
"spec": {
"egress": [
{
"toEndpoints": [
{
"matchLabels": {
"k8s-app": "kube-dns",
"k8s:io.kubernetes.pod.namespace": "kube-system"
}
}
],
"toPorts": [
{
"ports": [
{
"port": "53",
"protocol": "UDP"
},
{
"port": "53",
"protocol": "TCP"
}
]
}
]
},
{
"toEndpoints": [
{
"matchLabels": {
"app.kubernetes.io/instance": "backend",
"app.kubernetes.io/name": "backend",
"k8s:io.kubernetes.pod.namespace": "services"
}
}
],
"toPorts": [
{
"ports": [
{
"port": "8080",
"protocol": "TCP"
}
]
}
]
}
],
"endpointSelector": {
"matchLabels": {
"app.kubernetes.io/instance": "client",
"app.kubernetes.io/name": "client"
}
}
}
},
{
"apiVersion": "cilium.io/v2",
"kind": "CiliumNetworkPolicy",
"metadata": {
"name": "client-deployment-client-to-backend-ingress",
"namespace": "services"
},
"spec": {
"endpointSelector": {
"matchLabels": {
"app.kubernetes.io/instance": "backend",
"app.kubernetes.io/name": "backend"
}
},
"ingress": [
{
"fromEndpoints": [
{
"matchLabels": {
"app.kubernetes.io/instance": "client",
"app.kubernetes.io/name": "client",
"k8s:io.kubernetes.pod.namespace": "apps"
}
}
],
"toPorts": [
{
"ports": [
{
"port": "8080",
"protocol": "TCP"
}
]
}
]
}
]
}
},
{
"apiVersion": "cilium.io/v2",
"kind": "CiliumNetworkPolicy",
"metadata": {
"name": "services-default-deny",
"namespace": "services"
},
"spec": {
"egress": [
{}
],
"endpointSelector": {},
"ingress": [
{}
]
}
}
]
Warnings from the Kix library, 3:
kube-system/cilium (experimental/cilium): DaemonSet/cilium uses hostNetwork, so network policy does not filter its traffic.
Object: DaemonSet cilium
Fix: Restrict access to its ports on the node's host network.
kube-system/cilium (experimental/cilium): DaemonSet/cilium-envoy uses hostNetwork, so network policy does not filter its traffic.
Object: DaemonSet cilium-envoy
Fix: Restrict access to its ports on the node's host network.
kube-system/cilium (experimental/cilium): Deployment/cilium-operator uses hostNetwork, so network policy does not filter its traffic.
Object: Deployment cilium-operator
Fix: Restrict access to its ports on the node's host network. The client egress policy permits DNS and traffic to the backend’s http port.
Kix also places a matching ingress policy in services, scoped to the backend
pods and the client pods in apps. The two namespace default-deny policies
make those allowances effective.
Deploy the cluster, then list the policies installed by Cilium:
❱ kix deploy how-to-platform-network-policy❱ kubectl get ciliumnetworkpolicies --all-namespaces