Skip to content

Commit

Permalink
Cherry-pick elastic#14329 to 7.4: [Metricbeat]Kubernetes: add nil che…
Browse files Browse the repository at this point in the history
…ck for kubernetes parent object check (elastic#14469)

* [Metricbeat]Kubernetes: add nil check for kubernetes parent object check (elastic#14329)
  • Loading branch information
Pablo Mercado authored Nov 12, 2019
1 parent 4708a37 commit 8a14692
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix a race condition with the Kafka pipeline client, it is possible that `Close()` get called before `Connect()` . {issue}11945[11945]
- Fix memory leak in kubernetes autodiscover provider and add_kubernetes_metadata processor happening when pods are terminated without sending a delete event. {pull}14259[14259]
- Kubernetes watcher at `add_kubernetes_metadata` fails with StatefulSets {pull}13905[13905]
- Fix kubernetes `metaGenerator.ResourceMetadata` when parent reference controller is nil {issue}14320[14320] {pull}14329[14329]

*Auditbeat*

Expand Down
2 changes: 1 addition & 1 deletion libbeat/common/kubernetes/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (g *metaGenerator) ResourceMetadata(obj Resource) common.MapStr {
// Add controller metadata if present
if g.IncludeCreatorMetadata {
for _, ref := range accessor.GetOwnerReferences() {
if *ref.Controller {
if ref.Controller != nil && *ref.Controller {
switch ref.Kind {
// TODO grow this list as we keep adding more `state_*` metricsets
case "Deployment",
Expand Down
33 changes: 30 additions & 3 deletions libbeat/common/kubernetes/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

Expand All @@ -37,10 +37,12 @@ func TestPodMetadata(t *testing.T) {
True := true
False := false
tests := []struct {
name string
pod *Pod
meta common.MapStr
}{
{
name: "standalone Pod",
pod: &Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a.key": "foo", "a": "bar"},
Expand All @@ -62,6 +64,7 @@ func TestPodMetadata(t *testing.T) {
},
},
{
name: "Deployment + Replicaset owned Pod",
pod: &Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a.key": "foo", "a": "bar"},
Expand Down Expand Up @@ -94,6 +97,7 @@ func TestPodMetadata(t *testing.T) {
},
},
{
name: "StatefulSet + Deployment owned Pod",
pod: &Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a.key": "foo", "a": "bar"},
Expand Down Expand Up @@ -131,6 +135,29 @@ func TestPodMetadata(t *testing.T) {
"statefulset": common.MapStr{"name": "StatefulSet"},
},
},
{
name: "empty owner reference Pod",
pod: &Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a.key": "foo", "a": "bar"},
UID: types.UID(UID),
OwnerReferences: []metav1.OwnerReference{{}},
Namespace: test,
},
Spec: v1.PodSpec{
NodeName: test,
},
},
meta: common.MapStr{
"pod": common.MapStr{
"name": "",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"node": common.MapStr{"name": "test"},
"namespace": "test",
"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}},
},
},
}

for _, test := range tests {
Expand All @@ -142,9 +169,9 @@ func TestPodMetadata(t *testing.T) {

metaGen, err := NewMetaGenerator(config)
if err != nil {
t.Fatal(err)
t.Fatalf("case %q failed: %s", test.name, err.Error())
}
assert.Equal(t, metaGen.PodMetadata(test.pod), test.meta)
assert.Equal(t, metaGen.PodMetadata(test.pod), test.meta, "test failed for case %q", test.name)
}
}

Expand Down

0 comments on commit 8a14692

Please sign in to comment.