Sub topic 11
Applying policy with Constraints
This chapter assumes you have read the previous chapter on ConstraintTemplates. You should also know Pods, Namespaces and labels, and be able to use kubectl.
Template versus Constraint
A ConstraintTemplate defines a new policy kind and its Rego logic. A Constraint is an instance of that kind. It says which resources the policy applies to, what parameters it uses, and what happens on a violation. One template can back many Constraints, each with different scope and parameters.
Anatomy of a Constraint
The Constraint below uses the K8sRequiredLabels template from the previous chapter.
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: pods-must-have-team
spec:
enforcementAction: deny
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
labels: ["team"]The kind is the name the template declared. The spec has three parts:
matchselects which objects are evaluated.parametersis passed to Rego asinput.parameters. Its shape must follow the template'sopenAPIV3Schema.enforcementActionsets the response to a violation.denyrejects the request.
Matching
Apart from kinds, match supports these fields:
| Field | Effect |
|---|---|
| `namespaces` | Only these namespaces. Names ending in `*` act as prefixes. |
| `excludedNamespaces` | Skip these namespaces. |
| `namespaceSelector` | Select namespaces by their labels. |
| `labelSelector` | Select objects by their own labels. |
| `scope` | `Namespaced`, `Cluster` or `*`. |
All fields must match for a Constraint to apply. This example covers only Pods in namespaces labeled env: prod:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
namespaceSelector:
matchLabels:
env: prodApplying and checking
Apply the template first and wait for its CRD to be created. A Constraint applied too early fails because its kind does not exist yet.
kubectl apply -f template.yaml
kubectl apply -f constraint.yaml
kubectl run web --image=nginxThe last command should be rejected, and the error names the Constraint and prints the Rego message:
admission webhook "validation.gatekeeper.sh" denied the request:
[pods-must-have-team] missing required labels: {"team"}Run kubectl get k8srequiredlabels to list Constraints. kubectl describe shows the violations found by audit.
Common errors
- Constraint rejected as unknown kind. The template was not applied, or its CRD is not ready yet. Check
kubectl get constrainttemplatefor status. - Policy never fires. The
apiGroupsvalue is wrong. Core resources such as Pods use[""]. Deployments use["apps"]. - Pods are blocked but Deployments are accepted. The Constraint matches
Podonly. The Deployment is admitted, and its ReplicaSet then fails to create Pods. Match both kinds if you want early feedback. - Parameters ignored. The key in
parametersdoes not match the schema, or Rego reads a different name.
Exercise
You want one K8sRequiredLabels policy to require owner on Namespaces and team on Pods. Do you need a second ConstraintTemplate?
Show answer
No. Create two Constraints of the same kind. One matches Namespace with parameters.labels: ["owner"]. The other matches Pod with parameters.labels: ["team"].