Sub topic 10
Writing a ConstraintTemplate
This chapter assumes you know basic Rego syntax (sets, comprehensions, rules) and how Gatekeeper sits behind a validating admission webhook. A ConstraintTemplate packages a Rego policy and defines a new Kubernetes resource kind for using it.
What a template does
A ConstraintTemplate does two jobs:
- It declares a new custom resource kind (a Constraint kind) and the schema of its parameters.
- It holds the Rego code that decides whether a request violates the policy.
When you apply a template, Gatekeeper creates a CRD for the new kind. You then create Constraints of that kind to put the policy into effect.
A complete template
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8srequiredlabels
spec:
crd:
spec:
names:
kind: K8sRequiredLabels
validation:
openAPIV3Schema:
type: object
properties:
labels:
type: array
items:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8srequiredlabels
violation[{"msg": msg}] {
provided := {label | input.review.object.metadata.labels[label]}
required := {label | label := input.parameters.labels[_]}
missing := required - provided
count(missing) > 0
msg := sprintf("missing required labels: %v", [missing])
}How the pieces work
spec.crd.spec.names.kindis the kind of the generated CRD.metadata.namemust be the lowercase form of this kind, or Gatekeeper rejects the template.openAPIV3Schemadescribes theparametersa Constraint may carry. Without a schema, parameters are not validated and typos pass silently.targetselects the integration point. For admission control it is alwaysadmission.k8s.gatekeeper.sh.- Gatekeeper evaluates every rule named
violation. Each result that the rule produces is a separate violation. Amsgfield is required. - Rego exposes the object under review as
input.review.object. It exposes the Constraint's parameters asinput.parameters.
In the example, provided is the set of label keys on the object. required is the set from the Constraint. The rule body fires only when the set difference is not empty.
Checking that it loaded
kubectl apply -f k8srequiredlabels-template.yaml
kubectl get constrainttemplate k8srequiredlabels -o jsonpath='{.status.created}'
kubectl get crd k8srequiredlabels.constraints.gatekeeper.shstatus.created should be true. If it is not, run kubectl describe constrainttemplate k8srequiredlabels. Rego compile errors appear in the status.
Common errors
| Symptom | Likely cause |
|---|---|
| Template rejected on apply | `metadata.name` is not the lowercase of the kind |
| Rego compile error in status | Syntax error, or a variable used before it is assigned |
| Policy never fires | The rule is not named `violation`, or the `input` path is wrong |
| Parameters ignored | The schema does not declare them, so the field is pruned |
Exercise
A teammate renames the template's kind to K8sRequireLabels but leaves metadata.name: k8srequiredlabels. What happens, and how do they fix it?
Show answer
Gatekeeper rejects the template because the name must equal the lowercase of the kind. The fix is to set metadata.name to k8srequirelabels, and to update the package name to match, as the convention suggests.