CUE 是一种开源数据验证语言和推理引擎,源于逻辑编程。虽然该语言不是通用编程语言,但它有许多应用,如数据验证、数据模板、配置、查询、代码生成甚至脚本编写。推理引擎可用于验证代码中的数据,或将其作为代码生成管道的一部分。
cuelang with kubevela #
在 KubeVela 中,Component 最终被渲染为 Kubernetes 平台上的实际资源对象,通常是一组 Kubernetes 资源,例如 Deployment、Service 等,这取决于 Component 的定义中所包含的信息。
具体而言,KubeVela 的核心引擎会根据 CueLang 文件中的定义,将 Component 转换为一个或多个 Kubernetes 资源的 YAML 表示。这些资源的种类和内容是根据 CueLang 文件中的输出部分定义的。
以下是一个简单的示例,展示一个 CueLang 文件定义一个简单的 Component,并将其渲染为一个 Deployment 资源:
parameter: {
image: string
port: int
}
output: {
apiVersion: "apps/v1"
kind: "Deployment"
metadata: name: context.name
spec: {
selector: matchLabels: app: context.name
template: {
metadata: labels: app: context.name
spec: {
containers: [{
name: context.name
image: parameter.image
ports: [{
containerPort: parameter.port
}]
}]
}
}
}
}
上述 CueLang 文件定义了一个 Deployment,其中包含一个容器,该容器的镜像和端口由参数指定。在渲染过程中,KubeVela 将根据这个定义生成相应的 YAML 表示,例如:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: nginx:latest
ports:
- containerPort: 80
webservice
# Code generated by KubeVela templates. DO NOT EDIT. Please edit the original cue file.
# Definition source cue file: vela-templates/definitions/registry/webserver.cue
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
annotations:
definition.oam.dev/description: webserver was composed by deployment and service
name: webserver
namespace: vela-system
spec:
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"] != _|_ {
env: parameter.env
}
if context["config"] != _|_ {
env: context.config
}
ports: [{
containerPort: parameter.port
}]
if parameter["cpu"] != _|_ {
resources: {
limits: cpu: parameter.cpu
requests: cpu: parameter.cpu
}
}
}]
}
}
}
// workload can have extra object composition by using 'outputs' keyword
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
}
workload:
definition:
apiVersion: apps/v1
kind: Deployment
Deployment
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
ports:
- containerPort: parameter.port
# 根据条件判断是否包含 command
{{- if parameter["cmd"] != _|_ }}
command: parameter.cmd
{{- end }}
# 根据条件判断是否包含 env
{{- if parameter["env"] != _|_ }}
env: parameter.env
{{- end }}
# 根据条件判断是否包含 config
{{- if context["config"] != _|_ }}
env: context.config
{{- end }}
# 根据条件判断是否包含 resources
{{- if parameter["cpu"] != _|_ }}
resources:
limits:
cpu: parameter.cpu
requests:
cpu: parameter.cpu
{{- end }}
apiVersion: v1
kind: Service
spec:
selector:
app.oam.dev/component: context.name
ports:
- port: parameter.port
targetPort: parameter.port