built-in-references/Kubernetes/allowed-external-ips/template.yaml (29 lines of code) (raw):

apiVersion: templates.gatekeeper.sh/v1beta1 kind: ConstraintTemplate metadata: name: k8sazureexternalips spec: crd: spec: names: kind: K8sAzureExternalIPs validation: # Schema for the `parameters` field openAPIV3Schema: properties: allowedExternalIPs: type: array items: type: string targets: - target: admission.k8s.gatekeeper.sh rego: | package k8sazureexternalips violation[{"msg": msg}] { input.review.kind.kind == "Service" input.review.kind.group == "" allowedExternalIPs := {ip | ip := input.parameters.allowedExternalIPs[_]} externalIPs := {ip | ip := input.review.object.spec.externalIPs[_]} forbiddenIPs := externalIPs - allowedExternalIPs count(forbiddenIPs) > 0 msg := sprintf("service has forbidden external IPs: %v", [forbiddenIPs]) }