patch kubernetes cronjob with kustomize

8/23/2021

I am trying to patch a cronjob, but somehow it doesn't work as I would expect. I use the same folder structure for a deployment and that works.

This is the folder structure:

.
├── base
│   ├── kustomization.yaml
│   └── war.cron.yaml
└── overlays
    └── staging
        ├── kustomization.yaml
        ├── war.cron.patch.yaml
        └── war.cron.staging.env

base/kustomization.yaml

---
kind: Kustomization
resources:
- war.cron.yaml

base/war.cron.yaml

---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: war-event-cron
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: war-event-cron
            image: my-registry/war-service
            imagePullPolicy: IfNotPresent
            command:
            - python
            - run.py
            args:
            - sync-events
            envFrom:
            - secretRef:
                name: war-event-cron-secret
          restartPolicy: OnFailure

Then I am trying to patch this in the staging overlay.

overlays/staging/kustomization.yaml

---
kind: Kustomization
namespace: staging
bases:
- "../../base"
patchesStrategicMerge:
- war.cron.patch.yaml
secretGenerator:
- name: war-event-cron-secret
  behavior: create
  envs:
  - war.cron.staging.env

overlays/staging/war.cron.patch.yaml

---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: war-event-cron
spec:
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: war-event-cron
            image: my-registry/war-service:nightly
            args:
            - sync-events
            - --debug

But the result of kustomize build overlays/staging/ is not what I want. The command is gone and the secret is not referenced.

apiVersion: v1
data:
  ...
kind: Secret
metadata:
  name: war-event-cron-secret-d8m6bh7284
  namespace: staging
type: Opaque
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: war-event-cron
  namespace: staging
spec:
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - args:
            - sync-events
            - --debug
            image: my-registry/war-service:nightly
            name: war-event-cron
          restartPolicy: OnFailure
  schedule: '*/5 * * * *'
-- The Fool
kubernetes
kubernetes-cronjob
kustomize

1 Answer

8/24/2021

It's known bug in kustomize - check and follow this topic (created ~ one month ago) on GitHub for more information.

For now, fix for your issue is to use apiVersion:batch/v1beta1 instead of apiVersion: batch/v1 in base/war.cron.yaml and overlays/staging/war.cron.patch.yaml files.

-- Mikolaj S.
Source: StackOverflow