Skip to content

Commit db8533e

Browse files
committed
Include previous logs from pods if available
1 parent f37bbdb commit db8533e

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

pkg/collect/logs.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ func getPodLogs(client *kubernetes.Clientset, pod corev1.Pod, name, container st
125125
}
126126
}
127127

128+
fileKey := fmt.Sprintf("%s/%s", name, pod.Name)
129+
if container != "" {
130+
fileKey = fmt.Sprintf("%s/%s/%s", name, pod.Name, container)
131+
}
132+
133+
result := make(map[string][]byte)
134+
128135
req := client.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
129136
podLogs, err := req.Stream()
130137
if err != nil {
@@ -137,15 +144,25 @@ func getPodLogs(client *kubernetes.Clientset, pod corev1.Pod, name, container st
137144
if err != nil {
138145
return nil, errors.Wrap(err, "failed to copy log")
139146
}
147+
result[fileKey+".log"] = buf.Bytes()
140148

141-
fileKey := fmt.Sprintf("%s/%s.txt", name, pod.Name)
142-
if container != "" {
143-
fileKey = fmt.Sprintf("%s/%s/%s.txt", name, pod.Name, container)
149+
podLogOpts.Previous = true
150+
req = client.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
151+
podLogs, err = req.Stream()
152+
if err != nil {
153+
// maybe fail on !kuberneteserrors.IsNotFound(err)?
154+
return result, nil
155+
}
156+
defer podLogs.Close()
157+
158+
buf = new(bytes.Buffer)
159+
_, err = io.Copy(buf, podLogs)
160+
if err != nil {
161+
return nil, errors.Wrap(err, "failed to copy previous log")
144162
}
163+
result[fileKey+"-previous.log"] = buf.Bytes()
145164

146-
return map[string][]byte{
147-
fileKey: buf.Bytes(),
148-
}, nil
165+
return result, nil
149166
}
150167

151168
func (l LogsOutput) Redact() (LogsOutput, error) {

0 commit comments

Comments
 (0)