Version: v1.1

Terraform 组件

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

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

为云资源开发 ComponentDefinition

阿里云

弹性 IP为例。

为云资源开发一个 ComponentDefinition

这是 Terraform ComponentsDefinition 的脚手架。你只需要修改三个字段:metadata.namemetadata.annotations.definition.oam.dev/descriptionspec.schematic.terraform.configuration

  1. apiVersion: core.oam.dev/v1alpha2
  2. kind: ComponentDefinition
  3. metadata:
  4. name: # 1. ComponentDefinition name, like `alibaba-oss`
  5. namespace: {{.Values.systemDefinitionNamespace}}
  6. annotations:
  7. definition.oam.dev/description: # 2. description, like `Terraform configuration for Alibaba Cloud OSS object`
  8. labels:
  9. type: terraform
  10. spec:
  11. workload:
  12. definition:
  13. apiVersion: terraform.core.oam.dev/v1beta1
  14. kind: Configuration
  15. schematic:
  16. terraform:
  17. configuration: |
  18. # 3. The developed Terraform HCL

这里阿里云 EIP 的完整的 ComponentDefinition,我们热烈欢迎你将扩展的云资源的 ComponentDefinition 贡献到 oam-dev/kubevela

  1. apiVersion: core.oam.dev/v1alpha2
  2. kind: ComponentDefinition
  3. metadata:
  4. name: alibaba-eip
  5. namespace: {{.Values.systemDefinitionNamespace}}
  6. annotations:
  7. definition.oam.dev/description: Terraform configuration for Alibaba Cloud Elastic IP
  8. labels:
  9. type: terraform
  10. spec:
  11. workload:
  12. definition:
  13. apiVersion: terraform.core.oam.dev/v1beta1
  14. kind: Configuration
  15. schematic:
  16. terraform:
  17. configuration: |
  18. module "eip" {
  19. source = "github.com/zzxwill/terraform-alicloud-eip"
  20. name = var.name
  21. bandwidth = var.bandwidth
  22. }
  23. variable "name" {
  24. description = "Name to be used on all resources as prefix. Default to 'TF-Module-EIP'."
  25. default = "TF-Module-EIP"
  26. type = string
  27. }
  28. variable "bandwidth" {
  29. description = "Maximum bandwidth to the elastic public network, measured in Mbps (Mega bit per second)."
  30. type = number
  31. default = 5
  32. }
  33. output "EIP_ADDRESS" {
  34. description = "The elastic ip address."
  35. value = module.eip.this_eip_address.0
  36. }

验证

你可以通过 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 来验证。