Sub topic 9
Rego basics for admission policies
This chapter assumes you know Kubernetes objects and YAML, and that you have read how admission webhooks send an AdmissionReview to a policy engine. It covers the parts of Rego (the policy language of Open Policy Agent, or OPA) that you need to read and write Gatekeeper rules.
How Rego thinks
Rego is declarative. You do not write steps. You write rules that describe a bad situation. If every condition in a rule body is true, the rule produces a result. If any condition is false or undefined, the rule produces nothing.
Gatekeeper looks for a rule named violation. Each result it produces becomes one denial message.
The input document
Gatekeeper gives your policy a JSON document named input. These are the fields you will use most:
| Path | Meaning |
|---|---|
| `input.review.object` | The resource being created or updated |
| `input.review.oldObject` | The previous version, on updates |
| `input.review.operation` | `CREATE`, `UPDATE`, `DELETE` and so on |
| `input.parameters` | Values from the Constraint's `spec.parameters` |
A first rule
This rule flags any container that uses the latest tag.
package k8snolatest
violation[{"msg": msg}] {
container := input.review.object.spec.containers[_]
endswith(container.image, ":latest")
msg := sprintf("container %v uses the latest tag", [container.name])
}Read the body line by line:
container := ...containers[_]assigns a variable. The_means "any index", so Rego checks every container.endswith(...)is a condition. If it is false, that container is skipped.msg := sprintf(...)builds the message.
All lines are joined by AND. To get OR, write several rules with the same name.
Sets and comprehensions
The required-labels template from the Gatekeeper chapters relies on set comprehensions:
provided := {label | input.review.object.metadata.labels[label]}
required := {label | label := input.parameters.labels[_]}
missing := required - provided
count(missing) > 0A comprehension builds a set from {value | conditions}. The first one collects every label key on the object. Set subtraction then gives the required labels that are absent. The last line makes the rule fire only when something is missing.
Undefined is not false
If a path does not exist, for example an object with no labels, the expression is undefined. A rule body containing an undefined expression stops silently. This is why a rule that looks like it should deny sometimes lets a request through.
package k8shostnetwork
violation[{"msg": msg}] {
input.review.object.spec.hostNetwork == true
msg := "hostNetwork is not allowed"
}If hostNetwork is not set, the comparison is undefined and no violation is produced. That is the behavior you want here. For a "must be present" check, use not, as in not input.review.object.metadata.labels.team.
Testing Rego locally
You can test logic with the opa CLI before you place it in a template. Save a sample input as input.json with the same review structure, then run:
opa eval --data policy.rego --input input.json "data.k8snolatest.violation"An empty set means no violation. A set containing msg objects means the request would be denied.
Exercise
A rule checks input.review.object.spec.securityContext.runAsNonRoot == false. A Pod has no securityContext at all. Does the rule produce a violation? Why?
Show answer
No. The path does not exist, so the comparison is undefined, not false. A rule body with an undefined expression produces nothing. To catch Pods that omit the field, add a rule using not input.review.object.spec.securityContext.runAsNonRoot.