From a993c00790b226c79269b25fddde83a217bbaf34 Mon Sep 17 00:00:00 2001 From: Zach Zhu Date: Sat, 14 May 2022 13:40:41 +0800 Subject: [PATCH 1/2] tweak comment of the workqueue metrics file --- pkg/metrics/workqueue.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/metrics/workqueue.go b/pkg/metrics/workqueue.go index 8ca47235da..277b878810 100644 --- a/pkg/metrics/workqueue.go +++ b/pkg/metrics/workqueue.go @@ -21,8 +21,8 @@ import ( "k8s.io/client-go/util/workqueue" ) -// This file is copied and adapted from k8s.io/kubernetes/pkg/util/workqueue/prometheus -// which registers metrics to the default prometheus Registry. We require very +// This file is copied and adapted from k8s.io/component-base/metrics/prometheus/workqueue +// which registers metrics to the k8s legacy Registry. We require very // similar functionality, but must register metrics to a different Registry. // Metrics subsystem and all keys used by the workqueue. From 97515191a447732e05a5af2413767ee5fe16d3bd Mon Sep 17 00:00:00 2001 From: Zach Zhu Date: Sat, 14 May 2022 13:43:23 +0800 Subject: [PATCH 2/2] enable metrics of clientgo leader election --- pkg/manager/internal.go | 4 ++++ pkg/manager/manager.go | 1 + pkg/metrics/leaderelection.go | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 pkg/metrics/leaderelection.go diff --git a/pkg/manager/internal.go b/pkg/manager/internal.go index fb79c55441..073d252718 100644 --- a/pkg/manager/internal.go +++ b/pkg/manager/internal.go @@ -144,6 +144,9 @@ type controllerManager struct { // webhookServer if unset, and Add() it to controllerManager. webhookServerOnce sync.Once + // leaderElectionID is the name of the resource that leader election + // will use for holding the leader lock. + leaderElectionID string // leaseDuration is the duration that non-leader candidates will // wait to force acquire leadership. leaseDuration time.Duration @@ -637,6 +640,7 @@ func (cm *controllerManager) startLeaderElection(ctx context.Context) (err error }, }, ReleaseOnCancel: cm.leaderElectionReleaseOnCancel, + Name: cm.leaderElectionID, }) if err != nil { return err diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 072919058a..2facb1c915 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -439,6 +439,7 @@ func New(config *rest.Config, options Options) (Manager, error) { certDir: options.CertDir, tlsOpts: options.TLSOpts, webhookServer: options.WebhookServer, + leaderElectionID: options.LeaderElectionID, leaseDuration: *options.LeaseDuration, renewDeadline: *options.RenewDeadline, retryPeriod: *options.RetryPeriod, diff --git a/pkg/metrics/leaderelection.go b/pkg/metrics/leaderelection.go new file mode 100644 index 0000000000..a19c099602 --- /dev/null +++ b/pkg/metrics/leaderelection.go @@ -0,0 +1,40 @@ +package metrics + +import ( + "github.com/prometheus/client_golang/prometheus" + "k8s.io/client-go/tools/leaderelection" +) + +// This file is copied and adapted from k8s.io/component-base/metrics/prometheus/clientgo/leaderelection +// which registers metrics to the k8s legacy Registry. We require very +// similar functionality, but must register metrics to a different Registry. + +var ( + leaderGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "leader_election_master_status", + Help: "Gauge of if the reporting system is master of the relevant lease, 0 indicates backup, 1 indicates master. 'name' is the string used to identify the lease. Please make sure to group by name.", + }, []string{"name"}) +) + +func init() { + Registry.MustRegister(leaderGauge) + leaderelection.SetProvider(leaderelectionMetricsProvider{}) +} + +type leaderelectionMetricsProvider struct{} + +func (leaderelectionMetricsProvider) NewLeaderMetric() leaderelection.SwitchMetric { + return &switchAdapter{gauge: leaderGauge} +} + +type switchAdapter struct { + gauge *prometheus.GaugeVec +} + +func (s *switchAdapter) On(name string) { + s.gauge.WithLabelValues(name).Set(1.0) +} + +func (s *switchAdapter) Off(name string) { + s.gauge.WithLabelValues(name).Set(0.0) +}