Skip to content

Commit

Permalink
use polling instead of waiting and fix save empty data
Browse files Browse the repository at this point in the history
  • Loading branch information
DexterYan committed Nov 4, 2024
1 parent 14b8691 commit 77939a2
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions pkg/supportbundle/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,21 @@ func getExecOutputs(
return stdout.Bytes(), stderr.Bytes(), err
}

return stdout.Bytes(), stderr.Bytes(), nil
// Poll until stdout is non-empty or the context times out
ticker := time.NewTicker(100 * time.Millisecond) // Adjust polling frequency as needed
defer ticker.Stop()

for {
select {
case <-ticker.C:
if stdout.Len() > 0 {
return stdout.Bytes(), stderr.Bytes(), nil
}
case <-ctx.Done():
// Return whatever we have if context is canceled
return stdout.Bytes(), stderr.Bytes(), ctx.Err()
}
}
}

func runRemoteHostCollectors(ctx context.Context, hostCollectors []*troubleshootv1beta2.HostCollect, bundlePath string, opts SupportBundleCreateOpts) (map[string][]byte, error) {
Expand Down Expand Up @@ -357,10 +371,10 @@ func runRemoteHostCollectors(ctx context.Context, hostCollectors []*troubleshoot
for _, pod := range pods.Items {
eg.Go(func() error {
// TODO: set timeout waiting
err := waitForPodRunning(ctx, clientset, &pod)
if err != nil {
if err := waitForPodRunning(ctx, clientset, &pod); err != nil {
return err
}

results := map[string][]byte{}
for _, collectorSpec := range hostCollectors {
// convert host collectors into a HostCollector spec
Expand All @@ -375,17 +389,17 @@ func runRemoteHostCollectors(ctx context.Context, hostCollectors []*troubleshoot
if err != nil {
return err
}
result := map[string][]byte{}
json.Unmarshal(stdout, &result)

result := map[string]string{}
if err := json.Unmarshal(stdout, &result); err != nil {
return err
}

for file, data := range result {
results[file] = data
results[file] = []byte(data)
}
time.Sleep(1 * time.Second)
}

// wait for log stream to catch up
time.Sleep(1 * time.Second)

mu.Lock()
nodeLogs[pod.Spec.NodeName] = results
mu.Unlock()
Expand Down

0 comments on commit 77939a2

Please sign in to comment.