This repository demonstrates different approaches to implement Policy as Code in Kubernetes to prevent pods from using host networking. The policies block kubectl apply operations for pods that have hostNetwork: true configured.
Host networking in Kubernetes allows pods to use the host's network namespace, which can pose security risks. This repository provides three different implementations to enforce policies that prevent pods from using host networking:
- Gatekeeper - Using OPA Gatekeeper with both Rego and CEL validation engines
- Kyverno - Using Kyverno with both native YAML patterns and CEL expressions
- Vanilla Kubernetes - Using native Kubernetes ValidatingAdmissionPolicy with CEL
Contains OPA Gatekeeper implementation with two validation approaches:
- Rego-based validation: Uses Rego policy language for validation logic
- CEL-based validation: Uses Common Expression Language (CEL) for validation
Files:
install.yaml- Gatekeeper installation manifestrego-constraint-template.yaml- Rego-based constraint templaterego-constraint.yaml- Rego-based constraint instancecel-constraint-template.yaml- CEL-based constraint templatecel-constraint.yaml- CEL-based constraint instance
Contains Kyverno implementation with two validation approaches:
- Native policy: Uses Kyverno's native YAML pattern matching
- CEL policy: Uses CEL expressions for validation
Files:
install.yaml- Kyverno installation manifestnative-policy.yaml- Native Kyverno policy using YAML patternscel-policy.yaml- Kyverno policy using CEL expressions
Contains native Kubernetes implementation:
- ValidatingAdmissionPolicy: Uses Kubernetes' built-in admission controller with CEL
Files:
hostnetwork-not-allow.yaml- ValidatingAdmissionPolicy and binding
Contains test workloads:
failed.yaml- Pod withhostNetwork: true(should be blocked)success.yaml- Pod without host networking (should be allowed)
-
Deploy Gatekeeper:
kubectl apply -f gatekeeper/install.yaml
-
Deploy validation rules:
kubectl apply -f gatekeeper/rego-constraint-template.yaml kubectl apply -f gatekeeper/cel-constraint-template.yaml
-
Apply policy rule sets:
kubectl apply -f gatekeeper/rego-constraint.yaml kubectl apply -f gatekeeper/cel-constraint.yaml
-
Test admission webhook:
kubectl apply -f workloads/failed.yaml # Should see error message kubectl apply -f workloads/success.yaml # Should deploy successfully
-
Deploy Kyverno:
kubectl apply -f kyverno/install.yaml
-
Deploy validation rules:
kubectl apply -f kyverno/native-policy.yaml kubectl apply -f kyverno/cel-policy.yaml
-
Test admission webhook:
kubectl apply -f workloads/failed.yaml # Should see error message kubectl apply -f workloads/success.yaml # Should deploy successfully
-
Deploy ValidatingAdmissionPolicy:
kubectl apply -f vanilla/hostnetwork-not-allow.yaml
-
Test admission webhook:
kubectl apply -f workloads/failed.yaml # Should see error message kubectl apply -f workloads/success.yaml # Should deploy successfully
All implementations enforce the same policy logic:
- Allow: Pods that don't specify
hostNetworkor havehostNetwork: false - Deny: Pods that have
hostNetwork: true
The validation expression used in CEL-based policies:
!has(object.spec.hostNetwork) || object.spec.hostNetwork != true
When a pod with hostNetwork: true is applied, you should see an error message similar to:
- Gatekeeper: "HostNetwork usage is not allowed for pods"
- Kyverno: "HostNetwork usage is not allowed in Pods."
- Vanilla: "HostNetwork usage is not allowed in Pods."