forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexing.go
390 lines (324 loc) · 9.37 KB
/
indexing.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
package add_kubernetes_metadata
import (
"fmt"
"strings"
"sync"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/fmtstr"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/codec"
"github.com/elastic/beats/libbeat/outputs/codec/format"
)
//Names of indexers and matchers that have been defined.
const (
ContainerIndexerName = "container"
PodNameIndexerName = "pod_name"
FieldMatcherName = "fields"
FieldFormatMatcherName = "field_format"
)
// Indexing is the singleton Register instance where all Indexers and Matchers
// are stored
var Indexing = NewRegister()
// Indexer take known pods and generate all the metadata we need to enrich
// events in a efficient way. By preindexing the metadata in the way it will be
// checked when matching events
type Indexer interface {
// GetMetadata generates event metadata for the given pod, then returns the
// list of indexes to create, with the metadata to put on them
GetMetadata(pod *Pod) []MetadataIndex
// GetIndexes return the list of indexes the given pod belongs to. This function
// must return the same indexes than GetMetadata
GetIndexes(pod *Pod) []string
}
// MetadataIndex holds a pair of index -> metadata info
type MetadataIndex struct {
Index string
Data common.MapStr
}
// Matcher takes a new event and returns the index
type Matcher interface {
// MetadataIndex returns the index string to use in annotation lookups for the given
// event. A previous indexer should have generated that index for this to work
// This function can return "" if the event doesn't match
MetadataIndex(event common.MapStr) string
}
//GenMeta takes in pods to generate metadata for them
type GenMeta interface {
//GenerateMetaData generates metadata by taking in a pod as an input
GenerateMetaData(pod *Pod) common.MapStr
}
type Indexers struct {
sync.RWMutex
indexers []Indexer
}
type Matchers struct {
sync.RWMutex
matchers []Matcher
}
// Register contains Indexer and Matchers to use on pod indexing and event matching
type Register struct {
sync.RWMutex
indexers map[string]IndexerConstructor
matchers map[string]MatcherConstructor
defaultIndexerConfigs map[string]common.Config
defaultMatcherConfigs map[string]common.Config
}
type IndexerConstructor func(config common.Config, genMeta GenMeta) (Indexer, error)
type MatcherConstructor func(config common.Config) (Matcher, error)
// NewRegister creates and returns a new Register.
func NewRegister() *Register {
return &Register{
indexers: make(map[string]IndexerConstructor, 0),
matchers: make(map[string]MatcherConstructor, 0),
defaultIndexerConfigs: make(map[string]common.Config, 0),
defaultMatcherConfigs: make(map[string]common.Config, 0),
}
}
// AddIndexer to the register
func (r *Register) AddIndexer(name string, indexer IndexerConstructor) {
r.RWMutex.Lock()
defer r.RWMutex.Unlock()
r.indexers[name] = indexer
}
// AddMatcher to the register
func (r *Register) AddMatcher(name string, matcher MatcherConstructor) {
r.RWMutex.Lock()
defer r.RWMutex.Unlock()
r.matchers[name] = matcher
}
// AddIndexer to the register
func (r *Register) AddDefaultIndexerConfig(name string, config common.Config) {
r.defaultIndexerConfigs[name] = config
}
// AddMatcher to the register
func (r *Register) AddDefaultMatcherConfig(name string, config common.Config) {
r.defaultMatcherConfigs[name] = config
}
// AddIndexer to the register
func (r *Register) GetIndexer(name string) IndexerConstructor {
indexer, ok := r.indexers[name]
if ok {
return indexer
} else {
return nil
}
}
// AddMatcher to the register
func (r *Register) GetMatcher(name string) MatcherConstructor {
matcher, ok := r.matchers[name]
if ok {
return matcher
} else {
return nil
}
}
// GetMetadata returns the composed metadata list from all registered indexers
func (i *Indexers) GetMetadata(pod *Pod) []MetadataIndex {
var metadata []MetadataIndex
i.RLock()
defer i.RUnlock()
for _, indexer := range i.indexers {
for _, m := range indexer.GetMetadata(pod) {
metadata = append(metadata, m)
}
}
return metadata
}
// GetIndexes returns the composed index list from all registered indexers
func (i *Indexers) GetIndexes(pod *Pod) []string {
var indexes []string
i.RLock()
defer i.RUnlock()
for _, indexer := range i.indexers {
for _, i := range indexer.GetIndexes(pod) {
indexes = append(indexes, i)
}
}
return indexes
}
// MetadataIndex returns the index string for the first matcher from the Registry returning one
func (m *Matchers) MetadataIndex(event common.MapStr) string {
m.RLock()
defer m.RUnlock()
for _, matcher := range m.matchers {
index := matcher.MetadataIndex(event)
if index != "" {
return index
}
}
// No index returned
return ""
}
type GenDefaultMeta struct {
annotations []string
labels []string
labelsExclude []string
}
// GenerateMetaData generates default metadata for the given pod taking to account certain filters
func (g *GenDefaultMeta) GenerateMetaData(pod *Pod) common.MapStr {
labelMap := common.MapStr{}
annotationsMap := common.MapStr{}
if len(g.labels) == 0 {
for k, v := range pod.Metadata.Labels {
labelMap[k] = v
}
} else {
labelMap = generateMapSubset(pod.Metadata.Labels, g.labels)
}
// Exclude any labels that are present in the exclude_labels config
for _, label := range g.labelsExclude {
delete(labelMap, label)
}
annotationsMap = generateMapSubset(pod.Metadata.Annotations, g.annotations)
meta := common.MapStr{
"pod": common.MapStr{
"name": pod.Metadata.Name,
},
"namespace": pod.Metadata.Namespace,
}
if len(labelMap) != 0 {
meta["labels"] = labelMap
}
if len(annotationsMap) != 0 {
meta["annotations"] = annotationsMap
}
return meta
}
func generateMapSubset(input map[string]string, keys []string) common.MapStr {
output := common.MapStr{}
if input == nil {
return output
}
for _, key := range keys {
value, ok := input[key]
if ok {
output[key] = value
}
}
return output
}
// PodNameIndexer implements default indexer based on pod name
type PodNameIndexer struct {
genMeta GenMeta
}
func NewPodNameIndexer(_ common.Config, genMeta GenMeta) (Indexer, error) {
return &PodNameIndexer{genMeta: genMeta}, nil
}
func (p *PodNameIndexer) GetMetadata(pod *Pod) []MetadataIndex {
data := p.genMeta.GenerateMetaData(pod)
return []MetadataIndex{
{
Index: fmt.Sprintf("%s/%s", pod.Metadata.Namespace, pod.Metadata.Name),
Data: data,
},
}
}
func (p *PodNameIndexer) GetIndexes(pod *Pod) []string {
return []string{fmt.Sprintf("%s/%s", pod.Metadata.Namespace, pod.Metadata.Name)}
}
// ContainerIndexer indexes pods based on all their containers IDs
type ContainerIndexer struct {
genMeta GenMeta
}
func NewContainerIndexer(_ common.Config, genMeta GenMeta) (Indexer, error) {
return &ContainerIndexer{genMeta: genMeta}, nil
}
func (c *ContainerIndexer) GetMetadata(pod *Pod) []MetadataIndex {
commonMeta := c.genMeta.GenerateMetaData(pod)
var metadata []MetadataIndex
for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) {
cID := containerID(status)
if cID == "" {
continue
}
containerMeta := commonMeta.Clone()
containerMeta["container"] = common.MapStr{
"name": status.Name,
}
metadata = append(metadata, MetadataIndex{
Index: cID,
Data: containerMeta,
})
}
return metadata
}
func (c *ContainerIndexer) GetIndexes(pod *Pod) []string {
var containers []string
for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) {
cID := containerID(status)
if cID == "" {
continue
}
containers = append(containers, cID)
}
return containers
}
func containerID(status PodContainerStatus) string {
cID := status.ContainerID
if cID != "" {
parts := strings.Split(cID, "//")
if len(parts) == 2 {
return parts[1]
}
}
return ""
}
type FieldMatcher struct {
MatchFields []string
}
func NewFieldMatcher(cfg common.Config) (Matcher, error) {
config := struct {
LookupFields []string `config:"lookup_fields"`
}{}
err := cfg.Unpack(&config)
if err != nil {
return nil, fmt.Errorf("fail to unpack the `lookup_fields` configuration: %s", err)
}
if len(config.LookupFields) == 0 {
return nil, fmt.Errorf("lookup_fields can not be empty")
}
return &FieldMatcher{MatchFields: config.LookupFields}, nil
}
func (f *FieldMatcher) MetadataIndex(event common.MapStr) string {
for _, field := range f.MatchFields {
keyIface, err := event.GetValue(field)
if err == nil {
key, ok := keyIface.(string)
if ok {
return key
}
}
}
return ""
}
type FieldFormatMatcher struct {
Codec codec.Codec
}
func NewFieldFormatMatcher(cfg common.Config) (Matcher, error) {
config := struct {
Format string `config:"format"`
}{}
err := cfg.Unpack(&config)
if err != nil {
return nil, fmt.Errorf("fail to unpack the `format` configuration of `field_format` matcher: %s", err)
}
if config.Format == "" {
return nil, fmt.Errorf("`format` of `field_format` matcher can't be empty")
}
return &FieldFormatMatcher{
Codec: format.New(fmtstr.MustCompileEvent(config.Format)),
}, nil
}
func (f *FieldFormatMatcher) MetadataIndex(event common.MapStr) string {
bytes, err := f.Codec.Encode("", &beat.Event{
Fields: event,
})
if err != nil {
logp.Debug("kubernetes", "Unable to apply field format pattern on event")
}
if len(bytes) == 0 {
return ""
}
return string(bytes)
}