Skip to content

Replacements do not work with fields that were set in a parent kustomization. #4099

@zero-below

Description

@zero-below

Describe the bug

replacements does not work if the source resource comes from above the replacements in the dependency graph. This is the case whether kustomizations come as Component or Kustomization files.

Note, the example below uses commonAnnotations to simplify the demonstration, however the same behavior occurs with static resources (such as a configmap.yaml file that sets an annotation statically) -- and it occurs with other fields, not solely metadata.annotations.

The use case:
I have a top level kustomization.yaml for each deployed environment (dev/prod/test/etc) that adds commonAnnotations to all of the resources in its tree. It then does a resources: [ ../k8s/base/ ], and in the shared kustomization, there's a replacements entry to, for example, pull a DNS_HOSTNAME from an annotation and put it into a configmap data field, and other various resource spots it needs to go.

This worked well with vars, which seems to render all of the manifests then do the var replacements afterwards. But with replacements, kustomize does not see any fields that are created above itself.

Files that can reproduce the issue
replacementsinheritance_test.go.txt

a/kustomization.yaml

kind: Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1

commonAnnotations:
  replacement: child
 
configMapGenerator:
- name: dns-config
  options:
   disableNameSuffixHash: true
- name: deployment-config
  options:
    disableNameSuffixHash: true

replacements:
- source:
    kind: ConfigMap
    name: dns-config
    fieldPath: metadata.annotations.replacement
  targets:
  - select:
      kind: ConfigMap
      name: deployment-config
    fieldPaths:
    - data.REPLACEMENT
    options:
      create: true

b/kustomization.yaml

kind: Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1

commonAnnotations:
  replacement: parent

resources:
- ../a

kustomize build b

Note that all of the resources have the annotation parent, but the replacements does the replacement using the prior annotations. Additionally, if you comment out the commonAnnotations from a/kustomization.yaml and then run kustomize build b, it will panic with panic: runtime error: invalid memory address or nil pointer dereference.

===== ACTUAL END ==========================================
   EXPECTED                      ACTUAL
   --------                      ------
   apiVersion: v1                apiVersion: v1
   kind: ConfigMap               kind: ConfigMap
   metadata:                     metadata:
     annotations:                  annotations:
       replacement: parent           replacement: parent
     name: dns-config              name: dns-config
   ---                           ---
   apiVersion: v1                apiVersion: v1
   data:                         data:
X    REPLACEMENT: parent           REPLACEMENT: child
   kind: ConfigMap               kind: ConfigMap
   metadata:                     metadata:
     annotations:                  annotations:
       replacement: parent           replacement: parent
     name: deployment-config       name: deployment-config

Kustomize version

{Version:kustomize/v4.2.0 GitCommit:d53a2ad45d04b0264bcee9e19879437d851cb778 BuildDate:2021-07-01T00:44:28+01:00 GoOs:darwin GoArch:amd64}

Platform

Darwin 20.2.0 Darwin Kernel Version 20.2.0: Wed Dec 2 20:39:59 PST 2020; root:xnu-7195.60.75~1/RELEASE_X86_64 x86_64

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions