From ec6b00dde0ff67f8a0cccae300a5ce584dfcadcf Mon Sep 17 00:00:00 2001 From: chrismark Date: Fri, 2 Apr 2021 11:18:52 +0300 Subject: [PATCH 01/11] Add kubernetes_leaderelection provider Signed-off-by: chrismark --- .../elastic-agent-standalone-kubernetes.yml | 6 + .../elastic-agent-standalone-role.yaml | 6 + x-pack/elastic-agent/pkg/agent/cmd/include.go | 1 + .../kubernetesleaderelection/config.go | 17 ++ .../kubernetes_leaderelection.go | 156 ++++++++++++++++++ .../kubernetessecrets/kubernetes_secrets.go | 3 + 6 files changed, 189 insertions(+) create mode 100644 x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go create mode 100644 x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go diff --git a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml index 4118e2d4e80c..43d4f80c6d27 100644 --- a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml +++ b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml @@ -650,6 +650,12 @@ rules: - "/metrics" verbs: - get + - apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: + - '*' --- apiVersion: v1 kind: ServiceAccount diff --git a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml index dcf2b4a5ff29..0a8002080c34 100644 --- a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml +++ b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml @@ -33,3 +33,9 @@ rules: - "/metrics" verbs: - get + - apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: + - '*' diff --git a/x-pack/elastic-agent/pkg/agent/cmd/include.go b/x-pack/elastic-agent/pkg/agent/cmd/include.go index 5bc763c6df06..f28b5cb11b50 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/include.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/include.go @@ -11,6 +11,7 @@ import ( _ "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/providers/env" _ "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/providers/host" _ "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/providers/kubernetes" + _ "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection" _ "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets" _ "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/providers/local" _ "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/providers/localdynamic" diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go new file mode 100644 index 000000000000..4e3b84cef2ce --- /dev/null +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go @@ -0,0 +1,17 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +// TODO review the need for this +// +build linux darwin windows + +package kubernetesleaderelection + +// Config for kubernetes_leaderelection provider +type Config struct { + KubeConfig string `config:"kube_config"` + // Scope of the provider (cluster or node) + Scope string `config:"scope"` + // Name of the leaderelection lease + LeaderLease string `config:"leader_lease"` +} diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go new file mode 100644 index 000000000000..9145b638c897 --- /dev/null +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go @@ -0,0 +1,156 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package kubernetesleaderelection + +import ( + "context" + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/leaderelection" + "k8s.io/client-go/tools/leaderelection/resourcelock" + + "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" +) + +func init() { + composable.Providers.AddDynamicProvider("kubernetes_leaderelection", DynamicProviderBuilder) +} + +type dynamicProvider struct { + logger *logger.Logger + config *Config + comm composable.DynamicProviderComm + leaderElection *leaderelection.LeaderElectionConfig + cancelLeaderElection context.CancelFunc +} + +// DynamicProviderBuilder builds the dynamic provider. +func DynamicProviderBuilder(logger *logger.Logger, c *config.Config) (composable.DynamicProvider, error) { + var cfg Config + if c == nil { + c = config.New() + } + err := c.Unpack(&cfg) + if err != nil { + return nil, errors.New(err, "failed to unpack configuration") + } + return &dynamicProvider{logger, &cfg, nil, nil, nil}, nil +} + +// Run runs the environment context provider. +func (p *dynamicProvider) Run(comm composable.DynamicProviderComm) error { + client, err := kubernetes.GetKubernetesClient(p.config.KubeConfig) + if err != nil { + // info only; return nil (do nothing) + p.logger.Debugf("Kubernetes leaderelection provider skipped, unable to connect: %s", err) + return nil + } + if p.config.LeaderLease == "" { + p.logger.Debugf("Kubernetes leaderelection provider skipped, unable to define leader lease") + return nil + } + + agentInfo, err := info.NewAgentInfo() + if err != nil { + return err + } + id := "elastic-agent-leader-" + agentInfo.AgentID() + + ns, err := kubernetes.InClusterNamespace() + if err != nil { + ns = "default" + } + lease := metav1.ObjectMeta{ + Name: p.config.LeaderLease, + Namespace: ns, + } + metaUID := lease.GetObjectMeta().GetUID() + p.leaderElection = &leaderelection.LeaderElectionConfig{ + Lock: &resourcelock.LeaseLock{ + LeaseMeta: lease, + Client: client.CoordinationV1(), + LockConfig: resourcelock.ResourceLockConfig{ + Identity: id, + }, + }, + ReleaseOnCancel: true, + LeaseDuration: 15 * time.Second, + RenewDeadline: 10 * time.Second, + RetryPeriod: 2 * time.Second, + Callbacks: leaderelection.LeaderCallbacks{ + OnStartedLeading: func(ctx context.Context) { + p.logger.Debugf("leader election lock GAINED, id %v", id) + p.startLeading(string(metaUID)) + }, + OnStoppedLeading: func() { + p.logger.Debugf("leader election lock LOST, id %v", id) + p.stopLeading(string(metaUID)) + }, + }, + } + ctx, cancel := context.WithCancel(context.TODO()) + p.cancelLeaderElection = cancel + p.comm = comm + p.startLeaderElector(ctx) + + return nil +} + +// startLeaderElector starts a Leader Elector in the background with the provided config +func (p *dynamicProvider) startLeaderElector(ctx context.Context) { + le, err := leaderelection.NewLeaderElector(*p.leaderElection) + if err != nil { + p.logger.Errorf("error while creating Leader Elector: %v", err) + } + p.logger.Debugf("Starting Leader Elector") + go le.Run(ctx) +} + +func (p *dynamicProvider) startLeading(metaUID string) { + mapping := map[string]interface{}{ + "leader": true, + } + + processors := []map[string]interface{}{ + { + "add_fields": map[string]interface{}{ + "fields": mapping, + "target": "kubernetes_leaderelection", + }, + }, + } + + p.comm.AddOrUpdate(metaUID, 0, mapping, processors) +} + +func (p *dynamicProvider) stopLeading(metaUID string) { + mapping := map[string]interface{}{ + "leader": false, + } + + processors := []map[string]interface{}{ + { + "add_fields": map[string]interface{}{ + "fields": mapping, + "target": "kubernetes_leaderelection", + }, + }, + } + + p.comm.AddOrUpdate(metaUID, 0, mapping, processors) +} + +// Stop signals the stop channel to force the leader election loop routine to stop. +func (p *dynamicProvider) Stop() { + if p.cancelLeaderElection != nil { + p.cancelLeaderElection() + } +} diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go index 4af00bc766e5..5588a546a295 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go @@ -47,6 +47,9 @@ func ContextProviderBuilder(logger *logger.Logger, c *config.Config) (corecomp.C func (p *contextProviderK8sSecrets) Fetch(key string) (string, bool) { // key = "kubernetes_secrets.somenamespace.somesecret.value" + if p.client == nil { + return "", false + } tokens := strings.Split(key, ".") if len(tokens) > 0 && tokens[0] != "kubernetes_secrets" { return "", false From a9cac2ebeb3fc75b0f30c7fcdd81b8e068e44a66 Mon Sep 17 00:00:00 2001 From: chrismark Date: Fri, 2 Apr 2021 14:45:55 +0300 Subject: [PATCH 02/11] fix config Signed-off-by: chrismark --- .../pkg/composable/providers/kubernetesleaderelection/config.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go index 4e3b84cef2ce..3d7d27214b8e 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go @@ -10,8 +10,6 @@ package kubernetesleaderelection // Config for kubernetes_leaderelection provider type Config struct { KubeConfig string `config:"kube_config"` - // Scope of the provider (cluster or node) - Scope string `config:"scope"` // Name of the leaderelection lease LeaderLease string `config:"leader_lease"` } From b898bca1bd2e4b0b18036fc1ad9ccf5042a1334e Mon Sep 17 00:00:00 2001 From: chrismark Date: Fri, 2 Apr 2021 14:47:15 +0300 Subject: [PATCH 03/11] fix comment Signed-off-by: chrismark --- .../kubernetesleaderelection/kubernetes_leaderelection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go index 9145b638c897..ff774841a644 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go @@ -45,7 +45,7 @@ func DynamicProviderBuilder(logger *logger.Logger, c *config.Config) (composable return &dynamicProvider{logger, &cfg, nil, nil, nil}, nil } -// Run runs the environment context provider. +// Run runs the leaderelection dynamic provider. func (p *dynamicProvider) Run(comm composable.DynamicProviderComm) error { client, err := kubernetes.GetKubernetesClient(p.config.KubeConfig) if err != nil { From b6df824d2a8f99fa391dab5edaaddce198928ab2 Mon Sep 17 00:00:00 2001 From: chrismark Date: Fri, 2 Apr 2021 14:48:03 +0300 Subject: [PATCH 04/11] remove out of scope fix Signed-off-by: chrismark --- .../providers/kubernetessecrets/kubernetes_secrets.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go index 5588a546a295..4af00bc766e5 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetessecrets/kubernetes_secrets.go @@ -47,9 +47,6 @@ func ContextProviderBuilder(logger *logger.Logger, c *config.Config) (corecomp.C func (p *contextProviderK8sSecrets) Fetch(key string) (string, bool) { // key = "kubernetes_secrets.somenamespace.somesecret.value" - if p.client == nil { - return "", false - } tokens := strings.Split(key, ".") if len(tokens) > 0 && tokens[0] != "kubernetes_secrets" { return "", false From d3f90469f1564f28189c17fafc3932eb12cec70e Mon Sep 17 00:00:00 2001 From: chrismark Date: Mon, 5 Apr 2021 12:46:04 +0300 Subject: [PATCH 05/11] Fix leader holder identification Signed-off-by: chrismark --- .../kubernetes/elastic-agent-standalone-kubernetes.yml | 4 ++++ .../elastic-agent-standalone-daemonset.yaml | 4 ++++ .../kubernetes_leaderelection.go | 9 ++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml index 43d4f80c6d27..c79168d0dc87 100644 --- a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml +++ b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml @@ -39,6 +39,10 @@ spec: valueFrom: fieldRef: fieldPath: spec.nodeName + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name securityContext: runAsUser: 0 resources: diff --git a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-daemonset.yaml b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-daemonset.yaml index e97e07439263..f23e2cb1534b 100644 --- a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-daemonset.yaml +++ b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-daemonset.yaml @@ -38,6 +38,10 @@ spec: valueFrom: fieldRef: fieldPath: spec.nodeName + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name securityContext: runAsUser: 0 resources: diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go index ff774841a644..22fcaf49974c 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go @@ -6,6 +6,7 @@ package kubernetesleaderelection import ( "context" + "os" "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -62,7 +63,13 @@ func (p *dynamicProvider) Run(comm composable.DynamicProviderComm) error { if err != nil { return err } - id := "elastic-agent-leader-" + agentInfo.AgentID() + var id string + podName, found := os.LookupEnv("POD_NAME") + if found { + id = "elastic-agent-leader-" + podName + } else { + id = "elastic-agent-leader-" + agentInfo.AgentID() + } ns, err := kubernetes.InClusterNamespace() if err != nil { From 511cde31f06b960586ef363945f17f618839dfd2 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 7 Apr 2021 13:39:42 +0300 Subject: [PATCH 06/11] Fix k8s roles Signed-off-by: chrismark --- .../elastic-agent-standalone-kubernetes.yml | 28 +++++++++++++++++++ ...elastic-agent-standalone-role-binding.yaml | 14 ++++++++++ .../elastic-agent-standalone-role.yaml | 14 ++++++++++ deploy/kubernetes/metricbeat-kubernetes.yaml | 6 ---- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml index c79168d0dc87..2ba0d8c6d348 100644 --- a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml +++ b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml @@ -661,6 +661,34 @@ rules: verbs: - '*' --- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + namespace: kube-system + name: elastic-agent +subjects: + - kind: ServiceAccount + name: elastic-agent + namespace: kube-system +roleRef: + kind: Role + name: elastic-agent + apiGroup: rbac.authorization.k8s.io +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: elastic-agent + namespace: kube-system + labels: + k8s-app: elastic-agent +rules: + - apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: ["get", "create", "update"] +--- apiVersion: v1 kind: ServiceAccount metadata: diff --git a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role-binding.yaml b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role-binding.yaml index b352b2901d0d..f053b2463371 100644 --- a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role-binding.yaml +++ b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role-binding.yaml @@ -10,3 +10,17 @@ roleRef: kind: ClusterRole name: elastic-agent apiGroup: rbac.authorization.k8s.io +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + namespace: kube-system + name: elastic-agent +subjects: + - kind: ServiceAccount + name: elastic-agent + namespace: kube-system +roleRef: + kind: Role + name: elastic-agent + apiGroup: rbac.authorization.k8s.io diff --git a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml index 0a8002080c34..d9f61f12a6d7 100644 --- a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml +++ b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml @@ -39,3 +39,17 @@ rules: - leases verbs: - '*' +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: elastic-agent + namespace: kube-system + labels: + k8s-app: elastic-agent +rules: + - apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: ["get", "create", "update"] diff --git a/deploy/kubernetes/metricbeat-kubernetes.yaml b/deploy/kubernetes/metricbeat-kubernetes.yaml index ce685aa09753..87eedf13c4fc 100644 --- a/deploy/kubernetes/metricbeat-kubernetes.yaml +++ b/deploy/kubernetes/metricbeat-kubernetes.yaml @@ -270,12 +270,6 @@ rules: - "/metrics" verbs: - get -- apiGroups: - - coordination.k8s.io - resources: - - leases - verbs: - - '*' --- apiVersion: v1 kind: ServiceAccount From fd630f7274f2c62a71b1e3ec08924e175a6183f9 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 7 Apr 2021 13:49:05 +0300 Subject: [PATCH 07/11] Enable leaderelection by default and remove fields Signed-off-by: chrismark --- .../kubernetesleaderelection/config.go | 5 ++++ .../kubernetes_leaderelection.go | 26 ++----------------- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go index 3d7d27214b8e..a7f71cc32b5d 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/config.go @@ -13,3 +13,8 @@ type Config struct { // Name of the leaderelection lease LeaderLease string `config:"leader_lease"` } + +// InitDefaults initializes the default values for the config. +func (c *Config) InitDefaults() { + c.LeaderLease = "elastic-agent-cluster-leader" +} diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go index 22fcaf49974c..6afe814f4d9c 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go @@ -54,10 +54,6 @@ func (p *dynamicProvider) Run(comm composable.DynamicProviderComm) error { p.logger.Debugf("Kubernetes leaderelection provider skipped, unable to connect: %s", err) return nil } - if p.config.LeaderLease == "" { - p.logger.Debugf("Kubernetes leaderelection provider skipped, unable to define leader lease") - return nil - } agentInfo, err := info.NewAgentInfo() if err != nil { @@ -126,16 +122,7 @@ func (p *dynamicProvider) startLeading(metaUID string) { "leader": true, } - processors := []map[string]interface{}{ - { - "add_fields": map[string]interface{}{ - "fields": mapping, - "target": "kubernetes_leaderelection", - }, - }, - } - - p.comm.AddOrUpdate(metaUID, 0, mapping, processors) + p.comm.AddOrUpdate(metaUID, 0, mapping, nil) } func (p *dynamicProvider) stopLeading(metaUID string) { @@ -143,16 +130,7 @@ func (p *dynamicProvider) stopLeading(metaUID string) { "leader": false, } - processors := []map[string]interface{}{ - { - "add_fields": map[string]interface{}{ - "fields": mapping, - "target": "kubernetes_leaderelection", - }, - }, - } - - p.comm.AddOrUpdate(metaUID, 0, mapping, processors) + p.comm.AddOrUpdate(metaUID, 0, mapping, nil) } // Stop signals the stop channel to force the leader election loop routine to stop. From 04aac3614d9611f15b8fcb8873d425569a82bfd9 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 7 Apr 2021 14:06:03 +0300 Subject: [PATCH 08/11] Make provider of type ContextProvider instead of DynamicProvider Signed-off-by: chrismark --- .../kubernetes_leaderelection.go | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go index 6afe814f4d9c..acb5e732b824 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetesleaderelection/kubernetes_leaderelection.go @@ -18,23 +18,24 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + corecomp "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/composable" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) func init() { - composable.Providers.AddDynamicProvider("kubernetes_leaderelection", DynamicProviderBuilder) + composable.Providers.AddContextProvider("kubernetes_leaderelection", ContextProviderBuilder) } -type dynamicProvider struct { +type contextProvider struct { logger *logger.Logger config *Config - comm composable.DynamicProviderComm + comm corecomp.ContextProviderComm leaderElection *leaderelection.LeaderElectionConfig cancelLeaderElection context.CancelFunc } -// DynamicProviderBuilder builds the dynamic provider. -func DynamicProviderBuilder(logger *logger.Logger, c *config.Config) (composable.DynamicProvider, error) { +// ContextProviderBuilder builds the provider. +func ContextProviderBuilder(logger *logger.Logger, c *config.Config) (corecomp.ContextProvider, error) { var cfg Config if c == nil { c = config.New() @@ -43,11 +44,11 @@ func DynamicProviderBuilder(logger *logger.Logger, c *config.Config) (composable if err != nil { return nil, errors.New(err, "failed to unpack configuration") } - return &dynamicProvider{logger, &cfg, nil, nil, nil}, nil + return &contextProvider{logger, &cfg, nil, nil, nil}, nil } -// Run runs the leaderelection dynamic provider. -func (p *dynamicProvider) Run(comm composable.DynamicProviderComm) error { +// Run runs the leaderelection provider. +func (p *contextProvider) Run(comm corecomp.ContextProviderComm) error { client, err := kubernetes.GetKubernetesClient(p.config.KubeConfig) if err != nil { // info only; return nil (do nothing) @@ -108,7 +109,7 @@ func (p *dynamicProvider) Run(comm composable.DynamicProviderComm) error { } // startLeaderElector starts a Leader Elector in the background with the provided config -func (p *dynamicProvider) startLeaderElector(ctx context.Context) { +func (p *contextProvider) startLeaderElector(ctx context.Context) { le, err := leaderelection.NewLeaderElector(*p.leaderElection) if err != nil { p.logger.Errorf("error while creating Leader Elector: %v", err) @@ -117,24 +118,30 @@ func (p *dynamicProvider) startLeaderElector(ctx context.Context) { go le.Run(ctx) } -func (p *dynamicProvider) startLeading(metaUID string) { +func (p *contextProvider) startLeading(metaUID string) { mapping := map[string]interface{}{ "leader": true, } - p.comm.AddOrUpdate(metaUID, 0, mapping, nil) + err := p.comm.Set(mapping) + if err != nil { + p.logger.Errorf("Failed updating leaderelection status to leader TRUE: %s", err) + } } -func (p *dynamicProvider) stopLeading(metaUID string) { +func (p *contextProvider) stopLeading(metaUID string) { mapping := map[string]interface{}{ "leader": false, } - p.comm.AddOrUpdate(metaUID, 0, mapping, nil) + err := p.comm.Set(mapping) + if err != nil { + p.logger.Errorf("Failed updating leaderelection status to leader FALSE: %s", err) + } } // Stop signals the stop channel to force the leader election loop routine to stop. -func (p *dynamicProvider) Stop() { +func (p *contextProvider) Stop() { if p.cancelLeaderElection != nil { p.cancelLeaderElection() } From 69ea5b7a47dec0cc80faba7af969b07b94a13f83 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 7 Apr 2021 14:07:30 +0300 Subject: [PATCH 09/11] fix leftover role Signed-off-by: chrismark --- deploy/kubernetes/elastic-agent-standalone-kubernetes.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml index 2ba0d8c6d348..b541f11918a3 100644 --- a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml +++ b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yml @@ -654,12 +654,6 @@ rules: - "/metrics" verbs: - get - - apiGroups: - - coordination.k8s.io - resources: - - leases - verbs: - - '*' --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding From 1c1a4b9483de9b9914e81d93f990530dcb0a36b9 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 7 Apr 2021 14:08:52 +0300 Subject: [PATCH 10/11] fix leftover role Signed-off-by: chrismark --- .../elastic-agent-standalone-role.yaml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml index d9f61f12a6d7..506c2b4a846f 100644 --- a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml +++ b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml @@ -33,12 +33,6 @@ rules: - "/metrics" verbs: - get - - apiGroups: - - coordination.k8s.io - resources: - - leases - verbs: - - '*' --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role From 8f84f23689a8bea7f3f048b4b12b657362c151f0 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 7 Apr 2021 14:09:54 +0300 Subject: [PATCH 11/11] fix leftover role Signed-off-by: chrismark --- deploy/kubernetes/metricbeat-kubernetes.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/deploy/kubernetes/metricbeat-kubernetes.yaml b/deploy/kubernetes/metricbeat-kubernetes.yaml index 87eedf13c4fc..ce685aa09753 100644 --- a/deploy/kubernetes/metricbeat-kubernetes.yaml +++ b/deploy/kubernetes/metricbeat-kubernetes.yaml @@ -270,6 +270,12 @@ rules: - "/metrics" verbs: - get +- apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: + - '*' --- apiVersion: v1 kind: ServiceAccount