Sub topic 7
Applying policy with namespace labels: enforce, audit, warn
This chapter assumes you know Pods, Deployments, and Namespaces, can read YAML manifests, and have used kubectl. You should also know the three Pod Security Standards profiles (Privileged, Baseline, Restricted) from earlier chapters.
The label model
Pod Security Admission (PSA) reads its configuration from labels on a Namespace. Each label has this form:
pod-security.kubernetes.io/<mode>: <level>There are three modes and three levels. You can combine any mode with any level, and you can set all three modes on one namespace.
| Mode | What happens on a violation |
|---|---|
| `enforce` | The API server rejects the pod. |
| `audit` | The pod is admitted. The violation is recorded as an annotation on the audit log event. |
| `warn` | The pod is admitted. The client receives a warning, which `kubectl` prints. |
The levels are privileged, baseline, and restricted. They match the profiles you have already seen.
A namespace with all three modes
The following namespace blocks pods that violate Baseline. It also reports anything that would violate Restricted, without blocking it.
apiVersion: v1
kind: Namespace
metadata:
name: payments
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restrictedSetting enforce lower than audit and warn is a common pattern. It protects the namespace from the worst settings now. It also shows you how much work remains before you can tighten the policy.
Seeing the modes work
Apply the namespace, then try a pod that uses hostNetwork and a privileged container.
apiVersion: v1
kind: Pod
metadata:
name: privileged-demo
namespace: payments
spec:
hostNetwork: true
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]
securityContext:
privileged: trueBaseline forbids both settings, so enforce rejects the pod with a Forbidden error. The message names the violated controls, such as hostNetwork=true and privileged.
Now try a pod that passes Baseline but not Restricted, such as a plain busybox pod with no securityContext. The pod is created. Because warn is set to restricted, kubectl prints a warning listing the missing fields, for example allowPrivilegeEscalation != false. The same violations go into the audit log because of the audit label.
A pod that sets the required fields passes all three checks and produces no output:
apiVersion: v1
kind: Pod
metadata:
name: restricted-demo
namespace: payments
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]Choosing a mode
- Use
enforcewhen you are sure workloads comply, or when the level only blocks settings nobody should be using. - Use
warnto give developers immediate feedback when they apply manifests. - Use
auditto collect violations from controllers and automation that no person is watching. - Use
warnandaudittogether at a stricter level thanenforceto plan the next tightening step.
Common errors
- Wrong label key. A typo such as
pod-security.kubernetes.io/enfroceis not an error. The label is ignored and nothing is enforced. Check labels withkubectl get ns payments --show-labels. - Invalid level. An unrecognized level value is ignored and reported in the API server's warnings and logs. Use only
privileged,baseline, orrestricted. - Expecting existing pods to stop.
enforceis checked when a pod is created or updated. Pods that are already running keep running. - Unlabeled namespaces. Without labels, the cluster default applies. Unless an administrator has set one, that default is
privileged, which allows everything.
Exercise
A namespace has the labels enforce: baseline and warn: restricted. You apply a pod that meets Baseline but sets no seccompProfile. What happens, and where do you see it?
Show answer
The pod is admitted, because it passes the enforce level (Baseline). The warn level (Restricted) requires a seccompProfile, so the API server returns a warning, and kubectl prints it in the terminal right after the create or apply command.