Skip to content

Commit

Permalink
Fixed wait_for fields. (#182)
Browse files Browse the repository at this point in the history
* Fixed wait_for fields.

* Fixed count of conditions, and added additional test.
  • Loading branch information
alekc authored Oct 19, 2024
1 parent 583c218 commit 5f8c474
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
28 changes: 23 additions & 5 deletions kubernetes/resource_kubectl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,9 @@ func waitForConditions(ctx context.Context, restClient *RestClientResult, waitFi
return fmt.Errorf("%s could not cast resource to unstructured", name)
}

totalConditions := len(waitConditions) + len(waitFields)
totalMatches := 0

yamlJson, err := rawResponse.MarshalJSON()
if err != nil {
return err
Expand All @@ -1231,18 +1234,22 @@ func waitForConditions(ctx context.Context, restClient *RestClientResult, waitFi

for _, c := range waitConditions {
// Find the conditions by status and type
v := gq.Reset().From("status.conditions").
count := gq.Reset().From("status.conditions").
Where("type", "=", c.Type).
Where("status", "=", c.Status)
if v == nil {
Where("status", "=", c.Status).Count()
if count == 0 {
log.Printf("[TRACE] Condition %s with status %s not found in %s", c.Type, c.Status, name)
continue
}
log.Printf("[TRACE] Condition %s with status %s found in %s", c.Type, c.Status, name)
totalMatches++
}

for _, c := range waitFields {
// Find the key
v := gq.Reset().Find(c.Key)
if v == nil {
log.Printf("[TRACE] Key %s not found in %s", c.Key, name)
continue
}

Expand All @@ -1256,17 +1263,28 @@ func waitForConditions(ctx context.Context, restClient *RestClientResult, waitFi
}

if !matched {
log.Printf("[TRACE] Value %s does not match regex %s in %s (key %s)", stringVal, c.Value, name, c.Key)
continue
}

log.Printf("[TRACE] Value %s matches regex %s in %s (key %s)", stringVal, c.Value, name, c.Key)
totalMatches++

case "eq", "":
if stringVal != c.Value {
log.Printf("[TRACE] Value %s does not match %s in %s (key %s)", stringVal, c.Value, name, c.Key)
continue
}
log.Printf("[TRACE] Value %s matches %s in %s (key %s)", stringVal, c.Value, name, c.Key)
totalMatches++
}
}

done = true
if totalMatches == totalConditions {
log.Printf("[TRACE] All conditions met for %s", name)
done = true
continue
}
log.Printf("[TRACE] %d/%d conditions met for %s. Waiting for next ", totalMatches, totalConditions, name)
}

case <-ctx.Done():
Expand Down
80 changes: 80 additions & 0 deletions kubernetes/resource_kubectl_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,86 @@ YAML
},
})
}
func TestAccKubectl_WaitForNegativeField(t *testing.T) {
//language=hcl
config := `
resource "kubectl_manifest" "test_wait_for" {
timeouts {
create = "10s"
}
yaml_body = <<EOF
apiVersion: v1
kind: Namespace
metadata:
name: test-wait-for
EOF
wait_for {
field {
key = "status.phase"
value = "Activez"
}
}
}` //start := time.Now()
// atm the actual error is being hidden by the wait context being deleted. Fix this at some point
//errorRegex, _ := regexp.Compile(".*failed to wait for resource*")
errorRegex, _ := regexp.Compile(".*Wait returned an error*")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckkubectlDestroy,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: errorRegex,
},
},
})
log.Println(config)
}

func TestAccKubectl_WaitForNegativeCondition(t *testing.T) {
//language=hcl
config := `
resource "kubectl_manifest" "test" {
timeouts {
create = "20s"
}
wait_for {
condition {
type = "ContainersReady"
status = "Never"
}
}
yaml_body = <<YAML
apiVersion: v1
kind: Pod
metadata:
name: busybox-sleep
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "30"]
YAML
}` //start := time.Now()
// atm the actual error is being hidden by the wait context being deleted. Fix this at some point
//errorRegex, _ := regexp.Compile(".*failed to wait for resource*")
errorRegex, _ := regexp.Compile(".*Wait returned an error*")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckkubectlDestroy,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: errorRegex,
},
},
})
log.Println(config)
}

func TestAccKubectl_WaitForNS(t *testing.T) {
//language=hcl
Expand Down

0 comments on commit 5f8c474

Please sign in to comment.