TExcelTAD
Home › Kubernetes Packaging: Helm & Kustomize › Replacements & Environment Config

Replacements & Environment Config

Some values appear in several places: linkly's public hostname is in the HTTPRoute (hostnames) and in the app's BASE_URL setting, which it uses to build short links. Patch one and forget the other, and production hands out links to the staging domain. Kustomize has no variables in the template sense, but replacements copy a value from one field of one object into fields of others, at build time.

Copying a value

The production overlay sets the hostname once, on the HTTPRoute; a replacement copies it into the ConfigMap. options.delimiter and index let it replace only part of a string, here the host part of https://<host>:

bash · try it yourself
mkdir -p base production
cat > base/resources.yaml <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: linkly
spec:
  parentRefs: [{name: public, namespace: gateway-system}]
  hostnames: [lnk.example.com]
  rules: [{backendRefs: [{name: linkly, port: 80}]}]
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: linkly-config
data:
  BASE_URL: https://placeholder.invalid
  LOG_LEVEL: info
EOF
cat > base/kustomization.yaml <<'EOF'
resources: [resources.yaml]
replacements:
  - source:
      kind: HTTPRoute
      name: linkly
      fieldPath: spec.hostnames.0
    targets:
      - select: {kind: ConfigMap, name: linkly-config}
        fieldPaths: [data.BASE_URL]
        options: {delimiter: "//", index: 1}
EOF
cat > production/kustomization.yaml <<'EOF'
resources: [../base]
patches:
  - target: {kind: HTTPRoute, name: linkly}
    patch: |-
      - op: replace
        path: /spec/hostnames
        value: [go.acme.example]
EOF
echo "--- base:"; kustomize build base | yq 'select(.kind == "ConfigMap") | .data.BASE_URL'
echo "--- production:"; kustomize build production | yq 'select(.kind == "ConfigMap") | .data.BASE_URL'
--- base:
https://lnk.example.com
--- production:
https://lnk.example.com

Production's BASE_URL still says lnk.example.com. The replacement is declared in the base, so it ran when the base was built, before the production patch changed the hostname. Replacements run at the level where they're declared, against what has been accumulated there. Move the replacement to where the final value is known:

bash · try it yourself
mkdir -p components/base-url base production
cat > base/resources.yaml <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: linkly
spec:
  parentRefs: [{name: public, namespace: gateway-system}]
  hostnames: [lnk.example.com]
  rules: [{backendRefs: [{name: linkly, port: 80}]}]
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: linkly-config
data:
  BASE_URL: https://placeholder.invalid
EOF
printf 'resources: [resources.yaml]\ncomponents: [../components/base-url]\n' > base/kustomization.yaml
cat > components/base-url/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
replacements:
  - source: {kind: HTTPRoute, name: linkly, fieldPath: spec.hostnames.0}
    targets:
      - select: {kind: ConfigMap, name: linkly-config}
        fieldPaths: [data.BASE_URL]
        options: {delimiter: "//", index: 1}
EOF
cat > production/kustomization.yaml <<'EOF'
resources: [../base]
components: [../components/base-url]
patches:
  - target: {kind: HTTPRoute, name: linkly}
    patch: |-
      - op: replace
        path: /spec/hostnames
        value: [go.acme.example]
EOF
echo "--- base:"; kustomize build base | yq 'select(.kind == "ConfigMap") | .data.BASE_URL'
echo "--- production:"; kustomize build production | yq 'select(.kind == "ConfigMap") | .data.BASE_URL'
--- base:
https://lnk.example.com
--- production:
https://lnk.example.com

Production still shows the old host. Components run before the overlay's own patches, as Components explained, so the replacement again saw the hostname before the patch. The fix is to make the replacement run after the change: put the hostname change itself in a component listed first, or declare the replacement in the overlay's own replacements field, which runs after its patches:

bash · try it yourself
mkdir -p base production
cat > base/resources.yaml <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: linkly
spec:
  parentRefs: [{name: public, namespace: gateway-system}]
  hostnames: [lnk.example.com]
  rules: [{backendRefs: [{name: linkly, port: 80}]}]
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: linkly-config
data:
  BASE_URL: https://placeholder.invalid
EOF
printf 'resources: [resources.yaml]\n' > base/kustomization.yaml
cat > production/kustomization.yaml <<'EOF'
resources: [../base]
patches:
  - target: {kind: HTTPRoute, name: linkly}
    patch: |-
      - op: replace
        path: /spec/hostnames
        value: [go.acme.example]
replacements:
  - source: {kind: HTTPRoute, name: linkly, fieldPath: spec.hostnames.0}
    targets:
      - select: {kind: ConfigMap, name: linkly-config}
        fieldPaths: [data.BASE_URL]
        options: {delimiter: "//", index: 1}
EOF
kustomize build production | yq 'select(.kind == "ConfigMap") | .data.BASE_URL'
https://go.acme.example

The lesson generalises: in Kustomize, where a transformation is declared decides what it sees. When a result surprises you, ask which kustomization the field, patch or replacement lives in, and what had been built at that point.

Replacements vs vars

Older kustomizations use vars with $(VAR) placeholders in fields. vars are deprecated; kustomize edit fix --vars converts them to replacements (review the result, it may rewrite resource files). Replacements also work in fields that vars never supported, and can select many targets with select/reject and wildcards in fieldPaths (spec.template.spec.containers.*.env.[name=BASE_URL].value).

Patterns for environment config

Need Pattern
a few settings per environment configMapGenerator with behavior: merge in each overlay
many settings per environment a config.env file per overlay, envs: [config.env]
one value used in several objects set it once, copy it with replacements declared where the final value exists
a feature on in some environments a component
image tag per environment images: in the overlay, set by CI with kustomize edit set image
secrets per environment references to Secrets managed outside Git, never generators with real values

Test yourself

A replacement declared in the base copies the HTTPRoute hostname into a ConfigMap. Why doesn't the production overlay's hostname patch reach the ConfigMap?

  1. Replacements only work on Secrets
  2. The base's replacement ran when the base was built, before the overlay's patch
  3. Patches run before resources
  4. delimiter is wrong

What does options: {delimiter: "//", index: 1} do in a replacement target?

  1. Copies the value twice
  2. Splits the target string on // and replaces only the part at index 1
  3. Escapes slashes
  4. Selects the second target

What replaces the deprecated vars?

  1. patches
  2. replacements
  3. components
  4. configurations