/
local_observer.go
468 lines (411 loc) · 12.6 KB
/
local_observer.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright 2020 Authors of Hubble
// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package observer
import (
"context"
"errors"
"fmt"
"io"
"strings"
"sync/atomic"
"time"
flowpb "github.com/cilium/cilium/api/v1/flow"
observerpb "github.com/cilium/cilium/api/v1/observer"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/cilium/pkg/hubble/container"
"github.com/cilium/cilium/pkg/hubble/filters"
"github.com/cilium/cilium/pkg/hubble/metrics"
"github.com/cilium/cilium/pkg/hubble/observer/observeroption"
observerTypes "github.com/cilium/cilium/pkg/hubble/observer/types"
"github.com/cilium/cilium/pkg/hubble/parser"
parserErrors "github.com/cilium/cilium/pkg/hubble/parser/errors"
nodeTypes "github.com/cilium/cilium/pkg/node/types"
"github.com/golang/protobuf/ptypes"
"github.com/sirupsen/logrus"
)
// DefaultOptions to include in the server. Other packages may extend this
// in their init() function.
var DefaultOptions []observeroption.Option
// LocalObserverServer is an implementation of the server.Observer interface
// that's meant to be run embedded inside the Cilium process. It ignores all
// the state change events since the state is available locally.
type LocalObserverServer struct {
// ring buffer that contains the references of all flows
ring *container.Ring
// events is the channel used by the writer(s) to send the flow data
// into the observer server.
events chan *observerTypes.MonitorEvent
// stopped is mostly used in unit tests to signalize when the events
// channel is empty, once it's closed.
stopped chan struct{}
log logrus.FieldLogger
// channel to receive events from observer server.
eventschan chan *observerpb.GetFlowsResponse
// payloadParser decodes flowpb.Payload into flowpb.Flow
payloadParser *parser.Parser
opts observeroption.Options
// startTime is the time when this instance was started
startTime time.Time
// numObservedFlows counts how many flows have been observed
numObservedFlows uint64
}
// NewLocalServer returns a new local observer server.
func NewLocalServer(
payloadParser *parser.Parser,
logger logrus.FieldLogger,
options ...observeroption.Option,
) (*LocalObserverServer, error) {
opts := observeroption.Default // start with defaults
options = append(options, DefaultOptions...)
for _, opt := range options {
if err := opt(&opts); err != nil {
return nil, fmt.Errorf("failed to apply option: %v", err)
}
}
logger.WithFields(logrus.Fields{
"maxFlows": opts.MaxFlows,
"eventQueueSize": opts.MonitorBuffer,
}).Info("Configuring Hubble server")
s := &LocalObserverServer{
log: logger,
ring: container.NewRing(opts.MaxFlows),
events: make(chan *observerTypes.MonitorEvent, opts.MonitorBuffer),
stopped: make(chan struct{}),
eventschan: make(chan *observerpb.GetFlowsResponse, 100), // option here?
payloadParser: payloadParser,
startTime: time.Now(),
opts: opts,
}
for _, f := range s.opts.OnServerInit {
err := f.OnServerInit(s)
if err != nil {
s.log.WithError(err).Error("failed in OnServerInit")
return nil, err
}
}
return s, nil
}
// Start implements GRPCServer.Start.
func (s *LocalObserverServer) Start() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nextEvent:
for monitorEvent := range s.GetEventsChannel() {
for _, f := range s.opts.OnMonitorEvent {
stop, err := f.OnMonitorEvent(ctx, monitorEvent)
if err != nil {
s.log.WithError(err).WithField("event", monitorEvent).Info("failed in OnMonitorEvent")
}
if stop {
continue nextEvent
}
}
ev, err := s.payloadParser.Decode(monitorEvent)
if err != nil {
if !errors.Is(err, parserErrors.ErrUnknownEventType) {
s.log.WithError(err).WithField("event", monitorEvent).Debug("failed to decode payload")
}
continue
}
if flow, ok := ev.Event.(*flowpb.Flow); ok {
for _, f := range s.opts.OnDecodedFlow {
stop, err := f.OnDecodedFlow(ctx, flow)
if err != nil {
s.log.WithError(err).WithField("event", monitorEvent).Info("failed in OnDecodedFlow")
}
if stop {
continue nextEvent
}
}
atomic.AddUint64(&s.numObservedFlows, 1)
// FIXME: Convert metrics into an OnDecodedFlow function
metrics.ProcessFlow(flow)
}
s.GetRingBuffer().Write(ev)
}
close(s.GetStopped())
}
// GetEventsChannel returns the event channel to receive flowpb.Payload events.
func (s *LocalObserverServer) GetEventsChannel() chan *observerTypes.MonitorEvent {
return s.events
}
// GetRingBuffer implements GRPCServer.GetRingBuffer.
func (s *LocalObserverServer) GetRingBuffer() *container.Ring {
return s.ring
}
// GetLogger implements GRPCServer.GetLogger.
func (s *LocalObserverServer) GetLogger() logrus.FieldLogger {
return s.log
}
// GetStopped implements GRPCServer.GetStopped.
func (s *LocalObserverServer) GetStopped() chan struct{} {
return s.stopped
}
// GetPayloadParser implements GRPCServer.GetPayloadParser.
func (s *LocalObserverServer) GetPayloadParser() *parser.Parser {
return s.payloadParser
}
// GetOptions implements serveroptions.Server.GetOptions.
func (s *LocalObserverServer) GetOptions() observeroption.Options {
return s.opts
}
// ServerStatus should have a comment, apparently. It returns the server status.
func (s *LocalObserverServer) ServerStatus(
ctx context.Context, req *observerpb.ServerStatusRequest,
) (*observerpb.ServerStatusResponse, error) {
return &observerpb.ServerStatusResponse{
MaxFlows: s.GetRingBuffer().Cap(),
NumFlows: s.GetRingBuffer().Len(),
SeenFlows: atomic.LoadUint64(&s.numObservedFlows),
UptimeNs: uint64(time.Since(s.startTime).Nanoseconds()),
}, nil
}
// GetFlows implements the proto method for client requests.
func (s *LocalObserverServer) GetFlows(
req *observerpb.GetFlowsRequest,
server observerpb.Observer_GetFlowsServer,
) (err error) {
// This context is used for goroutines spawned specifically to serve this
// request, meaning it must be cancelled once the request is done and this
// function returns.
ctx, cancel := context.WithCancel(server.Context())
defer cancel()
for _, f := range s.opts.OnGetFlows {
ctx, err = f.OnGetFlows(ctx, req)
if err != nil {
return err
}
}
filterList := append(filters.DefaultFilters, s.opts.OnBuildFilter...)
whitelist, err := filters.BuildFilterList(ctx, req.Whitelist, filterList)
if err != nil {
return err
}
blacklist, err := filters.BuildFilterList(ctx, req.Blacklist, filterList)
if err != nil {
return err
}
start := time.Now()
log := s.GetLogger()
ring := s.GetRingBuffer()
i := uint64(0)
defer func() {
log.WithFields(logrus.Fields{
"number_of_flows": i,
"buffer_size": ring.Cap(),
"whitelist": logFilters(req.Whitelist),
"blacklist": logFilters(req.Blacklist),
"took": time.Since(start),
}).Debug("GetFlows finished")
}()
ringReader, err := newRingReader(ring, req, whitelist, blacklist)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
flowsReader, err := newFlowsReader(ringReader, req, log, whitelist, blacklist)
if err != nil {
return err
}
nextFlow:
for ; ; i++ {
resp, err := flowsReader.Next(ctx)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
for _, f := range s.opts.OnFlowDelivery {
stop, err := f.OnFlowDelivery(ctx, resp.GetFlow())
if err != nil {
return err
}
if stop {
continue nextFlow
}
}
err = server.Send(resp)
if err != nil {
return err
}
}
}
func logFilters(filters []*flowpb.FlowFilter) string {
s := make([]string, 0, len(filters))
for _, f := range filters {
s = append(s, f.String())
}
return "{" + strings.Join(s, ",") + "}"
}
// flowsReader reads flows using a RingReader. It applies the flow request
// criteria (blacklist, whitelist, follow, ...) before returning flows.
type flowsReader struct {
ringReader *container.RingReader
whitelist, blacklist filters.FilterFuncs
maxFlows uint64
follow, timeRange bool
flowsCount uint64
since, until *time.Time
}
// newFlowsReader creates a new flowsReader that uses the given RingReader to
// read through the ring buffer. Only flows that match the request criteria
// are returned.
func newFlowsReader(r *container.RingReader, req *observerpb.GetFlowsRequest, log logrus.FieldLogger, whitelist, blacklist filters.FilterFuncs) (*flowsReader, error) {
log.WithFields(logrus.Fields{
"req": req,
"whitelist": whitelist,
"blacklist": blacklist,
}).Debug("creating a new flowsReader")
reader := &flowsReader{
ringReader: r,
whitelist: whitelist,
blacklist: blacklist,
maxFlows: req.Number,
follow: req.Follow,
timeRange: req.Since != nil || req.Until != nil,
}
if req.Since != nil {
since, err := ptypes.Timestamp(req.Since)
if err != nil {
return nil, err
}
reader.since = &since
}
if req.Until != nil {
until, err := ptypes.Timestamp(req.Until)
if err != nil {
return nil, err
}
reader.until = &until
}
return reader, nil
}
// Next returns the next flow that matches the request criteria.
func (r *flowsReader) Next(ctx context.Context) (*observerpb.GetFlowsResponse, error) {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
var e *v1.Event
var err error
if r.follow {
e = r.ringReader.NextFollow(ctx)
} else {
if r.maxFlows > 0 && (r.flowsCount >= r.maxFlows) {
return nil, io.EOF
}
e, err = r.ringReader.Next()
if err != nil {
if errors.Is(err, container.ErrInvalidRead) {
// this error is sent over the wire and presented to the user
return nil, errors.New("requested data has been overwritten and is no longer available")
}
return nil, err
}
}
if e == nil {
return nil, io.EOF
}
if r.timeRange {
ts, err := ptypes.Timestamp(e.Timestamp)
if err != nil {
return nil, err
}
if r.until != nil && ts.After(*r.until) {
return nil, io.EOF
}
if r.since != nil && ts.Before(*r.since) {
continue
}
}
switch ev := e.Event.(type) {
case *flowpb.Flow:
if filters.Apply(r.whitelist, r.blacklist, e) {
r.flowsCount++
return &observerpb.GetFlowsResponse{
Time: ev.GetTime(),
NodeName: ev.GetNodeName(),
ResponseTypes: &observerpb.GetFlowsResponse_Flow{
Flow: ev,
},
}, nil
}
case *flowpb.LostEvent:
return &observerpb.GetFlowsResponse{
Time: e.Timestamp,
NodeName: nodeTypes.GetName(),
ResponseTypes: &observerpb.GetFlowsResponse_LostEvents{
LostEvents: ev,
},
}, nil
}
}
}
// newRingReader creates a new RingReader that starts at the correct ring
// offset to match the flow request.
func newRingReader(ring *container.Ring, req *observerpb.GetFlowsRequest, whitelist, blacklist filters.FilterFuncs) (*container.RingReader, error) {
if req.Follow && req.Number == 0 && req.Since == nil {
// no need to rewind
return container.NewRingReader(ring, ring.LastWriteParallel()), nil
}
var err error
var since time.Time
if req.Since != nil {
since, err = ptypes.Timestamp(req.Since)
if err != nil {
return nil, err
}
}
idx := ring.LastWriteParallel()
reader := container.NewRingReader(ring, idx)
var flowsCount uint64
// We need to find out what the right index is; that is the index with the
// oldest entry that is within time range boundaries (if any is defined)
// or until we find enough events.
// In order to avoid buffering events, we have to rewind first to find the
// correct index, then create a new reader that starts from there
for i := ring.Len(); i > 0; i, idx = i-1, idx-1 {
e, err := reader.Previous()
if errors.Is(err, container.ErrInvalidRead) {
idx++ // we went backward 1 too far
break
} else if err != nil {
return nil, err
}
_, ok := e.Event.(*flowpb.Flow)
if !ok || !filters.Apply(whitelist, blacklist, e) {
continue
}
flowsCount++
if req.Since != nil {
ts, err := ptypes.Timestamp(e.GetFlow().GetTime())
if err != nil {
return nil, err
}
if ts.Before(since) {
idx++ // we went backward 1 too far
break
}
} else if flowsCount == req.Number {
break // we went backward far enough
}
}
return container.NewRingReader(ring, idx), nil
}