-
Notifications
You must be signed in to change notification settings - Fork 148
/
pod.go
418 lines (359 loc) · 12.3 KB
/
pod.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package kubernetes
import (
"fmt"
"sync"
"time"
"github.com/elastic/elastic-agent-autodiscover/utils"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-autodiscover/kubernetes/metadata"
c "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/safemapstr"
k8s "k8s.io/client-go/kubernetes"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/composable"
)
type pod struct {
watcher kubernetes.Watcher
nodeWatcher kubernetes.Watcher
comm composable.DynamicProviderComm
metagen metadata.MetaGen
namespaceWatcher kubernetes.Watcher
config *Config
logger *logp.Logger
scope string
managed bool
cleanupTimeout time.Duration
// Mutex used by configuration updates not triggered by the main watcher,
// to avoid race conditions between cross updates and deletions.
// Other updaters must use a write lock.
crossUpdate sync.RWMutex
}
type providerData struct {
uid string
mapping map[string]interface{}
processors []map[string]interface{}
}
// NewPodEventer creates an eventer that can discover and process pod objects
func NewPodEventer(
comm composable.DynamicProviderComm,
cfg *Config,
logger *logp.Logger,
client k8s.Interface,
scope string,
managed bool) (Eventer, error) {
watcher, err := kubernetes.NewNamedWatcher("agent-pod", client, &kubernetes.Pod{}, kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
Node: cfg.Node,
Namespace: cfg.Namespace,
HonorReSyncs: true,
}, nil)
if err != nil {
return nil, errors.New(err, "couldn't create kubernetes watcher")
}
options := kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
Node: cfg.Node,
}
metaConf := cfg.AddResourceMetadata
nodeWatcher, err := kubernetes.NewNamedWatcher("agent-node", client, &kubernetes.Node{}, options, nil)
if err != nil {
logger.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Node{}, err)
}
namespaceWatcher, err := kubernetes.NewNamedWatcher("agent-namespace", client, &kubernetes.Namespace{}, kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
}, nil)
if err != nil {
logger.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Namespace{}, err)
}
rawConfig, err := c.NewConfigFrom(cfg)
if err != nil {
return nil, errors.New(err, "failed to unpack configuration")
}
metaGen := metadata.GetPodMetaGen(rawConfig, watcher, nodeWatcher, namespaceWatcher, metaConf)
p := &pod{
logger: logger,
cleanupTimeout: cfg.CleanupTimeout,
comm: comm,
scope: scope,
config: cfg,
metagen: metaGen,
watcher: watcher,
nodeWatcher: nodeWatcher,
namespaceWatcher: namespaceWatcher,
managed: managed,
}
watcher.AddEventHandler(p)
if nodeWatcher != nil && metaConf.Node.Enabled() {
updater := kubernetes.NewNodePodUpdater(p.unlockedUpdate, watcher.Store(), &p.crossUpdate)
nodeWatcher.AddEventHandler(updater)
}
if namespaceWatcher != nil && metaConf.Namespace.Enabled() {
updater := kubernetes.NewNamespacePodUpdater(p.unlockedUpdate, watcher.Store(), &p.crossUpdate)
namespaceWatcher.AddEventHandler(updater)
}
return p, nil
}
// Start starts the eventer
func (p *pod) Start() error {
if p.nodeWatcher != nil {
err := p.nodeWatcher.Start()
if err != nil {
return err
}
}
if p.namespaceWatcher != nil {
if err := p.namespaceWatcher.Start(); err != nil {
return err
}
}
return p.watcher.Start()
}
// Stop stops the eventer
func (p *pod) Stop() {
p.watcher.Stop()
if p.namespaceWatcher != nil {
p.namespaceWatcher.Stop()
}
if p.nodeWatcher != nil {
p.nodeWatcher.Stop()
}
}
func (p *pod) emitRunning(pod *kubernetes.Pod) {
namespaceAnnotations := kubernetes.PodNamespaceAnnotations(pod, p.namespaceWatcher)
data := generatePodData(pod, p.metagen, namespaceAnnotations)
data.mapping["scope"] = p.scope
if p.config.Hints.Enabled() { // This is "hints based autodiscovery flow"
if !p.managed {
if ann, ok := data.mapping["annotations"]; ok {
annotations, _ := ann.(mapstr.M)
hints := utils.GenerateHints(annotations, "", p.config.Prefix)
if len(hints) > 0 {
p.logger.Debugf("Extracted hints are :%v", hints)
hintsMapping := GenerateHintsMapping(hints, data.mapping, p.logger, "")
p.logger.Debugf("Generated hints mappings are :%v", hintsMapping)
_ = p.comm.AddOrUpdate(
data.uid,
PodPriority,
map[string]interface{}{"hints": hintsMapping},
data.processors,
)
}
}
}
} else { // This is the "template-based autodiscovery" flow
// emit normal mapping to be used in dynamic variable resolution
// Emit the pod
// We emit Pod + containers to ensure that configs matching Pod only
// get Pod metadata (not specific to any container)
_ = p.comm.AddOrUpdate(data.uid, PodPriority, data.mapping, data.processors)
}
// Emit all containers in the pod
// We should deal with init containers stopping after initialization
p.emitContainers(pod, namespaceAnnotations)
}
func (p *pod) emitContainers(pod *kubernetes.Pod, namespaceAnnotations mapstr.M) {
generateContainerData(p.comm, pod, p.metagen, namespaceAnnotations, p.logger, p.managed, p.config)
}
func (p *pod) emitStopped(pod *kubernetes.Pod) {
p.comm.Remove(string(pod.GetUID()))
for _, c := range pod.Spec.Containers {
// ID is the combination of pod UID + container name
eventID := fmt.Sprintf("%s.%s", pod.GetObjectMeta().GetUID(), c.Name)
p.comm.Remove(eventID)
}
for _, c := range pod.Spec.InitContainers {
// ID is the combination of pod UID + container name
eventID := fmt.Sprintf("%s.%s", pod.GetObjectMeta().GetUID(), c.Name)
p.comm.Remove(eventID)
}
}
// OnAdd ensures processing of pod objects that are newly added
func (p *pod) OnAdd(obj interface{}) {
p.crossUpdate.RLock()
defer p.crossUpdate.RUnlock()
p.logger.Debugf("pod add: %+v", obj)
p.emitRunning(obj.(*kubernetes.Pod))
}
// OnUpdate emits events for a given pod depending on the state of the pod,
// if it is terminating, a stop event is scheduled, if not, a stop and a start
// events are sent sequentially to recreate the resources assotiated to the pod.
func (p *pod) OnUpdate(obj interface{}) {
p.crossUpdate.RLock()
defer p.crossUpdate.RUnlock()
p.unlockedUpdate(obj)
}
func (p *pod) unlockedUpdate(obj interface{}) {
p.logger.Debugf("Watcher Pod update: %+v", obj)
pod, _ := obj.(*kubernetes.Pod)
p.emitRunning(pod)
}
// OnDelete stops pod objects that are deleted
func (p *pod) OnDelete(obj interface{}) {
p.crossUpdate.RLock()
defer p.crossUpdate.RUnlock()
p.logger.Debugf("pod delete: %+v", obj)
pod, _ := obj.(*kubernetes.Pod)
time.AfterFunc(p.cleanupTimeout, func() {
p.emitStopped(pod)
})
}
func generatePodData(
pod *kubernetes.Pod,
kubeMetaGen metadata.MetaGen,
namespaceAnnotations mapstr.M) providerData {
meta := kubeMetaGen.Generate(pod)
kubemetaMap, err := meta.GetValue("kubernetes")
if err != nil {
return providerData{}
}
// k8sMapping includes only the metadata that fall under kubernetes.*
// and these are available as dynamic vars through the provider
k8sMapping := map[string]interface{}(kubemetaMap.(mapstr.M).Clone())
if len(namespaceAnnotations) != 0 {
k8sMapping["namespace_annotations"] = namespaceAnnotations
}
// Pass annotations to all events so that it can be used in templating and by annotation builders.
annotations := mapstr.M{}
for k, v := range pod.GetObjectMeta().GetAnnotations() {
_ = safemapstr.Put(annotations, k, v)
}
k8sMapping["annotations"] = annotations
// Pass labels(not dedoted) to all events so that they can be used in templating.
labels := mapstr.M{}
for k, v := range pod.GetObjectMeta().GetLabels() {
_ = safemapstr.Put(labels, k, v)
}
k8sMapping["labels"] = labels
processors := []map[string]interface{}{}
// meta map includes metadata that go under kubernetes.*
// but also other ECS fields like orchestrator.*
for field, metaMap := range meta {
processor := map[string]interface{}{
"add_fields": map[string]interface{}{
"fields": metaMap,
"target": field,
},
}
processors = append(processors, processor)
}
return providerData{
uid: string(pod.GetUID()),
mapping: k8sMapping,
processors: processors,
}
}
func generateContainerData(
comm composable.DynamicProviderComm,
pod *kubernetes.Pod,
kubeMetaGen metadata.MetaGen,
namespaceAnnotations mapstr.M,
logger *logp.Logger,
managed bool,
config *Config) {
containers := kubernetes.GetContainersInPod(pod)
// Pass annotations to all events so that it can be used in templating and by annotation builders.
annotations := mapstr.M{}
for k, v := range pod.GetObjectMeta().GetAnnotations() {
_ = safemapstr.Put(annotations, k, v)
}
// Pass labels to all events so that it can be used in templating.
labels := mapstr.M{}
for k, v := range pod.GetObjectMeta().GetLabels() {
_ = safemapstr.Put(labels, k, v)
}
for _, c := range containers {
// If it doesn't have an ID, container doesn't exist in
// the runtime, emit only an event if we are stopping, so
// we are sure of cleaning up configurations.
if c.ID == "" {
continue
}
// ID is the combination of pod UID + container name
eventID := fmt.Sprintf("%s.%s", pod.GetObjectMeta().GetUID(), c.Spec.Name)
meta := kubeMetaGen.Generate(pod, metadata.WithFields("container.name", c.Spec.Name))
kubemetaMap, err := meta.GetValue("kubernetes")
if err != nil {
continue
}
// k8sMapping includes only the metadata that fall under kubernetes.*
// and these are available as dynamic vars through the provider
k8sMapping := map[string]interface{}(kubemetaMap.(mapstr.M).Clone())
if len(namespaceAnnotations) != 0 {
k8sMapping["namespace_annotations"] = namespaceAnnotations
}
// add annotations and labels to be discoverable by templates
k8sMapping["annotations"] = annotations
k8sMapping["labels"] = labels
//container ECS fields
cmeta := mapstr.M{
"id": c.ID,
"runtime": c.Runtime,
"image": mapstr.M{
"name": c.Spec.Image,
},
}
processors := []map[string]interface{}{
{
"add_fields": map[string]interface{}{
"fields": cmeta,
"target": "container",
},
},
}
// meta map includes metadata that go under kubernetes.*
// but also other ECS fields like orchestrator.*
for field, metaMap := range meta {
processor := map[string]interface{}{
"add_fields": map[string]interface{}{
"fields": metaMap,
"target": field,
},
}
processors = append(processors, processor)
}
// add container metadata under kubernetes.container.* to
// make them available to dynamic var resolution
containerMeta := mapstr.M{
"id": c.ID,
"name": c.Spec.Name,
"image": c.Spec.Image,
"runtime": c.Runtime,
}
if len(c.Spec.Ports) > 0 {
for _, port := range c.Spec.Ports {
_, _ = containerMeta.Put("port", fmt.Sprintf("%v", port.ContainerPort))
_, _ = containerMeta.Put("port_name", port.Name)
k8sMapping["container"] = containerMeta
if config.Hints.Enabled() { // This is "hints based autodiscovery flow"
if !managed {
if ann, ok := k8sMapping["annotations"]; ok {
annotations, _ := ann.(mapstr.M)
hints := utils.GenerateHints(annotations, "", config.Prefix)
if len(hints) > 0 {
logger.Debugf("Extracted hints are :%v", hints)
hintsMapping := GenerateHintsMapping(hints, k8sMapping, logger, c.ID)
logger.Debugf("Generated hints mappings are :%v", hintsMapping)
_ = comm.AddOrUpdate(
eventID,
PodPriority,
map[string]interface{}{"hints": hintsMapping},
processors,
)
}
}
}
} else { // This is the "template-based autodiscovery" flow
_ = comm.AddOrUpdate(eventID, ContainerPriority, k8sMapping, processors)
}
}
} else {
k8sMapping["container"] = containerMeta
_ = comm.AddOrUpdate(eventID, ContainerPriority, k8sMapping, processors)
}
}
}