Home > Enterprise >  Kubernetes patch multiple resources not working
Kubernetes patch multiple resources not working

Time:06-07

I'm trying to apply the same job history limits to a number of CronJobs using a patch like the following, named kubeJobHistoryLimit.yml:

apiVersion: batch/v1beta1
kind: CronJob
spec:
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1

My kustomization.yml looks like:

bases:
  - ../base
configMapGenerator:
- name: inductions-config
  env: config.properties
patches:
  - path: kubeJobHistoryLimit.yml
    target:
      kind: CronJob
patchesStrategicMerge:
  - job_specific_patch_1.yml
  - job_specific_patch_2.yml
  ...
resources:
  - secrets-uat.yml

And at some point in my CI pipeline I have:

kubectl --kubeconfig $kubeconfig apply --force -k ./

The kubectl version is 1.21.9.

The issue is that the job history limit values don't seem to be getting picked up. Is there something wrong w/ the configuration or the version of K8s I'm using?

CodePudding user response:

With kustomize 4.5.2, your patch as written doesn't apply; it fails with:

Error: trouble configuring builtin PatchTransformer with config: `
path: kubeJobHistoryLimit.yml
target:
  kind: CronJob
`: unable to parse SM or JSON patch from [apiVersion: batch/v1
kind: CronJob
spec:
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
]

This is because it's missing metadata.name, which is required, even if it's ignored when patching multiple objects. If I modify the patch to look like this:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: ignored
spec:
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1

It seems to work.

If I have base/cronjob1.yaml that looks like:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cronjob1
spec:
  failedJobsHistoryLimit: 2
  successfulJobsHistoryLimit: 5
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - command:
            - sleep
            - 60
            image: docker.io/alpine:latest
            name: example
  schedule: 30 3 * * *

Then using the above patch and a overlay/kustomization.yaml like this:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
patches:
- path: kubeJobHistoryLimit.yml
  target:
    kind: CronJob

I see the following output from kustomize build overlay:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cronjob2
spec:
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - command:
            - sleep
            - 60
            image: docker.io/alpine:latest
            name: example
  schedule: 30 3 * * *
  successfulJobsHistoryLimit: 1

You can see the two attributes have been updated correctly.

  • Related