Skip to content

Commit b278d36

Browse files
Add take_ownership attribute (#1680)
1 parent 1efcb0e commit b278d36

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

.changelog/1680.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement:
2+
`helm_release`: add support for the `take_ownership` field
3+
```

docs/resources/release.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ A Chart is a Helm package. It contains all of the resource definitions necessary
5555
- `set_list` (Block List) Custom list values to be merged with the values. (see [below for nested schema](#nestedblock--set_list))
5656
- `set_sensitive` (Block Set) Custom sensitive values to be merged with the values. (see [below for nested schema](#nestedblock--set_sensitive))
5757
- `skip_crds` (Boolean) If set, no CRDs will be installed. By default, CRDs are installed if not already present. Defaults to `false`.
58+
- `take_ownership` (Boolean) If set, allows Helm to adopt existing resources not marked as managed by the release. Defaults to `false`.
5859
- `timeout` (Number) Time in seconds to wait for any individual kubernetes operation. Defaults to 300 seconds.
5960
- `upgrade_install` (Boolean) If true, the provider will install the release at the specified version even if a release not controlled by the provider is present: this is equivalent to running 'helm upgrade --install' with the Helm CLI. WARNING: this may not be suitable for production use -- see the 'Upgrade Mode' note in the provider documentation. Defaults to `false`.
6061
- `values` (List of String) List of values in raw yaml format to pass to helm.

helm/resource_helm_release.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type HelmReleaseModel struct {
106106
SetSensitive types.List `tfsdk:"set_sensitive"`
107107
SkipCrds types.Bool `tfsdk:"skip_crds"`
108108
Status types.String `tfsdk:"status"`
109+
TakeOwnership types.Bool `tfsdk:"take_ownership"`
109110
Timeout types.Int64 `tfsdk:"timeout"`
110111
UpgradeInstall types.Bool `tfsdk:"upgrade_install"`
111112
Values types.List `tfsdk:"values"`
@@ -133,6 +134,7 @@ var defaultAttributes = map[string]interface{}{
133134
"reset_values": false,
134135
"reuse_values": false,
135136
"skip_crds": false,
137+
"take_ownership": false,
136138
"timeout": int64(300),
137139
"verify": false,
138140
"wait": true,
@@ -497,6 +499,12 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
497499
Computed: true,
498500
Description: "Status of the release",
499501
},
502+
"take_ownership": schema.BoolAttribute{
503+
Optional: true,
504+
Computed: true,
505+
Default: booldefault.StaticBool(defaultAttributes["take_ownership"].(bool)),
506+
Description: "If set, Helm will take ownership of resources not already annotated by this release. Useful for migrations or recovery.",
507+
},
500508
"timeout": schema.Int64Attribute{
501509
Optional: true,
502510
Computed: true,
@@ -823,6 +831,7 @@ func (r *HelmRelease) Create(ctx context.Context, req resource.CreateRequest, re
823831
client.WaitForJobs = state.WaitForJobs.ValueBool()
824832
client.Devel = state.Devel.ValueBool()
825833
client.DependencyUpdate = state.DependencyUpdate.ValueBool()
834+
client.TakeOwnership = state.TakeOwnership.ValueBool()
826835
client.Timeout = time.Duration(state.Timeout.ValueInt64()) * time.Second
827836
client.Namespace = state.Namespace.ValueString()
828837
client.ReleaseName = state.Name.ValueString()
@@ -1091,6 +1100,7 @@ func (r *HelmRelease) Update(ctx context.Context, req resource.UpdateRequest, re
10911100

10921101
client.Devel = plan.Devel.ValueBool()
10931102
client.Namespace = plan.Namespace.ValueString()
1103+
client.TakeOwnership = plan.TakeOwnership.ValueBool()
10941104
client.Timeout = time.Duration(plan.Timeout.ValueInt64()) * time.Second
10951105
client.Wait = plan.Wait.ValueBool()
10961106
client.WaitForJobs = plan.WaitForJobs.ValueBool()
@@ -1979,6 +1989,7 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
19791989
install.WaitForJobs = plan.WaitForJobs.ValueBool()
19801990
install.Devel = plan.Devel.ValueBool()
19811991
install.DependencyUpdate = plan.DependencyUpdate.ValueBool()
1992+
install.TakeOwnership = plan.TakeOwnership.ValueBool()
19821993
install.Timeout = time.Duration(plan.Timeout.ValueInt64()) * time.Second
19831994
install.Namespace = plan.Namespace.ValueString()
19841995
install.ReleaseName = plan.Name.ValueString()
@@ -2053,6 +2064,7 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
20532064
upgrade.ChartPathOptions = *cpo
20542065
upgrade.Devel = plan.Devel.ValueBool()
20552066
upgrade.Namespace = plan.Namespace.ValueString()
2067+
upgrade.TakeOwnership = plan.TakeOwnership.ValueBool()
20562068
upgrade.Timeout = time.Duration(plan.Timeout.ValueInt64()) * time.Second
20572069
upgrade.Wait = plan.Wait.ValueBool()
20582070
upgrade.DryRun = true

helm/resource_helm_release_stateupgrader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func (r *HelmRelease) buildUpgradeStateMap(_ context.Context) map[int64]resource
250250
"status": tftypes.String,
251251
"timeout": tftypes.Number,
252252
"upgrade_install": tftypes.Bool,
253+
"take_ownership": tftypes.Bool,
253254
"values": tftypes.List{ElementType: tftypes.String},
254255
"verify": tftypes.Bool,
255256
"version": tftypes.String,
@@ -267,6 +268,7 @@ func (r *HelmRelease) buildUpgradeStateMap(_ context.Context) map[int64]resource
267268
newType.AttributeTypes["set_wo"],
268269
[]tftypes.Value{},
269270
),
271+
"take_ownership": tftypes.NewValue(tftypes.Bool, false),
270272
"set_wo_revision": tftypes.NewValue(tftypes.Number, float64(1)),
271273
"atomic": oldState["atomic"],
272274
"chart": oldState["chart"],

helm/resource_helm_release_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func TestAccResourceRelease_import(t *testing.T) {
170170

171171
// Default values
172172
resource.TestCheckResourceAttr("helm_release.imported", "verify", "false"),
173+
resource.TestCheckResourceAttr("helm_release.imported", "take_ownership", "false"),
173174
resource.TestCheckResourceAttr("helm_release.imported", "timeout", "300"),
174175
resource.TestCheckResourceAttr("helm_release.imported", "wait", "true"),
175176
resource.TestCheckResourceAttr("helm_release.imported", "wait_for_jobs", "true"),
@@ -1710,6 +1711,54 @@ func TestAccResourceRelease_LintFailValues(t *testing.T) {
17101711
})
17111712
}
17121713

1714+
func TestAccResourceRelease_takeOwnership(t *testing.T) {
1715+
name := randName("takeownership")
1716+
namespace := createRandomNamespace(t)
1717+
defer deleteNamespace(t, namespace)
1718+
1719+
resource.Test(t, resource.TestCase{
1720+
ProtoV6ProviderFactories: protoV6ProviderFactories(),
1721+
Steps: []resource.TestStep{
1722+
{
1723+
Config: testAccHelmReleaseConfigWithTakeOwnership(testResourceName, namespace, name, "1.2.3", true),
1724+
Check: resource.ComposeAggregateTestCheckFunc(
1725+
resource.TestCheckResourceAttr("helm_release.test", "take_ownership", "true"),
1726+
resource.TestCheckResourceAttr("helm_release.test", "metadata.name", name),
1727+
resource.TestCheckResourceAttr("helm_release.test", "metadata.namespace", namespace),
1728+
resource.TestCheckResourceAttr("helm_release.test", "metadata.version", "1.2.3"),
1729+
resource.TestCheckResourceAttr("helm_release.test", "status", release.StatusDeployed.String()),
1730+
),
1731+
},
1732+
{
1733+
Config: testAccHelmReleaseConfigWithTakeOwnership(testResourceName, namespace, name, "1.2.3", true),
1734+
Check: resource.ComposeAggregateTestCheckFunc(
1735+
resource.TestCheckResourceAttr("helm_release.test", "take_ownership", "true"),
1736+
),
1737+
},
1738+
},
1739+
})
1740+
}
1741+
func testAccHelmReleaseConfigWithTakeOwnership(resource, ns, name, version string, takeOwnership bool) string {
1742+
return fmt.Sprintf(`
1743+
resource "helm_release" "%s" {
1744+
name = %q
1745+
namespace = %q
1746+
chart = "test-chart"
1747+
repository = %q
1748+
version = %q
1749+
description = "Test"
1750+
take_ownership = %t
1751+
1752+
set = [
1753+
{
1754+
name = "foo"
1755+
value = "bar"
1756+
}
1757+
]
1758+
}
1759+
`, resource, name, ns, testRepositoryURL, version, takeOwnership)
1760+
}
1761+
17131762
func TestAccResourceRelease_LintFailChart(t *testing.T) {
17141763
namespace := createRandomNamespace(t)
17151764
defer deleteNamespace(t, namespace)

0 commit comments

Comments
 (0)