From 50300511909554bcca7bde20596faa7939000250 Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Wed, 2 Dec 2020 10:04:26 +0200 Subject: [PATCH] Fix polling node when it is not ready and monitor by hostname (#22666) (#22814) (cherry picked from commit 09008c8d7c278d2cc404d3d6d73f35861c0814a2) Co-authored-by: Vijay Samuel --- CHANGELOG.next.asciidoc | 1 + .../autodiscover/providers/kubernetes/node.go | 11 ++++ .../providers/kubernetes/node_test.go | 59 ++++++++++++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 7f6bd27414f..26622c84bdb 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -401,6 +401,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Added Kafka version 2.2 to the list of supported versions. {pull}22328[22328] - Add support for ephemeral containers in kubernetes autodiscover and `add_kubernetes_metadata`. {pull}22389[22389] {pull}22439[22439] - Added support for wildcard fields and keyword fallback in beats setup commands. {pull}22521[22521] +- Fix polling node when it is not ready and monitor by hostname {pull}22666[22666] *Auditbeat* diff --git a/libbeat/autodiscover/providers/kubernetes/node.go b/libbeat/autodiscover/providers/kubernetes/node.go index 95e23b33d2a..0f37ade6b5a 100644 --- a/libbeat/autodiscover/providers/kubernetes/node.go +++ b/libbeat/autodiscover/providers/kubernetes/node.go @@ -168,6 +168,11 @@ func (n *node) emit(node *kubernetes.Node, flag string) { return } + // If the node is not in ready state then dont monitor it unless its a stop event + if !isNodeReady(node) && flag != "stop" { + return + } + eventID := fmt.Sprint(node.GetObjectMeta().GetUID()) meta := n.metagen.Generate(node) @@ -237,6 +242,12 @@ func getAddress(node *kubernetes.Node) string { } } + for _, address := range node.Status.Addresses { + if address.Type == v1.NodeHostName && address.Address != "" { + return address.Address + } + } + return "" } diff --git a/libbeat/autodiscover/providers/kubernetes/node_test.go b/libbeat/autodiscover/providers/kubernetes/node_test.go index 518a0121578..6eb22b185e1 100644 --- a/libbeat/autodiscover/providers/kubernetes/node_test.go +++ b/libbeat/autodiscover/providers/kubernetes/node_test.go @@ -156,6 +156,12 @@ func TestEmitEvent_Node(t *testing.T) { Address: "node1", }, }, + Conditions: []v1.NodeCondition{ + { + Type: v1.NodeReady, + Status: v1.ConditionTrue, + }, + }, }, }, Expected: bus.Event{ @@ -183,6 +189,57 @@ func TestEmitEvent_Node(t *testing.T) { "config": []*common.Config{}, }, }, + { + Message: "Test node start with just node name", + Flag: "start", + Node: &kubernetes.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + UID: types.UID(uid), + Labels: map[string]string{}, + Annotations: map[string]string{}, + }, + TypeMeta: typeMeta, + Status: v1.NodeStatus{ + Addresses: []v1.NodeAddress{ + { + Type: v1.NodeHostName, + Address: "node1", + }, + }, + Conditions: []v1.NodeCondition{ + { + Type: v1.NodeReady, + Status: v1.ConditionTrue, + }, + }, + }, + }, + Expected: bus.Event{ + "start": true, + "host": "node1", + "id": uid, + "provider": UUID, + "kubernetes": common.MapStr{ + "node": common.MapStr{ + "name": "metricbeat", + "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", + "hostname": "node1", + }, + "annotations": common.MapStr{}, + }, + "meta": common.MapStr{ + "kubernetes": common.MapStr{ + "node": common.MapStr{ + "name": "metricbeat", + "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", + "hostname": "node1", + }, + }, + }, + "config": []*common.Config{}, + }, + }, { Message: "Test service without host", Flag: "start", @@ -221,7 +278,7 @@ func TestEmitEvent_Node(t *testing.T) { }, Expected: bus.Event{ "stop": true, - "host": "", + "host": "node1", "id": uid, "provider": UUID, "kubernetes": common.MapStr{