Skip to content

Commit

Permalink
Fix Pod UID automatic enriching (elastic#10081)
Browse files Browse the repository at this point in the history
* Fix Pod UID automatic enriching

Kubernetes Pod events where not being enriched correctly. Before this
change, metadata was being added at the module level, which is correct
for all metricsets, except `pod` and `state_pod`. For these metricsets
metadata must be added at the root level because we are already under
`kubernetes.pod`. Existing metadata was being ignored.

This change takes this special case into account and updates events in
the right place, ensuring the events get the metadata correctly.
  • Loading branch information
exekias authored Jan 15, 2019
1 parent f10ad90 commit c60282a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*Journalbeat*

*Metricbeat*

- Fix panics in vsphere module when certain values where not returned by the API. {pull}9784[9784]
- Fix pod UID metadata enrichment in Kubernetes module. {pull}10081[10081]

*Packetbeat*

Expand Down
1 change: 1 addition & 0 deletions metricbeat/module/kubernetes/pod/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"pod": {
"name": "nginx-3137573019-pcfzh",
"uid": "b89a812e-18cd-11e9-b333-080027190d51",
"network": {
"rx": {
"bytes": 18999261,
Expand Down
1 change: 1 addition & 0 deletions metricbeat/module/kubernetes/state_pod/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"host_ip": "192.168.99.100",
"ip": "172.17.0.3",
"name": "tiller-deploy-3067024529-1gp80",
"uid": "659419d5-e27a-11e8-98fa-080027190d51",
"status": {
"phase": "running",
"ready": "true",
Expand Down
20 changes: 19 additions & 1 deletion metricbeat/module/kubernetes/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type enricher struct {
watcher kubernetes.Watcher
watcherStarted bool
watcherStartedLock sync.Mutex
isPod bool
}

// GetWatcher initializes a kubernetes watcher with the given
Expand Down Expand Up @@ -157,6 +158,12 @@ func NewResourceMetadataEnricher(
},
)

// Configure the enricher for Pods, so pod specific metadata ends up in the right place when
// calling Enrich
if _, ok := res.(*kubernetes.Pod); ok {
enricher.isPod = true
}

return enricher
}

Expand Down Expand Up @@ -243,7 +250,7 @@ func buildMetadataEnricher(
watcher kubernetes.Watcher,
update func(map[string]common.MapStr, kubernetes.Resource),
delete func(map[string]common.MapStr, kubernetes.Resource),
index func(e common.MapStr) string) Enricher {
index func(e common.MapStr) string) *enricher {

enricher := enricher{
metadata: map[string]common.MapStr{},
Expand Down Expand Up @@ -298,6 +305,17 @@ func (m *enricher) Enrich(events []common.MapStr) {
defer m.RUnlock()
for _, event := range events {
if meta := m.metadata[m.index(event)]; meta != nil {
if m.isPod {
// apply pod meta at metricset level
if podMeta, ok := meta["pod"].(common.MapStr); ok {
event.DeepUpdate(podMeta)
}

// don't apply pod metadata to module level
meta = meta.Clone()
delete(meta, "pod")
}

event.DeepUpdate(common.MapStr{
mb.ModuleDataKey: meta,
})
Expand Down
30 changes: 27 additions & 3 deletions metricbeat/module/kubernetes/util/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestBuildMetadataEnricher(t *testing.T) {
watcher := mockWatcher{}
funcs := mockFuncs{}
resource := &mockResource{
uid: "mockuid",
name: "enrich",
namespace: "default",
labels: map[string]string{
Expand Down Expand Up @@ -59,6 +60,23 @@ func TestBuildMetadataEnricher(t *testing.T) {
{"name": "unknown"},
{
"name": "enrich",
"_module": common.MapStr{"label": "value", "pod": common.MapStr{"name": "enrich", "uid": "mockuid"}},
},
}, events)

// Enrich a pod (metadata goes in root level)
events = []common.MapStr{
{"name": "unknown"},
{"name": "enrich"},
}
enricher.isPod = true
enricher.Enrich(events)

assert.Equal(t, []common.MapStr{
{"name": "unknown"},
{
"name": "enrich",
"uid": "mockuid",
"_module": common.MapStr{"label": "value"},
},
}, events)
Expand Down Expand Up @@ -87,7 +105,12 @@ type mockFuncs struct {

func (f *mockFuncs) update(m map[string]common.MapStr, obj kubernetes.Resource) {
f.updated = obj
meta := common.MapStr{}
meta := common.MapStr{
"pod": common.MapStr{
"name": obj.GetMetadata().GetName(),
"uid": obj.GetMetadata().GetUid(),
},
}
for k, v := range obj.GetMetadata().Labels {
meta[k] = v
}
Expand All @@ -105,12 +128,13 @@ func (f *mockFuncs) index(m common.MapStr) string {
}

type mockResource struct {
name, namespace string
labels map[string]string
name, namespace, uid string
labels map[string]string
}

func (r *mockResource) GetMetadata() *v1.ObjectMeta {
return &v1.ObjectMeta{
Uid: &r.uid,
Name: &r.name,
Namespace: &r.namespace,
Labels: r.labels,
Expand Down

0 comments on commit c60282a

Please sign in to comment.