CUE Advanced
This section will introduce how to use CUE to deliver KubeVela modules. You can can dynamically expand the platform as user needs change, adapt to growing number of users and scenarios, and meet the iterative demands of the company's long-term business development.
#
Convert Kubernetes API Objects Into Custom ComponentsLet's take the Kubernetes StatefulSet as an example to show how to use KubeVela to build custom modules and provide capabilities.
Save the YAML example of StatefulSet in the official document locally and name it as my-stateful.yaml
, then execute commande as below:
View the generated "my-stateful.cue" file:
Modify the generated file as follows:
- The example of the official StatefulSet website is a composite component composed of two objects
StatefulSet
andService
. According to KubeVela Rules for customize components, in composite components, core workloads such as StatefulSet need to be represented by thetemplate.output
field, and other auxiliary objects are represented bytemplate.outputs
, so we make some adjustments and all the automatically generated output and outputs are switched. - Then we fill in the apiVersion and kind data of the core workload into the part marked as
<change me>
After modification, you can use vela def vet
to do format check and verification.
The file after two steps of changes is as follows:
Install ComponentDefinition into the Kubernetes cluster:
You can see that a my-stateful
component via vela components
command:
When you put this customized component into Application
, it looks like:
#
Define Customized Parameters For ComponentIn previous section we have defined a ComponentDefinition that has no parameter. In this section we will show how to expose parameters.
In this example, we expose the following parameters to the user:
- Image name, allowing users to customize the image
- Instance name, allowing users to customize the instance name of the generated StatefulSet object and Service object
- The number of replica, the number of copies of the generated object
After modification, use vela def apply
to install to the cluster:
You can see the parameters of my-stateful ComponentDefinition as follows:
Updating the ComponentDefinition will not affect existing Applications. It will take effect only after updating the Applications next time.
You can specify the three new parameters in the application:
Save the file locally and name it app-stateful.yaml
, execute kubectl apply -f app-stateful.yaml
to update the application, you can see that the name, image, and number of instances of the StatefulSet object have been updated.
#
Dry-runIn order to ensure that the user's application can run correctly with the parameters, you can also use the vela dry-run
command to verify the trial run of your template.
By viewing the output, you can compare whether the generated object is consistent with the object you actually expect. You can even execute this YAML directly into the Kubernetes cluster and use the results of the operation for verification.
You can also use vela dry-run -h
to view more available function parameters.
context
to get runtime information#
Use In our Application example above, the name field in the properties and the name field of the Component are the same. So we can use the context
keyword that carries context information in the template, where context.name
is the runtime component Name, thus the name parameter in parameter
is no longer needed.
KubeVela has built-in application required context, you can configure it according to your needs.
#
Add Traits On DemandIn addition to modifying ComponentDefinitions and adding parameters, you can also use the TraitDefinition to patch configurations to Components. KubeVela has built-in operations to meet the following needs: adding labels, annotations, injecting environment variables, sidecars, adding volumes, and so on. You can also customize Trait to do more flexible patching.
You can use vela traits
to view, the traits marked with *
are general traits, which can operate on common Kubernetes resource objects.
Taking sidecar as an example, you can check the usage of sidecar:
Use the sidecar directly to inject a container, the application description is as follows:
Deploy and run the application, and you can see that a fluentd sidecar has been deployed and running in the StatefulSet.
You can also use vela def
to get the CUE source file of the sidecar to modify, add parameters, etc.
The customization of operation and maintenance capabilities is similar to component customization, so we won’t go into details here. You can read Customize Trait for more detailed functions.
#
SummarizeThis section introduces how to deliver complete modular capabilities through CUE. The core is that it can dynamically increase configuration capabilities according to user needs, and gradually expose more functions and usages, so as to reduce the overall learning threshold for users and ultimately improve R&D efficient. The out-of-the-box capabilities provided by KubeVela, including components, traits, policy, and workflow, are also designed as pluggable and modifiable capabilities.
#
NextGet to know about how to customize: