hashicorp/terraform-cloud-operator该Kubernetes Operator是一款用于在Kubernetes环境中管理Terraform Cloud资源的工具。它通过Kubernetes自定义资源(CR)实现声明式管理,允许用户直接通过Kubernetes API定义和控制Terraform Cloud的workspaces、runs、variables等资源,实现Kubernetes与Terraform Cloud的无缝集成与统一管理。
TerraformWorkspace、TerraformRun等自定义资源定义(CRD),支持通过Kubernetes资源声明Terraform Cloud资源状态yaml# terraform-operator-crd.yaml apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: terraformworkspaces.terraform-operator.example.com spec: group: terraform-operator.example.com versions: - name: v1alpha1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: name: type: string description: type: string terraformVersion: type: string variables: type: array items: type: object properties: key: type: string value: type: string valueFrom: type: object properties: secretKeyRef: type: object properties: name: type: string key: type: string category: type: string sensitive: type: boolean scope: Namespaced names: plural: terraformworkspaces singular: terraformworkspace kind: TerraformWorkspace shortNames: - tfws
应用CRD:
bashkubectl apply -f terraform-operator-crd.yaml
创建存储Terraform Cloud API令牌的Secret:
bashkubectl create secret generic terraform-cloud-credentials \ --from-literal=api-token="your-terraform-cloud-api-token"
yaml# terraform-operator-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: terraform-operator namespace: default spec: replicas: 1 selector: matchLabels: app: terraform-operator template: metadata: labels: app: terraform-operator spec: containers: - name: operator image: [operator-image]:[version] # 替换为实际镜像名称和版本 env: - name: TERRAFORM_CLOUD_API_TOKEN valueFrom: secretKeyRef: name: terraform-cloud-credentials key: api-token - name: TF_CLOUD_ORGANIZATION value: "your-terraform-org" # 替换为实际Terraform组织名 - name: LOG_LEVEL value: "info" resources: requests: cpu: 100m memory: 128Mi limits: cpu: 500m memory: 256Mi
部署Operator:
bashkubectl apply -f terraform-operator-deployment.yaml
yaml# example-workspace.yaml apiVersion: terraform-operator.example.com/v1alpha1 kind: TerraformWorkspace metadata: name: production-app spec: name: "production-app" description: "Production environment workspace managed by Kubernetes" terraformVersion: "1.6.0" variables: - key: "environment" value: "production" category: "terraform" sensitive: false - key: "db_password" valueFrom: secretKeyRef: name: db-credentials key: password category: "env" sensitive: true
应用资源:
bashkubectl apply -f example-workspace.yaml
| 环境变量名 | 描述 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| TERRAFORM_CLOUD_API_TOKEN | Terraform Cloud API访问令牌 | string | 是 | - |
| TF_CLOUD_ORGANIZATION | Terraform Cloud组织名称 | string | 是 | - |
| LOG_LEVEL | 日志级别(debug/info/warn/error) | string | 否 | info |
| OPERATOR_NAMESPACE | 监听的命名空间(默认所有) | string | 否 | ""(所有) |
| SYNC_INTERVAL | 状态同步间隔(秒) | int | 否 | 30 |
| 字段路径 | 描述 | 类型 | 必填 |
|---|---|---|---|
| spec.name | Terraform Cloud工作区名称 | string | 是 |
| spec.description | 工作区描述信息 | string | 否 |
| spec.terraformVersion | 指定Terraform版本 | string | 否 |
| spec.variables | 工作区变量列表 | array | 否 |
| spec.variables[].key | 变量键名 | string | 是 |
| spec.variables[].value | 非敏感变量值 | string | 二选一 |
| spec.variables[].valueFrom.secretKeyRef | 敏感变量的Secret引用 | object | 二选一 |
| spec.variables[].category | 变量类别(terraform/env) | string | 是 |
| spec.variables[].sensitive | 是否为敏感变量 | boolean | 否 |
workspaces:read、workspaces:write、runs:read、runs:write权限
manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务