/
cell.go
98 lines (80 loc) · 2.65 KB
/
cell.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package agent
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/cilium/cilium/pkg/common"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/maps/eventsmap"
)
// Cell provides the monitor agent, which monitors the cilium events perf event
// buffer and forwards events to consumers/listeners. It also handles
// multicasting of other agent events.
var Cell = cell.Module(
"monitor-agent",
"Consumes the cilium events map and distributes those and other agent events",
cell.Provide(newMonitorAgent),
cell.Config(defaultConfig),
)
type AgentConfig struct {
// EnableMonitor enables the monitor unix domain socket server
EnableMonitor bool
// MonitorQueueSize is the size of the monitor event queue
MonitorQueueSize int
}
var defaultConfig = AgentConfig{
EnableMonitor: true,
}
func (def AgentConfig) Flags(flags *pflag.FlagSet) {
flags.Bool("enable-monitor", def.EnableMonitor, "Enable the monitor unix domain socket server")
flags.Int("monitor-queue-size", 0, "Size of the event queue when reading monitor events")
}
type agentParams struct {
cell.In
Lifecycle cell.Lifecycle
Log logrus.FieldLogger
Config AgentConfig
EventsMap eventsmap.Map `optional:"true"`
}
func newMonitorAgent(params agentParams) Agent {
ctx, cancel := context.WithCancel(context.Background())
agent := newAgent(ctx)
params.Lifecycle.Append(cell.Hook{
OnStart: func(cell.HookContext) error {
if params.EventsMap == nil {
// If there's no event map, function only for agent events.
log.Info("No eventsmap: monitor works only for agent events.")
return nil
}
err := agent.AttachToEventsMap(defaults.MonitorBufferPages)
if err != nil {
log.WithError(err).Error("encountered error when attaching the monitor agent to eventsmap")
return fmt.Errorf("encountered error when attaching the monitor agent: %w", err)
}
if params.Config.EnableMonitor {
queueSize := params.Config.MonitorQueueSize
if queueSize == 0 {
queueSize = common.GetNumPossibleCPUs(log) * defaults.MonitorQueueSizePerCPU
if queueSize > defaults.MonitorQueueSizePerCPUMaximum {
queueSize = defaults.MonitorQueueSizePerCPUMaximum
}
}
err = ServeMonitorAPI(ctx, agent, queueSize)
if err != nil {
log.WithError(err).Error("encountered error serving monitor agent API")
return fmt.Errorf("encountered error serving monitor agent API: %w", err)
}
}
return err
},
OnStop: func(cell.HookContext) error {
cancel()
return nil
},
})
return agent
}