Skip to content

Commit 7db1b34

Browse files
authored
kubernetes_manifest: fix waiter crash when conditions are not present (#2008)
1 parent ebe88d9 commit 7db1b34

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

.changelog/2008.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
kubernetes_manifest: fix crash when waiting on conditions that are not yet present
3+
```

manifest/provider/waiter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (w *ConditionsWaiter) Wait(ctx context.Context) error {
343343
}
344344

345345
if status, ok := res.Object["status"].(map[string]interface{}); ok {
346-
if conditions := status["conditions"].([]interface{}); ok && len(conditions) > 0 {
346+
if conditions, ok := status["conditions"].([]interface{}); ok && len(conditions) > 0 {
347347
conditionsMet := true
348348
for _, c := range w.conditions {
349349
var condition map[string]tftypes.Value
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "kubernetes_manifest" "test" {
2+
manifest = {
3+
apiVersion = "v1"
4+
kind = "Namespace"
5+
6+
metadata = {
7+
name = var.name
8+
}
9+
}
10+
11+
wait {
12+
condition {
13+
type = "Ready"
14+
status = "True"
15+
}
16+
}
17+
18+
timeouts {
19+
create = "3s"
20+
}
21+
}

manifest/test/acceptance/wait_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package acceptance
55

66
import (
77
"context"
8+
"strings"
89
"testing"
910
"time"
1011

@@ -185,3 +186,37 @@ func TestKubernetesManifest_WaitCondition_Pod(t *testing.T) {
185186
"kubernetes_manifest.test.wait.0.condition.1.status": "True",
186187
})
187188
}
189+
190+
func TestKubernetesManifest_Wait_InvalidCondition(t *testing.T) {
191+
// NOTE: this tests that specifying a condition for a resource that
192+
// will never have one does not crash the provider
193+
194+
ctx := context.Background()
195+
196+
name := randName()
197+
198+
reattachInfo, err := provider.ServeTest(ctx, hclog.Default(), t)
199+
if err != nil {
200+
t.Errorf("Failed to create provider instance: %q", err)
201+
}
202+
203+
tf := tfhelper.RequireNewWorkingDir(ctx, t)
204+
tf.SetReattachInfo(ctx, reattachInfo)
205+
defer func() {
206+
tf.Destroy(ctx)
207+
tf.Close()
208+
k8shelper.AssertResourceDoesNotExist(t, "v1", "namespaces", name)
209+
}()
210+
211+
tfvars := TFVARS{
212+
"name": name,
213+
}
214+
tfconfig := loadTerraformConfig(t, "Wait/wait_for_condition_invalid.tf", tfvars)
215+
tf.SetConfig(ctx, tfconfig)
216+
tf.Init(ctx)
217+
218+
err = tf.Apply(ctx)
219+
if err == nil || !strings.Contains(err.Error(), "Terraform timed out waiting on the operation to complete") {
220+
t.Fatalf("Waiter should have timed out")
221+
}
222+
}

0 commit comments

Comments
 (0)