@@ -20,8 +20,15 @@ import (
2020 "context"
2121 "encoding/json"
2222 "fmt"
23+ "sync"
2324 "time"
2425
26+ kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
27+ kcprbacinformers "github.com/kcp-dev/client-go/informers/rbac/v1"
28+ kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
29+ kcprbaclisters "github.com/kcp-dev/client-go/listers/rbac/v1"
30+ kcpmetrics "github.com/kcp-dev/kcp/pkg/server/metrics"
31+ "github.com/kcp-dev/logicalcluster/v3"
2532 authenticationv1 "k8s.io/api/authentication/v1"
2633 rbacv1 "k8s.io/api/rbac/v1"
2734 "k8s.io/apimachinery/pkg/api/equality"
@@ -33,11 +40,6 @@ import (
3340 "k8s.io/client-go/util/workqueue"
3441 "k8s.io/klog/v2"
3542
36- kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
37- kcprbacinformers "github.com/kcp-dev/client-go/informers/rbac/v1"
38- kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
39- kcprbaclisters "github.com/kcp-dev/client-go/listers/rbac/v1"
40-
4143 "github.com/kcp-dev/kcp/pkg/logging"
4244 "github.com/kcp-dev/kcp/pkg/reconciler/events"
4345 corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
@@ -58,6 +60,7 @@ func NewController(
5860 kubeClusterClient kcpkubernetesclientset.ClusterInterface ,
5961 logicalClusterInformer corev1alpha1informers.LogicalClusterClusterInformer ,
6062 clusterRoleBindingInformer kcprbacinformers.ClusterRoleBindingClusterInformer ,
63+ shardName string ,
6164) * Controller {
6265 c := & Controller {
6366 queue : workqueue .NewTypedRateLimitingQueueWithConfig (
@@ -69,6 +72,8 @@ func NewController(
6972 kubeClusterClient : kubeClusterClient ,
7073 logicalClusterLister : logicalClusterInformer .Lister (),
7174 clusterRoleBindingLister : clusterRoleBindingInformer .Lister (),
75+ shardName : shardName ,
76+ countedClusters : make (map [string ]bool ),
7277 }
7378
7479 _ , _ = logicalClusterInformer .Informer ().AddEventHandler (cache.ResourceEventHandlerFuncs {
@@ -104,6 +109,9 @@ type Controller struct {
104109 logicalClusterLister corev1alpha1listers.LogicalClusterClusterLister
105110
106111 clusterRoleBindingLister kcprbaclisters.ClusterRoleBindingClusterLister
112+ mu sync.Mutex
113+ countedClusters map [string ]bool
114+ shardName string
107115}
108116
109117func (c * Controller ) enqueue (obj interface {}) {
@@ -186,11 +194,18 @@ func (c *Controller) process(ctx context.Context, key string) error {
186194
187195 logicalCluster , err := c .logicalClusterLister .Cluster (clusterName ).Get (corev1alpha1 .LogicalClusterName )
188196 if err != nil {
189- if ! apierrors .IsNotFound (err ) {
197+ if apierrors .IsNotFound (err ) {
198+ c .mu .Lock ()
199+ if c .countedClusters [clusterName .String ()] {
200+ delete (c .countedClusters , clusterName .String ())
201+ kcpmetrics .DecrementLogicalClusterCount (c .shardName )
202+ logger .V (4 ).Info ("LogicalCluster deleted, decremented metrics" , "cluster" , clusterName )
203+ }
204+ c .mu .Unlock ()
205+ } else {
190206 logger .Error (err , "failed to get LogicalCluster from lister" , "cluster" , clusterName )
191207 }
192-
193- return nil // nothing we can do here
208+ return nil
194209 }
195210
196211 logger = logging .WithObject (logger , logicalCluster )
@@ -201,6 +216,18 @@ func (c *Controller) process(ctx context.Context, key string) error {
201216 return nil
202217 }
203218
219+ c .mu .Lock ()
220+ clusterKey := string (logicalcluster .From (logicalCluster ))
221+ alreadyCounted := c .countedClusters [clusterKey ]
222+ if logicalCluster .Status .Phase == corev1alpha1 .LogicalClusterPhaseReady && ! alreadyCounted {
223+ c .countedClusters [clusterKey ] = true
224+ kcpmetrics .IncrementLogicalClusterCount (c .shardName )
225+ } else if logicalCluster .Status .Phase != corev1alpha1 .LogicalClusterPhaseReady && alreadyCounted {
226+ delete (c .countedClusters , clusterKey )
227+ kcpmetrics .DecrementLogicalClusterCount (c .shardName )
228+ }
229+ c .mu .Unlock ()
230+
204231 // need to create ClusterRoleBinding for owner.
205232 ownerAnnotation := logicalCluster .Annotations [tenancyv1alpha1 .ExperimentalWorkspaceOwnerAnnotationKey ]
206233 // some older installations of kcp might have produced an annotation with empty value, so we should
0 commit comments