A machine-readable index of this documentation for AI agents is available at https://kubevela.io/llms.txt. Markdown versions of documentation pages are available by appending .md to their URLs.
Skip to main content
Version: Terraform Controller 0.9

How It Works

terraform-controller has two objects of its own, Configuration and Provider, both in terraform.core.oam.dev. Everything else it does is ordinary Kubernetes: a Job runs Terraform, and Secrets and ConfigMaps carry what goes in and what comes out.

Configuration​

A Configuration (terraform.core.oam.dev/v1beta2) is one Terraform root module and its inputs:

FieldWhat it is
hclThe Terraform, inline.
remote, path, gitRefOr a git repository holding it, a directory inside it, and a branch, tag or commit. One of hcl and remote is required, not both.
variableValues for the module's variables.
writeConnectionSecretToRefThe Secret the outputs go to, one key per Terraform output.
providerRefThe Provider holding the cloud credentials. Defaults to default in default.
inlineCredentialstrue when the Terraform needs no Provider: no credentials, or credentials it finds itself.
backendWhere the state goes, if not the default: see State.
deleteResourcefalse keeps the cloud resources when the Configuration is deleted. Defaults to true.
forceDeleteDelete the Configuration even if terraform destroy can't run. The resources may be left behind.
customRegionA region that overrides the Provider's.
gitCredentialsSecretReferenceSSH credentials for a private remote.
JobEnvExtra environment variables for the Terraform Job.

A KubeVela component of a Terraform type becomes one of these: vela-core sets hcl or remote from the ComponentDefinition's schematic.terraform, and turns the component's properties into variable, except the fields above, which it passes through. So writeConnectionSecretToRef, providerRef and deleteResource work as component properties on every Terraform component type.

Provider​

A Provider (terraform.core.oam.dev/v1beta1) holds one set of cloud credentials:

apiVersion: terraform.core.oam.dev/v1beta1
kind: Provider
metadata:
name: default
namespace: default
spec:
provider: aws # alibaba, aws, azure, baidu, ec, gcp, huawei, tencent, ucloud, vsphere or custom
region: us-east-1
credentials:
source: Secret
secretRef:
namespace: vela-system
name: aws-account-creds
key: credentials

The controller reads the Secret, checks it has what that cloud needs, and marks the Provider ready. When a Configuration runs, the credentials go to the Terraform Job as the environment variables that cloud's Terraform provider reads (AWS_ACCESS_KEY_ID, ALICLOUD_ACCESS_KEY, ...). On KubeVela, vela config create -t terraform-<cloud> writes both the Secret and the Provider.

What the controller creates​

For a Configuration called random-id in default:

ObjectWhereHolds
ConfigMap tf-random-idConfiguration's namespaceThe Terraform, with the backend block added.
Secret variable-random-idConfiguration's namespaceThe variables and credentials, as environment variables for the Job.
Job random-id-applyConfiguration's namespaceterraform init and terraform apply, in the terraformImage the chart sets. Started again when the HCL or a variable changes.
Secret tfstate-default-random-idthe backend namespaceThe Terraform state.
Secret from writeConnectionSecretToRefwhere it saysThe outputs.
Job random-id-destroyConfiguration's namespaceterraform destroy, when the Configuration is deleted.

The Jobs run as the tf-executor-service-account ServiceAccount, which the controller creates in each namespace it runs a Job in. With the chart's controllerNamespace set, every Job, ConfigMap and variable Secret goes to that one namespace instead, named by the Configuration's UID rather than its name.

The controller labels the connection Secret as the Configuration's own (terraform.core.oam.dev/owned-by, owned-namespace, created-by). It only writes into, and only deletes, a Secret with its own labels, so two Configurations can't share one, and it never takes over a Secret it didn't create.

State​

By default the state goes to a Secret through Terraform's kubernetes backend, in the namespace the chart's backend.namespace names (vela-system unless set). Set spec.backend to put it elsewhere:

spec:
backend:
backendType: kubernetes
kubernetes:
namespace: tf-state
secret_suffix: my-app

backendType: s3 stores it in an S3 bucket instead. A backend block written in the HCL itself is replaced by the controller's.

Lifecycle​

status.apply.state says where a Configuration is:

(new) ──▶ ProvisioningAndChecking ──▶ GeneratingTerraformOutputs ──▶ Available
│ │
▼ HCL or variable changes
ApplyFailed │
▼
ConfigurationReloading ──▶ ProvisioningAndChecking

deleted ──▶ Destroying ──▶ (gone) or DestroyFailed
StateMeans
ProvisioningAndCheckingThe apply Job is running.
GeneratingTerraformOutputsThe apply finished; the outputs are being written to the Secret. It stays here if the Secret is refused, with the reason in status.apply.message.
AvailableDone: the resources exist and the outputs are written.
ConfigurationReloadingThe HCL or a variable changed; a new apply Job is starting.
ApplyFailed, TerraformInitErrorTerraform failed. In v0.9.1 these are never reached: a failing run stays in ProvisioningAndChecking, and the error is only in the Job's pod log (see Change, Fail and Delete).
ProviderNotFound, ProviderNotReadyThe providerRef names no Provider, or one whose credentials don't check out.
ConfigurationSpecNotValidBoth or neither of hcl and remote are set.
InvalidRegion, InvalidGitCredentialsSecretReference, ...A referenced setting is missing or malformed.
Destroying, DestroyFailedThe destroy Job is running, or failed. The finalizer keeps the Configuration until it succeeds.

On delete, the controller runs the destroy Job and removes the Configuration's finalizer once it succeeds. With deleteResource: false it skips the destroy and only cleans up its own objects. The AllowDeleteProvisioningResource feature gate, on by default, lets you delete a Configuration whose apply never finished, destroying whatever it did create.