-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hatowang <[email protected]> add leader metric for kruise_manager.
- Loading branch information
Showing
4 changed files
with
422 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.