-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
parser.go
169 lines (155 loc) · 5 KB
/
parser.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
// Copyright 2019 Authors of Hubble
//
// 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 parser
import (
pb "github.com/cilium/cilium/api/v1/flow"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
observerTypes "github.com/cilium/cilium/pkg/hubble/observer/types"
"github.com/cilium/cilium/pkg/hubble/parser/agent"
"github.com/cilium/cilium/pkg/hubble/parser/debug"
"github.com/cilium/cilium/pkg/hubble/parser/errors"
"github.com/cilium/cilium/pkg/hubble/parser/getters"
"github.com/cilium/cilium/pkg/hubble/parser/options"
"github.com/cilium/cilium/pkg/hubble/parser/seven"
"github.com/cilium/cilium/pkg/hubble/parser/threefour"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/proxy/accesslog"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// Parser for all flows
type Parser struct {
l34 *threefour.Parser
l7 *seven.Parser
dbg *debug.Parser
}
// New creates a new parser
func New(
log logrus.FieldLogger,
endpointGetter getters.EndpointGetter,
identityGetter getters.IdentityGetter,
dnsGetter getters.DNSGetter,
ipGetter getters.IPGetter,
serviceGetter getters.ServiceGetter,
opts ...options.Option,
) (*Parser, error) {
l34, err := threefour.New(log, endpointGetter, identityGetter, dnsGetter, ipGetter, serviceGetter)
if err != nil {
return nil, err
}
l7, err := seven.New(log, dnsGetter, ipGetter, serviceGetter, opts...)
if err != nil {
return nil, err
}
dbg, err := debug.New(log, endpointGetter)
if err != nil {
return nil, err
}
return &Parser{
l34: l34,
l7: l7,
dbg: dbg,
}, nil
}
func lostEventSourceToProto(source int) pb.LostEventSource {
switch source {
case observerTypes.LostEventSourcePerfRingBuffer:
return pb.LostEventSource_PERF_EVENT_RING_BUFFER
case observerTypes.LostEventSourceEventsQueue:
return pb.LostEventSource_OBSERVER_EVENTS_QUEUE
case observerTypes.LostEventSourceHubbleRingBuffer:
return pb.LostEventSource_HUBBLE_RING_BUFFER
default:
return pb.LostEventSource_UNKNOWN_LOST_EVENT_SOURCE
}
}
// Decode decodes a cilium monitor 'payload' and returns a v1.Event with
// the Event field populated.
func (p *Parser) Decode(monitorEvent *observerTypes.MonitorEvent) (*v1.Event, error) {
if monitorEvent == nil {
return nil, errors.ErrEmptyData
}
// TODO: Pool decoded flows instead of allocating new objects each time.
ts := timestamppb.New(monitorEvent.Timestamp)
ev := &v1.Event{
Timestamp: ts,
}
switch payload := monitorEvent.Payload.(type) {
case *observerTypes.PerfEvent:
if payload.Data == nil || len(payload.Data) == 0 {
return nil, errors.ErrEmptyData
} else if payload.Data[0] == monitorAPI.MessageTypeDebug {
// Debug is currently the only perf ring buffer event without any
// associated captured network packet header, so we treat it as
// a special case
dbg, err := p.dbg.Decode(payload.Data, payload.CPU)
if err != nil {
return nil, err
}
ev.Event = dbg
return ev, nil
}
flow := &pb.Flow{}
if err := p.l34.Decode(payload.Data, flow); err != nil {
return nil, err
}
// FIXME: Time and NodeName are now part of GetFlowsResponse. We
// populate these fields for compatibility with old clients.
flow.Time = ts
flow.NodeName = monitorEvent.NodeName
ev.Event = flow
return ev, nil
case *observerTypes.AgentEvent:
switch payload.Type {
case monitorAPI.MessageTypeAccessLog:
flow := &pb.Flow{}
logrecord, ok := payload.Message.(accesslog.LogRecord)
if !ok {
return nil, errors.ErrInvalidAgentMessageType
}
if err := p.l7.Decode(&logrecord, flow); err != nil {
return nil, err
}
// FIXME: Time and NodeName are now part of GetFlowsResponse. We
// populate these fields for compatibility with old clients.
flow.Time = ts
flow.NodeName = monitorEvent.NodeName
ev.Event = flow
return ev, nil
case monitorAPI.MessageTypeAgent:
agentNotifyMessage, ok := payload.Message.(monitorAPI.AgentNotifyMessage)
if !ok {
return nil, errors.ErrInvalidAgentMessageType
}
ev.Event = agent.NotifyMessageToProto(agentNotifyMessage)
return ev, nil
default:
return nil, errors.ErrUnknownEventType
}
case *observerTypes.LostEvent:
ev.Event = &pb.LostEvent{
Source: lostEventSourceToProto(payload.Source),
NumEventsLost: payload.NumLostEvents,
Cpu: &wrapperspb.Int32Value{
Value: int32(payload.CPU),
},
}
return ev, nil
case nil:
return ev, errors.ErrEmptyData
default:
return nil, errors.ErrUnknownEventType
}
}