Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StateMigrator for pass_credentials and wait_for_jobs #982

Merged
merged 6 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/982.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Crash: Add StateMigrator for pass_credentials and wait_for_jobs (Inconsistent Plan)
```
37 changes: 37 additions & 0 deletions helm/resource_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,43 @@ func resourceRelease() *schema.Resource {
},
},
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceReleaseUpgrader().CoreConfigSchema().ImpliedType(),
Upgrade: resourceReleaseStateUpgradeV0,
Version: 0,
},
},
}
}

func resourceReleaseStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
if rawState["pass_credentials"] == nil {
rawState["pass_credentials"] = false
}
if rawState["wait_for_jobs"] == nil {
rawState["wait_for_jobs"] = false
}
return rawState, nil
}

func resourceReleaseUpgrader() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"pass_credentials": {
Type: schema.TypeBool,
Optional: true,
Description: "Pass credentials to all domains",
Default: defaultAttributes["pass_credentials"],
},
"wait_for_jobs": {
Type: schema.TypeBool,
Optional: true,
Default: defaultAttributes["wait_for_jobs"],
Description: "If wait is enabled, will wait until all Jobs have been completed before marking the release as successful.",
},
},
}
}

Expand Down
30 changes: 29 additions & 1 deletion helm/resource_release_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package helm

import (
"context"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -100,6 +102,7 @@ func TestAccResourceRelease_import(t *testing.T) {
resource.TestCheckResourceAttr("helm_release.imported", "timeout", "300"),
resource.TestCheckResourceAttr("helm_release.imported", "wait", "true"),
resource.TestCheckResourceAttr("helm_release.imported", "wait_for_jobs", "true"),
resource.TestCheckResourceAttr("helm_release.imported", "pass_credentials", "false"),
resource.TestCheckResourceAttr("helm_release.imported", "disable_webhooks", "false"),
resource.TestCheckResourceAttr("helm_release.imported", "atomic", "false"),
resource.TestCheckResourceAttr("helm_release.imported", "render_subchart_notes", "true"),
Expand Down Expand Up @@ -1328,7 +1331,7 @@ func TestAccResourceRelease_manifestUnknownValues(t *testing.T) {
CheckDestroy: testAccCheckHelmReleaseDestroy(namespace),
Steps: []resource.TestStep{
// NOTE this is a regression test to apply a configuration which supplies
// unknown values to the release at plan time, we simply want to test here
// unknown values to the release at plan time, we simply expected to test here
// that applying the config doesn't produce an inconsistent final plan error
{
Config: testAccHelmReleaseConfigManifestUnknownValues(testResourceName, namespace, name, "1.2.3"),
Expand Down Expand Up @@ -1768,3 +1771,28 @@ func removeSubcharts(chartName string) error {
}
return os.RemoveAll(chartsPath)
}

func TestResourceExampleInstanceStateUpgradeV0(t *testing.T) {
expected := map[string]any{
"wait_for_jobs": false,
"pass_credentials": false,
}
states := []map[string]any{
{
"wait_for_jobs": nil,
"pass_credentials": nil,
},
{},
}

for _, state := range states {
actual, err := resourceReleaseStateUpgradeV0(context.Background(), state, nil)
if err != nil {
t.Fatalf("error migrating state: %s", err)
}

if !reflect.DeepEqual(expected, actual) {
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
}
}
}
Binary file added helm/test-chart-1.2.3.tgz
Binary file not shown.