Sub topic 3
Kyverno: architecture and the shape of a policy
This chapter assumes you know Pods, labels, and kubectl. It also assumes you know that the API server can call admission webhooks. Kyverno is a policy engine that runs inside the cluster and expresses policies as Kubernetes YAML, so you don't need a separate policy language.
How Kyverno fits into admission
Kyverno installs as Deployments in its own namespace, usually kyverno. It registers validating and mutating webhook configurations with the API server. When a request matches, the API server sends an AdmissionReview to Kyverno. Kyverno evaluates your policies and answers allow, deny, or allow with a patch.
Kyverno also runs a background controller. It scans resources that already exist and produces policy reports. That is why a policy can set background: true.
The shape of a policy
A policy is a custom resource. ClusterPolicy applies cluster-wide. Policy is the namespaced version and only affects its own namespace. Both contain a list of rules.
Each rule has:
- A
name, unique within the policy. - A
matchblock, which selects the resources the rule applies to. An optionalexcludeblock removes some of them. - One action:
validate,mutate,generate, orverifyImages.
Here is a complete policy that requires a team label on every Pod.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-team-label
spec:
validationFailureAction: Enforce
background: true
rules:
- name: check-team-label
match:
any:
- resources:
kinds:
- Pod
validate:
message: "The label 'team' is required."
pattern:
metadata:
labels:
team: "?*"In match, the any list means a resource matches if it satisfies at least one entry. ?* is a wildcard that means "at least one character", so an empty label value fails.
validationFailureAction decides what happens on a violation. Enforce blocks the request. Audit lets it through and records the violation in a report. Later chapters cover this in detail.
Trying it
Apply the policy, then create a Pod without a team label.
kubectl apply -f require-team-label.yaml
kubectl run web --image=nginxThe second command fails with an error that includes your message text. Add --labels=team=platform and the Pod is created.
Check what Kyverno knows about the policy:
kubectl get clusterpolicy require-team-label
kubectl get policyreport -ACommon errors
- Policy not ready. The
READYcolumn stays false when the policy has a syntax or schema problem. Runkubectl describe clusterpolicy <name>to see why. - Controllers match more than Pods. Kyverno auto-generates extra rules for Pod controllers such as Deployments, so the policy also rejects a Deployment whose template lacks the label. This is intended, and it means failures appear earlier than the Pod.
- System namespaces blocked. A broad
matchcan reject Pods inkube-system. Useexcludeto protect them.
Exercise
A rule uses match with kinds: [Pod] and a validate.pattern of team: "?*". A Pod arrives with the label team: "". Is it admitted under Enforce, and why?
Show answer
No. The pattern ?* requires at least one character, so an empty value fails the check and Kyverno rejects the request with the policy's message.