Skip to content

Commit

Permalink
chore: dump previous logs as well
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Nov 7, 2024
1 parent 7e8abfa commit 1c1f41e
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions internal/integration/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,27 +542,40 @@ func dumpKubePods(ctx context.Context, kubeClient optional.Option[kubernetes.Cli
continue
}
for _, container := range pod.Spec.Containers {
path := filepath.Join(dumpPath, pod.Name, container.Name+".log")
req := client.CoreV1().Pods(kubeNamespace).GetLogs(pod.Name, &kubecore.PodLogOptions{Container: container.Name})
podLogs, err := req.Stream(context.Background())
if err != nil {
Infof("Error getting logs for pod %s: %v", pod.Name, err)
continue
}
defer func() {
_ = podLogs.Close()
}()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
Infof("Error copying logs for pod %s: %v", pod.Name, err)
continue
}
str := buf.String()
err = os.WriteFile(path, []byte(str), 0644) // #nosec
if err != nil {
Infof("Error writing logs for pod %s: %v", pod.Name, err)
for _, prev := range []bool{false, true} {
var path string
if prev {
path = filepath.Join(dumpPath, pod.Name, container.Name+"-previous.log")
} else {
path = filepath.Join(dumpPath, pod.Name, container.Name+".log")
}
req := client.CoreV1().Pods(kubeNamespace).GetLogs(pod.Name, &kubecore.PodLogOptions{Container: container.Name, Previous: prev})
podLogs, err := req.Stream(context.Background())
if err != nil {
if prev {
// This is pretty normal not to have previous logs
continue
}
Infof("Error getting logs for pod %s: %v previous: %v", pod.Name, err, prev)
continue
}
defer func() {
_ = podLogs.Close()
}()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
Infof("Error copying logs for pod %s: %v", pod.Name, err)
continue
}
str := buf.String()
err = os.WriteFile(path, []byte(str), 0644) // #nosec
if err != nil {
Infof("Error writing logs for pod %s: %v", pod.Name, err)
}

}

}
}
}
Expand Down

0 comments on commit 1c1f41e

Please sign in to comment.