TExcelTAD

A Tenant Namespace Kit

A tenant kit is the set of objects every team's namespace gets, rendered from one list of teams. Adding a team is then a three-line pull request, and fixing the kit (a missing DNS rule, a new label) fixes every namespace at once through GitOps. This lesson builds the kit as a Helm chart, renders it for two teams and validates the output.

Design

Decision In the kit
Quotas as t-shirt sizes, not free numbers size: s or m maps to CPU, memory and Pod counts; changing a team's size is a reviewed one-word change
No LoadBalancer or NodePort Services services.loadbalancers: "0", services.nodeports: "0": traffic enters through the shared Gateway
Defaults for containers that forget requests a LimitRange with default requests and limits, and a max
Pod Security restricted namespace labels; Pods that run as root or privileged are rejected
RBAC from identity-provider groups admin for team leads, edit for developers (built-in ClusterRoles bound per namespace)
Network default deny in and out, then allow same-namespace traffic, the Gateway and Prometheus namespaces in, and DNS out
Cost labels acme.io/tenant and acme.io/cost-center on the namespace, read by OpenCost

The kit as a chart

bash · try it yourself
mkdir -p tenants/templates && cd tenants
cat > Chart.yaml <<'EOF'
apiVersion: v2
name: tenants
description: One namespace kit per team
version: 1.0.0
EOF
cat > values.yaml <<'EOF'
sizes:                     # t-shirt sizes for quotas
  s: {cpu: "4", memory: 8Gi, pods: "40"}
  m: {cpu: "16", memory: 32Gi, pods: "150"}
tenants:
  - name: team-growth
    size: s
    costCenter: cc-4410
    admins: [growth-leads]
    developers: [growth-devs]
  - name: team-links
    size: m
    costCenter: cc-4420
    admins: [links-leads]
    developers: [links-devs]
EOF
cat > templates/tenant.yaml <<'EOF'
{{- range .Values.tenants }}
{{- $q := index $.Values.sizes .size }}
---
apiVersion: v1
kind: Namespace
metadata:
  name: {{ .name }}
  labels:
    acme.io/tenant: {{ .name }}
    acme.io/cost-center: {{ .costCenter }}
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/enforce-version: latest
---
apiVersion: v1
kind: ResourceQuota
metadata: {name: tenant-quota, namespace: {{ .name }}}
spec:
  hard:
    requests.cpu: {{ $q.cpu | quote }}
    requests.memory: {{ $q.memory }}
    limits.memory: {{ $q.memory }}
    pods: {{ $q.pods | quote }}
    services.loadbalancers: "0"
    services.nodeports: "0"
---
apiVersion: v1
kind: LimitRange
metadata: {name: tenant-defaults, namespace: {{ .name }}}
spec:
  limits:
    - type: Container
      default: {cpu: 500m, memory: 256Mi}
      defaultRequest: {cpu: 50m, memory: 64Mi}
      max: {memory: 4Gi}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: {name: tenant-admins, namespace: {{ .name }}}
roleRef: {apiGroup: rbac.authorization.k8s.io, kind: ClusterRole, name: admin}
subjects:
{{- range .admins }}
  - {apiGroup: rbac.authorization.k8s.io, kind: Group, name: {{ . }}}
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: {name: tenant-developers, namespace: {{ .name }}}
roleRef: {apiGroup: rbac.authorization.k8s.io, kind: ClusterRole, name: edit}
subjects:
{{- range .developers }}
  - {apiGroup: rbac.authorization.k8s.io, kind: Group, name: {{ . }}}
{{- end }}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: default-deny, namespace: {{ .name }}}
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: allow-baseline, namespace: {{ .name }}}
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]
  ingress:
    - from:
        - podSelector: {}
        - namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: gateway-system}}
        - namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: monitoring}}
  egress:
    - to: [{podSelector: {}}]
    - to: [{namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: kube-system}}}]
      ports: [{protocol: UDP, port: 53}, {protocol: TCP, port: 53}]
{{- end }}
EOF
helm lint . | tail -1
helm template tenants . > rendered.yaml
yq -N '.kind + "/" + .metadata.name' rendered.yaml | sort | uniq -c
kubeconform -n 1 -strict -summary rendered.yaml
yq 'select(.kind == "ResourceQuota" and .metadata.namespace == "team-links") | .spec.hard' rendered.yaml
1 chart(s) linted, 0 chart(s) failed
      2 LimitRange/tenant-defaults
      1 Namespace/team-growth
      1 Namespace/team-links
      2 NetworkPolicy/allow-baseline
      2 NetworkPolicy/default-deny
      2 ResourceQuota/tenant-quota
      2 RoleBinding/tenant-admins
      2 RoleBinding/tenant-developers
Summary: 14 resources found in 1 file - Valid: 14, Invalid: 0, Errors: 0, Skipped: 0
requests.cpu: "16"
requests.memory: 32Gi
limits.memory: 32Gi
pods: "150"
services.loadbalancers: "0"
services.nodeports: "0"

Fourteen objects, seven per team, all valid against the Kubernetes 1.34 schemas in strict mode. team-links got the m quota.

Things the kit deliberately leaves to the team:

  • Egress beyond DNS. Each app adds its own NetworkPolicy for what it needs (its database, an external API). The default deny makes those dependencies explicit and reviewable.
  • Application RBAC. CI or Argo CD deploys with its own service account and a narrower Role (next lesson).

Delivering it with GitOps

The chart lives in the platform's GitOps repository and is applied by Argo CD with cluster-wide rights (namespaces, quotas and RBAC are platform objects; teams can't change them). Adding a team:

yaml
# platform-deploy/tenants/values.yaml (the only file teams' PRs touch)
tenants:
  - name: team-qr
    size: s
    costCenter: cc-4430
    admins: [qr-leads]
    developers: [qr-devs]

An Argo CD Application (sample) points at the chart; its ignoreDifferences aren't needed, because nothing else should change these objects:

yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: tenants
  namespace: argocd
spec:
  project: platform
  source:
    repoURL: https://github.com/acme/platform-deploy.git
    targetRevision: main
    path: tenants
  destination:
    server: https://kubernetes.default.svc
  syncPolicy:
    automated: {prune: false, selfHeal: true}

prune: false is deliberate: deleting a tenant from the list should not delete its namespace (and every workload in it) on the next sync. Off-boarding a team is a separate, explicit procedure. (Argo CD's per-resource argocd.argoproj.io/sync-options: Delete=false annotation is the finer-grained alternative.)

Test yourself

Why does the kit set services.loadbalancers: "0" in the quota?

  1. Load balancers are deprecated
  2. So teams can't create their own cloud load balancers; traffic enters through the shared Gateway
  3. To save memory
  4. Quotas require it

Why does the tenants Application use prune: false?

  1. Pruning is slow
  2. Removing a team from the values file must not silently delete its namespace and workloads
  3. Argo CD can't prune namespaces
  4. It enables self-heal

What does the LimitRange in the kit do for a container without resource requests?

  1. Rejects it
  2. Gives it the default requests and limits
  3. Ignores it
  4. Moves it to another namespace