扩展 Terraform 插件

Terraform Controller 是为 KubeVela 提供部署和管理云资源的核心控制器。 它已经支持一些云供应商,包括 AWS、Azure、GCP、阿里云,腾讯云,百度云等等。 但 KubeVela 只支持部分云供应商的 Terraform Addons。

本指南将告诉你如何扩展 Terraform Addon 以支持你的云供应商。

如果你想支持的云供应商不在 Terraform Controller 支持的云供应商之列,你必须先支持它。 如果它已经被Terraform Controller支持,你可以继续扩展该插件。

  • 克隆 oam-dev/catalog
  1. $ git clone https://github.com/kubevela/catalog.git
  • 准备 metadata 文件

编辑你的云服务商的 metadata 文件 hack/addons/terraform/terraform-provider-scaffold/metadata.yaml

  1. ...
  2. # -------------------------------------Configuration Metadata for a Terraform Addon-------------------------------------
  3. # provider short name
  4. shortCloudName: tencent
  5. # The Cloud name of the provider
  6. completeCloudName: Tencent Cloud
  7. # When enabling a Terraform provider, these properties need to set for authentication. For Tencent Cloud,
  8. # name: Environment variable name when authenticating Terraform, like https://github.com/oam-dev/terraform-controller/blob/master/controllers/provider/credentials.go#L59
  9. # secretKey: Secret key when storing authentication information in a Kubernetes, like https://github.com/oam-dev/terraform-controller/blob/master/controllers/provider/credentials.go#L109.
  10. cloudProperties:
  11. - name: TENCENTCLOUD_SECRET_ID
  12. secretKey: secretID
  13. description: Get TENCENTCLOUD_SECRET_ID per this guide https://cloud.tencent.com/document/product/1213/67093
  14. - name: TENCENTCLOUD_SECRET_KEY
  15. secretKey: secretKey
  16. description: Get TENCENTCLOUD_SECRET_KEY per this guide https://cloud.tencent.com/document/product/1213/67093
  17. # If one property is region, please set `isRegion` to true
  18. - name: TENCENTCLOUD_REGION
  19. description: Get TENCENTCLOUD_REGION by picking one RegionId from Tencent Cloud region list https://cloud.tencent.com/document/api/1140/40509#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
  20. isRegion: true

为你的云服务商生成一个 Terraform 插件。生成的插件代码将存储在 addons/terraform-tencent

  1. $ make terraform-addon-gen
  2. go run hack/addons/terraform/gen.go hack/addons/terraform/provider-sample.yaml
  3. Generating addon for provider tencent in addons/terraform-tencent
  4. Rendering hack/addons/terraform/terraform-provider-skaffold/metadata.yaml
  5. Rendering hack/addons/terraform/terraform-provider-skaffold/readme.md
  6. Rendering hack/addons/terraform/terraform-provider-skaffold/resources/account-creds.cue
  7. Rendering hack/addons/terraform/terraform-provider-skaffold/resources/parameter.cue
  8. Rendering hack/addons/terraform/terraform-provider-skaffold/resources/provider.cue
  9. Rendering hack/addons/terraform/terraform-provider-skaffold/template.yaml
  10. $ ls addons/terraform-tencent
  11. definitions metadata.yaml readme.md resources template.yaml

启用该插件 检查是否创建了名称与你的云提供商相同的 Provider

  1. $ vela addon enable ./addons/terraform-tencent TENCENTCLOUD_SECRET_ID=xxx TENCENTCLOUD_SECRET_KEY=yyy TENCENTCLOUD_REGION=ap-chengdu
  2. I0207 10:15:14.005269 32481 apply.go:106] "patching object" name="addon-terraform-tencent" resource="core.oam.dev/v1beta1, Kind=Application"
  3. I0207 10:15:14.138645 32481 apply.go:106] "patching object" name="addon-secret-terraform-tencent" resource="/v1, Kind=Secret"
  4. Addon: terraform-tencent enabled Successfully.
  5. $ kubectl get provider
  6. NAME STATE AGE
  7. tencent ready 1d

我们鼓励你通过部署云资源进一步验证提供商。

提交 ./addons 中生成的代码,并创建一个 pull request。

为你的云供应商编写Terraform Addon启用文档,并将其添加到所有支持的云供应商

Last updated on 2023年8月4日 by Daniel Higuero