/
monitor.go
195 lines (165 loc) · 6 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
package trireme
import (
"github.com/aporeto-inc/trireme-lib/constants"
"github.com/aporeto-inc/trireme-lib/internal/monitor"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/cni"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/docker"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/linux"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/uid"
"github.com/aporeto-inc/trireme-lib/rpc/events"
"github.com/aporeto-inc/trireme-lib/rpc/processor"
)
// MonitorOption is provided using functional arguments.
type MonitorOption func(*monitor.Config)
// CNIMonitorOption is provided using functional arguments.
type CNIMonitorOption func(*cnimonitor.Config)
// UIDMonitorOption is provided using functional arguments.
type UIDMonitorOption func(*uidmonitor.Config)
// DockerMonitorOption is provided using functional arguments.
type DockerMonitorOption func(*dockermonitor.Config)
// LinuxMonitorOption is provided using functional arguments.
type LinuxMonitorOption func(*linuxmonitor.Config)
// SubOptionMonitorLinuxExtractor provides a way to specify metadata extractor for linux monitors.
func SubOptionMonitorLinuxExtractor(extractor events.EventMetadataExtractor) func(*linuxmonitor.Config) {
return func(cfg *linuxmonitor.Config) {
cfg.EventMetadataExtractor = extractor
}
}
// optionMonitorLinux provides a way to add a linux monitor and related configuration to be used with New().
func optionMonitorLinux(
host bool,
opts ...func(*linuxmonitor.Config),
) func(*monitor.Config) {
lc := linuxmonitor.DefaultConfig(host)
// Collect all docker options
for _, opt := range opts {
opt(lc)
}
return func(cfg *monitor.Config) {
if host {
cfg.Monitors[monitor.LinuxHost] = lc
} else {
cfg.Monitors[monitor.LinuxProcess] = lc
}
}
}
// OptionMonitorLinuxHost provides a way to add a linux host monitor and related configuration to be used with New().
func OptionMonitorLinuxHost(
opts ...func(*linuxmonitor.Config),
) func(*monitor.Config) {
return optionMonitorLinux(true, opts...)
}
// OptionMonitorLinuxProcess provides a way to add a linux process monitor and related configuration to be used with New().
func OptionMonitorLinuxProcess(
opts ...func(*linuxmonitor.Config),
) func(*monitor.Config) {
return optionMonitorLinux(false, opts...)
}
// SubOptionMonitorCNIExtractor provides a way to specify metadata extractor for CNI monitors.
func SubOptionMonitorCNIExtractor(extractor events.EventMetadataExtractor) func(*cnimonitor.Config) {
return func(cfg *cnimonitor.Config) {
cfg.EventMetadataExtractor = extractor
}
}
// OptionMonitorCNI provides a way to add a cni monitor and related configuration to be used with New().
func OptionMonitorCNI(
opts ...func(*cnimonitor.Config),
) func(*monitor.Config) {
cc := cnimonitor.DefaultConfig()
// Collect all docker options
for _, opt := range opts {
opt(cc)
}
return func(cfg *monitor.Config) {
cfg.Monitors[monitor.CNI] = cc
}
}
// SubOptionMonitorUIDExtractor provides a way to specify metadata extractor for UID monitors.
func SubOptionMonitorUIDExtractor(extractor events.EventMetadataExtractor) func(*uidmonitor.Config) {
return func(cfg *uidmonitor.Config) {
cfg.EventMetadataExtractor = extractor
}
}
// OptionMonitorUID provides a way to add a UID monitor and related configuration to be used with New().
func OptionMonitorUID(
opts ...func(*uidmonitor.Config),
) func(*monitor.Config) {
uc := uidmonitor.DefaultConfig()
// Collect all docker options
for _, opt := range opts {
opt(uc)
}
return func(cfg *monitor.Config) {
cfg.Monitors[monitor.UID] = uc
}
}
// SubOptionMonitorDockerExtractor provides a way to specify metadata extractor for docker.
func SubOptionMonitorDockerExtractor(extractor dockermonitor.MetadataExtractor) func(*dockermonitor.Config) {
return func(cfg *dockermonitor.Config) {
cfg.EventMetadataExtractor = extractor
}
}
// SubOptionDockerMonitorMode provides a way to set the mode for docker monitor
func SubOptionDockerMonitorMode(mode constants.DockerMonitorMode) func(*dockermonitor.Config) {
return func(cfg *dockermonitor.Config) {
switch mode {
case constants.DockerMode:
case constants.KubernetesMode:
cfg.NoProxyMode = false
case constants.NoProxyMode:
cfg.NoProxyMode = true
default:
cfg.NoProxyMode = false
}
}
}
// SubOptionMonitorDockerSocket provides a way to specify socket info for docker.
func SubOptionMonitorDockerSocket(socketType, socketAddress string) func(*dockermonitor.Config) {
return func(cfg *dockermonitor.Config) {
cfg.SocketType = socketType
cfg.SocketAddress = socketAddress
}
}
// SubOptionMonitorDockerFlags provides a way to specify configuration flags info for docker.
func SubOptionMonitorDockerFlags(syncAtStart, killContainerOnPolicyError bool) func(*dockermonitor.Config) {
return func(cfg *dockermonitor.Config) {
cfg.KillContainerOnPolicyError = killContainerOnPolicyError
cfg.SyncAtStart = syncAtStart
}
}
// OptionMonitorDocker provides a way to add a docker monitor and related configuration to be used with New().
func OptionMonitorDocker(opts ...func(*dockermonitor.Config)) func(*monitor.Config) {
dc := &dockermonitor.Config{}
// Collect all docker options
for _, opt := range opts {
opt(dc)
}
return func(cfg *monitor.Config) {
cfg.Monitors[monitor.Docker] = dc
}
}
// OptionSynchronizationHandler provides options related to processor configuration to be used with New().
func OptionSynchronizationHandler(
s processor.SynchronizationHandler,
) func(*monitor.Config) {
return func(cfg *monitor.Config) {
cfg.Common.SyncHandler = s
}
}
// OptionMergeTags provides a way to add merge tags to be used with New().
func OptionMergeTags(tags []string) func(*monitor.Config) {
return func(cfg *monitor.Config) {
cfg.MergeTags = tags
cfg.Common.MergeTags = tags
}
}
// NewMonitor provides a configuration for monitors.
func NewMonitor(opts ...MonitorOption) *monitor.Config {
cfg := &monitor.Config{
Monitors: make(map[monitor.Type]interface{}),
}
for _, opt := range opts {
opt(cfg)
}
return cfg
}