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

Fix Unknown Value in Manifest Diff #966

Merged
merged 3 commits into from
Oct 11, 2022
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/966.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Crash Fix: Fix Unknown Value in Manifest Diff
```
29 changes: 28 additions & 1 deletion helm/resource_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,17 @@ func resourceDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{})
debug("%s Release validated", logID)

if m.ExperimentEnabled("manifest") {
// NOTE we need to check that the values supplied to the release are
// fully known at plan time otherwise we can't supply them to the
// action to perform a dry run
if !valuesKnown(d) {
// NOTE it would be nice to surface a warning diagnostic here
// but this is not possible with the SDK
debug("not all values are known, skipping dry run to render manifest")
d.SetNewComputed("manifest")
return d.SetNewComputed("version")
}

var postRenderer postrender.PostRenderer
if cmd := d.Get("postrender.0.binary_path").(string); cmd != "" {
av := d.Get("postrender.0.args")
Expand Down Expand Up @@ -846,7 +857,7 @@ func resourceDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{})
install.CreateNamespace = d.Get("create_namespace").(bool)
install.PostRenderer = postRenderer

values, _ := getValues(d)
values, err := getValues(d)
if err != nil {
return fmt.Errorf("error getting values: %v", err)
}
Expand Down Expand Up @@ -1369,3 +1380,19 @@ func resultToError(r *action.LintResult) error {

return fmt.Errorf("malformed chart or values: \n\t%s", strings.Join(messages, "\n\t"))
}

// valuesKnown returns true if all of the values supplied to the release are known at plan time
func valuesKnown(d *schema.ResourceDiff) bool {
rawPlan := d.GetRawPlan()
checkAttributes := []string{
"values",
"set",
"set_sensitive",
}
for _, attr := range checkAttributes {
if !rawPlan.GetAttr(attr).IsWhollyKnown() {
return false
}
}
return true
}
61 changes: 61 additions & 0 deletions helm/resource_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,33 @@ func TestAccResourceRelease_manifest(t *testing.T) {
})
}

func TestAccResourceRelease_manifestUnknownValues(t *testing.T) {
name := "example"
namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
ExternalProviders: map[string]resource.ExternalProvider{
"random": {
Source: "hashicorp/random",
},
},
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
// that applying the config doesn't produce an inconsistent final plan error
{
Config: testAccHelmReleaseConfigManifestUnknownValues(testResourceName, namespace, name, "1.2.3"),
},
},
})
}

func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {
dockerPath, err := exec.LookPath("docker")
if err != nil {
Expand Down Expand Up @@ -1502,6 +1529,40 @@ func testAccHelmReleaseConfigManifestExperimentEnabled(resource, ns, name, versi
`, resource, name, ns, testRepositoryURL, version)
}

func testAccHelmReleaseConfigManifestUnknownValues(resource, ns, name, version string) string {
return fmt.Sprintf(`
provider helm {
experiments {
manifest = true
}
}
resource "random_string" "random_label" {
length = 16
special = false
}
resource "helm_release" "%s" {
name = %q
namespace = %q
repository = %q
version = %q
chart = "test-chart"
set {
name = "podAnnotations.random"
value = random_string.random_label.result
}
set_sensitive {
name = "podAnnotations.sensitive"
value = random_string.random_label.result
}
values = [<<EOT
podAnnotations:
test: ${random_string.random_label.result}
EOT
]
}
`, resource, name, ns, testRepositoryURL, version)
}

func testAccHelmReleaseConfigDependencyUpdateWithLint(resource, ns, name string, dependencyUpdate bool) string {
return fmt.Sprintf(`
resource "helm_release" "%s" {
Expand Down