Patch Traits
Patch is a very common pattern of trait definitions, i.e. the app operators can amend/patch 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:
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:
#
Known LimitationsBy 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 patchreplicas
field will fail, a.k.a you should not exposereplicas
field in its component definition schematic.
- For example, if a component instance already been set with value
- 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 PatchStrategy Patch is effective by adding annotation, and supports the following two ways
Note that this is not a standard CUE feature, KubeVela enhanced CUE in this case.
+patchKey=<key_name>
annotation#
1. With This is useful for patching array list, 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 with 'patchKey' will like below:
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
.
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
.
+patchStrategy=retainkeys
annotation#
2. With Similar to strategy retainkeys in K8s strategic merge patch
In some scenarios that the entire object needs to be replaced, retainkeys strategy is the best choice. the example as follows:
Assume the Deployment is the base resource
Now want to replace rollingUpdate strategy with a new strategy, you can write the patch trait like below
Then the base resource becomes as follows
#
More Use Cases of Patch TraitPatch trait is in general pretty useful to separate operational concerns from the component definition, here are some more examples.
#
Add LabelsFor example, patch common label (virtual group) to the component instance.
Then it could be used like:
#
Add AnnotationsSimilar to common labels, you could also patch the component instance with annotations. The annotation value should be a JSON string.
#
Add Pod EnvironmentsInject 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:
ServiceAccount
Based on External Auth Service#
Inject 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.
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.
InitContainer
#
Add InitContainer
is useful to pre-define operations in an image and run it before app container.
Below is an example:
The usage could be: