Patch in the Definitions
When we are writing the definition, sometimes we need to patch to the corresponding component or traits. We can use the patch
capability when you're writing trait definitions or workflow step definitions.
Patch Strategy
By default, KubeVela will merge patched values with CUE's merge. However, CUE cannot handle conflicting fields currently.
For example, if replicas=5
has been set in a component instance, once there is another trait, attempting to patch the value of the replicas field, it will fail. So we recommend that you need to plan ahead and don't use duplicate fields between components and traits.
But in some cases, we do need to deal with overwriting fields that have already been assigned a value. For example, when set up resources in multi-environments, we hope that the envs
in different environments are different: i.e., the default env
is MODE=PROD
, and in the test environment, it needs to be modified to MODE=DEV DEBUG=true
.
In this case, we can apply the following application:
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: deploy-with-override
spec:
components:
- name: nginx-with-override
type: webservice
properties:
image: nginx
env:
- name: MODE
value: prod
policies:
- name: test
type: topology
properties:
clusters: ["local"]
namespace: test
- name: prod
type: topology
properties:
clusters: ["local"]
namespace: prod
- name: override-env
type: override
properties:
components:
- name: nginx-with-override
traits:
- type: env
properties:
env:
MODE: test
DEBUG: "true"
workflow:
steps:
- type: deploy
name: deploy-test
properties:
policies: ["test", "override-env"]
- type: deploy
name: deploy-prod
properties:
policies: ["prod"]
After deploying the application, you can see that in the test
namespace, the envs
of the nginx application are as follows:
spec:
containers:
- env:
- name: MODE
value: test
- name: DEBUG
value: "true"
At the same time, in the prod
namespace, the envs
are as follows:
spec:
containers:
- env:
- name: MODE
value: prod
deploy-test
will deploy nginx to the test namespace. At the same time, the env
trait overwrite the same envs by using the patch strategy, thus adding MODE=test DEBUG=true
in the test namespace, while the nginx in the prod namespace will retain the original MODE=prod
env.
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.
For the usage of all patch strategies, please refer to Patch Strategy.
Patch in Traits
Patch is a very common pattern of trait definitions, i.e. the app operators can amend/patch attributes to the component instance or traits 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.