<p>I'm using kustomize to manage a rather standard deployment. I have a <code>namePrefix</code> to modify the resource names.</p>
<p>I need to add a custom resource to my configuration which itself autogenerates a secret after creation. The secret name consists of a fixed prefix and the name of the custom resource. I want to reference this secret in my deployment.</p>
<pre class="lang-yaml prettyprint-override"><code># kustomization.yaml
resources:
  - deployment.yaml
  - custom-resource.yaml

namePrefix: my-prefix-

secretGenerator:
- name: my-secret
  files:
    - password.txt
</code></pre>
<pre class="lang-yaml prettyprint-override"><code># deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-image
        image: my-image:latest
        envFrom:
          - secretRef:
            name: my-secret
          - secretRef:
            name: prefix-my-custom-resource  &lt;-- does not work
</code></pre>
<pre class="lang-yaml prettyprint-override"><code># custom-resource.yaml
apiVersion: some.crd.io/v1
kind: CustomResource
metadata:
  name: my-custom-resource
</code></pre>
<p>The custom resource will autogenerate: (not result of <code>kubectl kustomize .</code>)</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Secret
metadata:
  name: prefix-my-custom-resource
</code></pre>
<p>Due to the use of the PrefixTransformer, the name of the custom resource is changed to <code>my-prefix-my-custom-resource</code>. Therefore, the <code>secretRef</code> in the deployment yaml needs to be updated to <code>prefix-my-prefix-my-custom-resource</code>. I tried to solve this with a nameReference configuration, but I don't think the <code>fieldSpec</code> allows for a substring. Is there any solution to this?</p>

<p>With the help of a colleague, we were able to solve this with vars:</p>
<pre class="lang-yaml prettyprint-override"><code># kustomization.yaml
resources:
  - deployment.yaml
  - custom-resource.yaml

namePrefix: my-prefix-

secretGenerator:
- name: my-secret
  files:
    - password.txt

configurations:
  - configurations/var-reference.yaml

vars:
  - name: MY-VARIABLE
    objref:
      kind: CustomResource
      name: my-custom-resource
      apiVersion: some.crd.io/v1
    fieldref:
      fieldpath: metadata.name
</code></pre>
<pre class="lang-yaml prettyprint-override"><code># configurations/var-reference.yaml
varReference:
  - kind: Deployment
    path: spec/template/spec/containers/envFrom/secretRef/name
</code></pre>
<pre class="lang-yaml prettyprint-override"><code># deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-image
        image: my-image:latest
        envFrom:
          - secretRef:
            name: my-secret
          - secretRef:
            name: prefix-$(MY-VARIABLE)
</code></pre>
<p>I'm aware that vars are deprecated and might try to find a solution with replacements, but for now I'm good with this solution.</p>
