Version: v1.1

Kustomize 补丁

本小节将介绍如何使用 trait 对 Kustomize 组件做差异化配置。

功能说明

Trait简介
kustomize-patch支持以 inline YAML 字符串形式支持 strategy Merge 和 JSON6902 格式的 patch。
kustomize-json-patch支持以 JSON6902 格式对 kustomize 进行 patch
kustomize-strategy-merge支持以 YAML 格式对 kustomize 进行 patch

kustomize-patch 字段说明

kustomize-patch 类型的 trait 以字符串形式描述 patch 内容。

  1. vela show kustomize-patch

输出如下:

  1. # Properties
  2. +---------+---------------------------------------------------------------+-----------------------+----------+---------+
  3. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  4. +---------+---------------------------------------------------------------+-----------------------+----------+---------+
  5. | patches | a list of StrategicMerge or JSON6902 patch to selected target | [[]patches](#patches) | true | |
  6. +---------+---------------------------------------------------------------+-----------------------+----------+---------+
  7. ## patches
  8. +--------+---------------------------------------------------+-------------------+----------+---------+
  9. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  10. +--------+---------------------------------------------------+-------------------+----------+---------+
  11. | patch | Inline patch string, in yaml style | string | true | |
  12. | target | Specify the target the patch should be applied to | [target](#target) | true | |
  13. +--------+---------------------------------------------------+-------------------+----------+---------+
  14. ### target
  15. +--------------------+-------------+--------+----------+---------+
  16. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  17. +--------------------+-------------+--------+----------+---------+
  18. | name | | string | false | |
  19. | group | | string | false | |
  20. | version | | string | false | |
  21. | kind | | string | false | |
  22. | namespace | | string | false | |
  23. | annotationSelector | | string | false | |
  24. | labelSelector | | string | false | |
  25. +--------------------+-------------+--------+----------+---------+

如何使用

使用示例如下

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: bucket-app
  5. spec:
  6. components:
  7. - name: bucket-comp
  8. type: kustomize
  9. # ... omitted for brevity
  10. traits:
  11. - type: kustomize-patch
  12. properties:
  13. patches:
  14. - patch: |-
  15. apiVersion: v1
  16. kind: Pod
  17. metadata:
  18. name: not-used
  19. labels:
  20. app.kubernetes.io/part-of: test-app
  21. target:
  22. labelSelector: "app=podinfo"

上面的例子给原本的 kustomize 添加了一个 patch : 筛选出带有 app=podinfo 标签的 Pod 打了 patch。

kustomize-json-patch 字段说明

可以以 JSON6902 格式进行 patch。先来了解其信息:

  1. vela show kustomize-json-patch
  1. # Properties
  2. +-------------+---------------------------+-------------------------------+----------+---------+
  3. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  4. +-------------+---------------------------+-------------------------------+----------+---------+
  5. | patchesJson | A list of JSON6902 patch. | [[]patchesJson](#patchesJson) | true | |
  6. +-------------+---------------------------+-------------------------------+----------+---------+
  7. ## patchesJson
  8. +--------+-------------+-------------------+----------+---------+
  9. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  10. +--------+-------------+-------------------+----------+---------+
  11. | patch | | [patch](#patch) | true | |
  12. | target | | [target](#target) | true | |
  13. +--------+-------------+-------------------+----------+---------+
  14. #### target
  15. +--------------------+-------------+--------+----------+---------+
  16. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  17. +--------------------+-------------+--------+----------+---------+
  18. | name | | string | false | |
  19. | group | | string | false | |
  20. | version | | string | false | |
  21. | kind | | string | false | |
  22. | namespace | | string | false | |
  23. | annotationSelector | | string | false | |
  24. | labelSelector | | string | false | |
  25. +--------------------+-------------+--------+----------+---------+
  26. ### patch
  27. +-------+-------------+--------+----------+---------+
  28. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  29. +-------+-------------+--------+----------+---------+
  30. | path | | string | true | |
  31. | op | | string | true | |
  32. | value | | string | false | |
  33. +-------+-------------+--------+----------+---------+

如何使用

使用示例如下:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: bucket-app
  5. spec:
  6. components:
  7. - name: bucket-comp
  8. type: kustomize
  9. # ... omitted for brevity
  10. traits:
  11. - type: kustomize-json-patch
  12. properties:
  13. patchesJson:
  14. - target:
  15. version: v1
  16. kind: Deployment
  17. name: podinfo
  18. patch:
  19. - op: add
  20. path: /metadata/annotations/key
  21. value: value

上面这个例子中给所有 Deployment 对象的 annotations 添加了一条:key: value

kustomize-strategy-merge 字段说明

可以以 格式进行 patch。先来了解其信息:

  1. vela show kustomize-json-patch
  1. # Properties
  2. +-----------------------+-----------------------------------------------------------+---------------------------------------------------+----------+---------+
  3. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  4. +-----------------------+-----------------------------------------------------------+---------------------------------------------------+----------+---------+
  5. | patchesStrategicMerge | a list of strategicmerge, defined as inline yaml objects. | [[]patchesStrategicMerge](#patchesStrategicMerge) | true | |
  6. +-----------------------+-----------------------------------------------------------+---------------------------------------------------+----------+---------+
  7. ## patchesStrategicMerge
  8. +-----------+-------------+--------------------------------------------------------+----------+---------+
  9. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  10. +-----------+-------------+--------------------------------------------------------+----------+---------+
  11. | undefined | | map[string](null|bool|string|bytes|{...}|[...]|number) | true | |

如何使用

使用示例如下:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: bucket-app
  5. spec:
  6. components:
  7. - name: bucket-comp
  8. type: kustomize
  9. # ... omitted for brevity
  10. traits:
  11. - type: kustomize-strategy-merge
  12. properties:
  13. patchesStrategicMerge:
  14. - apiVersion: apps/v1
  15. kind: Deployment
  16. metadata:
  17. name: podinfo
  18. spec:
  19. template:
  20. spec:
  21. serviceAccount: custom-service-account

上面这个例子中用 YAML 原生格式(即非内嵌字符串格式)对原本 kustomize 进行了patch。