Kubernetes is permissive by default, and it is worth being blunt about how permissive. Out of the box, a developer can deploy a pod that runs as root, mounts the host filesystem at /, and declares no memory limit at all. The API server accepts it without a warning. If someone then finds a remote code execution bug in that application, the distance between “compromised container” and “compromised node” is very short.
The mechanism for stopping that is admission control, and there are now four credible ways to do it. Picking between them is less about features than about where the check runs and what happens when it breaks.
Why workload security policies matter more than a review process
Misconfiguration, not an unpatched CVE, is what most Kubernetes security incidents come down to. Red Hat’s annual State of Kubernetes Security report has put misconfiguration and exposure at or near the top of respondents’ reported incident causes for several years running, consistently ahead of vulnerabilities found in software.
That points at a process problem rather than a patching problem, and process controls scale badly. A security engineer reviewing pull requests is a real control, but it runs after the fact and only on the changes that go through review. By the time someone reads the diff, the non-compliant workload may have been running in production for a day.
Policy as code moves the check to the one place every change has to pass through. When a request reaches the API server, it goes through authentication, then authorization through RBAC, and then admission control. That third phase is the last point at which an object can be rejected or modified before it is written to etcd, and it does not care whether the request came from a pipeline, a kubectl apply, or someone’s laptop at midnight.
Where each tool actually runs
Before comparing syntax, it is worth being precise about the pipeline, because the position of each tool explains most of its limitations.
Two details from that diagram do most of the explanatory work.
Mutation happens before validation. So a validating policy always sees the object after every mutating webhook has finished with it. If Kyverno injects a default memory limit and a validating rule then requires one, the order works out. Reverse it and nothing would ever pass.
Validating webhooks run in parallel, mutating webhooks run in sequence. Validation is a pure predicate, so order does not matter and any single rejection rejects the request. Mutation is not: each webhook sees the previous one’s output. Two controllers patching the same field can fight, which is why mutations need to be idempotent, patching only when the field is absent rather than unconditionally.
Pod Security Admission
PSA is built into Kubernetes, enabled by default since 1.23 and GA since 1.25, and it replaced the removed PodSecurityPolicy. It enforces the Pod Security Standards, and it is configured entirely with namespace labels.
There are three cumulative profiles.
| Profile | What it allows | Where it fits |
|---|---|---|
privileged | Everything, including every known privilege escalation | Trusted infrastructure workloads: CNI plugins, storage drivers, node agents |
baseline | Blocks known privilege escalations, permits ordinary application shapes | A sensible default for most application namespaces |
restricted | Enforces hardening: non-root, dropped capabilities, seccomp, no privilege escalation | Untrusted workloads and anything handling sensitive data |
Each profile can be applied in three modes, and you can use several at once.
enforcerejects the pod.warnallows it and returns a warning to whoever submitted it.auditallows it and annotates the audit log.
Tip
Combining the modes is what makes a rollout survivable. Set baseline to enforce so the serious problems are blocked today, and set restricted to warn and audit at the same time. Developers see exactly what would break before it does, you get audit-log data on the real violation rate, and you flip restricted to enforce once that rate reaches zero. Going straight to enforcing restricted on an existing cluster breaks deployments on a Friday afternoon.
Applying it is two labels:
apiVersion: v1kind: Namespacemetadata: name: namespace-prod labels: pod-security.kubernetes.io/enforce: baseline pod-security.kubernetes.io/enforce-version: latest # Both at once: block the serious things now, surface the rest. pod-security.kubernetes.io/warn: restricted pod-security.kubernetes.io/audit: restrictedPinning enforce-version to a specific release rather than latest is worth considering. With latest, a cluster upgrade that tightens a standard can start rejecting workloads that were fine yesterday.
PSA’s limitation is not a configuration gap, it is the design. It understands the Pod Security Standards and nothing else. It cannot restrict which registry an image comes from, cannot require resource limits, cannot check a custom label, and cannot generate a NetworkPolicy for a new namespace. For any of those you need a policy engine, and the reason PSA cannot add a default limit is structural: it only participates in the validating phase, and adding something to an object requires mutation.
OPA Gatekeeper
Gatekeeper is an admission controller built on Open Policy Agent, evaluating policies written in Rego. It splits a policy into two objects, which is the part that confuses people initially and turns out to be the point.
A ConstraintTemplate holds the Rego logic and declares a schema for the parameters the rule accepts. It is a reusable blueprint, and applying one causes Gatekeeper to generate a new CRD named after it.
A Constraint is an instance of that blueprint. It supplies the parameters, such as the list of allowed registries, and scopes the rule to particular namespaces and resource kinds.
The separation means a platform team maintains a library of generic templates while application teams instantiate them with their own values, without either side writing Rego twice. A typical GitOps repository ends up looking like this:
Directorygatekeeper/
Directorytemplates/
- k8sallowedrepos.yaml the Rego, written once
- k8srequiredlabels.yaml
- k8sblockprivileged.yaml
Directoryconstraints/
Directoryproduction/
- allowed-repos.yaml parameters and scope only
- required-labels.yaml
Directorystaging/
- allowed-repos.yaml
Directoryconfig/
- config.yaml sync the resources policies need to read
- exempt-namespaces.yaml
Because OPA is a general-purpose engine, the same Rego can validate a Kubernetes manifest, a Terraform plan, and Envoy authorization requests. Gatekeeper also supports external data providers, so a policy can call an HTTP endpoint during admission to check something the cluster does not know, such as whether an image digest appears in an approved-builds database.
The cost is Rego. It is a genuinely capable language and a genuinely unfamiliar one, and debugging a non-obvious Rego rule at the point where it is blocking a deployment is not a pleasant introduction. If nobody on your team writes Rego today, the “one language everywhere” argument is a projection rather than an existing asset.
Kyverno
Kyverno takes the opposite bet. It is built only for Kubernetes, and policies are written in Kubernetes YAML rather than a separate language. If you can write a Deployment, you can read a Kyverno policy on first encounter, which matters a great deal when the person reading it is a developer trying to work out why their deploy was rejected.
Policies use a match and validate structure, with CEL and JMESPath available when the declarative pattern is not enough. Beyond validation, Kyverno does two things Gatekeeper does less naturally: it mutates incoming resources to inject missing configuration, and it generates entirely new resources in response to events, such as creating a default NetworkPolicy and ResourceQuota whenever a namespace appears.
Rolling out a policy safely follows the same shape every time:
-
Write the policy with
failureAction: Audit. It reports violations through PolicyReport objects and blocks nothing. -
Query the reports across the cluster with
kubectl get polr -Ato see the real violation count. This number is almost always higher than the team expects, and finding that out before enforcement is the entire purpose of the step. -
Fix the violations, or add explicit exclusions for the workloads that legitimately need an exception. Write down why in the policy annotation, because in six months nobody will remember.
-
Flip to
failureAction: Enforce, scoped to one namespace first rather than cluster-wide. -
Expand the scope namespace by namespace, watching the admission-webhook error rate and latency as you go.
Careful here
Kyverno renamed this field. spec.validationFailureAction was deprecated in favour of spec.rules[].validate.failureAction, which is per-rule rather than per-policy, and the values are Audit and Enforce. Plenty of blog posts and copy-pasteable examples still use the old top-level field. It continues to work for now, and it will not forever, so check which version of the syntax you are reading.
The same three policies in each tool
Abstract comparisons are less useful than seeing the same requirement written three ways.
Blocking privileged containers
A privileged container has effectively the same access to the host as a process running directly on the node. Blocking it is the most basic hardening step there is.
One label. The baseline profile already forbids privileged containers, along with host namespaces, host ports and all hostPath volumes.
kubectl label ns app-team pod-security.kubernetes.io/enforce=baselineTwo objects. First the template carrying the Rego:
apiVersion: templates.gatekeeper.sh/v1kind: ConstraintTemplatemetadata: name: k8spspprivilegedcontainerspec: crd: spec: names: kind: K8sPSPPrivilegedContainer targets: - target: admission.k8s.gatekeeper.sh rego: | package k8spspprivileged
violation[{"msg": msg, "details": {}}] { c := input_containers[_] c.securityContext.privileged msg := sprintf("Privileged container is not allowed: %v", [c.name]) }
# All three container lists, or you have a gap. input_containers[c] { c := input.review.object.spec.containers[_] } input_containers[c] { c := input.review.object.spec.initContainers[_] } input_containers[c] { c := input.review.object.spec.ephemeralContainers[_] }Then the constraint that activates it:
apiVersion: constraints.gatekeeper.sh/v1beta1kind: K8sPSPPrivilegedContainermetadata: name: psp-privileged-containerspec: match: kinds: - apiGroups: [''] kinds: ['Pod'] excludedNamespaces: ['kube-system', 'gatekeeper-system']Note the two input_containers rules. A policy that checks spec.containers and forgets initContainers looks correct and leaves a hole, and this is one of the more common Rego mistakes.
One object, and the pattern reads like the resource it is matching.
apiVersion: kyverno.io/v1kind: ClusterPolicymetadata: name: disallow-privileged-containersspec: rules: - name: privileged-containers match: any: - resources: kinds: ['Pod'] validate: failureAction: Enforce message: 'Privileged mode is not allowed.' pattern: spec: =(ephemeralContainers): - =(securityContext): =(privileged): 'false' =(initContainers): - =(securityContext): =(privileged): 'false' containers: - =(securityContext): =(privileged): 'false'The =() prefix means “if this key exists, it must match”, so an absent securityContext passes rather than failing. All three container lists are covered, for the same reason as in the Gatekeeper version.
Enforcing an image registry
Pulling from arbitrary public registries is a supply chain risk. Images should come from your own registry, where they have been scanned.
Not possible. PSA evaluates the Pod Security Standards only, and image provenance is not part of them. This is the requirement that usually makes teams install a policy engine.
The K8sAllowedRepos template from the Gatekeeper policy library, instantiated with your registry:
apiVersion: constraints.gatekeeper.sh/v1beta1kind: K8sAllowedReposmetadata: name: require-approved-registryspec: match: kinds: - apiGroups: [''] kinds: ['Pod'] excludedNamespaces: ['kube-system', 'gatekeeper-system'] parameters: repos: - 'registry.internal.company.com/'The reusability shows here: the template is written once, and every team’s constraint differs only in the repos list.
A wildcard match in the pattern:
apiVersion: kyverno.io/v1kind: ClusterPolicymetadata: name: restrict-image-registriesspec: rules: - name: validate-registry match: any: - resources: kinds: ['Pod'] validate: failureAction: Enforce message: 'Images must come from registry.internal.company.com.' pattern: spec: =(ephemeralContainers): - image: 'registry.internal.company.com/*' =(initContainers): - image: 'registry.internal.company.com/*' containers: - image: 'registry.internal.company.com/*'Kyverno can go further here in a way Gatekeeper cannot without external data: verifyImages checks a Cosign signature or an attestation during admission, so the rule becomes “signed by our build system” rather than “came from the right hostname”. That is a stronger guarantee, since a registry path is easy to spoof if someone can push to your registry.
Requiring resource limits
A container with no memory limit can consume every byte on a node and take its neighbours down with it.
Not possible, for the structural reason described earlier. Even validating this is outside the Pod Security Standards, and injecting a default would require mutation, which PSA does not do at all.
Use a LimitRange in the namespace instead. It is built in, it sets defaults for containers that omit them, and it long predates all of this.
Validating is a straightforward Rego template checking for the presence of resources.limits. Mutating is where it gets harder: Gatekeeper’s Assign resource can inject a default, and its syntax is powerful and unpleasant to debug.
apiVersion: mutations.gatekeeper.sh/v1kind: Assignmetadata: name: default-memory-limitspec: applyTo: - groups: [''] kinds: ['Pod'] versions: ['v1'] match: scope: Namespaced excludedNamespaces: ['kube-system', 'gatekeeper-system'] location: 'spec.containers[name:*].resources.limits.memory' parameters: assign: value: '512Mi' pathTests: # Only assign when it is missing, so we never overwrite a real value. - subPath: 'spec.containers[name:*].resources.limits.memory' condition: MustNotExistThat pathTests block is the idempotency guard. Without it the mutation fires unconditionally and silently overrides a limit a team set deliberately.
Mutation is a first-class operation, and the +() prefix means “add only if absent”, which gives you idempotency without a separate condition block.
apiVersion: kyverno.io/v1kind: ClusterPolicymetadata: name: add-default-resource-limitsspec: rules: - name: add-default-limits match: any: - resources: kinds: ['Pod'] mutate: foreach: - list: 'request.object.spec.containers' patchStrategicMerge: spec: containers: - name: '{{ element.name }}' resources: limits: +(memory): '512Mi' +(cpu): '500m'The webhook risk nobody mentions until it bites
Both Gatekeeper and Kyverno run as dynamic admission webhooks, outside the API server. That means every matching API request involves a network call from the control plane to a pod in your cluster, and that pod is now on the critical path of resource creation.
Which forces a decision with no good answer. Every webhook registration carries a failurePolicy telling the API server what to do when the webhook times out or errors.
failurePolicy: Ignore lets the request through. Availability wins, the cluster keeps working, and during the outage your policies are simply not enforced. Nothing announces this. Unless you alert on webhook error rates, the first you hear about it is when you find a privileged pod that was admitted during a fifteen-minute window last Tuesday.
failurePolicy: Fail rejects the request. Security wins, nothing non-compliant gets through, and if the policy controller is down you cannot deploy anything. On a cluster-wide rule, that includes the pods you would need to deploy to fix the outage. This failure mode is genuinely circular and has taken clusters down.
The way out is not to pick a side, it is to scope the webhook so Fail is survivable:
# Exclude the namespaces that have to work when policy is broken.namespaceSelector: matchExpressions: - key: kubernetes.io/metadata.name operator: NotIn values: ['kube-system', 'kyverno', 'gatekeeper-system']Alongside that: run the controller with several replicas and a PodDisruptionBudget, keep the webhook timeout low (timeoutSeconds: 5 or less, because the API server waits on it), and narrow rules so the webhook is only consulted for the resources it actually has policies about. A webhook matching */* is asking to be in the path of every operation in the cluster, including ones you never wrote a policy for.
Mutating webhooks add the ordering problem on top. They run sequentially, each seeing the previous output, so a non-idempotent mutation can fight another controller and cause the API server to re-run the chain. Keep the logic simple and conditional, which is what both the pathTests and +() examples above are doing.
So which one should you use
The honest summary is that the tools are less interchangeable than a feature matrix suggests.
| Engine | Where it fits | The real drawback |
|---|---|---|
| Pod Security Admission | You want root and privileged containers blocked with nothing new to run or monitor | Understands only the Pod Security Standards. No custom logic, no registry rules, no generation |
| ValidatingAdmissionPolicy | Custom validation without a webhook, in-process, using CEL | Validation only. No mutation, no resource generation |
| OPA Gatekeeper | You already run OPA for Terraform, CI or an API gateway, and want one policy language across all of it | Rego is hard to learn and harder to debug. Large constraint libraries consume real memory |
| Kyverno | Kubernetes-focused teams wanting mutation, generation and supply chain verification in plain YAML | Policies are Kubernetes-only. You cannot reuse them to gate anything outside the cluster |
The option worth calling out separately is ValidatingAdmissionPolicy, which reached GA in Kubernetes 1.30. It lets you write validation rules in CEL that the API server evaluates in-process, so there is no webhook, no failurePolicy dilemma, and no network latency on the admission path. It cannot mutate or generate, so it does not replace Kyverno for a platform team that relies on those. But for pure validation it removes the entire failure mode this article spent a section on, and it is new enough that most comparison articles predate it.
If I were setting up a cluster today: PSA at baseline everywhere immediately, because it costs nothing and works when everything else is broken. Then restricted in warn and audit mode to find out what would break. Then one engine on top, chosen by whether the team already writes Rego, for the rules PSA cannot express. Running both Gatekeeper and Kyverno is possible and rarely worth two failure domains and two skill sets.
Frequently Asked Questions
No, and expecting it to be causes trouble. PSP could be scoped to a service account through RBAC and could mutate pods to add defaults. PSA does neither: it is namespace-scoped through labels and it is validating only. Most non-trivial PSP setups do not translate directly, and the migration usually ends up as PSA for the standard hardening plus a policy engine for whatever the PSPs did beyond that.
Technically yes. They register separate webhooks and do not conflict. In practice it means two controllers to keep healthy, two failure domains on the admission path, two policy languages for people to learn, and two places to check when a deployment is rejected without an obvious reason. The usual reason to do it is a migration, and the usual mistake is not finishing the migration.
It adds latency to every API request the webhook matches, which is why the rules scoping matters more than the policy count. A webhook matching everything sits in the path of every operation. In steady state a well-scoped webhook adds single-digit milliseconds; the number that hurts is the timeout, because the API server waits for it. Set timeoutSeconds low and alert on the apiserver_admission_webhook_rejection_count and duration metrics.
Both engines have an audit mode, and both have a CLI for testing outside the cluster. Kyverno’s kyverno apply runs a policy against manifest files locally and is easy to put in a pipeline, so a developer finds out at commit time rather than at deploy time. Gatekeeper has gator test for the same purpose. Do both: audit mode tells you the violation rate on what is already running, and the CLI stops new violations before they are submitted.
Nothing, immediately. Admission control only runs on create and update, so an existing pod keeps running until something touches it. That is a trap: the policy looks enforced, and the violations are still there. Kyverno generates PolicyReport objects for existing resources, and Gatekeeper’s audit controller reports violations on a schedule. Check those, or your first sign of the problem is a rescheduled pod that suddenly cannot start.
Yes. They solve different problems at different layers. RBAC decides who may submit the request at all, and admission control decides whether the object they submitted is acceptable. NetworkPolicy governs traffic between pods once they are running, which no admission controller sees. A policy engine can help by generating a default deny-all NetworkPolicy for every new namespace, which is one of the better uses of Kyverno’s generation feature.
For validation, plausibly, over time. Being in-process removes the failure mode that makes webhooks operationally awkward, and CEL is easier to learn than Rego. But it does not mutate or generate resources, and it has no equivalent of Kyverno’s image verification, so it does not replace either engine for a platform team using those. The likely end state is VAP handling the simple validation most clusters need, with an engine layered on for mutation, generation and supply chain checks.
References
- Kubernetes: Pod Security Admission
- Kubernetes: Pod Security Standards
- Kubernetes: enforcing Pod Security Standards
- Kubernetes: dynamic admission control
- Kubernetes: good practices for admission webhooks
- Kubernetes: Validating Admission Policy
- Kubernetes 1.30 release notes, including ValidatingAdmissionPolicy going GA
- OPA Gatekeeper documentation
- Gatekeeper policy library, including the privileged-container and allowed-repos templates
- Gatekeeper mutation with the Assign resource
- Open Policy Agent: the Rego language
- Kyverno documentation
- Kyverno: validate rules and failureAction
- Kyverno: mutate rules
- Kyverno: verifying image signatures with Cosign
- Kyverno policy library
- Red Hat: State of Kubernetes Security report









