Gatekeeper
An OPA integration tool for Kubernetes
An OPA integration tool for Kubernetes
Gatekeeper is an admission controller that integrates OPA (Open Policy Agent) into a Kubernetes cluster to enforce declarative policies. It allows or denies all Kubernetes resource creation/modification requests, such as Pod, Service, and Ingress, according to policies.
Gatekeeper uses two CRDs called ConstraintTemplate and Constraint. ConstraintTemplate defines the policy logic in the Rego language, and Constraint applies that template to specific resources. This allows policies such as "prohibit privileged containers," "allow only images from specific registries," and "require resource limits" to be managed declaratively.
The OPA Gatekeeper library (gatekeeper-library) provides ready-to-use policy templates. Policies that implement Pod Security Standards (PSS) with Gatekeeper, CIS benchmark policies, and others can be applied right away.
It is often compared with Kyverno: Gatekeeper can express complex policies in the Rego language, while Kyverno is YAML-based and has a lower learning curve. The choice depends on the complexity of the organization's policies and the team's Rego proficiency.
# Gatekeeper μ€μΉ
helm repo add gatekeeper https://open-policy-agent.github.io/gatekeeper/charts
helm install gatekeeper gatekeeper/gatekeeper -n gatekeeper-system --create-namespace
# ConstraintTemplate: νμ©λ λ μ§μ€νΈλ¦¬λ§ μ΄λ―Έμ§ μ¬μ©
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8sallowedrepos
spec:
crd:
spec:
names:
kind: K8sAllowedRepos
validation:
openAPIV3Schema:
type: object
properties:
repos:
type: array
items:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8sallowedrepos
violation[{"msg": msg}] {
container := input.review.object.spec.containers[_]
satisfied := [good | repo = input.parameters.repos[_]; good = startswith(container.image, repo)]
not any(satisfied)
msg := sprintf("container <%v> has an invalid image repo <%v>, allowed repos are %v", [container.name, container.image, input.parameters.repos])
}
---
# Constraint: νλ‘λμ
λ€μμ€νμ΄μ€μ μ μ©
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sAllowedRepos
metadata:
name: prod-allowed-repos
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
namespaces: ["production"]
parameters:
repos:
- "ghcr.io/myorg/"
- "gcr.io/myproject/"
# μ μ±
μλ° ν
μ€νΈ
kubectl run nginx --image=nginx -n production
# Error: admission webhook "validation.gatekeeper.sh" denied the request:
# container has an invalid image repo , allowed repos are ["ghcr.io/myorg/", "gcr.io/myproject/"]
Platform: "We found a privileged container in production. Let's add a blocking policy with Gatekeeper."
Dev team: "Our monitoring agent needs privileged. What should we do?"
Platform: "We can handle exceptions. You can exclude the kube-system namespace, or create a Constraint that allows only a specific ServiceAccount."
Interviewer: "How would you manage Kubernetes policies?"
Candidate: "I would implement admission policies with OPA Gatekeeper. I would apply the PSS policies from gatekeeper-library as the baseline and add custom ConstraintTemplates that fit the organization's requirements. I would use Audit mode to understand the current violations among existing resources and gradually switch to Deny."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
Reviewer: "In this ConstraintTemplate Rego, I think using count(satisfied) == 0 instead of not any(satisfied) would be more readable."
Author: "Agreed. I'll also add the namespace to the violation message."