-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
event_type.go
77 lines (68 loc) · 2.06 KB
/
event_type.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package filters
import (
"context"
flowpb "github.com/cilium/cilium/api/v1/flow"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
)
func filterByEventType(types []*flowpb.EventTypeFilter) FilterFunc {
return func(ev *v1.Event) bool {
switch ev.Event.(type) {
case *flowpb.Flow:
event := ev.GetFlow().GetEventType()
if event == nil {
return false
}
for _, typeFilter := range types {
if t := typeFilter.GetType(); t != 0 && event.Type != t {
continue
}
if typeFilter.GetMatchSubType() && typeFilter.GetSubType() != event.SubType {
continue
}
return true
}
case *flowpb.AgentEvent:
for _, typeFilter := range types {
if t := typeFilter.GetType(); t != 0 && t != monitorAPI.MessageTypeAgent {
continue
}
agentEventType := int32(ev.GetAgentEvent().GetType())
if typeFilter.GetMatchSubType() && typeFilter.GetSubType() != agentEventType {
continue
}
return true
}
case *flowpb.DebugEvent:
for _, typeFilter := range types {
if t := typeFilter.GetType(); t != 0 && t != monitorAPI.MessageTypeDebug {
continue
}
debugEventType := int32(ev.GetDebugEvent().GetType())
if typeFilter.GetMatchSubType() && typeFilter.GetSubType() != debugEventType {
continue
}
return true
}
case *flowpb.LostEvent:
// Currently there's no way in the Hubble CLI and API to filter lost events,
// thus always include them. They are very uncommon and only occur on
// overloaded systems, in which case a user would anyway want to get them.
return true
}
return false
}
}
// EventTypeFilter implements filtering based on event type
type EventTypeFilter struct{}
// OnBuildFilter builds an event type filter
func (e *EventTypeFilter) OnBuildFilter(ctx context.Context, ff *flowpb.FlowFilter) ([]FilterFunc, error) {
var fs []FilterFunc
types := ff.GetEventType()
if len(types) > 0 {
fs = append(fs, filterByEventType(types))
}
return fs, nil
}