Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Pod UID automatic enriching #10081

Merged
merged 3 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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
18 changes: 17 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having conditional logic we could maybe have an specialized enricher for pods.

}

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

if _, ok := res.(*kubernetes.Pod); ok {
enricher.isPod = true
}
jsoriano marked this conversation as resolved.
Show resolved Hide resolved

return enricher
}

Expand Down Expand Up @@ -243,7 +248,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 +303,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",
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
"_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