Skip to content

Instantly share code, notes, and snippets.

@jkroepke
Created July 10, 2025 21:18
Show Gist options
  • Select an option

  • Save jkroepke/e75b403388389bed6913fb1bc6927ed7 to your computer and use it in GitHub Desktop.

Select an option

Save jkroepke/e75b403388389bed6913fb1bc6927ed7 to your computer and use it in GitHub Desktop.
Kubernetes ValidatingAdmissionPolicy to deny short namespaces.
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: deny-short-namespace-names
spec:
failurePolicy: Deny # Deny the request if the validation fails
matchConstraints:
resourceRules:
- apiGroups: [""] # "" represents the core API group for Namespaces
apiVersions: ["v1"]
operations: ["CREATE"] # Only apply this policy during creation
resources: ["namespaces"]
validations:
- expression: "object.metadata.name.length() >= 4"
message: "Namespace names must be at least 4 characters long."
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: deny-short-namespace-names-binding
spec:
policyName: deny-short-namespace-names
validationActions: [Deny] # Ensure the policy actually denies the request
matchResources:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment