Version: v1.0

Patch Traits

Patch is a very common pattern of trait definitions, i.e. the app operators can amend/path attributes to the component instance (normally the workload) to enable certain operational features such as sidecar or node affinity rules (and this should be done before the resources applied to target cluster).

This pattern is extremely useful when the component definition is provided by third-party component provider (e.g. software distributor) so app operators do not have privilege to change its template.

Note that even patch trait itself is defined by CUE, it can patch any component regardless how its schematic is defined (i.e. CUE, Helm, and any other supported schematic approaches).

Below is an example for node-affinity trait:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "affinity specify node affinity and toleration"
  6. name: node-affinity
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: true
  12. schematic:
  13. cue:
  14. template: |
  15. patch: {
  16. spec: template: spec: {
  17. if parameter.affinity != _|_ {
  18. affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: [{
  19. matchExpressions: [
  20. for k, v in parameter.affinity {
  21. key: k
  22. operator: "In"
  23. values: v
  24. },
  25. ]}]
  26. }
  27. if parameter.tolerations != _|_ {
  28. tolerations: [
  29. for k, v in parameter.tolerations {
  30. effect: "NoSchedule"
  31. key: k
  32. operator: "Equal"
  33. value: v
  34. }]
  35. }
  36. }
  37. }
  38. parameter: {
  39. affinity?: [string]: [...string]
  40. tolerations?: [string]: string
  41. }

The patch trait above assumes the target component instance have spec.template.spec.affinity field. Hence, we need to use appliesToWorkloads to enforce the trait only applies to those workload types have this field.

Another important field is podDisruptive, this patch trait will patch to the pod template field, so changes on any field of this trait will cause the pod to restart, We should add podDisruptive and make it to be true to tell users that applying this trait will cause the pod to restart.

Now the users could declare they want to add node affinity rules to the component instance as below:

  1. apiVersion: core.oam.dev/v1alpha2
  2. kind: Application
  3. metadata:
  4. name: testapp
  5. spec:
  6. components:
  7. - name: express-server
  8. type: webservice
  9. properties:
  10. image: oamdev/testapp:v1
  11. traits:
  12. - type: "node-affinity"
  13. properties:
  14. affinity:
  15. server-owner: ["owner1","owner2"]
  16. resource-pool: ["pool1","pool2","pool3"]
  17. tolerations:
  18. resource-pool: "broken-pool1"
  19. server-owner: "old-owner"

Known Limitations

By default, patch trait in KubeVela leverages the CUE merge operation. It has following known constraints though:

  • Can not handle conflicts.
    • For example, if a component instance already been set with value replicas=5, then any patch trait to patch replicas field will fail, a.k.a you should not expose replicas field in its component definition schematic.
  • Array list in the patch will be merged following the order of index. It can not handle the duplication of the array list members. This could be fixed by another feature below.

Strategy Patch

The strategy patch is useful for patching array list.

Note that this is not a standard CUE feature, KubeVela enhanced CUE in this case.

With //+patchKey=<key_name> annotation, merging logic of two array lists will not follow the CUE behavior. Instead, it will treat the list as object and use a strategy merge approach:

  • if a duplicated key is found, the patch data will be merge with the existing values;
  • if no duplication found, the patch will append into the array list.

The example of strategy patch trait will like below:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "add sidecar to the app"
  6. name: sidecar
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: true
  12. schematic:
  13. cue:
  14. template: |
  15. patch: {
  16. // +patchKey=name
  17. spec: template: spec: containers: [parameter]
  18. }
  19. parameter: {
  20. name: string
  21. image: string
  22. command?: [...string]
  23. }

In above example we defined patchKey is name which is the parameter key of container name. In this case, if the workload don’t have the container with same name, it will be a sidecar container append into the spec.template.spec.containers array list. If the workload already has a container with the same name of this sidecar trait, then merge operation will happen instead of append (which leads to duplicated containers).

If patch and outputs both exist in one trait definition, the patch operation will be handled first and then render the outputs.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "expose the app"
  6. name: expose
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: true
  12. schematic:
  13. cue:
  14. template: |
  15. patch: {spec: template: metadata: labels: app: context.name}
  16. outputs: service: {
  17. apiVersion: "v1"
  18. kind: "Service"
  19. metadata: name: context.name
  20. spec: {
  21. selector: app: context.name
  22. ports: [
  23. for k, v in parameter.http {
  24. port: v
  25. targetPort: v
  26. },
  27. ]
  28. }
  29. }
  30. parameter: {
  31. http: [string]: int
  32. }

So the above trait which attaches a Service to given component instance will patch an corresponding label to the workload first and then render the Service resource based on template in outputs.

More Use Cases of Patch Trait

Patch trait is in general pretty useful to separate operational concerns from the component definition, here are some more examples.

Add Labels

For example, patch common label (virtual group) to the component instance.

  1. apiVersion: core.oam.dev/v1alpha2
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "Add virtual group labels"
  6. name: virtualgroup
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: true
  12. schematic:
  13. cue:
  14. template: |
  15. patch: {
  16. spec: template: {
  17. metadata: labels: {
  18. if parameter.scope == "namespace" {
  19. "app.namespace.virtual.group": parameter.group
  20. }
  21. if parameter.scope == "cluster" {
  22. "app.cluster.virtual.group": parameter.group
  23. }
  24. }
  25. }
  26. }
  27. parameter: {
  28. group: *"default" | string
  29. scope: *"namespace" | string
  30. }

Then it could be used like:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. spec:
  4. ...
  5. traits:
  6. - type: virtualgroup
  7. properties:
  8. group: "my-group1"
  9. scope: "cluster"

Add Annotations

Similar to common labels, you could also patch the component instance with annotations. The annotation value should be a JSON string.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "Specify auto scale by annotation"
  6. name: kautoscale
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: false
  12. schematic:
  13. cue:
  14. template: |
  15. import "encoding/json"
  16. patch: {
  17. metadata: annotations: {
  18. "my.custom.autoscale.annotation": json.Marshal({
  19. "minReplicas": parameter.min
  20. "maxReplicas": parameter.max
  21. })
  22. }
  23. }
  24. parameter: {
  25. min: *1 | int
  26. max: *3 | int
  27. }

Add Pod Environments

Inject system environments into Pod is also very common use case.

This case relies on strategy merge patch, so don’t forget add +patchKey=name as below:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "add env into your pods"
  6. name: env
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: true
  12. schematic:
  13. cue:
  14. template: |
  15. patch: {
  16. spec: template: spec: {
  17. // +patchKey=name
  18. containers: [{
  19. name: context.name
  20. // +patchKey=name
  21. env: [
  22. for k, v in parameter.env {
  23. name: k
  24. value: v
  25. },
  26. ]
  27. }]
  28. }
  29. }
  30. parameter: {
  31. env: [string]: string
  32. }

Inject ServiceAccount Based on External Auth Service

In this example, the service account was dynamically requested from an authentication service and patched into the service.

This example put UID token in HTTP header but you can also use request body if you prefer.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "dynamically specify service account"
  6. name: service-account
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: true
  12. schematic:
  13. cue:
  14. template: |
  15. processing: {
  16. output: {
  17. credentials?: string
  18. }
  19. http: {
  20. method: *"GET" | string
  21. url: parameter.serviceURL
  22. request: {
  23. header: {
  24. "authorization.token": parameter.uidtoken
  25. }
  26. }
  27. }
  28. }
  29. patch: {
  30. spec: template: spec: serviceAccountName: processing.output.credentials
  31. }
  32. parameter: {
  33. uidtoken: string
  34. serviceURL: string
  35. }

The processing.http section is an advanced feature that allow trait definition to send a HTTP request during rendering the resource. Please refer to Execute HTTP Request in Trait Definition section for more details.

Add InitContainer

InitContainer is useful to pre-define operations in an image and run it before app container.

Below is an example:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: TraitDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: "add an init container and use shared volume with pod"
  6. name: init-container
  7. spec:
  8. appliesToWorkloads:
  9. - webservice
  10. - worker
  11. podDisruptive: true
  12. schematic:
  13. cue:
  14. template: |
  15. patch: {
  16. spec: template: spec: {
  17. // +patchKey=name
  18. containers: [{
  19. name: context.name
  20. // +patchKey=name
  21. volumeMounts: [{
  22. name: parameter.mountName
  23. mountPath: parameter.appMountPath
  24. }]
  25. }]
  26. initContainers: [{
  27. name: parameter.name
  28. image: parameter.image
  29. if parameter.command != _|_ {
  30. command: parameter.command
  31. }
  32. // +patchKey=name
  33. volumeMounts: [{
  34. name: parameter.mountName
  35. mountPath: parameter.initMountPath
  36. }]
  37. }]
  38. // +patchKey=name
  39. volumes: [{
  40. name: parameter.mountName
  41. emptyDir: {}
  42. }]
  43. }
  44. }
  45. parameter: {
  46. name: string
  47. image: string
  48. command?: [...string]
  49. mountName: *"workdir" | string
  50. appMountPath: string
  51. initMountPath: string
  52. }

The usage could be:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: testapp
  5. spec:
  6. components:
  7. - name: express-server
  8. type: webservice
  9. properties:
  10. image: oamdev/testapp:v1
  11. traits:
  12. - type: "init-container"
  13. properties:
  14. name: "install-container"
  15. image: "busybox"
  16. command:
  17. - wget
  18. - "-O"
  19. - "/work-dir/index.html"
  20. - http://info.cern.ch
  21. mountName: "workdir"
  22. appMountPath: "/usr/share/nginx/html"
  23. initMountPath: "/work-dir"