Lesson 07 of 11 · Self-service with guard-rails
Self-service onboarding
Let teams onboard themselves while the platform guarantees the guard-rails: a namespace request through Git or a template, and a policy engine that generates quotas, default limits, default-deny networking and access for every team namespace on every cluster, and puts them back if someone deletes them.
Onboarding without tickets
Every team needs the same starting kit on every cluster: a namespace, access for its group, a resource quota, default resource limits and a default-deny network policy. Doing this by ticket doesn't scale, and copying it into every team's manifests means some teams forget, or "temporarily" delete the network policy.
The pattern: the team asks; the platform generates the guard-rails.
team request (Backstage template or PR) → namespace with label platform.example.io/team=shop
│ (synced to every cluster by GitOps)
policy engine on each cluster sees the label ─┘
generates: RoleBinding for the team's IdP group, ResourceQuota, LimitRange,
default-deny NetworkPolicy (with same-namespace and DNS allowed)
and puts them back if anyone deletes or edits them
When a new club is registered, the office automatically gives it a room key for its members, a shelf of a fixed size, and a rule card on the door. If someone takes the rule card down, the caretaker puts a new one up the next morning.
The generate policy
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: team-onboarding
spec:
rules:
- name: default-deny-networkpolicy
match:
any:
- resources:
kinds: [ Namespace ]
selector:
matchExpressions: [ { key: platform.example.io/team, operator: Exists } ]
generate:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
name: team-default-deny
namespace: "{{ request.object.metadata.name }}"
synchronize: true
data:
spec:
podSelector: {}
policyTypes: [ Ingress, Egress ]
ingress:
- from: [ { podSelector: {} } ] # same namespace only
egress:
- to: [ { podSelector: {} } ]
- to:
- namespaceSelector: { matchLabels: { kubernetes.io/metadata.name: kube-system } }
podSelector: { matchLabels: { k8s-app: kube-dns } }
ports: [ { protocol: UDP, port: 53 }, { protocol: TCP, port: 53 } ]
- name: resource-quota
match:
any:
- resources:
kinds: [ Namespace ]
selector:
matchExpressions: [ { key: platform.example.io/team, operator: Exists } ]
generate:
apiVersion: v1
kind: ResourceQuota
name: team-quota
namespace: "{{ request.object.metadata.name }}"
synchronize: true
data:
spec:
hard: { requests.cpu: "2", requests.memory: 4Gi, limits.memory: 8Gi, pods: "20" }
- name: limit-range
match:
any:
- resources:
kinds: [ Namespace ]
selector:
matchExpressions: [ { key: platform.example.io/team, operator: Exists } ]
generate:
apiVersion: v1
kind: LimitRange
name: team-defaults
namespace: "{{ request.object.metadata.name }}"
synchronize: true
data:
spec:
limits:
- type: Container
default: { cpu: 500m, memory: 256Mi }
defaultRequest: { cpu: 100m, memory: 128Mi }
Kyverno ClusterPolicy is deprecated
From Kyverno 1.19, kyverno.io/v1 ClusterPolicy prints a deprecation warning: it will be removed in a future release in favour of the CEL-based ValidatingPolicy, MutatingPolicy, GeneratingPolicy and ImageValidatingPolicy (policies.kyverno.io). The example above still works today; write new policies in the new kinds (migration guide). Kubernetes' built-in admission policies can't create objects, so generate rules like these stay with Kyverno (GeneratingPolicy) or move into a GitOps chart.
Add a fourth rule generating a RoleBinding that binds a tenant role (such as tenant-developer from the multi-tenant SaaS course, not the built-in edit, which can delete NetworkPolicies) to the team's IdP group, read from another label or annotation on the namespace.
Note the egress rule: namespace and pod selectors in the same entry (AND), so only CoreDNS is allowed, not all of kube-system.
Ship policies like any other add-on
The policy engine and the policies are platform add-ons, delivered to every cluster by GitOps:
- An ApplicationSet with a
clustersgenerator installs the Kyverno Helm chart on every labelled cluster. UseServerSideApply=true: its CRDs are too large for client-side apply. - A second ApplicationSet syncs a
platform/policiesrepository to every cluster. Order matters: CRDs must exist before policies (sync waves, orSkipDryRunOnMissingResource=true). - Roll policy changes out by ring, and new validation policies in Audit mode first.
Admission webhooks are in the critical path
If the policy engine's webhook is down and configured to fail closed, nobody can create pods. Run it with several replicas and a PodDisruptionBudget, exclude system namespaces, alert on webhook errors and latency, and keep a runbook for removing a dangling webhook configuration.
The request side
Two equally good front doors:
- A Backstage template "Onboard a team" that opens a pull request adding the namespace (with labels) to the platform's GitOps repository.
- A pull request directly, for teams that prefer Git.
Either way, the change is reviewed, recorded and rolled out to every cluster by the same machinery as everything else.
Try it
Install Kyverno on both workload clusters through an ApplicationSet, sync the policy above from a Gitea repository, then create namespace team-shop with label platform.example.io/team: shop (also via Git). Check the generated objects on both clusters. Delete the NetworkPolicy and watch it come back. Finally, deploy a pod without resource settings and confirm the LimitRange defaults were applied.
Recap
- Teams request namespaces; the platform generates quotas, limits, default-deny networking and access from a label.
synchronize: truemakes guard-rails self-healing.- Policies and the engine are fleet add-ons delivered by GitOps, rolled out by ring, with the webhook run like critical infrastructure.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.