-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
pod.go
442 lines (377 loc) · 13.1 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
// 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/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/safemapstr"
k8s "k8s.io/client-go/kubernetes"
"github.com/elastic/beats/v7/libbeat/common/kubernetes"
"github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable"
)
type pod struct {
logger *logp.Logger
cleanupTimeout time.Duration
comm composable.DynamicProviderComm
scope string
config *Config
metagen metadata.MetaGen
namespaceWatcher kubernetes.Watcher
// 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{}
}
type containerInPod struct {
id string
runtime string
spec kubernetes.Container
status kubernetes.PodContainerStatus
}
// podUpdaterHandlerFunc is a function that handles pod updater notifications.
type podUpdaterHandlerFunc func(interface{})
// podUpdaterStore is the interface that an object needs to implement to be
// used as a pod updater store.
type podUpdaterStore interface {
List() []interface{}
}
// namespacePodUpdater notifies updates on pods when their namespaces are updated.
type namespacePodUpdater struct {
handler podUpdaterHandlerFunc
store podUpdaterStore
locker sync.Locker
}
// NewPodWatcher creates a watcher that can discover and process pod objects
func NewPodWatcher(
comm composable.DynamicProviderComm,
cfg *Config,
logger *logp.Logger,
client k8s.Interface,
scope string) (kubernetes.Watcher, error) {
watcher, err := kubernetes.NewWatcher(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
if metaConf == nil {
metaConf = metadata.GetDefaultResourceMetadataConfig()
}
nodeWatcher, err := kubernetes.NewWatcher(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.NewWatcher(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 := common.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,
namespaceWatcher: namespaceWatcher,
}
watcher.AddEventHandler(&p)
if namespaceWatcher != nil && metaConf.Namespace.Enabled() {
updater := newNamespacePodUpdater(p.unlockedUpdate, watcher.Store(), &p.crossUpdate)
namespaceWatcher.AddEventHandler(updater)
}
return watcher, nil
}
func (p *pod) emitRunning(pod *kubernetes.Pod) {
namespaceAnnotations := podNamespaceAnnotations(pod, p.namespaceWatcher)
data := generatePodData(pod, p.config, p.metagen, namespaceAnnotations)
data.mapping["scope"] = p.scope
// 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
// TODO: deal with init containers stopping after initialization
p.emitContainers(pod, namespaceAnnotations)
}
func (p *pod) emitContainers(pod *kubernetes.Pod, namespaceAnnotations common.MapStr) {
generateContainerData(p.comm, pod, p.config, p.metagen, namespaceAnnotations)
}
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,
cfg *Config,
kubeMetaGen metadata.MetaGen,
namespaceAnnotations common.MapStr) 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.(common.MapStr).Clone())
if len(namespaceAnnotations) != 0 {
// TODO: convert it to namespace.annotations for 8.0
k8sMapping["namespace_annotations"] = namespaceAnnotations
}
// Pass annotations to all events so that it can be used in templating and by annotation builders.
annotations := common.MapStr{}
for k, v := range pod.GetObjectMeta().GetAnnotations() {
safemapstr.Put(annotations, k, v)
}
k8sMapping["annotations"] = annotations
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,
cfg *Config,
kubeMetaGen metadata.MetaGen,
namespaceAnnotations common.MapStr) {
containers := getContainersInPod(pod)
// Pass annotations to all events so that it can be used in templating and by annotation builders.
annotations := common.MapStr{}
for k, v := range pod.GetObjectMeta().GetAnnotations() {
safemapstr.Put(annotations, 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.(common.MapStr).Clone())
if len(namespaceAnnotations) != 0 {
// TODO: convert it to namespace.annotations for 8.0
k8sMapping["namespace_annotations"] = namespaceAnnotations
}
// add annotations to be discoverable by templates
k8sMapping["annotations"] = annotations
//container ECS fields
cmeta := common.MapStr{
"id": c.id,
"runtime": c.runtime,
"image": common.MapStr{
"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 := common.MapStr{
"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
comm.AddOrUpdate(eventID, ContainerPriority, k8sMapping, processors)
}
} else {
k8sMapping["container"] = containerMeta
comm.AddOrUpdate(eventID, ContainerPriority, k8sMapping, processors)
}
}
}
// podNamespaceAnnotations returns the annotations of the namespace of the pod
func podNamespaceAnnotations(pod *kubernetes.Pod, watcher kubernetes.Watcher) common.MapStr {
if watcher == nil {
return nil
}
rawNs, ok, err := watcher.Store().GetByKey(pod.Namespace)
if !ok || err != nil {
return nil
}
namespace, ok := rawNs.(*kubernetes.Namespace)
if !ok {
return nil
}
annotations := common.MapStr{}
for k, v := range namespace.GetAnnotations() {
safemapstr.Put(annotations, k, v)
}
return annotations
}
// newNamespacePodUpdater creates a namespacePodUpdater
func newNamespacePodUpdater(handler podUpdaterHandlerFunc, store podUpdaterStore, locker sync.Locker) *namespacePodUpdater {
return &namespacePodUpdater{
handler: handler,
store: store,
locker: locker,
}
}
// OnUpdate handles update events on namespaces.
func (n *namespacePodUpdater) OnUpdate(obj interface{}) {
ns, ok := obj.(*kubernetes.Namespace)
if !ok {
return
}
// n.store.List() returns a snapshot at this point. If a delete is received
// from the main watcher, this loop may generate an update event after the
// delete is processed, leaving configurations that would never be deleted.
// Also this loop can miss updates, what could leave outdated configurations.
// Avoid these issues by locking the processing of events from the main watcher.
if n.locker != nil {
n.locker.Lock()
defer n.locker.Unlock()
}
for _, pod := range n.store.List() {
pod, ok := pod.(*kubernetes.Pod)
if ok && pod.Namespace == ns.Name {
n.handler(pod)
}
}
}
// OnAdd handles add events on namespaces. Nothing to do, if pods are added to this
// namespace they will generate their own add events.
func (*namespacePodUpdater) OnAdd(interface{}) {}
// OnDelete handles delete events on namespaces. Nothing to do, if pods are deleted from this
// namespace they will generate their own delete events.
func (*namespacePodUpdater) OnDelete(interface{}) {}
// getContainersInPod returns all the containers defined in a pod and their statuses.
// It includes init and ephemeral containers.
func getContainersInPod(pod *kubernetes.Pod) []*containerInPod {
var containers []*containerInPod
for _, c := range pod.Spec.Containers {
containers = append(containers, &containerInPod{spec: c})
}
for _, c := range pod.Spec.InitContainers {
containers = append(containers, &containerInPod{spec: c})
}
for _, c := range pod.Spec.EphemeralContainers {
c := kubernetes.Container(c.EphemeralContainerCommon)
containers = append(containers, &containerInPod{spec: c})
}
statuses := make(map[string]*kubernetes.PodContainerStatus)
mapStatuses := func(s []kubernetes.PodContainerStatus) {
for i := range s {
statuses[s[i].Name] = &s[i]
}
}
mapStatuses(pod.Status.ContainerStatuses)
mapStatuses(pod.Status.InitContainerStatuses)
mapStatuses(pod.Status.EphemeralContainerStatuses)
for _, c := range containers {
if s, ok := statuses[c.spec.Name]; ok {
c.id, c.runtime = kubernetes.ContainerIDWithRuntime(*s)
c.status = *s
}
}
return containers
}