Skip to content

Commit 421cccf

Browse files
authored
Add timeout context log collector (#914)
1 parent be26462 commit 421cccf

File tree

1 file changed

+68
-46
lines changed

1 file changed

+68
-46
lines changed

pkg/collect/logs.go

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -46,66 +46,88 @@ func (c *CollectLogs) Collect(progressChan chan<- interface{}) (CollectorResult,
4646

4747
ctx := context.Background()
4848

49-
if c.SinceTime != nil {
50-
if c.Collector.Limits == nil {
51-
c.Collector.Limits = new(troubleshootv1beta2.LogLimits)
49+
const timeout = 60 //timeout in seconds used for context timeout value
50+
51+
// timeout context
52+
ctxTimeout, cancel := context.WithTimeout(context.Background(), timeout*time.Second)
53+
defer cancel()
54+
55+
errCh := make(chan error, 1)
56+
resultCh := make(chan CollectorResult, 1)
57+
58+
//wrapped code go func for context timeout solution
59+
go func() {
60+
61+
output := NewResult()
62+
63+
if c.SinceTime != nil {
64+
if c.Collector.Limits == nil {
65+
c.Collector.Limits = new(troubleshootv1beta2.LogLimits)
66+
}
67+
c.Collector.Limits.SinceTime = metav1.NewTime(*c.SinceTime)
5268
}
53-
c.Collector.Limits.SinceTime = metav1.NewTime(*c.SinceTime)
54-
}
5569

56-
pods, podsErrors := listPodsInSelectors(ctx, client, c.Collector.Namespace, c.Collector.Selector)
57-
if len(podsErrors) > 0 {
58-
output.SaveResult(c.BundlePath, getLogsErrorsFileName(c.Collector), marshalErrors(podsErrors))
59-
}
70+
pods, podsErrors := listPodsInSelectors(ctx, client, c.Collector.Namespace, c.Collector.Selector)
71+
if len(podsErrors) > 0 {
72+
output.SaveResult(c.BundlePath, getLogsErrorsFileName(c.Collector), marshalErrors(podsErrors))
73+
}
6074

61-
if len(pods) > 0 {
62-
for _, pod := range pods {
63-
if len(c.Collector.ContainerNames) == 0 {
64-
// make a list of all the containers in the pod, so that we can get logs from all of them
65-
containerNames := []string{}
66-
for _, container := range pod.Spec.Containers {
67-
containerNames = append(containerNames, container.Name)
68-
}
69-
for _, container := range pod.Spec.InitContainers {
70-
containerNames = append(containerNames, container.Name)
71-
}
75+
if len(pods) > 0 {
76+
for _, pod := range pods {
77+
if len(c.Collector.ContainerNames) == 0 {
78+
// make a list of all the containers in the pod, so that we can get logs from all of them
79+
containerNames := []string{}
80+
for _, container := range pod.Spec.Containers {
81+
containerNames = append(containerNames, container.Name)
82+
}
83+
for _, container := range pod.Spec.InitContainers {
84+
containerNames = append(containerNames, container.Name)
85+
}
7286

73-
for _, containerName := range containerNames {
74-
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true)
75-
if err != nil {
76-
key := fmt.Sprintf("%s/%s-errors.json", c.Collector.Name, pod.Name)
77-
if containerName != "" {
78-
key = fmt.Sprintf("%s/%s/%s-errors.json", c.Collector.Name, pod.Name, containerName)
79-
}
80-
err := output.SaveResult(c.BundlePath, key, marshalErrors([]string{err.Error()}))
87+
for _, containerName := range containerNames {
88+
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true)
8189
if err != nil {
82-
return nil, err
90+
key := fmt.Sprintf("%s/%s-errors.json", c.Collector.Name, pod.Name)
91+
if containerName != "" {
92+
key = fmt.Sprintf("%s/%s/%s-errors.json", c.Collector.Name, pod.Name, containerName)
93+
}
94+
err := output.SaveResult(c.BundlePath, key, marshalErrors([]string{err.Error()}))
95+
if err != nil {
96+
errCh <- err
97+
}
98+
continue
8399
}
84-
continue
85-
}
86-
for k, v := range podLogs {
87-
output[k] = v
100+
output.AddResult(podLogs)
101+
102+
resultCh <- output
88103
}
89-
}
90-
} else {
91-
for _, container := range c.Collector.ContainerNames {
92-
containerLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, container, c.Collector.Limits, false, true)
93-
if err != nil {
94-
key := fmt.Sprintf("%s/%s/%s-errors.json", c.Collector.Name, pod.Name, container)
95-
err := output.SaveResult(c.BundlePath, key, marshalErrors([]string{err.Error()}))
104+
} else {
105+
for _, container := range c.Collector.ContainerNames {
106+
containerLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, container, c.Collector.Limits, false, true)
96107
if err != nil {
97-
return nil, err
108+
key := fmt.Sprintf("%s/%s/%s-errors.json", c.Collector.Name, pod.Name, container)
109+
err := output.SaveResult(c.BundlePath, key, marshalErrors([]string{err.Error()}))
110+
if err != nil {
111+
errCh <- err
112+
}
113+
continue
98114
}
99-
continue
100-
}
101-
for k, v := range containerLogs {
102-
output[k] = v
115+
output.AddResult(containerLogs)
116+
resultCh <- output
103117
}
104118
}
105119
}
106120
}
121+
}()
122+
123+
select {
124+
case <-ctxTimeout.Done():
125+
return nil, fmt.Errorf("%s (%s) collector timeout exceeded", c.Title(), c.Collector.CollectorName)
126+
case o := <-resultCh:
127+
output = o
128+
case err := <-errCh:
129+
return nil, err
107130
}
108-
109131
return output, nil
110132
}
111133

0 commit comments

Comments
 (0)