Skip to content

Commit

Permalink
add kruise_manager_is_leader metric
Browse files Browse the repository at this point in the history
Signed-off-by: hatowang <[email protected]>

add leader metric for kruise_manager.
  • Loading branch information
wangyeyu.wyy authored and hatowang committed Mar 3, 2022
1 parent 7722528 commit 4c5fad6
Show file tree
Hide file tree
Showing 4 changed files with 422 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
appsv1beta1 "github.com/openkruise/kruise/apis/apps/v1beta1"
policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1"
"github.com/openkruise/kruise/pkg/controller"
_ "github.com/openkruise/kruise/pkg/util/metrics/leadership"
// +kubebuilder:scaffold:imports
)

Expand Down
44 changes: 44 additions & 0 deletions pkg/util/metrics/leadership/leader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package leadership

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"k8s.io/client-go/tools/leaderelection"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var leaderMetric = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "kruise_manager_is_leader",
Help: "Gauge the leader of kruise-manager",
},
)

func init() {
metrics.Registry.MustRegister(leaderMetric)
leaderelection.SetProvider(newMetricProvider())
}

func newMetricProvider() leaderelection.MetricsProvider {
return metricsProvider{}
}

type metricsProvider struct{}
type switchMetric struct{}

var (
_ leaderelection.MetricsProvider = metricsProvider{}
_ leaderelection.SwitchMetric = switchMetric{}
)

func (_ metricsProvider) NewLeaderMetric() leaderelection.SwitchMetric {
return switchMetric{}
}

func (_ switchMetric) On(_ string) {
leaderMetric.Set(1)
}

func (s switchMetric) Off(string) {
leaderMetric.Set(0)
}
Loading

0 comments on commit 4c5fad6

Please sign in to comment.