Skip to content

Overlays — One App, Many Places

Outcome: two overlays built over the same base, and every difference between their outputs and the base's traced to the line in an overlay file that caused it — using the transformer fields that do most of kustomize's daily work.

An overlay is a folder whose kustomization lists a base under resources: and then states its differences. Nothing else about it is special: it builds with the same command and prints the same kind of stream.

Staging

The lab's staging overlay, in full (transcript t03):

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../../base
namePrefix: staging-
commonLabels:
  env: staging
commonAnnotations:
  team.example.com/contact: web-platform
images:
  - name: webhello
    newTag: 1.1.0
replicas:
  - name: webhello
    count: 1
patches:
  - target:                 # chapter 7's subject;
      kind: Deployment      # it adds LOG_LEVEL=debug
      name: webhello
    patch: |-
      - op: add
        path: /spec/template/spec/containers/0/env/-
        value:
          name: LOG_LEVEL
          value: debug

Five differences, each one line of intent:

  • namePrefix: staging- — every resource is renamed: staging-webhello, staging-webhello-config-t9599k79c5, and so on. The references follow, as they did in chapter 4: the Deployment's secret reference now reads staging-webhello-secrets-b955cdg665, its volume the staging ConfigMap. Two environments can now live in one namespace without a single name collision.
  • commonLabels stamps env: staging onto every resource — metadata, pod template, everywhere a label belongs — so kubectl get pods -l env=staging has something to find.
  • commonAnnotations stamps the contact address the same way. Annotations carry information for people and tools; labels carry it for selectors. The split is Kubernetes', and kustomize stamps both kinds.
  • images: finds the container using image webhello and sets its tag to 1.1.0 — by name, without knowing or caring which file or line the image lives on.
  • replicas: sets the Deployment named webhello to one replica: staging is a rehearsal, and one copy is a cheaper rehearsal.

Build it, then diff it against the base build (transcript t05):

$ kustomize build app/overlays/staging > staging.yaml
$ diff base.yaml staging.yaml

The diff is longer than five lines — renames ripple into references — but every hunk belongs to one of the five statements above. Nothing else moved. That auditability is the point of the exercise, and chapter 10 builds a habit on it.

The warning you will see

Building either overlay prints this to standard error (kept in the transcripts, t03 and t04):

# Warning: 'commonLabels' is deprecated. Please use 'labels' instead. Run 'kustomize edit fix' to update your Kustomization automatically.

The build succeeds; the tool is giving notice. commonLabels and commonAnnotations are the original fields; current kustomize prefers the labels: and annotations: transformer fields, which spell out whether the labels should also enter selectors (includeSelectors) and exactly which label positions they stamp. The lab keeps the older fields because a great deal of real-world kustomization — and the documentation you will search — still uses them, and because the warning itself is worth meeting in a guide rather than in production. Chapter 9 runs kustomize edit fix on a copy and shows the converted file, selectors decision and all. Whichever spelling you choose, choose it deliberately: a label that enters a Deployment's selector is a label you can never change again on that Deployment, because selectors are immutable.

Production, briefly

The production overlay (chapter 7 and 8 take it apart) uses the same fields — prod- prefix, env: production, image 1.2.0 — and then goes past them: where staging's statements fit the transformer fields, production's remaining differences (four replicas and larger memory limits, a different greeting) do not, and are expressed as a patch and a replacement instead. Same base, same audit: build it, diff it (t04, t05), and every hunk still has a named cause.

Two habits to take from this chapter:

  • The base never mentions an environment. If you find yourself editing the base "for production", the fact you are adding belongs in an overlay.
  • An overlay should read like a list of differences — because that is all it is. When an overlay file grows longer than the base, the base is probably in the wrong shape.