How-to
In this section, it will introduce how to use CUE to declare app components via ComponentDefinition
.
Here is a CUE based ComponentDefinition
example which provides a abstraction for stateless workload type:
In detail:
.spec.workload
is required to indicate the workload type of this component..spec.schematic.cue.template
is a CUE template, specifically:- The
output
filed defines the template for the abstraction. - The
parameter
filed defines the template parameters, i.e. the configurable properties exposed in theApplication
abstraction (and JSON schema will be automatically generated based on them).
- The
Let’s declare another component named task
, i.e. an abstraction for run-to-completion workload.
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
name: task
annotations:
definition.oam.dev/description: "Describes jobs that run code or a script to completion."
spec:
workload:
definition:
apiVersion: batch/v1
kind: Job
schematic:
cue:
template: |
output: {
apiVersion: "batch/v1"
kind: "Job"
spec: {
parallelism: parameter.count
completions: parameter.count
template: spec: {
restartPolicy: parameter.restart
containers: [{
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
}]
}
}
}
parameter: {
count: *1 | int
image: string
restart: *"Never" | string
cmd?: [...string]
}
Save above ComponentDefinition
objects to files and install them to your Kubernetes cluster by $ kubectl apply -f stateless-def.yaml task-def.yaml
Declare an Application
apiVersion: core.oam.dev/v1alpha2
kind: Application
metadata:
name: website
spec:
components:
- name: hello
type: stateless
properties:
image: crccheck/hello-world
name: mysvc
- name: countdown
type: task
properties:
image: centos:7
cmd:
- "-c"
- "for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done"
Above application resource will generate and manage following Kubernetes resources in your target cluster based on the output
in CUE template and user input in Application
properties.
KubeVela allows you to reference the runtime information of your application via context
keyword.
The most widely used context is application name(context.appName
) component name(context.name
).
context: {
appName: string
name: string
}
For example, let’s say you want to use the component name filled in by users as the container name in the workload instance:
parameter: {
image: string
}
output: {
...
spec: {
containers: [{
name: context.name
image: parameter.image
}]
}
...
}
Full available information in CUE context
Composition
It’s common that a component definition is composed by multiple API resources, for example, a webserver
component that is composed by a Deployment and a Service. CUE is a great solution to achieve this in simplified primitives.
KubeVela requires you to define the template of workload type in output
section, and leave all the other resource templates in outputs
section with format as below:
Below is the example for webserver
definition:
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
name: webserver
annotations:
definition.oam.dev/description: "webserver is a combo of Deployment + Service"
spec:
workload:
definition:
apiVersion: apps/v1
kind: Deployment
schematic:
cue:
template: |
output: {
apiVersion: "apps/v1"
kind: "Deployment"
spec: {
selector: matchLabels: {
"app.oam.dev/component": context.name
}
template: {
metadata: labels: {
"app.oam.dev/component": context.name
}
spec: {
containers: [{
name: context.name
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
if parameter["env"] != _|_ {
}
if context["config"] != _|_ {
env: context.config
}
containerPort: parameter.port
}]
if parameter["cpu"] != _|_ {
resources: {
limits:
cpu: parameter.cpu
requests:
cpu: parameter.cpu
}
}
}]
}
}
}
}
// an extra template
outputs: service: {
apiVersion: "v1"
kind: "Service"
spec: {
selector: {
"app.oam.dev/component": context.name
}
ports: [
{
port: parameter.port
targetPort: parameter.port
},
]
}
}
parameter: {
image: string
cmd?: [...string]
port: *80 | int
env?: [...{
name: string
value?: string
valueFrom?: {
secretKeyRef: {
name: string
key: string
}
}
}]
cpu?: string
}
The user could now declare an Application
with it:
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: webserver-demo
namespace: default
spec:
components:
- name: hello-world
type: webserver
properties:
image: crccheck/hello-world
port: 8000
env:
- name: "foo"
value: "bar"
cpu: "100m"
It will generate and manage below API resources in target cluster:
What’s Next
Please check the documentation about why we support CUE as first-class templating solution and more details about using CUE efficiently.