-
Notifications
You must be signed in to change notification settings - Fork 9
/
logs.go
150 lines (121 loc) · 3.68 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubernetes
import (
"bytes"
"context"
"errors"
"io"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
)
func (r *Cluster) FetchLogsTo(ctx context.Context, reference *runtimepb.ContainerReference, opts runtime.FetchLogsOpts, callback func(runtime.ContainerLogLine)) error {
cpr := &kubedef.ContainerPodReference{}
if err := reference.Opaque.UnmarshalTo(cpr); err != nil {
return fnerrors.InternalError("invalid reference: %w", err)
}
return fetchPodLogs(ctx, r.cli, cpr.Namespace, cpr.PodName, cpr.Container, opts, callback)
}
func fetchPodLogs(ctx context.Context, cli *kubernetes.Clientset, namespace, podName, containerName string, opts runtime.FetchLogsOpts, callback func(runtime.ContainerLogLine)) error {
if opts.FetchLastFailure && opts.Follow {
return fnerrors.InternalError("can't follow logs of previous failure")
}
logOpts := &corev1.PodLogOptions{
Follow: opts.Follow,
Container: containerName,
Timestamps: true,
}
if opts.FetchLastFailure {
// Consider adding the restart policy to ContainerPodReference to avoid this call.
pod, err := cli.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return err
}
if pod.Spec.RestartPolicy != corev1.RestartPolicyNever {
logOpts.Previous = true
}
}
if opts.TailLines > 0 {
var tailLines int64 = int64(opts.TailLines)
logOpts.TailLines = &tailLines
}
var buf bytes.Buffer
chunk := make([]byte, 4096)
for {
logsReq := cli.CoreV1().Pods(namespace).GetLogs(podName, logOpts)
content, err := logsReq.Stream(ctx)
if err != nil {
return err
}
callback(runtime.ContainerLogLine{
Timestamp: time.Now(),
Event: runtime.ContainerLogLineEvent_Connected,
})
defer content.Close()
for {
n, err := content.Read(chunk)
if n > 0 {
buf.Write(chunk[:n])
for {
if i := bytes.IndexByte(buf.Bytes(), '\n'); i >= 0 {
lineb := makeOrReuse(chunk, i+1) // Read the \n as well.
_, _ = buf.Read(lineb)
line := dropCR(lineb[0:i]) // Drop the \n and the \r.
ev := runtime.ContainerLogLine{
Event: runtime.ContainerLogLineEvent_LogLine,
}
// Look for the timestamp.
if k := bytes.IndexByte(line, ' '); k > 0 {
if ts, err := time.Parse(time.RFC3339Nano, string(line[:k])); err == nil {
ev.LogLine = line[k+1:]
ev.Timestamp = ts
}
}
if ev.LogLine == nil {
ev.LogLine = line
ev.MissingTimestamp = true
}
callback(ev)
} else {
break
}
}
}
if err == io.EOF || errors.Is(err, context.Canceled) {
return nil
} else if err != nil {
if !logOpts.Follow {
return fnerrors.InternalError("log streaming failed: %w", err)
}
// Got an unexpected error, lets try to resume.
callback(runtime.ContainerLogLine{
Timestamp: time.Now(),
Event: runtime.ContainerLogLineEvent_Resuming,
ResumeErr: err,
})
break
}
}
// When resuming, don't re-tail.
logOpts.TailLines = nil
}
}
func makeOrReuse(buf []byte, n int) []byte {
if len(buf) >= n {
return buf[:n]
}
return make([]byte, n)
}
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
}