-
Notifications
You must be signed in to change notification settings - Fork 51
/
monitor.go
221 lines (188 loc) · 6.35 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
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
package podmonitor
import (
"context"
"errors"
"fmt"
"time"
"go.aporeto.io/trireme-lib/monitor/config"
"go.aporeto.io/trireme-lib/monitor/extractors"
"go.aporeto.io/trireme-lib/monitor/registerer"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// PodMonitor implements a monitor that sends pod events upstream
// It is implemented as a filter on the standard DockerMonitor.
// It gets all the PU events from the DockerMonitor and if the container is the POD container from Kubernetes,
// It connects to the Kubernetes API and adds the tags that are coming from Kuberntes that cannot be found
type PodMonitor struct {
localNode string
handlers *config.ProcessorConfig
metadataExtractor extractors.PodMetadataExtractor
netclsProgrammer extractors.PodNetclsProgrammer
pidsSetMaxProcsProgrammer extractors.PodPidsSetMaxProcsProgrammer
resetNetcls extractors.ResetNetclsKubepods
sandboxExtractor extractors.PodSandboxExtractor
enableHostPods bool
workers int
kubeCfg *rest.Config
kubeClient client.Client
eventsCh chan event.GenericEvent
}
// New returns a new kubernetes monitor.
func New() *PodMonitor {
podMonitor := &PodMonitor{
eventsCh: make(chan event.GenericEvent),
}
return podMonitor
}
// SetupConfig provides a configuration to implmentations. Every implmentation
// can have its own config type.
func (m *PodMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
defaultConfig := DefaultConfig()
if cfg == nil {
cfg = defaultConfig
}
kubernetesconfig, ok := cfg.(*Config)
if !ok {
return fmt.Errorf("Invalid configuration specified (type '%T')", cfg)
}
kubernetesconfig = SetupDefaultConfig(kubernetesconfig)
// build kubernetes config
var kubeCfg *rest.Config
if len(kubernetesconfig.Kubeconfig) > 0 {
var err error
kubeCfg, err = clientcmd.BuildConfigFromFlags("", kubernetesconfig.Kubeconfig)
if err != nil {
return err
}
} else {
var err error
kubeCfg, err = rest.InClusterConfig()
if err != nil {
return err
}
}
if kubernetesconfig.MetadataExtractor == nil {
return fmt.Errorf("missing metadata extractor")
}
if kubernetesconfig.NetclsProgrammer == nil {
return fmt.Errorf("missing net_cls programmer")
}
if kubernetesconfig.ResetNetcls == nil {
return fmt.Errorf("missing reset net_cls implementation")
}
if kubernetesconfig.SandboxExtractor == nil {
return fmt.Errorf("missing SandboxExtractor implementation")
}
if kubernetesconfig.Workers < 1 {
return fmt.Errorf("number of Kubernetes monitor workers must be at least 1")
}
// Setting up Kubernetes
m.kubeCfg = kubeCfg
m.localNode = kubernetesconfig.Nodename
m.enableHostPods = kubernetesconfig.EnableHostPods
m.metadataExtractor = kubernetesconfig.MetadataExtractor
m.netclsProgrammer = kubernetesconfig.NetclsProgrammer
m.pidsSetMaxProcsProgrammer = kubernetesconfig.PidsSetMaxProcsProgrammer
m.sandboxExtractor = kubernetesconfig.SandboxExtractor
m.resetNetcls = kubernetesconfig.ResetNetcls
m.workers = kubernetesconfig.Workers
return nil
}
// Run starts the monitor.
func (m *PodMonitor) Run(ctx context.Context) error {
if m.kubeCfg == nil {
return errors.New("pod: missing kubeconfig")
}
if err := m.handlers.IsComplete(); err != nil {
return fmt.Errorf("pod: %s", err.Error())
}
// ensure to run the reset net_cls
// NOTE: we also call this during resync, however, that is not called at startup
if m.resetNetcls == nil {
return errors.New("pod: missing net_cls reset implementation")
}
if err := m.resetNetcls(ctx); err != nil {
return fmt.Errorf("pod: failed to reset net_cls cgroups: %s", err.Error())
}
syncPeriod := time.Second * 30
mgr, err := manager.New(m.kubeCfg, manager.Options{
SyncPeriod: &syncPeriod,
})
if err != nil {
return fmt.Errorf("pod: %s", err.Error())
}
// Create the delete event controller first
dc := NewDeleteController(mgr.GetClient(), m.handlers, m.sandboxExtractor, m.eventsCh)
if err := mgr.Add(dc); err != nil {
return fmt.Errorf("pod: %s", err.Error())
}
// Create the main controller for the monitor
r := newReconciler(mgr, m.handlers, m.metadataExtractor, m.netclsProgrammer, m.sandboxExtractor, m.localNode, m.enableHostPods, dc.GetDeleteCh(), dc.GetReconcileCh())
if err := addController(mgr, r, m.workers, m.eventsCh); err != nil {
return fmt.Errorf("pod: %s", err.Error())
}
controllerStarted := make(chan struct{})
if err := mgr.Add(&runnable{ch: controllerStarted}); err != nil {
return fmt.Errorf("pod: %s", err.Error())
}
// starting the manager is a bit awkward:
// - it does not use contexts
// - we pass in a fake signal handler channel
// - we start another go routine which waits for the context to be cancelled
// and closes that channel if that is the case
// -
z := make(chan struct{})
errCh := make(chan error, 2)
go func() {
<-ctx.Done()
close(z)
errCh <- ctx.Err()
}()
go func() {
if err := mgr.Start(z); err != nil {
errCh <- err
}
}()
select {
case err := <-errCh:
return fmt.Errorf("pod: %s", err.Error())
case <-time.After(5 * time.Second):
// we give the controller 5 seconds to report back
return errors.New("pod: controller did not start within 5s")
case <-controllerStarted:
m.kubeClient = mgr.GetClient()
return nil
}
}
// SetupHandlers sets up handlers for monitors to invoke for various events such as
// processing unit events and synchronization events. This will be called before Start()
// by the consumer of the monitor
func (m *PodMonitor) SetupHandlers(c *config.ProcessorConfig) {
m.handlers = c
}
// Resync requests to the monitor to do a resync.
func (m *PodMonitor) Resync(ctx context.Context) error {
if m.resetNetcls != nil {
if err := m.resetNetcls(ctx); err != nil {
return err
}
}
if m.kubeClient == nil {
return errors.New("pod: client has not been initialized yet")
}
return ResyncWithAllPods(ctx, m.kubeClient, m.eventsCh)
}
type runnable struct {
ch chan struct{}
}
func (r *runnable) Start(z <-chan struct{}) error {
// close the indicator channel which means that the manager has been started successfully
close(r.ch)
// stay up and running, the manager needs that
<-z
return nil
}