Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 17d1457

Browse files
author
Michael Sauter
authored
Merge pull request #266 from opendevstack/refactor/logs-streaming
Simplify logs streaming code
2 parents f4076f7 + 0793440 commit 17d1457

File tree

1 file changed

+23
-52
lines changed

1 file changed

+23
-52
lines changed

pkg/tasktesting/logs.go

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package tasktesting
22

33
import (
4+
"bufio"
45
"context"
56
"fmt"
6-
"io"
7-
"io/ioutil"
87
"log"
9-
"time"
108

119
corev1 "k8s.io/api/core/v1"
1210
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -64,59 +62,32 @@ func streamContainerLogs(
6462
return fmt.Errorf("error watching pods: %s", err)
6563
}
6664

67-
containerState := "waiting"
68-
var logStream io.ReadCloser
6965
for {
70-
select {
71-
case ev := <-w.ResultChan():
72-
if cs, ok := containerFromEvent(ev, podName, containerName); ok {
73-
if cs.State.Running != nil {
74-
if containerState == "waiting" {
75-
log.Printf("---------------------- Logs from %s -------------------------\n", containerName)
76-
req := c.CoreV1().Pods(podNamespace).GetLogs(podName, &corev1.PodLogOptions{
77-
Follow: true,
78-
Container: containerName,
79-
})
80-
ls, err := req.Stream(context.Background())
81-
if err != nil {
82-
return fmt.Errorf("could not create log stream for pod %s in namespace %s: %w", podName, podNamespace, err)
83-
}
84-
logStream = ls
85-
defer logStream.Close()
86-
}
87-
containerState = "running"
66+
ev := <-w.ResultChan()
67+
if cs, ok := containerFromEvent(ev, podName, containerName); ok {
68+
if cs.State.Running != nil {
69+
log.Printf("---------------------- Logs from %s -------------------------\n", containerName)
70+
// Set up log stream using a new ctx so that it's not cancelled
71+
// when the task is done before all logs have been read.
72+
ls, err := c.CoreV1().Pods(podNamespace).GetLogs(podName, &corev1.PodLogOptions{
73+
Follow: true,
74+
Container: containerName,
75+
}).Stream(context.Background())
76+
if err != nil {
77+
return fmt.Errorf("could not create log stream for pod %s in namespace %s: %w", podName, podNamespace, err)
8878
}
89-
if containerState != "waiting" && cs.State.Terminated != nil {
90-
// read reminder of the log stream
91-
logs, err := ioutil.ReadAll(logStream)
92-
if err != nil {
93-
return fmt.Errorf("could not read log stream for pod %s in namespace %s: %w", podName, podNamespace, err)
79+
defer ls.Close()
80+
reader := bufio.NewScanner(ls)
81+
for reader.Scan() {
82+
select {
83+
case <-ctx.Done():
84+
fmt.Println(reader.Text())
85+
return nil
86+
default:
87+
fmt.Println(reader.Text())
9488
}
95-
fmt.Println(string(logs))
96-
return nil
97-
}
98-
}
99-
100-
default:
101-
// if log stream has started, read some bytes
102-
if logStream != nil {
103-
buf := make([]byte, 100)
104-
105-
numBytes, err := logStream.Read(buf)
106-
if numBytes == 0 {
107-
continue
108-
}
109-
if err == io.EOF {
110-
log.Printf("logs for %s ended\n", containerName)
111-
return nil
11289
}
113-
if err != nil {
114-
return fmt.Errorf("error in reading log stream: %w", err)
115-
}
116-
117-
fmt.Print(string(buf[:numBytes]))
118-
} else {
119-
time.Sleep(time.Second)
90+
return reader.Err()
12091
}
12192
}
12293
}

0 commit comments

Comments
 (0)