Skip to content

Commit

Permalink
Always include Pod UID as part of Pod metadata (#9517)
Browse files Browse the repository at this point in the history
We realized Pod UID is a really useful info to have around so we are
always including it.

Users wanted to opt-out from this can use the `drop_fields` processor
like this:

```
processors:
  - drop_fields:
      fields: ["kubernetes.pod.uid"]
```

closes #9360
  • Loading branch information
exekias authored and ruflin committed Dec 14, 2018
1 parent 77c34fc commit 571b882
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha1...master[Check the HEAD d
- Use _doc as document type of the Elasticsearch major version is 7. {pull}9056[9056]
- Add cache.ttl to add_host_metadata. {pull}9359[9359]
- Add support for index lifecycle management (beta). {pull}7963[7963]
- Always include Pod UID as part of Pod metadata. {pull]9517[9517]

*Auditbeat*

Expand Down
5 changes: 5 additions & 0 deletions libbeat/autodiscover/providers/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestEmitEvent(t *testing.T) {
namespace := "default"
podIP := "127.0.0.1"
containerID := "docker://foobar"
uid := "005f3b90-4b9d-12f8-acf0-31020a840133"
containerImage := "elastic/filebeat:6.3.0"
node := "node"
UUID, err := uuid.NewV4()
Expand All @@ -176,6 +177,7 @@ func TestEmitEvent(t *testing.T) {
Pod: &v1.Pod{
Metadata: &metav1.ObjectMeta{
Name: &name,
Uid: &uid,
Namespace: &namespace,
Labels: map[string]string{},
Annotations: map[string]string{},
Expand Down Expand Up @@ -213,6 +215,7 @@ func TestEmitEvent(t *testing.T) {
},
"pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"node": common.MapStr{
"name": "node",
Expand All @@ -227,6 +230,7 @@ func TestEmitEvent(t *testing.T) {
"name": "filebeat",
}, "pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
}, "node": common.MapStr{
"name": "node",
},
Expand All @@ -241,6 +245,7 @@ func TestEmitEvent(t *testing.T) {
Pod: &v1.Pod{
Metadata: &metav1.ObjectMeta{
Name: &name,
Uid: &uid,
Namespace: &namespace,
Labels: map[string]string{},
Annotations: map[string]string{},
Expand Down
7 changes: 1 addition & 6 deletions libbeat/common/kubernetes/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type MetaGeneratorConfig struct {
IncludeAnnotations []string `config:"include_annotations"`

// Undocumented settings, to be deprecated in favor of `drop_fields` processor:
IncludePodUID bool `config:"include_pod_uid"`
IncludeCreatorMetadata bool `config:"include_creator_metadata"`
}

Expand Down Expand Up @@ -118,11 +117,7 @@ func (g *metaGenerator) ResourceMetadata(obj Resource) common.MapStr {
func (g *metaGenerator) PodMetadata(pod *Pod) common.MapStr {
podMeta := g.ResourceMetadata(pod)

// Add UID metadata if enabled
if g.IncludePodUID {
safemapstr.Put(podMeta, "pod.uid", pod.GetMetadata().GetUid())
}

safemapstr.Put(podMeta, "pod.uid", pod.GetMetadata().GetUid())
safemapstr.Put(podMeta, "pod.name", pod.GetMetadata().GetName())
safemapstr.Put(podMeta, "node.name", pod.Spec.GetNodeName())

Expand Down
32 changes: 8 additions & 24 deletions libbeat/common/kubernetes/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
)

func TestPodMetadataDeDot(t *testing.T) {
withUID, _ := common.NewConfigFrom(map[string]interface{}{"include_pod_uid": true})

UID := "005f3b90-4b9d-12f8-acf0-31020a840133"
Deployment := "Deployment"
test := "test"
Expand All @@ -52,33 +50,16 @@ func TestPodMetadataDeDot(t *testing.T) {
NodeName: &test,
},
},
meta: common.MapStr{
"pod": common.MapStr{"name": ""},
"node": common.MapStr{"name": "test"},
"namespace": "test",
"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}},
},
config: common.NewConfig(),
},
{
pod: &Pod{
Metadata: &metav1.ObjectMeta{
Labels: map[string]string{"a.key": "foo", "a": "bar"},
Uid: &UID,
},
Spec: &v1.PodSpec{
NodeName: &test,
},
},
meta: common.MapStr{
"pod": common.MapStr{
"name": "",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"node": common.MapStr{"name": "test"},
"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}},
"node": common.MapStr{"name": "test"},
"namespace": "test",
"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}},
},
config: withUID,
config: common.NewConfig(),
},
{
pod: &Pod{
Expand All @@ -103,7 +84,10 @@ func TestPodMetadataDeDot(t *testing.T) {
},
},
meta: common.MapStr{
"pod": common.MapStr{"name": ""},
"pod": common.MapStr{
"name": "",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"node": common.MapStr{"name": "test"},
"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}},
"deployment": common.MapStr{"name": "test"},
Expand Down
1 change: 1 addition & 0 deletions libbeat/docs/processors-using.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ metadata based on which Kubernetes pod the event originated from. Each event is
annotated with:

* Pod Name
* Pod UID
* Namespace
* Labels

Expand Down
1 change: 1 addition & 0 deletions libbeat/docs/shared-autodiscover.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ event:
* kubernetes.namespace
* kubernetes.node.name
* kubernetes.pod.name
* kubernetes.pod.uid

If the `include_annotations` config is added to the provider config, then the list of annotations present in the config
are added to the event.
Expand Down
10 changes: 10 additions & 0 deletions libbeat/processors/add_kubernetes_metadata/indexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ func TestPodIndexer(t *testing.T) {
assert.Nil(t, err)

podName := "testpod"
uid := "005f3b90-4b9d-12f8-acf0-31020a840133"
ns := "testns"
nodeName := "testnode"
pod := kubernetes.Pod{
Metadata: &metav1.ObjectMeta{
Name: &podName,
Uid: &uid,
Namespace: &ns,
Labels: map[string]string{
"labelkey": "labelvalue",
Expand All @@ -60,6 +62,7 @@ func TestPodIndexer(t *testing.T) {
expected := common.MapStr{
"pod": common.MapStr{
"name": "testpod",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"namespace": "testns",
"labels": common.MapStr{
Expand Down Expand Up @@ -111,6 +114,7 @@ func TestPodUIDIndexer(t *testing.T) {
expected := common.MapStr{
"pod": common.MapStr{
"name": "testpod",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"namespace": "testns",
"labels": common.MapStr{
Expand All @@ -136,13 +140,15 @@ func TestContainerIndexer(t *testing.T) {

podName := "testpod"
ns := "testns"
uid := "005f3b90-4b9d-12f8-acf0-31020a840133"
container := "container"
initContainer := "initcontainer"
nodeName := "testnode"

pod := kubernetes.Pod{
Metadata: &metav1.ObjectMeta{
Name: &podName,
Uid: &uid,
Namespace: &ns,
Labels: map[string]string{
"labelkey": "labelvalue",
Expand All @@ -162,6 +168,7 @@ func TestContainerIndexer(t *testing.T) {
expected := common.MapStr{
"pod": common.MapStr{
"name": "testpod",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"namespace": "testns",
"labels": common.MapStr{
Expand Down Expand Up @@ -340,12 +347,14 @@ func TestIpPortIndexer(t *testing.T) {

podName := "testpod"
ns := "testns"
uid := "005f3b90-4b9d-12f8-acf0-31020a840133"
container := "container"
ip := "1.2.3.4"
port := int32(80)
pod := kubernetes.Pod{
Metadata: &metav1.ObjectMeta{
Name: &podName,
Uid: &uid,
Namespace: &ns,
Labels: map[string]string{
"labelkey": "labelvalue",
Expand Down Expand Up @@ -375,6 +384,7 @@ func TestIpPortIndexer(t *testing.T) {
expected := common.MapStr{
"pod": common.MapStr{
"name": "testpod",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"namespace": "testns",
"labels": common.MapStr{
Expand Down

0 comments on commit 571b882

Please sign in to comment.