Skip to content

Commit

Permalink
Fix: only recompute metadata when the version in the metadata changes (
Browse files Browse the repository at this point in the history
…#1458)

* Update resource_release.go

* Update resource_release_test.go

* Update resource_release.go

* Create 1458.txt

* Update helm/resource_release.go

* Update helm/resource_release.go
  • Loading branch information
chotiwat authored Sep 20, 2024
1 parent 2ff6a63 commit 62d34d2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/1458.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
`resource/helm_release`: Fix: only recompute metadata when the version in the metadata changes
```
10 changes: 4 additions & 6 deletions helm/resource_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,18 +930,16 @@ func resourceDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{})
}
if d.HasChanges(recomputeMetadataFields...) {
d.SetNewComputed("metadata")
}

if !useChartVersion(d.Get("chart").(string), d.Get("repository").(string)) {
if d.HasChange("version") {
} else if !useChartVersion(d.Get("chart").(string), d.Get("repository").(string)) {
if d.HasChange("metadata.0.version") {
// only recompute metadata if the version actually changes
// chart versioning is not consistent and some will add
// a `v` prefix to the chart version after installation
old, new := d.GetChange("version")
old, new := d.GetChange("metadata.0.version")
oldVersion := strings.TrimPrefix(old.(string), "v")
newVersion := strings.TrimPrefix(new.(string), "v")
debug("%s oldVersion: %s, newVersion: %s", logID, oldVersion, newVersion)
if oldVersion != newVersion && newVersion != "" {
if oldVersion != newVersion {
d.SetNewComputed("metadata")
}
}
Expand Down
42 changes: 42 additions & 0 deletions helm/resource_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,48 @@ func TestAccResourceRelease_emptyVersion(t *testing.T) {
})
}

// NOTE this is a regression test for: https://github.com/hashicorp/terraform-provider-helm/issues/1344
func TestAccResourceRelease_inexactVersion(t *testing.T) {
name := randName("basic")
namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: map[string]func() (*schema.Provider, error){
"helm": func() (*schema.Provider, error) {
return Provider(), nil
},
},
CheckDestroy: testAccCheckHelmReleaseDestroy(namespace),
Steps: []resource.TestStep{
{
Config: testAccHelmReleaseConfigBasic(testResourceName, namespace, name, "v1.2"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("helm_release.test", "version", "1.2.3"),
resource.TestCheckResourceAttr("helm_release.test", "metadata.0.version", "1.2.3"),
),
},
{
Config: testAccHelmReleaseConfigBasic(testResourceName, namespace, name, "1.2"),
PlanOnly: true,
},
{
Config: testAccHelmReleaseConfigBasic(testResourceName, namespace, name, "v1.2.x"),
PlanOnly: true,
},
{
Config: testAccHelmReleaseConfigBasic(testResourceName, namespace, name, "1.2.x"),
PlanOnly: true,
},
{
Config: testAccHelmReleaseConfigBasic(testResourceName, namespace, name, "~1.2.2"),
PlanOnly: true,
},
},
})
}

// "upgrade_install" without a previously installed release (effectively equivalent to TestAccResourceRelease_basic)
func TestAccResourceRelease_upgrade_with_install_coldstart(t *testing.T) {
name := randName("basic")
Expand Down

0 comments on commit 62d34d2

Please sign in to comment.