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

[Metricbeat]Kubernetes: add nil check for kubernetes parent object check #14329

Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix panics that could result from invalid TLS certificates. This can affect Beats that connect over
TLS or Beats that accept connections over TLS and validate client certificates. {pull}14146[14146]
- Support usage of custom builders without hints and mappers {pull}13839[13839]
- 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"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not me, but goimports + vscode

I've seen this before at other projects and is not usually a blocker, but can remove it if that's a concern of some sorts.

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