Version: v1.1

编辑管理模块定义

在 KubeVela CLI (>= v1.1.0) 工具中,vela def 命令组为开发者提供了一系列便捷的 X-Definition 编写工具,使得 Definition 的编写将全部在 CUE 文件中进行,避免将 Template CUE 与 Kubernetes 的 YAML 格式进行混合,方便进行格式化与校验。

init

vela def init 是一个用来帮助用户初始化新的 Definition 的脚手架命令。用户可以通过 vela def init my-trait -t trait --desc "My trait description." 来创建一个新的空白 TraitDefinition ,如下

  1. "my-trait": {
  2. annotations: {}
  3. attributes: {
  4. appliesToWorkloads: []
  5. conflictsWith: []
  6. definitionRef: ""
  7. podDisruptive: false
  8. workloadRefPath: ""
  9. }
  10. description: "My trait description."
  11. labels: {}
  12. type: "trait"
  13. }
  14. template: patch: {}

或者是采用 vela def init my-comp --interactive 来交互式地创建新的 Definition 。

  1. $ vela def init my-comp --interactive
  2. Please choose one definition type from the following values: component, trait, policy, workload, scope, workflow-step
  3. > Definition type: component
  4. > Definition description: My component definition.
  5. Please enter the location the template YAML file to build definition. Leave it empty to generate default template.
  6. > Definition template filename:
  7. Please enter the output location of the generated definition. Leave it empty to print definition to stdout.
  8. > Definition output filename: my-component.cue
  9. Definition written to my-component.cue

除此之外,如果用户创建 ComponentDefinition 的目的是一个 Deployment(或者是其他的 Kubernetes Object ),而这个 Deployment 已经有了 YAML 格式的模版,用户还可以通过 --template-yaml 参数来完成从 YAML 到 CUE 的自动转换。例如如下的 my-deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: hello-world
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app.kubernetes.io/name: hello-world
  10. template:
  11. metadata:
  12. labels:
  13. app.kubernetes.io/name: hello-world
  14. spec:
  15. containers:
  16. - name: hello-world
  17. image: somefive/hello-world
  18. ports:
  19. - name: http
  20. containerPort: 80
  21. protocol: TCP
  22. ---
  23. apiVersion: v1
  24. kind: Service
  25. metadata:
  26. name: hello-world-service
  27. spec:
  28. selector:
  29. app: hello-world
  30. ports:
  31. - name: http
  32. protocol: TCP
  33. port: 80
  34. targetPort: 8080
  35. type: LoadBalancer

运行 vela def init my-comp -t component --desc "My component." --template-yaml ./my-deployment.yaml 可以得到 CUE 格式的 ComponentDefinition

  1. "my-comp": {
  2. annotations: {}
  3. attributes: workload: definition: {
  4. apiVersion: "<change me> apps/v1"
  5. kind: "<change me> Deployment"
  6. }
  7. description: "My component."
  8. labels: {}
  9. type: "component"
  10. }
  11. template: {
  12. output: {
  13. metadata: name: "hello-world"
  14. spec: {
  15. replicas: 1
  16. selector: matchLabels: "app.kubernetes.io/name": "hello-world"
  17. template: {
  18. metadata: labels: "app.kubernetes.io/name": "hello-world"
  19. spec: containers: [{
  20. name: "hello-world"
  21. image: "somefive/hello-world"
  22. ports: [{
  23. name: "http"
  24. containerPort: 80
  25. protocol: "TCP"
  26. }]
  27. }]
  28. }
  29. }
  30. apiVersion: "apps/v1"
  31. kind: "Deployment"
  32. }
  33. outputs: "hello-world-service": {
  34. metadata: name: "hello-world-service"
  35. spec: {
  36. ports: [{
  37. name: "http"
  38. protocol: "TCP"
  39. port: 80
  40. targetPort: 8080
  41. }]
  42. selector: app: "hello-world"
  43. type: "LoadBalancer"
  44. }
  45. apiVersion: "v1"
  46. kind: "Service"
  47. }
  48. parameter: {}
  49. }

