<p>I want to build the the <code>secretName</code> dynamically base on the value of the <code>my-label</code> key (trough a <code>ENV</code>). Is this possible?</p>
<p>I used the a similar approach to use label values as ARGs which worked.</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: &quot;*/1 * * * *&quot;
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            my-label: &quot;my-value&quot;
        spec:
          containers:
          - name: my-container
            image: my-image
            env:
            - name: MY_ENV_VAR
              valueFrom:
                fieldRef:
                  fieldPath: metadata.labels['my-label']
            volumeMounts:
            - name: my-secret
              mountPath: /path/to/my-secret
          volumes:
          - name: my-secret
            secret:
              secretName: my-secret-$(MY_ENV_VAR)
</code></pre>

<p>The fastest solution is surely to use kustomize.</p>
<p>Following your data, first organize the repository by creating a folder called &quot;base&quot; and one called &quot;dev&quot;.</p>
<p>Then move the &quot;my-cronjob&quot; manifest into the &quot;base&quot; folder and add a kustomization.yaml file that invokes the CronJob.</p>
<p>Finally, create a file called kustomization.yaml inside the &quot;dev&quot; folder, calling the files from the &quot;base&quot; folder plus the patch.</p>
<p>Example:</p>
<p><a href="https://i.stack.imgur.com/LnpCF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LnpCF.png" alt="Repo structure" /></a></p>
<p>base/kustomization.yaml</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ./my-cronjob.yaml
</code></pre>
<p>dev/kustomization.yaml</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

bases:
  - ../base

patches:
- target:
    kind: CronJob
    name: my-cronjob
  patch: |-
    - op: replace
      path: /spec/jobTemplate/spec/template/spec/containers/0/env/0/valueFrom/fieldRef/fieldPath
      value: metadata.labels['DEV']
</code></pre>
<p>To replicate to other environments, just copy the &quot;dev&quot; folder and paste it into a &quot;prod&quot; folder (for example) and edit the patch with the correct parameter.</p>
