-
Notifications
You must be signed in to change notification settings - Fork 148
/
pod.go
557 lines (485 loc) · 17.5 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
package kubernetes
import (
"fmt"
"sync"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-autodiscover/kubernetes/metadata"
"github.com/elastic/elastic-agent-autodiscover/utils"
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
replicasetWatcher kubernetes.Watcher
jobWatcher 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{}
}
// hintsData hold the generated mapping data needed for hints based autodsicovery
type hintsData struct {
composableMapping mapstr.M
processors []mapstr.M
}
// 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")
}
var replicaSetWatcher, jobWatcher, namespaceWatcher, nodeWatcher kubernetes.Watcher
metaConf := cfg.AddResourceMetadata
if metaConf.Node.Enabled() || cfg.Hints.Enabled {
nodeWatcher, err = kubernetes.NewNamedWatcher("agent-node", client, &kubernetes.Node{}, kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
Node: cfg.Node,
HonorReSyncs: true,
}, nil)
if err != nil {
logger.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Node{}, err)
}
}
if metaConf.Namespace.Enabled() || cfg.Hints.Enabled {
namespaceWatcher, err = kubernetes.NewNamedWatcher("agent-namespace", client, &kubernetes.Namespace{}, kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
Namespace: cfg.Namespace,
HonorReSyncs: true,
}, nil)
if err != nil {
logger.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Namespace{}, err)
}
}
// Resource is Pod, so we need to create watchers for Replicasets and Jobs that it might belong to
// in order to be able to retrieve 2nd layer Owner metadata like in case of:
// Deployment -> Replicaset -> Pod
// CronJob -> job -> Pod
if metaConf.Deployment {
metadataClient, err := kubernetes.GetKubernetesMetadataClient(cfg.KubeConfig, cfg.KubeClientOptions)
if err != nil {
logger.Errorf("Error creating metadata client for %T due to error %+v", &kubernetes.Namespace{}, err)
}
// use a custom watcher here, so we can provide a transform function and limit the data we're storing
replicaSetWatcher, err = kubernetes.NewNamedMetadataWatcher(
"resource_metadata_enricher_rs",
client,
metadataClient,
schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "replicasets"},
kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
Namespace: cfg.Namespace,
HonorReSyncs: true,
}, nil, metadata.RemoveUnnecessaryReplicaSetData)
if err != nil {
logger.Errorf("Error creating watcher for %T due to error %+v", &kubernetes.Namespace{}, err)
}
}
if metaConf.CronJob {
jobWatcher, err = kubernetes.NewNamedWatcher("resource_metadata_enricher_job", client, &kubernetes.Job{}, kubernetes.WatchOptions{
SyncTimeout: cfg.SyncPeriod,
Namespace: cfg.Namespace,
HonorReSyncs: true,
}, nil)
if err != nil {
logger.Errorf("Error creating watcher for %T due to error %+v", &kubernetes.Job{}, 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, replicaSetWatcher, jobWatcher, metaConf)
p := &pod{
logger: logger,
cleanupTimeout: cfg.CleanupTimeout,
comm: comm,
scope: scope,
config: cfg,
metagen: metaGen,
watcher: watcher,
nodeWatcher: nodeWatcher,
namespaceWatcher: namespaceWatcher,
replicasetWatcher: replicaSetWatcher,
jobWatcher: jobWatcher,
managed: managed,
}
watcher.AddEventHandler(p)
if nodeWatcher != nil && metaConf.Node.Enabled() {
updater := kubernetes.NewNodePodUpdater(p.unlockedUpdate, watcher.Store(), p.nodeWatcher, &p.crossUpdate)
nodeWatcher.AddEventHandler(updater)
}
if namespaceWatcher != nil && metaConf.Namespace.Enabled() {
updater := kubernetes.NewNamespacePodUpdater(p.unlockedUpdate, watcher.Store(), p.namespaceWatcher, &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
}
}
if p.replicasetWatcher != nil {
if err := p.replicasetWatcher.Start(); err != nil {
return err
}
}
if p.jobWatcher != nil {
if err := p.jobWatcher.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()
}
if p.replicasetWatcher != nil {
p.replicasetWatcher.Stop()
}
if p.jobWatcher != nil {
p.jobWatcher.Stop()
}
}
func (p *pod) emitRunning(pod *kubernetes.Pod) {
if pod.Status.Phase == v1.PodPending || pod.Status.Phase == v1.PodUnknown {
return
}
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, _ := hintsCheck(annotations, "", p.config.Prefix, true, allSupportedHints, p.logger, pod)
if len(hints) > 0 {
p.logger.Debugf("Extracted hints are :%v", hints)
hintsMapping := GenerateHintsMapping(hints, data.mapping, p.logger, "")
p.logger.Debugf("Generated Pods' 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 {
hintData := GetHintsMapping(k8sMapping, logger, config.Prefix, c.ID)
if len(hintData.composableMapping) > 0 {
if len(hintData.processors) > 0 {
processors = updateProcessors(hintData.processors, processors)
}
_ = comm.AddOrUpdate(
eventID,
PodPriority,
map[string]interface{}{"hints": hintData.composableMapping},
processors,
)
} else if config.Hints.DefaultContainerLogs {
// in case of no package detected in the hints fallback to the generic log collection
_, _ = hintData.composableMapping.Put("container_logs.enabled", true)
_, _ = hintData.composableMapping.Put("container_id", c.ID)
if len(hintData.processors) > 0 {
processors = updateProcessors(hintData.processors, processors)
}
_ = comm.AddOrUpdate(
eventID,
PodPriority,
map[string]interface{}{"hints": hintData.composableMapping},
processors,
)
}
}
} else { // This is the "template-based autodiscovery" flow
_ = comm.AddOrUpdate(eventID, ContainerPriority, k8sMapping, processors)
}
}
} else {
k8sMapping["container"] = containerMeta
if config.Hints.Enabled { // This is "hints based autodiscovery flow"
if !managed {
hintData := GetHintsMapping(k8sMapping, logger, config.Prefix, c.ID)
if len(hintData.composableMapping) > 0 {
if len(hintData.processors) > 0 {
processors = updateProcessors(hintData.processors, processors)
}
_ = comm.AddOrUpdate(
eventID,
PodPriority,
map[string]interface{}{"hints": hintData.composableMapping},
processors,
)
} else if config.Hints.DefaultContainerLogs {
// in case of no package detected in the hints fallback to the generic log collection
_, _ = hintData.composableMapping.Put("container_logs.enabled", true)
_, _ = hintData.composableMapping.Put("container_id", c.ID)
if len(hintData.processors) > 0 {
processors = updateProcessors(hintData.processors, processors)
}
_ = comm.AddOrUpdate(
eventID,
PodPriority,
map[string]interface{}{"hints": hintData.composableMapping},
processors,
)
}
}
} else { // This is the "template-based autodiscovery" flow
_ = comm.AddOrUpdate(eventID, ContainerPriority, k8sMapping, processors)
}
}
}
}
// Updates processors map with any additional processors identfied from annotations
func updateProcessors(newprocessors []mapstr.M, processors []map[string]interface{}) []map[string]interface{} {
for _, processor := range newprocessors {
processors = append(processors, processor)
}
return processors
}
// HintsCheck geenrates hints from provided annotations of the pod and logs any possible incorrect annotations that have been provided in the pod
func hintsCheck(annotations mapstr.M, container string, prefix string, validate bool, allSupportedHints []string, logger *logp.Logger, pod *kubernetes.Pod) (mapstr.M, []string) {
hints, incorrecthints := utils.GenerateHints(annotations, container, prefix, validate, allSupportedHints)
for _, value := range incorrecthints { //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that have prefix co.elastic
logger.Warnf("provided hint: %s/%s is not recognised as supported annotation for pod %s in namespace %s", prefix, value, pod.Name, pod.ObjectMeta.Namespace)
}
return hints, incorrecthints
}