7

I'm having difficulty trying to get kustomize to replace contents of an item in a list.

My kustomize file

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

resources:
  - resource.yaml

patches:
  - patch.yaml

My patch.yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-service
spec:
  template:
    spec:
      initContainers:
        - name: web-service-migration
          env:
            - name: PG_DATABASE
              value: web-pgdb


My resource.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-service
spec:
  template:
    spec:
      initContainers:
        - name: web-service-migration
          env:
            - name: PG_DATABASE
              valueFrom:
                secretKeyRef:
                  name: web-pgdb
                  key: database


kustomize build returns

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-service
spec:
  template:
    spec:
      initContainers:
      - env:
        - name: PG_DATABASE
          value: web-pgdb
          valueFrom:
            secretKeyRef:
              key: database
              name: web-pgdb
        name: web-service-migration

what i want kustomize build to return

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-service
spec:
  template:
    spec:
      initContainers:
      - env:
        - name: PG_DATABASE
          value: web-pgdb
        name: web-service-migration
sebastian
  • 2,008
  • 4
  • 31
  • 49
  • I am curious about this behavior as well. I think the docs are lacking in explaining this kind of behavior as it's even the same with `patchesStrategicMerge` in my test. – jordanm Apr 21 '22 at 02:54
  • yep, i've tried patchesStrategicMerge as well and it has the same issue. – sebastian Apr 21 '22 at 15:24

1 Answers1

5

If I remember correctly patches in kustomize by default uses strategic merge, so you need to nullify valueFrom, so your patch should look like:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-service
spec:
  template:
    spec:
      initContainers:
        - name: web-service-migration
          env:
            - name: PG_DATABASE
              value: web-pgdb
              valueFrom: null 

More details about strategic merge patch and how to delete maps: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md#maps

Ottovsky
  • 2,068
  • 15
  • 22