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 a wait_for condition #180

Merged
merged 1 commit into from
Oct 16, 2024
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
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
Loading