Skip to content

Commit 25379d1

Browse files
fead: add lease monitor (#78)
* add feat lease monitor * add comment * rename runnable * rename runnable * add log * add log * add log * add log * add log * add license
1 parent 883cc7d commit 25379d1

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

controller/runnable/initializer.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2025 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package runnable
18+
19+
import (
20+
"github.com/go-logr/logr"
21+
controllerruntime "sigs.k8s.io/controller-runtime"
22+
"sigs.k8s.io/controller-runtime/pkg/manager"
23+
24+
"kusionstack.io/kube-utils/metrics"
25+
)
26+
27+
var runnable []manager.Runnable
28+
29+
// InitializeRunnable initialize runnable for manager
30+
func InitializeRunnable(mgr manager.Manager, opts controllerruntime.Options) error {
31+
// register leader metrics
32+
metrics.RegisterLeaderRunningMetrics()
33+
// register runnable
34+
addRunnable(opts, mgr.GetLogger())
35+
for _, runnable := range runnable {
36+
if err := mgr.Add(runnable); err != nil {
37+
return err
38+
}
39+
}
40+
return nil
41+
}
42+
43+
func addRunnable(opts controllerruntime.Options, logger logr.Logger) {
44+
runnable = append(runnable,
45+
&LeaderMetricsRunnable{leaseName: opts.LeaderElectionID, logger: logger},
46+
&NoneLeaderMetricRunnable{enableLeaderElection: opts.LeaderElection, leaseName: opts.LeaderElectionID, logger: logger},
47+
)
48+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2025 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package runnable
18+
19+
import (
20+
"context"
21+
22+
"github.com/go-logr/logr"
23+
24+
"kusionstack.io/kube-utils/metrics"
25+
)
26+
27+
// LeaderMetricsRunnable is a runnable that records the leader metrics
28+
type LeaderMetricsRunnable struct {
29+
logger logr.Logger
30+
leaseName string
31+
}
32+
33+
func (l LeaderMetricsRunnable) NeedLeaderElection() bool {
34+
return true
35+
}
36+
37+
func (l LeaderMetricsRunnable) Start(_ context.Context) error {
38+
m := metrics.LeaderRunningMetrics{Lease: l.leaseName}
39+
m.Lead()
40+
l.logger.Info("leader election enabled, start LeaderMetricsRunnable, record leader metrics")
41+
return nil
42+
}
43+
44+
// NoneLeaderMetricRunnable is a runnable that records the none leader metrics
45+
type NoneLeaderMetricRunnable struct {
46+
logger logr.Logger
47+
leaseName string
48+
enableLeaderElection bool
49+
}
50+
51+
func (nl NoneLeaderMetricRunnable) NeedLeaderElection() bool {
52+
return false
53+
}
54+
55+
func (nl NoneLeaderMetricRunnable) Start(_ context.Context) error {
56+
m := metrics.LeaderRunningMetrics{Lease: nl.leaseName}
57+
if nl.enableLeaderElection {
58+
m.UnLead()
59+
nl.logger.Info("leader election enabled, start NoneLeaderMetricRunnable, record none leader metrics")
60+
} else {
61+
m.Lead()
62+
nl.logger.Info("leader election disabled, start NoneLeaderMetricRunnable, record leader metrics")
63+
}
64+
return nil
65+
}

metrics/leader_election.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2025 The KusionStack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
"github.com/prometheus/client_golang/prometheus"
21+
"sigs.k8s.io/controller-runtime/pkg/metrics"
22+
)
23+
24+
var leaderRunningCount = "leader_running_gauge"
25+
26+
var leaderRunningGaugeVec = prometheus.NewGaugeVec(
27+
prometheus.GaugeOpts{
28+
Subsystem: ReconcileSubSystem,
29+
Name: leaderRunningCount,
30+
Help: "leader controller set gauge to 1, otherwise set to 0",
31+
},
32+
[]string{"Lease"},
33+
)
34+
35+
func RegisterLeaderRunningMetrics() {
36+
metrics.Registry.MustRegister(leaderRunningGaugeVec)
37+
}
38+
39+
type LeaderRunningMetrics struct {
40+
Lease string
41+
}
42+
43+
func (m *LeaderRunningMetrics) Lead() {
44+
leaderRunningGaugeVec.WithLabelValues(m.Lease).Set(1)
45+
}
46+
47+
func (m *LeaderRunningMetrics) UnLead() {
48+
leaderRunningGaugeVec.WithLabelValues(m.Lease).Set(0)
49+
}

0 commit comments

Comments
 (0)