Add timeout context log collector
@@ -46,6 +46,20 @@ func (c *CollectLogs) Collect(progressChan chan<- interface{}) (CollectorResult,
ctx := context.Background()
+ const timeout = 60 //timeout in seconds used for context timeout value
+
+ // timeout context
+ ctxTimeout, cancel := context.WithTimeout(context.Background(), timeout*time.Second)
+ defer cancel()
+
+ errCh := make(chan error, 1)
+ resultCh := make(chan CollectorResult, 1)
+
+ //wrapped code go func for context timeout solution
+ go func() {
+
+ output := NewResult()
+
if c.SinceTime != nil {
if c.Collector.Limits == nil {
c.Collector.Limits = new(troubleshootv1beta2.LogLimits)
@@ -79,13 +93,13 @@ func (c *CollectLogs) Collect(progressChan chan<- interface{}) (CollectorResult,
}
err := output.SaveResult(c.BundlePath, key, marshalErrors([]string{err.Error()}))
if err != nil {
- return nil, err
+ errCh <- err
}
continue
}
- for k, v := range podLogs {
- output[k] = v
- }
+ output.AddResult(podLogs)
+
+ resultCh <- output
}
} else {
for _, container := range c.Collector.ContainerNames {
@@ -94,18 +108,26 @@ func (c *CollectLogs) Collect(progressChan chan<- interface{}) (CollectorResult,
key := fmt.Sprintf("%s/%s/%s-errors.json", c.Collector.Name, pod.Name, container)
err := output.SaveResult(c.BundlePath, key, marshalErrors([]string{err.Error()}))
if err != nil {
- return nil, err
+ errCh <- err
}
continue
}
- for k, v := range containerLogs {
- output[k] = v
- }
+ output.AddResult(containerLogs)
+ resultCh <- output
}
}
}
}
+ }()
+ select {
+ case <-ctxTimeout.Done():
+ return nil, fmt.Errorf("%s (%s) collector timeout exceeded", c.Title(), c.Collector.CollectorName)
+ case o := <-resultCh:
+ output = o
+ case err := <-errCh:
+ return nil, err
+ }
return output, nil
}
