-
Notifications
You must be signed in to change notification settings - Fork 217
/
logs.go
60 lines (55 loc) · 1.58 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package istio
import (
"bufio"
"context"
"fmt"
"io"
"strings"
"github.com/spaceuptech/helpers"
v1 "k8s.io/api/core/v1"
"github.com/spaceuptech/space-cloud/runner/model"
"github.com/spaceuptech/space-cloud/runner/utils"
)
// GetLogs get logs of specified services
func (i *Istio) GetLogs(ctx context.Context, projectID string, info *model.LogRequest) (io.ReadCloser, error) {
if info.TaskID == "" {
arr := strings.Split(info.ReplicaID, "-")
if len(arr) < 2 {
return nil, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Invalid replica id", nil, nil)
}
info.TaskID = arr[0]
}
// get logs of pods
req := i.kube.CoreV1().Pods(projectID).GetLogs(info.ReplicaID, &v1.PodLogOptions{
Container: info.TaskID,
Follow: info.IsFollow,
SinceSeconds: info.Since,
SinceTime: info.SinceTime,
TailLines: info.Tail,
})
b, err := req.Stream(ctx)
if err != nil {
return nil, err
}
pipeReader, pipeWriter := io.Pipe()
helpers.Logger.LogDebug(helpers.GetRequestID(ctx), "Sending logs to client", map[string]interface{}{})
go func() {
defer utils.CloseTheCloser(b)
defer utils.CloseTheCloser(pipeWriter)
// read logs
rd := bufio.NewReader(b)
for {
str, err := rd.ReadString('\n')
if err != nil {
if err == io.EOF && !info.IsFollow {
helpers.Logger.LogDebug(helpers.GetRequestID(ctx), "End of file reached for logs", map[string]interface{}{})
return
}
_ = helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to read logs from container", err, nil)
return
}
fmt.Fprint(pipeWriter, str)
}
}()
return pipeReader, nil
}