Patch strategy
KubeVela provides a series of patching strategies to help resolve conflicting issues. When writing patch traits and workflow steps, you can use these patch strategies to solve conflicting values. Note that the patch strategy is not an official capability provided by CUE, but an extension developed by KubeVela.
Let’s write an env-patch trait to show how to use these patch strategies.
If you want to add multiple environment variables for a specific container, you can use the annotation to find the container. In this case, KubeVela will merge these environment variables by default. This means that patchKey
cannot handle duplicate fields.
Apply the following definition to your cluster:
Use the above myenv
trait in your application:
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: webservice-app
spec:
components:
- name: express-server
type: webservice
properties:
image: crccheck/hello-world
env:
- name: OLD
value: old
traits:
- type: myenv
properties:
env:
NEW: new
spec:
containers:
- env:
- name: OLD
value: old
After using the myenv
patch trait, the env
in the application is like:
spec:
containers:
- env:
- name: OLD
value: old
- name: NEW
value: new
Finally, we can see that the application’s env
contains two environment variables: OLD=old
and NEW=new
.
You can use the +patchStrategy=retainKeys
annotation if you want to be able to override duplicate values while merging variables.
The strategy of this annotation is similar to the Kubernetes official [retainKeys](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#use-strategic-merge-patch- to-update-a-deployment-using-the-retainkeys-strategy) strategy.
Use the above myenv
trait in your application:
kind: Application
metadata:
name: webservice-app
spec:
components:
- name: express-server
type: webservice
properties:
image: crccheck/hello-world
env:
- name: OLD
value: old
- name: OLD2
value: old2
traits:
- type: myenv
properties:
env:
NEW: new
OLD2: override
Before using the myenv
patch trait, the env
in the application is like:
spec:
containers:
- env:
- name: OLD
value: old
- name: OLD2
value: old2
spec:
containers:
- env:
- name: OLD
value: old
- name: OLD2
- name: NEW
value: new
Finally, we can see that the application’s env
contains three environment variables: , OLD2=override
and NEW=new
.
If you wish to replace the entire env array directly, you can use the +patchStrategy=replace
annotation.
Use the above myenv
trait in your application:
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: webservice-app
spec:
components:
- name: express-server
type: webservice
properties:
image: crccheck/hello-world
env:
- name: OLD
value: old
- name: OLD2
value: old2
traits:
- type: myenv
properties:
env:
NEW: replace
Before using the myenv
patch trait, the env
in the application is like:
spec:
containers:
- env:
- name: OLD
value: old
- name: OLD2
value: old2
After using the myenv
patch trait, the env
in the application is like:
spec:
containers:
- env:
- name: NEW
Finally, we can see that the application’s env
contains one environment variable: .