patches

Patch resources

Patches (also call overlays) add or override fields on resources. They are provided using the patches Kustomization field.

The patches field contains a list of patches to be applied in the order they are specified.

Each patch may:

  • be either a strategic merge patch, or a JSON patch
  • be either a file, or an inline string
  • target a single resource or multiple resources

The patch target selects resources by group, version, kind, name, namespace, labelSelector and annotationSelector. Any resource which matches all the specified fields has the patch applied to it (regular expressions).

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. patches:
  4. - path: patch.yaml
  5. target:
  6. group: apps
  7. version: v1
  8. kind: Deployment
  9. name: deploy.*
  10. labelSelector: "env=dev"
  11. annotationSelector: "zone=west"
  12. - patch: |-
  13. - op: replace
  14. path: /some/existing/path
  15. value: new value
  16. target:
  17. kind: MyKind
  18. labelSelector: "env=dev"

The name and namespace fields of the patch target selector are automatically anchored regular expressions. This means that the value myapp is equivalent to ^myapp$.

Consider the following deployment.yaml common for both the examples:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: the-deployment
  5. spec:
  6. replicas: 5
  7. template:
  8. containers:
  9. - name: the-container
  10. image: registry/conatiner:latest

Example I

Intent

To Make the container image point to a specific version and not to the latest container in the registry.

File Input

  1. # kustomization.yaml
  2. resources:
  3. - deployment.yaml
  4. patches:
  5. - path: patch.yaml
  1. # patch.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: the-deployment
  6. spec:
  7. template:
  8. containers:
  9. - name: the-container
  10. image: registry/conatiner:1.0.0

Build Output

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: the-deployment
  5. spec:
  6. replicas: 5
  7. template:
  8. containers:
  9. - image: registry/conatiner:1.0.0
  10. name: the-container

Example II

Intent

To Make the container image point to a specific version and not to the latest container in the registry.

File Input

  1. # kustomization.yaml
  2. resources:
  3. - deployment.yaml
  4. patches:
  5. - target:
  6. kind: Deployment
  7. name: the-deployment
  8. path: patch.json
  1. # patch.json
  2. [
  3. {"op": "replace", "path": "/spec/template/containers/0/image", "value": "registry/conatiner:1.0.0"}
  4. ]

Build Output

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: the-deployment
  5. spec:
  6. replicas: 5
  7. template:
  8. containers:
  9. - image: registry/conatiner:1.0.0
  10. name: the-container