Skip to content

Commit 7ca23dc

Browse files
authored
Merge pull request #1471 from lynqtech/main
Control `init -upgrade` behaviour
2 parents 4c3c155 + d4adb42 commit 7ca23dc

File tree

7 files changed

+117
-2
lines changed

7 files changed

+117
-2
lines changed

api/v1alpha2/terraform_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ type TerraformSpec struct {
229229
// +optional
230230
RunnerTerminationGracePeriodSeconds *int64 `json:"runnerTerminationGracePeriodSeconds,omitempty"`
231231

232+
// UpgradeOnInit configures to upgrade modules and providers on initialization of a stack
233+
// +kubebuilder:default:=true
234+
// +optional
235+
UpgradeOnInit bool `json:"upgradeOnInit,omitempty"`
236+
232237
// RefreshBeforeApply forces refreshing of the state before the apply step.
233238
// +kubebuilder:default:=false
234239
// +optional

charts/tofu-controller/crds/crds.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10452,6 +10452,11 @@ spec:
1045210452
Defaults to `0s` which will behave as though `LockTimeout` was not set
1045310453
type: string
1045410454
type: object
10455+
upgradeOnInit:
10456+
default: true
10457+
description: UpgradeOnInit configures to upgrade modules and providers
10458+
on initialization of a stack
10459+
type: boolean
1045510460
values:
1045610461
description: |-
1045710462
Values map to the Terraform variable "values", which is an object of arbitrary values.

config/crd/bases/infra.contrib.fluxcd.io_terraforms.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10452,6 +10452,11 @@ spec:
1045210452
Defaults to `0s` which will behave as though `LockTimeout` was not set
1045310453
type: string
1045410454
type: object
10455+
upgradeOnInit:
10456+
default: true
10457+
description: UpgradeOnInit configures to upgrade modules and providers
10458+
on initialization of a stack
10459+
type: boolean
1045510460
values:
1045610461
description: |-
1045710462
Values map to the Terraform variable "values", which is an object of arbitrary values.

controllers/tf_controller_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ terraform {
344344

345345
initRequest := &runner.InitRequest{
346346
TfInstance: tfInstance,
347-
Upgrade: true,
347+
Upgrade: terraform.Spec.UpgradeOnInit,
348348
ForceCopy: true,
349349
// Terraform: terraformBytes,
350350
}

docs/References/terraform.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,18 @@ large, complex or slow-moving Terraform managed resources.</p>
17781778
</tr>
17791779
<tr>
17801780
<td>
1781+
<code>upgradeOnInit</code><br>
1782+
<em>
1783+
bool
1784+
</em>
1785+
</td>
1786+
<td>
1787+
<em>(Optional)</em>
1788+
<p>UpgradeOnInit configures to upgrade modules and providers on initialization of a stack</p>
1789+
</td>
1790+
</tr>
1791+
<tr>
1792+
<td>
17811793
<code>refreshBeforeApply</code><br>
17821794
<em>
17831795
bool
@@ -2374,6 +2386,18 @@ large, complex or slow-moving Terraform managed resources.</p>
23742386
</tr>
23752387
<tr>
23762388
<td>
2389+
<code>upgradeOnInit</code><br>
2390+
<em>
2391+
bool
2392+
</em>
2393+
</td>
2394+
<td>
2395+
<em>(Optional)</em>
2396+
<p>UpgradeOnInit configures to upgrade modules and providers on initialization of a stack</p>
2397+
</td>
2398+
</tr>
2399+
<tr>
2400+
<td>
23772401
<code>refreshBeforeApply</code><br>
23782402
<em>
23792403
bool
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
```

docs/use-tf-controller/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@
3030
- [How to troubleshoot with **Break the Glass** mode](troubleshooting-with-break-the-glass-mode.md)
3131
- [How to enable cross-namespace references](use-cross-namespace-refs.md)
3232
- [How to run TF-controller in Azure Kubernetes Service](with-azure.md)
33-
- [How to upgrade TF-controller to a newer version](upgrade-tf-controller.md)
33+
- [How to upgrade TF-controller to a newer version](upgrade-tf-controller.md)
34+
- [How to control the `init -upgrade` behaviour](control-init-upgrade.md)

0 commit comments

Comments
 (0)