-
Notifications
You must be signed in to change notification settings - Fork 93
/
monitor.go
209 lines (164 loc) · 3.78 KB
/
monitor.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package kail
import (
"bytes"
"context"
"fmt"
"io"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
lifecycle "github.com/boz/go-lifecycle"
logutil "github.com/boz/go-logutil"
)
const (
logBufsiz = 1024 * 16 // 16k max message size
monitorDeliverWait = time.Millisecond
)
var (
canaryLog = []byte("unexpected stream type \"\"")
)
type monitorConfig struct {
since time.Duration
}
type monitor interface {
Shutdown()
Done() <-chan struct{}
}
func newMonitor(c *controller, source EventSource, config monitorConfig) monitor {
lc := lifecycle.New()
go lc.WatchContext(c.ctx)
log := c.log.WithComponent(
fmt.Sprintf("monitor [%v]", source))
m := &_monitor{
rc: c.rc,
source: source,
config: config,
eventch: c.eventch,
log: log,
lc: lc,
ctx: c.ctx,
}
go m.run()
return m
}
type _monitor struct {
rc *rest.Config
source EventSource
config monitorConfig
eventch chan<- Event
log logutil.Log
lc lifecycle.Lifecycle
ctx context.Context
}
func (m *_monitor) Shutdown() {
m.lc.ShutdownAsync(nil)
}
func (m *_monitor) Done() <-chan struct{} {
return m.lc.Done()
}
func (m *_monitor) run() {
defer m.log.Un(m.log.Trace("run"))
defer m.lc.ShutdownCompleted()
ctx, cancel := context.WithCancel(m.ctx)
client, err := m.makeClient(ctx)
if err != nil {
m.lc.ShutdownInitiated(err)
cancel()
return
}
donech := make(chan struct{})
go m.mainloop(ctx, client, donech)
err = <-m.lc.ShutdownRequest()
m.lc.ShutdownInitiated(err)
cancel()
<-donech
}
func (m *_monitor) makeClient(ctx context.Context) (corev1.CoreV1Interface, error) {
cs, err := kubernetes.NewForConfig(m.rc)
if err != nil {
return nil, err
}
return cs.CoreV1(), nil
}
func (m *_monitor) mainloop(
ctx context.Context, client corev1.CoreV1Interface, donech chan struct{}) {
defer m.log.Un(m.log.Trace("mainloop"))
defer close(donech)
// todo: backoff handled by k8 client?
sinceSecs := int64(m.config.since / time.Second)
since := &sinceSecs
m.log.Debugf("displaying logs since %v seconds", sinceSecs)
for i := 0; ctx.Err() == nil; i++ {
m.log.Debugf("readloop count: %v", i)
err := m.readloop(ctx, client, since)
switch {
case err == io.EOF:
case err == nil:
case ctx.Err() != nil:
m.lc.ShutdownAsync(nil)
return
default:
m.log.ErrWarn(err, "streaming done")
m.lc.ShutdownAsync(err)
return
}
sinceSecs = 1
}
}
func (m *_monitor) readloop(
ctx context.Context, client corev1.CoreV1Interface, since *int64) error {
defer m.log.Un(m.log.Trace("readloop"))
opts := &v1.PodLogOptions{
Container: m.source.Container(),
Follow: true,
SinceSeconds: since,
}
req := client.
Pods(m.source.Namespace()).
GetLogs(m.source.Name(), opts)
stream, err := req.Stream(ctx)
if err != nil {
return err
}
defer stream.Close()
logbuf := make([]byte, logBufsiz)
buffer := newBuffer(m.source)
for ctx.Err() == nil {
nread, err := stream.Read(logbuf)
switch {
case err == io.EOF:
return err
case ctx.Err() != nil:
return ctx.Err()
case err != nil:
return m.log.Err(err, "error while reading logs")
case nread == 0:
return io.EOF
}
log := logbuf[0:nread]
if bytes.Equal(canaryLog, log) {
m.log.Debugf("received 'unexpect stream type'")
continue
}
if events := buffer.process(log); len(events) > 0 {
m.deliverEvents(ctx, events)
}
}
return nil
}
func (m *_monitor) deliverEvents(ctx context.Context, events []Event) {
t := time.NewTimer(monitorDeliverWait)
defer t.Stop()
for i, event := range events {
select {
case m.eventch <- event:
case <-t.C:
m.log.Warnf("event buffer full. dropping %v logs", len(events)-i)
return
case <-ctx.Done():
return
}
}
}