-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathindexers.go
307 lines (260 loc) · 8.74 KB
/
indexers.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package add_kubernetes_metadata
import (
"fmt"
"github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/kubernetes"
"github.com/elastic/beats/v7/libbeat/logp"
)
const (
ContainerIndexerName = "container"
PodNameIndexerName = "pod_name"
PodUIDIndexerName = "pod_uid"
IPPortIndexerName = "ip_port"
)
// 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 *kubernetes.Pod) []MetadataIndex
// GetIndexes return the list of indexes the given pod belongs to. This function
// must return the same indexes than GetMetadata
GetIndexes(pod *kubernetes.Pod) []string
}
// MetadataIndex holds a pair of index -> metadata info
type MetadataIndex struct {
Index string
Data common.MapStr
}
type Indexers struct {
indexers []Indexer
}
// IndexerConstructor builds a new indexer from its settings
type IndexerConstructor func(config common.Config, metaGen metadata.MetaGen) (Indexer, error)
// NewIndexers builds indexers object
func NewIndexers(configs PluginConfig, metaGen metadata.MetaGen) *Indexers {
indexers := []Indexer{}
for _, pluginConfigs := range configs {
for name, pluginConfig := range pluginConfigs {
indexFunc := Indexing.GetIndexer(name)
if indexFunc == nil {
logp.Warn("Unable to find indexing plugin %s", name)
continue
}
indexer, err := indexFunc(pluginConfig, metaGen)
if err != nil {
logp.Warn("Unable to initialize indexing plugin %s due to error %v", name, err)
continue
}
indexers = append(indexers, indexer)
}
}
return &Indexers{
indexers: indexers,
}
}
// GetIndexes returns the composed index list from all registered indexers
func (i *Indexers) GetIndexes(pod *kubernetes.Pod) []string {
var indexes []string
for _, indexer := range i.indexers {
for _, i := range indexer.GetIndexes(pod) {
indexes = append(indexes, i)
}
}
return indexes
}
// GetMetadata returns the composed metadata list from all registered indexers
func (i *Indexers) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
var metadata []MetadataIndex
for _, indexer := range i.indexers {
for _, m := range indexer.GetMetadata(pod) {
metadata = append(metadata, m)
}
}
return metadata
}
// Empty returns true if indexers list is empty
func (i *Indexers) Empty() bool {
if len(i.indexers) == 0 {
return true
}
return false
}
// PodNameIndexer implements default indexer based on pod name
type PodNameIndexer struct {
metaGen metadata.MetaGen
}
// NewPodNameIndexer initializes and returns a PodNameIndexer
func NewPodNameIndexer(_ common.Config, metaGen metadata.MetaGen) (Indexer, error) {
return &PodNameIndexer{metaGen: metaGen}, nil
}
// GetMetadata returns metadata for the given pod, if it matches the index
func (p *PodNameIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
data := p.metaGen.Generate(pod)
return []MetadataIndex{
{
Index: fmt.Sprintf("%s/%s", pod.GetObjectMeta().GetNamespace(), pod.GetObjectMeta().GetName()),
Data: data,
},
}
}
// GetIndexes returns the indexes for the given Pod
func (p *PodNameIndexer) GetIndexes(pod *kubernetes.Pod) []string {
return []string{fmt.Sprintf("%s/%s", pod.GetObjectMeta().GetNamespace(), pod.GetObjectMeta().GetName())}
}
// PodUIDIndexer indexes pods based on the pod UID
type PodUIDIndexer struct {
metaGen metadata.MetaGen
}
// NewPodUIDIndexer initializes and returns a PodUIDIndexer
func NewPodUIDIndexer(_ common.Config, metaGen metadata.MetaGen) (Indexer, error) {
return &PodUIDIndexer{metaGen: metaGen}, nil
}
// GetMetadata returns the composed metadata from PodNameIndexer and the pod UID
func (p *PodUIDIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
data := p.metaGen.Generate(pod)
return []MetadataIndex{
{
Index: string(pod.GetObjectMeta().GetUID()),
Data: data,
},
}
}
// GetIndexes returns the indexes for the given Pod
func (p *PodUIDIndexer) GetIndexes(pod *kubernetes.Pod) []string {
return []string{string(pod.GetObjectMeta().GetUID())}
}
// ContainerIndexer indexes pods based on all their containers IDs
type ContainerIndexer struct {
metaGen metadata.MetaGen
}
// NewContainerIndexer initializes and returns a ContainerIndexer
func NewContainerIndexer(_ common.Config, metaGen metadata.MetaGen) (Indexer, error) {
return &ContainerIndexer{metaGen: metaGen}, nil
}
// GetMetadata returns the composed metadata list from all registered indexers
func (c *ContainerIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
var m []MetadataIndex
for _, status := range getContainerStatusesInPod(pod) {
cID, runtime := kubernetes.ContainerIDWithRuntime(status)
if cID == "" {
continue
}
m = append(m, MetadataIndex{
Index: cID,
Data: c.metaGen.Generate(
pod,
metadata.WithFields("container.name", status.Name),
metadata.WithFields("container.image", status.Image),
metadata.WithFields("container.id", cID),
metadata.WithFields("container.runtime", runtime),
),
})
}
return m
}
// GetIndexes returns the indexes for the given Pod
func (c *ContainerIndexer) GetIndexes(pod *kubernetes.Pod) []string {
var containers []string
for _, status := range getContainerStatusesInPod(pod) {
cID := kubernetes.ContainerID(status)
if cID == "" {
continue
}
containers = append(containers, cID)
}
return containers
}
// IPPortIndexer indexes pods based on all their host:port combinations
type IPPortIndexer struct {
metaGen metadata.MetaGen
}
// NewIPPortIndexer creates and returns a new indexer for pod IP & ports
func NewIPPortIndexer(_ common.Config, metaGen metadata.MetaGen) (Indexer, error) {
return &IPPortIndexer{metaGen: metaGen}, nil
}
// GetMetadata returns metadata for the given pod, if it matches the index
func (h *IPPortIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
var m []MetadataIndex
if pod.Status.PodIP == "" {
return m
}
// Add pod IP
m = append(m, MetadataIndex{
Index: pod.Status.PodIP,
Data: h.metaGen.Generate(pod),
})
cIDs := make(map[string]string)
runtimes := make(map[string]string)
for _, status := range getContainerStatusesInPod(pod) {
cID, runtime := kubernetes.ContainerIDWithRuntime(status)
if cID == "" {
continue
}
cIDs[status.Name] = cID
runtimes[status.Name] = runtime
}
for _, container := range pod.Spec.Containers {
for _, port := range container.Ports {
if port.ContainerPort != 0 {
m = append(m, MetadataIndex{
Index: fmt.Sprintf("%s:%d", pod.Status.PodIP, port.ContainerPort),
Data: h.metaGen.Generate(
pod,
metadata.WithFields("container.name", container.Name),
metadata.WithFields("container.image", container.Image),
metadata.WithFields("container.id", cIDs[container.Name]),
metadata.WithFields("container.runtime", runtimes[container.Name]),
),
})
}
}
}
return m
}
// GetIndexes returns the indexes for the given Pod
func (h *IPPortIndexer) GetIndexes(pod *kubernetes.Pod) []string {
var hostPorts []string
if pod.Status.PodIP == "" {
return hostPorts
}
// Add pod IP
hostPorts = append(hostPorts, pod.Status.PodIP)
for _, container := range pod.Spec.Containers {
ports := container.Ports
for _, port := range ports {
if port.ContainerPort != 0 {
hostPorts = append(hostPorts, fmt.Sprintf("%s:%d", pod.Status.PodIP, port.ContainerPort))
}
}
}
return hostPorts
}
func getContainerStatusesInPod(pod *kubernetes.Pod) []kubernetes.PodContainerStatus {
if pod == nil {
return nil
}
var statuses []kubernetes.PodContainerStatus
statuses = append(statuses, pod.Status.ContainerStatuses...)
statuses = append(statuses, pod.Status.InitContainerStatuses...)
statuses = append(statuses, pod.Status.EphemeralContainerStatuses...)
return statuses
}