扩展云资源

对云资源的集成需求往往是最频繁出现,比如你可能希望数据库、中间件等服务使用阿里云、AWS 等云厂商的,以获得生产级别的可用性并免去运维的麻烦。 Terraform 是目前业内支持云资源最广泛也最受欢迎的组件,KubeVela 对 Terraform 进行了额外的支持,使得用户可以通过 Kubernetes CRD 的方式配合 Terraform 使用任意的云资源。

为了使最终用户能够部署和消费云资源,当用户的要求超出了 内置云资源的能力, 管理员需要要为云资源准备 ComponentDefinitions。

以下是为云供应商阿里云、AWS 和 Azure 创建 Terraform 类型的云资源 ComponentDefinitions 的指南。

依赖

开发 Terraform 资源或模块

为云资源开发创建 Terraform 资源或模块。

比如我们为 AWS S3 bucket 开发了 Terraform 资源,并写入本地文件 aws_s3_bucket.tf 里,内容如下:

  1. resource "aws_s3_bucket" "bucket-acl" {
  2. bucket = var.bucket
  3. acl = var.acl
  4. }
  5. output "BUCKET_NAME" {
  6. value = aws_s3_bucket.bucket-acl.bucket_domain_name
  7. }
  8. variable "bucket" {
  9. description = "S3 bucket name"
  10. default = "vela-website"
  11. type = string
  12. }
  13. variable "acl" {
  14. description = "S3 bucket ACL"
  15. default = "private"
  16. type = string
  17. }

我们也给阿里云 EIP 开发了 Terraform 模板,并存储在 GitHub 库 https://github.com/oam-dev/terraform-alibaba-eip.git。

生成 ComponentDefinition

通过运行 vela def init 命令,我们可以基于 Terraform 资源或模块的云资源生成一个 ComponentDefinition,Terraform 资源或模板可以来自本地文件, 也可以来自远程 GitHub 仓库。

  1. $vela def init -h
  2. --git string Specify which git repository the configuration(HCL) is stored in. Valid when --provider/-p is set.
  3. --local string Specify the local path of the configuration(HCL) file. Valid when --provider/-p is set.

我们使用 --local 来接受来自本地文件的 Terraform 资源或模块来生成 ComponentDefinition。

  1. $vela def init s3 --type component --provider aws --desc "Terraform configuration for AWS S3" --local aws_s3_bucket.tf
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: ComponentDefinition
  4. metadata:
  5. annotations:
  6. definition.oam.dev/description: Terraform configuration for AWS S3
  7. creationTimestamp: null
  8. labels:
  9. type: terraform
  10. name: aws-s3
  11. namespace: vela-system
  12. spec:
  13. schematic:
  14. terraform:
  15. configuration: |
  16. resource "aws_s3_bucket" "bucket-acl" {
  17. bucket = var.bucket
  18. acl = var.acl
  19. }
  20. output "BUCKET_NAME" {
  21. value = aws_s3_bucket.bucket-acl.bucket_domain_name
  22. }
  23. variable "bucket" {
  24. description = "S3 bucket name"
  25. default = "vela-website"
  26. type = string
  27. }
  28. variable "acl" {
  29. description = "S3 bucket ACL"
  30. default = "private"
  31. type = string
  32. }
  33. workload:
  34. definition:
  35. apiVersion: terraform.core.oam.dev/v1beta1
  36. kind: Configuration
  37. status: {}

我们使用 --git 来接受来自远程 GitHub 仓库的 Terraform 模块或资源来生成 ComponentDefinition。

  1. $ vela def init eip --type component --provider alibaba --desc "Terraform configuration for Alibaba Cloud Elastic IP" --git https://github.com/oam-dev/terraform-alibaba-eip.git
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: ComponentDefinition
  4. metadata:
  5. annotations:
  6. definition.oam.dev/description: Terraform configuration for Alibaba Cloud Elastic
  7. IP
  8. creationTimestamp: null
  9. labels:
  10. type: terraform
  11. name: alibaba-eip
  12. namespace: vela-system
  13. spec:
  14. schematic:
  15. terraform:
  16. configuration: https://github.com/oam-dev/terraform-alibaba-eip.git
  17. type: remote
  18. workload:
  19. definition:
  20. apiVersion: terraform.core.oam.dev/v1beta1
  21. kind: Configuration
  22. status: {}

我们热烈欢迎你将扩展的云资源的 ComponentDefinition 贡献到 oam-dev/catalog

应用 ComponentDefinition

将生成的ComponentDefinition写入到文件中并将文件命名为terraform-<ComponentDefinition_NAME>.yaml,然后将其应用到正在运行中的kubernetes集群上。

  1. kubectl apply -f <FILENAME>

验证

你可以通过 vela show 命令快速验证 ComponentDefinition。

  1. $ vela show alibaba-eip
  2. # Properties
  3. +----------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------+----------+---------+
  4. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  5. +----------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------+----------+---------+
  6. | name | Name to be used on all resources as prefix. Default to 'TF-Module-EIP'. | string | true | |
  7. | bandwidth | Maximum bandwidth to the elastic public network, measured in Mbps (Mega bit per second). | number | true | |
  8. | writeConnectionSecretToRef | The secret which the cloud resource connection will be written to | [writeConnectionSecretToRef](#writeConnectionSecretToRef) | false | |
  9. +----------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------+----------+---------+
  10. ## writeConnectionSecretToRef
  11. +-----------+-----------------------------------------------------------------------------+--------+----------+---------+
  12. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  13. +-----------+-----------------------------------------------------------------------------+--------+----------+---------+
  14. | name | The secret name which the cloud resource connection will be written to | string | true | |
  15. | namespace | The secret namespace which the cloud resource connection will be written to | string | false | |
  16. +-----------+-----------------------------------------------------------------------------+--------+----------+---------+

如果表格能正常出来,ComponentDefinition 应该就可以工作了。更进一步,你可以通过文档部署云资源创建一个实际的 EIP 来验证。

生成文档

我们鼓励你为你的 ComponentDefinition 生成文档并提交给 [KubeVela官方网站](https://github.com/kubevela/kubevela.io)。

通过运行 vela def doc-gen 命令,我们可以基于已经应用在kubernetes集群上的ComponentDefinition生成相关文档,也可以基于本地ComponentDefinition文件生成相关文档。

基于已经应用在kubernetes集群上的ComponentDefinition生成文档时,需要提供该ComponentDefinition运行的namespace。

  1. $ vela def doc-gen alibaba-eip -n vela-system
  2. Generated docs for alibaba-eip in ./kubevela.io/docs/end-user/components/cloud-services/terraform/alibaba-eip.md

基于本地文件生成文档时,需要提供文件路径。

  1. $ vela def doc-gen alibaba-eip.yaml

将生成的文件移到 kubevela/kubevela.io 库。参考 贡献指南 来提交文档。