Skip to content

Commit

Permalink
Kubernetes leaderelection improvements (#21896) (#21953)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9dc2f8c)
  • Loading branch information
ChrsMark authored Oct 19, 2020
1 parent 6be236c commit 760faa1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions libbeat/autodiscover/providers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -262,7 +266,7 @@ func NewLeaderElectionManager(
Identity: id,
},
},
ReleaseOnCancel: true,
ReleaseOnCancel: false,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Expand Down
6 changes: 3 additions & 3 deletions libbeat/common/kubernetes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 760faa1

Please sign in to comment.