接下来,用户就可以在该文件的基础上进一步做进一步的修改了。比如将属性中对于 workload.definition 中的 \<change me> 去掉。

vet

在初始化 Definition 文件之后,可以运行 vela def vet my-comp.cue 来校验 Definition 是否在语法上有错误。比如如果少写了一个括号,该命令能够帮助用户识别出来。

  1. $ vela def vet my-comp.cue
  2. Validation succeed.

render / apply

确认 Definition 撰写无误后,开发者可以运行 vela def apply my-comp.cue --namespace my-namespace 来将该 Definition 应用在 Kubernetes 的 my-namespace 命名空间中。如果想了解一下 CUE 格式的 Definition 文件会被渲染成什么样的 Kubernetes YAML 文件,可以使用 vela def apply my-comp.cue --dry-run 或者 vela def render my-comp.cue -o my-comp.yaml 来预先渲染一下 YAML 文件进行确认。

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: ComponentDefinition
  3. metadata:
  4. annotations:
  5. definition.oam.dev/description: My component.
  6. labels: {}
  7. name: my-comp
  8. namespace: vela-system
  9. spec:
  10. schematic:
  11. cue:
  12. template: |
  13. output: {
  14. metadata: name: "hello-world"
  15. spec: {
  16. replicas: 1
  17. selector: matchLabels: "app.kubernetes.io/name": "hello-world"
  18. template: {
  19. metadata: labels: "app.kubernetes.io/name": "hello-world"
  20. spec: containers: [{
  21. name: "hello-world"
  22. image: "somefive/hello-world"
  23. ports: [{
  24. name: "http"
  25. containerPort: 80
  26. protocol: "TCP"
  27. }]
  28. }]
  29. }
  30. }
  31. apiVersion: "apps/v11"
  32. kind: "Deployment"
  33. }
  34. outputs: "hello-world-service": {
  35. metadata: name: "hello-world-service"
  36. spec: {
  37. ports: [{
  38. name: "http"
  39. protocol: "TCP"
  40. port: 80
  41. targetPort: 8080
  42. }]
  43. selector: app: "hello-world"
  44. type: "LoadBalancer"
  45. }
  46. apiVersion: "v1"
  47. kind: "Service"
  48. }
  49. parameter: {}
  50. workload:
  51. definition:
  52. apiVersion: apps/v1
  53. kind: Deployment
  1. $ vela def apply my-comp.cue -n my-namespace
  2. ComponentDefinition my-comp created in namespace my-namespace.

get / list / edit / del

在 apply 命令后,开发者可以采用原生的 kubectl 来对结果进行确认,但是正如我们上文提到的,YAML 格式的结果会相对复杂。使用 vela def get 命令可以自动将其转换成 CUE 格式,方便用户查看。

  1. $ vela def get my-comp -t component

或者用户可以通过 vela def list 命令来查看当前系统中安装的所有 Definition(可以指定命名空间及类型)。

  1. $ vela def list -n my-namespace -t component
  2. NAME TYPE NAMESPACE DESCRIPTION
  3. my-comp ComponentDefinition my-namespace My component.

同样的,在使用 vela def edit 命令来编辑 Definition 时,用户也只需要对转换过的 CUE 格式 Definition 进行修改,该命令会自动完成格式转换。用户也可以通过设定环境变量 EDITOR 来使用自己想要使用的编辑器。

  1. $ EDITOR=vim vela def edit my-comp

类似的,用户可以运行 vela def del 来删除相应的 Definition。

  1. $ vela def del my-comp -n my-namespace
  2. Are you sure to delete the following definition in namespace my-namespace?
  3. ComponentDefinition my-comp: My component.
  4. [yes|no] > yes
  5. ComponentDefinition my-comp in namespace my-namespace deleted.

下一步