|
| 1 | +# Control Tofu-Controller behaviour on `terraform init` |
| 2 | +...and pin your providers via `.terraform.lock.hcl` |
| 3 | + |
| 4 | +You may or may not ship `.terraform.lock.hcl` with your stack, which pins the used providers. |
| 5 | + |
| 6 | +The Tofu-Controller, by default, does a `tofu init -upgrade` when starting a runner pod and updates the used providers |
| 7 | +to their latest available version, as specified in your code. |
| 8 | + |
| 9 | +To disable the automatic upgrade, simply add the flag `upgradeOnInit: false` |
| 10 | +```yaml hl_lines="7" |
| 11 | +apiVersion: infra.contrib.fluxcd.io/v1alpha2 |
| 12 | +kind: Terraform |
| 13 | +metadata: |
| 14 | + name: helloworld |
| 15 | + namespace: flux-system |
| 16 | +spec: |
| 17 | + # [...] |
| 18 | + upgradeOnInit: false |
| 19 | +``` |
| 20 | +
|
| 21 | +## Inject a `.terraform.lock.hcl` to pin a provider |
| 22 | +At certain times you want to pin a provider to a certain version. Simply combine multiple features of the controller here - `FileMapping` and `upgradeOnInit` |
| 23 | + |
| 24 | +1. example `.terraform.lock.hcl` |
| 25 | + ```hcl |
| 26 | + provider "registry.terraform.io/hashicorp/aws" { |
| 27 | + version = "5.70.0" |
| 28 | + hashes = [ |
| 29 | + "h1:LKnWZnujHcQPm3MAk4elP3H9VXNjlO6rNqlO5s330Yg=", |
| 30 | + "zh:09cbec93c324e6f03a866244ecb2bae71fdf1f5d3d981e858b745c90606b6b6d", |
| 31 | + "zh:19685d9f4c9ddcfa476a9a428c6c612be4a1b4e8e1198fbcbb76436b735284ee", |
| 32 | + "zh:3358ee6a2b24c982b7c83fac0af6898644d1bbdabf9c4e0589e91e427641ba88", |
| 33 | + "zh:34f9f2936de7384f8ed887abdbcb54aea1ce7b0cf2e85243a3fd3904d024747f", |
| 34 | + "zh:4a99546cc2140304c90d9ccb9db01589d4145863605a0fcd90027a643ea3ec5d", |
| 35 | + "zh:4da32fec0e10dab5aa3dea3c9fe57adc973cc73a71f5d59da3f65d85d925dc3f", |
| 36 | + "zh:659cf94522bc38ce0af70f7b0371b2941a0e0bcad02d17c1a7b264575fe07224", |
| 37 | + "zh:6f1c172c9b98bc86e4f0526872098ee3246c2620f7b323ce0c2ce6427987f7d2", |
| 38 | + "zh:79bf8fb8f37c308742e287694a9de081ff8502b065a390d1bcfbd241b4eca203", |
| 39 | + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", |
| 40 | + "zh:b7a5e1dfd9e179d70a169ddd4db44b56da90309060e27d36b329fe5fb3528e29", |
| 41 | + "zh:c2cc728cb18ffd5c4814a10c203452c71f5ab0c46d68f9aa9183183fa60afd87", |
| 42 | + "zh:c89bb37d2b8947c9a0d62b0b86ace51542f3327970f4e56a68bf81d9d0b8b65b", |
| 43 | + "zh:ef2a61e8112c3b5e70095508aadaadf077e904b62b9cfc22030337f773bba041", |
| 44 | + "zh:f714550b858d141ea88579f25247bda2a5ba461337975e77daceaf0bb7a9c358", |
| 45 | + ] |
| 46 | + } |
| 47 | + ``` |
| 48 | +2. Kubernetes secret `terraform-lock-hcl` |
| 49 | + ```yaml |
| 50 | + kind: Secret |
| 51 | + apiVersion: v1 |
| 52 | + data: |
| 53 | + lock: <base64 encoded data of above> |
| 54 | + metadata: |
| 55 | + name: terraform-lock-hcl |
| 56 | + namespace: flux-system |
| 57 | + type: Opaque |
| 58 | + ``` |
| 59 | +3. Add a `FileMapping` + disable upgrade on init |
| 60 | + ```yaml |
| 61 | + apiVersion: infra.contrib.fluxcd.io/v1alpha2 |
| 62 | + kind: Terraform |
| 63 | + metadata: |
| 64 | + name: helloworld |
| 65 | + namespace: flux-system |
| 66 | + spec: |
| 67 | + # [...] |
| 68 | + upgradeOnInit: false |
| 69 | + FileMapping: |
| 70 | + - location: workspace |
| 71 | + path: .terraform.lock.hcl |
| 72 | + secretRef: |
| 73 | + key: lock |
| 74 | + name: terraform-lock-hcl |
| 75 | + ``` |
0 commit comments