Skip to content

Instantly share code, notes, and snippets.

@jmvbxx
Created March 6, 2024 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmvbxx/1c0e74c8a100d116d8b1f053470071fa to your computer and use it in GitHub Desktop.
Save jmvbxx/1c0e74c8a100d116d8b1f053470071fa to your computer and use it in GitHub Desktop.
---
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: k8sallowedrepos
spec:
crd:
spec:
names:
kind: K8sAllowedRepos
validation:
# Define schema for the parameters
openAPIV3Schema:
properties:
repos:
type: array
items:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8sallowedrepos
violation[{"msg": msg, "details": {"rejected_repo": repo}}] {
container := input.review.object.spec.containers[_]
repo := container.image
not startswith(repo, input.parameters.repos[_])
msg := sprintf("image repo is not in the allowed list: %v", [repo])
}
---
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sAllowedRepos
metadata:
name: allowed-repos
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
repos:
- "ghcr.io"
- "docker.io/official"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment