From 32debe1763e75de1eaad9a2d46d5902b9e22ae21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Fri, 9 Feb 2018 18:30:04 +0100 Subject: [PATCH] Avoid infinite loop in Kubernetes watcher In case of unmarshalling errors the Watcher was ending up in a infinite loop, this code ignores those errors and only restarts the watcher in case of EOF. --- CHANGELOG.asciidoc | 2 ++ libbeat/common/kubernetes/watcher.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index c8e20b37325..8897e80cc9b 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -30,6 +30,8 @@ https://github.com/elastic/beats/compare/v6.2.0...6.2[Check the HEAD diff] *Affecting all Beats* +- Fix infinite loop when event unmarshal fails in Kubernetes pod watcher. {pull}6353[6353] + *Auditbeat* *Filebeat* diff --git a/libbeat/common/kubernetes/watcher.go b/libbeat/common/kubernetes/watcher.go index cf112dfe36b..2dfc7828345 100644 --- a/libbeat/common/kubernetes/watcher.go +++ b/libbeat/common/kubernetes/watcher.go @@ -3,6 +3,7 @@ package kubernetes import ( "context" "errors" + "io" "sync" "time" @@ -144,8 +145,16 @@ func (p *podWatcher) watch() { _, apiPod, err := watcher.Next() if err != nil { logp.Err("kubernetes: Watching API error %v", err) - watcher.Close() - break + + // In case of EOF, stop watching and restart the process + if err == io.EOF || err == io.ErrUnexpectedEOF { + watcher.Close() + time.Sleep(time.Second) + break + } + + // Otherwise, this is probably an unknown event (unmarshal error), ignore it + continue } // Update last resource version