Skip to content

Commit 37ba8e9

Browse files
committed
add phase label for count metric and process it
Signed-off-by: Karol Szwaj <[email protected]> On-behalf-of: @SAP [email protected]
1 parent 9ccda86 commit 37ba8e9

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

pkg/reconciler/tenancy/logicalcluster/logicalcluster_controller.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewController(
7474
logicalClusterLister: logicalClusterInformer.Lister(),
7575
clusterRoleBindingLister: clusterRoleBindingInformer.Lister(),
7676
shardName: shardName,
77-
countedClusters: make(map[string]bool),
77+
countedClusters: make(map[string]string),
7878
}
7979

8080
_, _ = logicalClusterInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
@@ -84,7 +84,7 @@ func NewController(
8484
},
8585
UpdateFunc: func(oldObj, newObj any) {
8686
c.enqueue(newObj)
87-
c.handleMetrics(newObj)
87+
c.handleMetricsOnUpdate(oldObj, newObj)
8888
},
8989
DeleteFunc: func(obj any) {
9090
c.enqueue(obj)
@@ -119,7 +119,7 @@ type Controller struct {
119119

120120
clusterRoleBindingLister kcprbaclisters.ClusterRoleBindingClusterLister
121121
mu sync.Mutex
122-
countedClusters map[string]bool
122+
countedClusters map[string]string
123123
shardName string
124124
}
125125

@@ -276,12 +276,45 @@ func (c *Controller) handleMetrics(obj any) {
276276
}
277277

278278
c.mu.Lock()
279+
defer c.mu.Unlock()
280+
279281
clusterKey := string(logicalcluster.From(logicalCluster))
280-
if !c.countedClusters[clusterKey] {
281-
c.countedClusters[clusterKey] = true
282-
kcpmetrics.IncrementLogicalClusterCount(c.shardName)
282+
phase := string(logicalCluster.Status.Phase)
283+
if _, exists := c.countedClusters[clusterKey]; !exists {
284+
c.countedClusters[clusterKey] = phase
285+
if phase != "" {
286+
kcpmetrics.IncrementLogicalClusterCount(c.shardName, phase)
287+
}
288+
}
289+
}
290+
291+
func (c *Controller) handleMetricsOnUpdate(oldObj, newObj any) {
292+
oldLogicalCluster, ok := oldObj.(*corev1alpha1.LogicalCluster)
293+
if !ok {
294+
return
295+
}
296+
297+
newLogicalCluster, ok := newObj.(*corev1alpha1.LogicalCluster)
298+
if !ok {
299+
return
300+
}
301+
302+
c.mu.Lock()
303+
defer c.mu.Unlock()
304+
305+
clusterKey := string(logicalcluster.From(newLogicalCluster))
306+
oldPhase := string(oldLogicalCluster.Status.Phase)
307+
newPhase := string(newLogicalCluster.Status.Phase)
308+
309+
if oldPhase != newPhase {
310+
if oldPhase != "" {
311+
kcpmetrics.DecrementLogicalClusterCount(c.shardName, oldPhase)
312+
}
313+
if newPhase != "" {
314+
kcpmetrics.IncrementLogicalClusterCount(c.shardName, newPhase)
315+
}
316+
c.countedClusters[clusterKey] = newPhase
283317
}
284-
c.mu.Unlock()
285318
}
286319

287320
func (c *Controller) handleMetricsOnDelete(obj any) {
@@ -298,10 +331,13 @@ func (c *Controller) handleMetricsOnDelete(obj any) {
298331
}
299332

300333
c.mu.Lock()
334+
defer c.mu.Unlock()
335+
301336
clusterKey := string(logicalcluster.From(logicalCluster))
302-
if c.countedClusters[clusterKey] {
337+
if phase, exists := c.countedClusters[clusterKey]; exists {
303338
delete(c.countedClusters, clusterKey)
304-
kcpmetrics.DecrementLogicalClusterCount(c.shardName)
339+
if phase != "" {
340+
kcpmetrics.DecrementLogicalClusterCount(c.shardName, phase)
341+
}
305342
}
306-
c.mu.Unlock()
307343
}

pkg/server/metrics/metrics.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ var (
2525
logicalClusterCount = metrics.NewGaugeVec(
2626
&metrics.GaugeOpts{
2727
Name: "kcp_logicalcluster_count",
28-
Help: "Number of logical clusters currently running on this shard.",
28+
Help: "Number of logical clusters currently running with specific phases on this shard.",
2929
StabilityLevel: metrics.ALPHA,
3030
},
31-
[]string{"shard"},
31+
[]string{"shard", "phase"},
3232
)
3333
)
3434

3535
func init() {
3636
legacyregistry.MustRegister(logicalClusterCount)
3737
}
3838

39-
// IncrementLogicalClusterCount increments the count for the given shard.
40-
func IncrementLogicalClusterCount(shardName string) {
41-
logicalClusterCount.WithLabelValues(shardName).Inc()
39+
// IncrementLogicalClusterCount increments the count for the given shard and phase.
40+
func IncrementLogicalClusterCount(shardName string, phase string) {
41+
logicalClusterCount.WithLabelValues(shardName, phase).Inc()
4242
}
4343

44-
// DecrementLogicalClusterCount decrements the count for the given shard.
45-
func DecrementLogicalClusterCount(shardName string) {
46-
logicalClusterCount.WithLabelValues(shardName).Dec()
44+
// DecrementLogicalClusterCount decrements the count for the given shard and phase.
45+
func DecrementLogicalClusterCount(shardName string, phase string) {
46+
logicalClusterCount.WithLabelValues(shardName, phase).Dec()
4747
}

0 commit comments

Comments
 (0)