From 9dc2f8c3e873a62b4d0aaac5abc63633c61fa56a Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Mon, 19 Oct 2020 11:17:33 +0300 Subject: [PATCH] Kubernetes leaderelection improvements (#21896) --- libbeat/autodiscover/providers/kubernetes/kubernetes.go | 8 ++++++-- libbeat/common/kubernetes/util.go | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libbeat/autodiscover/providers/kubernetes/kubernetes.go b/libbeat/autodiscover/providers/kubernetes/kubernetes.go index 190c646ef0c..e0c5dd103c0 100644 --- a/libbeat/autodiscover/providers/kubernetes/kubernetes.go +++ b/libbeat/autodiscover/providers/kubernetes/kubernetes.go @@ -249,9 +249,13 @@ func NewLeaderElectionManager( } else { id = "beats-leader-" + uuid.String() } + ns, err := kubernetes.InClusterNamespace() + if err != nil { + ns = "default" + } lease := metav1.ObjectMeta{ Name: cfg.LeaderLease, - Namespace: "default", + Namespace: ns, } metaUID := lease.GetObjectMeta().GetUID() lem.leaderElection = leaderelection.LeaderElectionConfig{ @@ -262,7 +266,7 @@ func NewLeaderElectionManager( Identity: id, }, }, - ReleaseOnCancel: true, + ReleaseOnCancel: false, LeaseDuration: 15 * time.Second, RenewDeadline: 10 * time.Second, RetryPeriod: 2 * time.Second, diff --git a/libbeat/common/kubernetes/util.go b/libbeat/common/kubernetes/util.go index ff60a7fa591..a92c81e6d21 100644 --- a/libbeat/common/kubernetes/util.go +++ b/libbeat/common/kubernetes/util.go @@ -101,7 +101,7 @@ func DiscoverKubernetesNode(log *logp.Logger, host string, inCluster bool, clien } ctx := context.TODO() if inCluster { - ns, err := inClusterNamespace() + ns, err := InClusterNamespace() if err != nil { log.Errorf("kubernetes: Couldn't get namespace when beat is in cluster with error: %+v", err.Error()) return defaultNode @@ -158,9 +158,9 @@ func machineID() string { return "" } -// inClusterNamespace gets namespace from serviceaccount when beat is in cluster. +// InClusterNamespace gets namespace from serviceaccount when beat is in cluster. // code borrowed from client-go with some changes. -func inClusterNamespace() (string, error) { +func InClusterNamespace() (string, error) { // get namespace associated with the service account token, if available data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") if err != nil {