1

I want to build the the secretName dynamically base on the value of the my-label key (trough a ENV). Is this possible?

I used the a similar approach to use label values as ARGs which worked.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            my-label: "my-value"
        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)

1 Answers1

1

The fastest solution is surely to use kustomize.

Following your data, first organize the repository by creating a folder called "base" and one called "dev".

Then move the "my-cronjob" manifest into the "base" folder and add a kustomization.yaml file that invokes the CronJob.

Finally, create a file called kustomization.yaml inside the "dev" folder, calling the files from the "base" folder plus the patch.

Example:

Repo structure

base/kustomization.yaml

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

resources:
  - ./my-cronjob.yaml

dev/kustomization.yaml

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']

To replicate to other environments, just copy the "dev" folder and paste it into a "prod" folder (for example) and edit the patch with the correct parameter.

glv
  • 994
  • 1
  • 1
  • 15