Advanced Features
As a Data Configuration Language, CUE allows you to do some advanced templating magic in definition objects.
#
Render Multiple Resources With a LoopYou can define the for-loop inside the outputs
.
Note that in this case the type of
parameter
field used in the for-loop must be a map.
Below is an example that will render multiple Kubernetes Services in one trait:
The usage of this trait could be:
#
Execute HTTP Request in Trait DefinitionThe trait definition can send a HTTP request and capture the response to help you rendering the resource with keyword processing
.
You can define HTTP request method
, url
, body
, header
and trailer
in the processing.http
section, and the returned data will be stored in processing.output
.
Please ensure the target HTTP server returns a JSON data.
output
.
Then you can reference the returned data from processing.output
in patch
or output/outputs
.
Below is an example:
In above example, this trait definition will send request to get the token
data, and then patch the data to given component instance.
#
Data PassingA trait definition can read the generated API resources (rendered from output
and outputs
) of given component definition.
KubeVela will ensure the component definitions are always rendered before traits definitions.
Specifically, the context.output
contains the rendered workload API resource (whose GVK is indicated by spec.workload
in component definition), and use context.outputs.<xx>
to contain all the other rendered API resources.
Below is an example for data passing:
In detail, during rendering worker
ComponentDefinition
:
- the rendered Kubernetes Deployment resource will be stored in the
context.output
, - all other rendered resources will be stored in
context.outputs.<xx>
, with<xx>
is the unique name in everytemplate.outputs
.
Thus, in TraitDefinition
, it can read the rendered API resources (e.g. context.outputs.gameconfig.data.enemies
) from the context
.