<p>I'm updating some of my Kubernetes configurations to use <code>'replacements'</code> and <code>'resources'</code> in kustomize as <code>'vars'</code> and <code>'bases'</code> have been deprecated.</p>
<p>Previously, I used <code>'vars'</code> in a base (<code>/base/secrets/</code>) like this:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

secretGenerator:
- name: test_secret
  env: secret.env

vars:
- name : SECRET_VALUE
  objref:
    kind: Secret
    name: test_secret
    apiVersion: v1
  fieldref:
    fieldpath: metadata.name

</code></pre>
<p>This base was used in various overlays for different services:</p>
<pre><code>namespace: test-overlay

bases:
- ../../base/secrets/
- ../../base/service/
</code></pre>
<p>Now, with <code>'resources'</code> and <code>'replacements'</code>, my understanding is that it's not possible to replace values in <code>/base/service/</code> from <code>/base/secrets/</code> as before. I could apply the <code>'replacement'</code> in the overlay itself and target the base I want to modify, but I would prefer to perform the operation from a base for reusability and ease of use.</p>
<p>Here's what I'm trying to do:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

secretGenerator:
- name: test_secret
  env: secret.env

replacements:
- source:
    name: test_secret
    kind: Secret
  targets:
  - select:
      kind: Deployment
      name: service
    fieldPaths:
    - spec.template.spec.&lt;field&gt;
</code></pre>
<p>In the <code>'replacements'</code> directive,<code> spec.template.spec.&lt;field&gt;</code> is the field in the Deployment resource that I'm trying to replace.</p>
<p>I'm using kustomize version <code>v5.1.0</code>.</p>
<p>How can I get <code>'replacements'</code> to target other bases so that they can be used from any overlay? What's the best practice for this scenario?</p>
<p>I've attempted to apply the 'replacements' in the overlay itself and target the base I want to modify like this:</p>
<pre><code>namespace: test-overlay

resources:
- ../../base/secrets/
- ../../base/service/

replacements:
- source:
    kind: Secret
    name: test_secret
  targets:
  - select:
      kind: Deployment
      name: service
    fieldPaths:
    - spec.template.spec.&lt;field&gt;
</code></pre>
<p>While this does apply the replacement to the service, it's not a satisfactory solution for me because I have multiple overlays that all need to use the same replacement for various deployments. I would prefer to define the replacement once in a base, rather than having to define it in each overlay.</p>
<p><strong>Edit: A more clear minimal reproducible example</strong></p>
<pre><code>/base
  /secrets
    kustomization.yaml
  /service
    deployment.yaml
    kustomization.yaml
/overlays
  /test-overlay
    kustomization.yaml
</code></pre>
<p>With the <code>/secrets/</code> implemented as:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

secretGenerator:
- name: test_secret
  env: secret.env

replacements:
- source:
    name: test_secret
    kind: Secret
  targets:
  - select:
      kind: Deployment
      name: service
    fieldPaths:
    - spec.template.spec.volumes.name
</code></pre>
<p>This would be the <code>/service/</code>:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
    
resources:
- deployment.yaml

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service
spec:
  template:
    spec:
      volumes:
      - name: placeholder_value
        emptyDir: {}
</code></pre>
<p>With <code>/test-overlay/</code></p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: test-overlay

resources:
- ../../base/secrets/
- ../../base/service/
</code></pre>
<p>I've tried including the <code>'replacements'</code> in the test-overlays <code>kustomization</code> file which did work but is less desirable then letting the replacement be defined in a base</p>

unitop
<p>Thanks for updating your question with an example.</p>
<p>The answer I've posted here is still the correct solution in terms of sharing a replacement configuration among multiple overlays, but there are some errors in your <code>replacement</code> syntax: you cannot target <code>spec.template.spec.volumes.name</code>, because <code>volumes</code> is a list and has no <code>name</code> attribute.</p>
<p>You can only target list elements with a <code>[name=value]</code> style selector, so:</p>
<pre><code>replacements:
  - source:
      name: test_secret
      kind: Secret
    targets:
      - select:
          kind: Deployment
          name: service
        fieldPaths:
          - spec.template.spec.volumes.[name=placeholder_value].name
</code></pre>
<hr />
<p>A <code>kustomization.yaml</code> can only apply transformations (labels, patches, replacements, etc) to resources that are emitted by that <code>kustomization.yaml</code> -- which means that if you want a transformation to affect all resources, it needs to be applied in the &quot;outermost&quot; kustomization.</p>
<p>This means that you can't place something in a &quot;base&quot; that will modify resources generated in your overlays.</p>
<p>But don't worry, there is a solution! <a href="https://github.com/kubernetes-sigs/kustomize/blob/master/examples/components.md" rel="nofollow noreferrer">Components</a> allow you to reuse kustomization fragments. If we move your replacement configuration into a component, we can get the behavior you want.</p>
<p>For example, here is a project with a base and two overlays:</p>
<pre><code>.
├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
├── components
│   └── replace-username-password
│       └── kustomization.yaml
└── overlay
    ├── env1
    │   └── kustomization.yaml
    └── env2
        └── kustomization.yaml
</code></pre>
<p><code>base/deployment.yaml</code> looks like this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  replicas: 2
  template:
    spec:
      containers:
        - name: example
          image: docker.io/alpine:latest
          command:
            - sleep
            - inf
          env:
            - name: USER_NAME
              value: update-via-replacement
            - name: USER_PASSWORD
              value: update-via-replacement
</code></pre>
<p>And <code>base/kustomization.yaml</code> looks like:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: replacement-example

resources:
  - deployment.yaml

secretGenerator:
  - name: example
    literals:
      - password=secret

configMapGenerator:
  - name: example
    literals:
      - username=alice
</code></pre>
<p>So the <code>base</code> directory results in a Deployment, a Secret, and a ConfigMap. There are two overlays, <code>env1</code> and <code>env2</code>. In both overlays I want to apply the same replacement configuration, so I put that into <code>components/replace-username-password/kustomization.yaml</code>:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component

replacements:
  - source:
      kind: ConfigMap
      name: example
      fieldPath: data.username
    targets:
      - select:
          kind: Deployment
          name: example
        fieldPaths:
          - spec.template.spec.containers.[name=example].env.[name=USER_NAME].value
  - source:
      kind: Secret
      name: example
      fieldPath: data.password
    targets:
      - select:
          kind: Deployment
          name: example
        fieldPaths:
          - spec.template.spec.containers.[name=example].env.[name=USER_PASSWORD].value
</code></pre>
<p>Now in <code>overlays/env1/kustomization.yaml</code> I can make use of this component:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  envName: env1

resources:
  - ../../base

components:
  - ../../components/replace-username-password
</code></pre>
<p>And the same in <code>overlays/env2</code>:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  envName: env2

resources:
  - ../../base

components:
  - ../../components/replace-username-password
</code></pre>
