From d8322f02744d3f57d1571bc4ff5e9096391efdc4 Mon Sep 17 00:00:00 2001 From: John Houston Date: Thu, 16 Feb 2023 22:58:26 -0500 Subject: [PATCH 1/3] Fix waiter crash when conditions are not present --- manifest/provider/waiter.go | 2 +- manifest/test/acceptance/wait_test.go | 35 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/manifest/provider/waiter.go b/manifest/provider/waiter.go index 626aa98ceb..cadedc3cd4 100644 --- a/manifest/provider/waiter.go +++ b/manifest/provider/waiter.go @@ -343,7 +343,7 @@ func (w *ConditionsWaiter) Wait(ctx context.Context) error { } if status, ok := res.Object["status"].(map[string]interface{}); ok { - if conditions := status["conditions"].([]interface{}); ok && len(conditions) > 0 { + if conditions, ok := status["conditions"].([]interface{}); ok && len(conditions) > 0 { conditionsMet := true for _, c := range w.conditions { var condition map[string]tftypes.Value diff --git a/manifest/test/acceptance/wait_test.go b/manifest/test/acceptance/wait_test.go index 3de035fd56..883a99be35 100644 --- a/manifest/test/acceptance/wait_test.go +++ b/manifest/test/acceptance/wait_test.go @@ -5,6 +5,7 @@ package acceptance import ( "context" + "strings" "testing" "time" @@ -185,3 +186,37 @@ func TestKubernetesManifest_WaitCondition_Pod(t *testing.T) { "kubernetes_manifest.test.wait.0.condition.1.status": "True", }) } + +func TestKubernetesManifest_Wait_InvalidCondition(t *testing.T) { + // NOTE: this tests that specifying a condition for a resource that + // will never have one does not crash the provider + + ctx := context.Background() + + name := randName() + + reattachInfo, err := provider.ServeTest(ctx, hclog.Default(), t) + if err != nil { + t.Errorf("Failed to create provider instance: %q", err) + } + + tf := tfhelper.RequireNewWorkingDir(ctx, t) + tf.SetReattachInfo(ctx, reattachInfo) + defer func() { + tf.Destroy(ctx) + tf.Close() + k8shelper.AssertResourceDoesNotExist(t, "v1", "namespaces", name) + }() + + tfvars := TFVARS{ + "name": name, + } + tfconfig := loadTerraformConfig(t, "Wait/wait_for_condition_invalid.tf", tfvars) + tf.SetConfig(ctx, tfconfig) + tf.Init(ctx) + + err = tf.Apply(ctx) + if err == nil || !strings.Contains(err.Error(), "Terraform timed out waiting on the operation to complete") { + t.Fatalf("Waiter should have timed out") + } +} From a42832a344af90a805a991ab82fc80058d374c4d Mon Sep 17 00:00:00 2001 From: John Houston Date: Fri, 17 Feb 2023 13:51:06 -0500 Subject: [PATCH 2/3] Add missing test fixture --- .../Wait/wait_for_condition_invalid.tf | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 manifest/test/acceptance/testdata/Wait/wait_for_condition_invalid.tf diff --git a/manifest/test/acceptance/testdata/Wait/wait_for_condition_invalid.tf b/manifest/test/acceptance/testdata/Wait/wait_for_condition_invalid.tf new file mode 100644 index 0000000000..a309e99f61 --- /dev/null +++ b/manifest/test/acceptance/testdata/Wait/wait_for_condition_invalid.tf @@ -0,0 +1,21 @@ +resource "kubernetes_manifest" "test" { + manifest = { + apiVersion = "v1" + kind = "Namespace" + + metadata = { + name = var.name + } + } + + wait { + condition { + type = "Ready" + status = "True" + } + } + + timeouts { + create = "3s" + } +} From 93a68af35fb1bdd060018814e9ec369311c25e6b Mon Sep 17 00:00:00 2001 From: John Houston Date: Fri, 17 Feb 2023 13:51:58 -0500 Subject: [PATCH 3/3] Add changelog entry --- .changelog/2008.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2008.txt diff --git a/.changelog/2008.txt b/.changelog/2008.txt new file mode 100644 index 0000000000..10743df417 --- /dev/null +++ b/.changelog/2008.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +kubernetes_manifest: fix crash when waiting on conditions that are not yet present +```