Inline Patch

A kustomization file supports patching in three ways:

  • patchesStrategicMerge: A list of patch files where each file is parsed as a Strategic Merge Patch.
  • patchesJSON6902: A list of patches and associated targetes, where each file is parsed as a JSON Patch and can only be applied to one target resource.
  • patches: A list of patches and their associated targets. The patch can be applied to multiple objects. It auto detects whether the patch is a Strategic Merge Patch or JSON Patch.

Since 3.2.0, all three support inline patch, where the patch content is put inside the kustomization file as a single string. With this feature, no separate patch files need to be created.

Make a base kustomization containing a Deployment resource.

Define a place to work:

  1. DEMO_HOME = $(mktemp -d)

/base

Define a common base:

  1. $ cd $DEMO_HOME
  2. $ mkdir base
  3. $ cd base

Create a Sample Pod File and Kustomize file in base

  1. $ vim kustomization.yaml
  1. # kustomization.yaml contents
  2. resources:
  3. - deployments.yaml
  1. # deployments.yaml contents
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: deploy
  6. spec:
  7. template:
  8. metadata:
  9. labels:
  10. foo: bar
  11. spec:
  12. containers:
  13. - name: nginx
  14. image: nginx
  15. args:
  16. - arg1
  17. - arg2

PatchesStrategicMerge

patch

Create an overlay and add an inline patch in patchesStrategicMerge field to the kustomization file to change the image from nginx to nginx:latest.

  1. $ cd $DEMO_HOME
  2. $ mkdir smp_patch
  3. $ cd smp_patch

Create a Kustomize file in smp_patch

  1. # kustomization.yaml contents
  2. resources:
  3. - ../base
  4. patchesStrategicMerge:
  5. - |-
  6. apiVersion: apps/v1
  7. kind: Deployment
  8. metadata:
  9. name: deploy
  10. spec:
  11. template:
  12. spec:
  13. containers:
  14. - name: nginx
  15. image: nginx:latest

Running kustomize build, in the output confirm that image is updated successfully.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: deploy
  5. spec:
  6. template:
  7. metadata:
  8. labels:
  9. foo: bar
  10. spec:
  11. containers:
  12. - args:
  13. - arg1
  14. - arg2
  15. image: nginx:latest
  16. name: nginx

$patch: delete and $patch: replace also work in the inline patch. Change the inline patch to delete the container nginx.

patch: delete

  1. $ cd $DEMO_HOME
  2. $ mkdir smp_delete
  3. $ cd smp_delete

Create a Kustomize file in smp_delete

  1. # kustomization.yaml contents
  2. resources:
  3. - ../base
  4. patchesStrategicMerge:
  5. - |-
  6. apiVersion: apps/v1
  7. kind: Deployment
  8. metadata:
  9. name: deploy
  10. spec:
  11. template:
  12. spec:
  13. containers:
  14. - name: nginx
  15. $patch: delete

Running kustomize build, in the output confirm that image is updated successfully.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: deploy
  5. spec:
  6. template:
  7. metadata:
  8. labels:
  9. foo: bar
  10. spec:
  11. containers: []

patch: replace

  1. $ cd $DEMO_HOME
  2. $ mkdir smp_replace
  3. $ cd smp_replace

Create a Kustomize file in smp_replace

  1. # kustomization.yaml contents
  2. resources:
  3. - ../base
  4. patchesStrategicMerge:
  5. - |-
  6. apiVersion: apps/v1
  7. kind: Deployment
  8. metadata:
  9. name: deploy
  10. spec:
  11. template:
  12. spec:
  13. containers:
  14. - name: nginx
  15. image: nginx:1.7.9
  16. $patch: replace

Running kustomize build, in the output confirm that image is updated successfully. Since we are replacing notice that the arguments set in the base file are gone.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: deploy
  5. spec:
  6. template:
  7. metadata:
  8. labels:
  9. foo: bar
  10. spec:
  11. containers:
  12. - image: nginx:1.7.9
  13. name: nginx

PatchesJson6902

Create an overlay and add an inline patch in patchesJSON6902 field to the kustomization file to change the image from nginx to nginx:latest.

  1. $ cd $DEMO_HOME
  2. $ mkdir json
  3. $ cd json

Create a Kustomize file in json

  1. # kustomization.yaml contents
  2. resources:
  3. - ../base
  4. patchesJSON6902:
  5. - target:
  6. group: apps
  7. version: v1
  8. kind: Deployment
  9. name: deploy
  10. patch: |-
  11. - op: replace
  12. path: /spec/template/spec/containers/0/image
  13. value: nginx:latest

Running kustomize build, in the output confirm that image is updated successfully.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: deploy
  5. spec:
  6. template:
  7. metadata:
  8. labels:
  9. foo: bar
  10. spec:
  11. containers:
  12. - args:
  13. - arg1
  14. - arg2
  15. image: nginx:latest
  16. name: nginx

Patches

Create an overlay and add an inline patch in patches field to the kustomization file to change the image from nginx to nginx:latest.

  1. $ cd $DEMO_HOME
  2. $ mkdir patch
  3. $ cd patch

Create a Kustomize file in patch

  1. # kustomization.yaml contents
  2. resources:
  3. - ../base
  4. patches:
  5. - target:
  6. kind: Deployment
  7. name: deploy
  8. patch: |-
  9. apiVersion: apps/v1
  10. kind: Deployment
  11. metadata:
  12. name: deploy
  13. spec:
  14. template:
  15. spec:
  16. containers:
  17. - name: nginx
  18. image: nginx:latest

Running kustomize build, in the output confirm that image is updated successfully.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: deploy
  5. spec:
  6. template:
  7. metadata:
  8. labels:
  9. foo: bar
  10. spec:
  11. containers:
  12. - args:
  13. - arg1
  14. - arg2
  15. image: nginx:latest
  16. name: nginx