Skip to content

Commit

Permalink
Fix a wait_for condition where the status is already present in the…
Browse files Browse the repository at this point in the history
… first observation
  • Loading branch information
alekc committed Oct 16, 2024
1 parent 54d0ee9 commit 583c218
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
17 changes: 13 additions & 4 deletions kubernetes/resource_kubectl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,18 +1198,25 @@ func waitForApiService(ctx context.Context, provider *KubeProvider, name string,
func waitForConditions(ctx context.Context, restClient *RestClientResult, waitFields []types.WaitForField, waitConditions []types.WaitForStatusCondition, name string, resourceVersion string, timeout time.Duration) error {
timeoutSeconds := int64(timeout.Seconds())

watcher, err := restClient.ResourceInterface.Watch(ctx, meta_v1.ListOptions{Watch: true, TimeoutSeconds: &timeoutSeconds, FieldSelector: fields.OneTermEqualSelector("metadata.name", name).String(), ResourceVersion: resourceVersion})
watcher, err := restClient.ResourceInterface.Watch(
ctx,
meta_v1.ListOptions{
Watch: true,
TimeoutSeconds: &timeoutSeconds,
FieldSelector: fields.OneTermEqualSelector("metadata.name", name).String(),
},
)
if err != nil {
return err
}

defer watcher.Stop()

done := false
for !done {
select {
case event := <-watcher.ResultChan():
if event.Type == watch.Modified {
log.Printf("[TRACE] Received event type %s for %s", event.Type, name)
if event.Type == watch.Modified || event.Type == watch.Added {
rawResponse, ok := event.Object.(*meta_v1_unstruct.Unstructured)
if !ok {
return fmt.Errorf("%s could not cast resource to unstructured", name)
Expand All @@ -1224,7 +1231,9 @@ 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").Where("type", "=", c.Type).Where("status", "=", c.Status)
v := gq.Reset().From("status.conditions").
Where("type", "=", c.Type).
Where("status", "=", c.Status)
if v == nil {
continue
}
Expand Down
34 changes: 34 additions & 0 deletions kubernetes/resource_kubectl_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,40 @@ YAML
})
}

func TestAccKubectl_WaitForNS(t *testing.T) {
//language=hcl
config := `
resource "kubectl_manifest" "test_wait_for" {
timeouts {
create = "200s"
}
yaml_body = <<EOF
apiVersion: v1
kind: Namespace
metadata:
name: test-wait-for
EOF
wait_for {
field {
key = "status.phase"
value = "Active"
}
}
}` //start := time.Now()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckkubectlDestroy,
Steps: []resource.TestStep{
{
Config: config,
//todo: improve checking
},
},
})
log.Println(config)
}
func TestAccKubectl_WaitForField(t *testing.T) {
//language=hcl
config := `
Expand Down

0 comments on commit 583c218

Please sign in to comment.