Gatekeeper
OPA Gatekeeper is an admission
controller/policy engine for Kubernetes. It enforces (or dry-run reports on) custom policies,
written in Rego, against incoming
API requests via ConstraintTemplate/Constraint CRDs.
This wrapper (chart version 3.11.0) pins the upstream gatekeeper/gatekeeper chart (currently
3.23.0, from https://open-policy-agent.github.io/gatekeeper/charts) and ships two
KubeAid-authored policies plus a network policy.
Why it's in KubeAid
Gatekeeper is KubeAid's admission-time policy engine — it can block or flag Kubernetes objects that don't meet cluster rules before they're created, as opposed to Kyverno's chart in this repo, which in KubeAid is scoped specifically to mutating image references for Harbor proxy-cache (see ../kyverno/README.md) rather than general admission policy.
Prerequisites
- The OPA CLI to run policy unit tests locally
(
make test, see below).
Key values / KubeAid-specific configuration
networkPolicy: true(default) — renderstemplates/networkpolicy.yaml, a Calico-flavoredNetworkPolicy(crd.projectcalico.org/v1) restricting the Gatekeeper pod's egress to the apiserver on 443.policy.RequireRequestCpuMemAndLimitMem: true(default) — enablestemplates/K8sRequiredResources.yaml, aConstraintTemplate/K8sRequiredResourcesconstraint (backed bypolicies/K8sRequiredResources.rego) requiring every Pod to set CPU+memory requests and a memory limit.policy.CronJobForbidConcurrency— enablestemplates/CronJobForbidConcurrency.yaml, intended to require CronJobs to setconcurrencyPolicy: Forbid.policy.ExcludedNamespaces— list of namespaces excluded from both constraints above.policy.EnforcementAction(defaultdryrunwhen unset) — passed straight through to each constraint'senforcementAction; set todenyto actually block non-conforming objects instead of just reporting violations.gatekeeper.revisionHistoryLimit: 0.
Operational notes
templates/CronJobForbidConcurrency.yamlloads its Rego body via.Files.Get "policies/CronJobsPolicy.rego", but the file actually shipped underpolicies/is namedCronJobForbidConcurrency.rego..Files.Getsilently returns an empty string for a missing path, so as currently wired thisConstraintTemplaterenders with no policy logic — worth fixing or confirming intentional before relying onpolicy.CronJobForbidConcurrency.- Policies live under
policies/*.regowith matching*_test.regofiles, and are unit-tested withmake test(wrapsopa test -v ./policies) — seeargocd-helm-charts/gatekeeper/policiesandMakefile. - Because ArgoCD renders everything from
helm templatein one pass, make sureConstraintTemplates sync before their corresponding Constraint CRs — Gatekeeper generates the CRD for each policy from the template, so a Constraint applied first will fail. - If Gatekeeper pods CrashLoopBackOff on bare-metal clusters after enabling the network policy,
that's usually the readiness/liveness probes failing under the policy; set
networkPolicy: falseas a workaround. - If
kubectl describe <ConstraintKind>shows a violation count higher than the violations listed, raiseconstraintViolationsLimitin the upstream Gatekeeper values. - Check violations for the shipped policies with
kubectl describe K8sRequiredResources/kubectl describe CronJobForbidConcurrency.
Adding a new policy
-
Write a
.regofile describing the policy (Constraint Framework) underargocd-helm-charts/gatekeeper/policies. -
Add a
ConstraintTemplatethat imports the policy, and aConstraintdescribing which Kubernetes objects it applies to; combine both into a single manifest underargocd-helm-charts/gatekeeper/templates. -
Test locally with the OPA CLI:
cd argocd-helm-charts/gatekeepermake test -
Verify the rendered objects with
helm templatebefore pushing.
Docs links
- Upstream chart: https://github.com/open-policy-agent/gatekeeper/tree/master/charts/gatekeeper
- Gatekeeper docs: https://open-policy-agent.github.io/gatekeeper/website/docs/howto
- Policy library: https://github.com/open-policy-agent/gatekeeper-library
- Rego: https://www.openpolicyagent.org/docs/latest/policy-language/
- Constraint Framework: https://github.com/open-policy-agent/frameworks/tree/master/constraint