@@ -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
287320func (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}
0 commit comments