@@ -23,12 +23,6 @@ import (
2323 "sync"
2424 "time"
2525
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"
3226 authenticationv1 "k8s.io/api/authentication/v1"
3327 rbacv1 "k8s.io/api/rbac/v1"
3428 "k8s.io/apimachinery/pkg/api/equality"
@@ -40,8 +34,15 @@ import (
4034 "k8s.io/client-go/util/workqueue"
4135 "k8s.io/klog/v2"
4236
37+ kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
38+ kcprbacinformers "github.com/kcp-dev/client-go/informers/rbac/v1"
39+ kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
40+ kcprbaclisters "github.com/kcp-dev/client-go/listers/rbac/v1"
41+ "github.com/kcp-dev/logicalcluster/v3"
42+
4343 "github.com/kcp-dev/kcp/pkg/logging"
4444 "github.com/kcp-dev/kcp/pkg/reconciler/events"
45+ kcpmetrics "github.com/kcp-dev/kcp/pkg/server/metrics"
4546 corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
4647 tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
4748 corev1alpha1informers "github.com/kcp-dev/kcp/sdk/client/informers/externalversions/core/v1alpha1"
@@ -77,23 +78,31 @@ func NewController(
7778 }
7879
7980 _ , _ = logicalClusterInformer .Informer ().AddEventHandler (cache.ResourceEventHandlerFuncs {
80- AddFunc : func (obj interface {}) { c .enqueue (obj ) },
81- UpdateFunc : func (obj , _ interface {}) { c .enqueue (obj ) },
82- DeleteFunc : func (obj interface {}) { c .enqueue (obj ) },
81+ AddFunc : func (obj any ) {
82+ c .enqueue (obj )
83+ c .handleMetrics (obj )
84+ },
85+ UpdateFunc : func (oldObj , newObj any ) {
86+ c .enqueue (newObj )
87+ c .handleMetrics (newObj )
88+ },
89+ DeleteFunc : func (obj any ) {
90+ c .enqueue (obj )
91+ c .handleMetricsOnDelete (obj )
92+ },
8393 })
84-
8594 _ , _ = clusterRoleBindingInformer .Informer ().AddEventHandler (events .WithoutSyncs (cache.FilteringResourceEventHandler {
86- FilterFunc : func (obj interface {} ) bool {
95+ FilterFunc : func (obj any ) bool {
8796 crb , ok := obj .(* rbacv1.ClusterRoleBinding )
8897 if ! ok {
8998 return false
9099 }
91100 return crb .Name == workspaceAdminClusterRoleBindingName
92101 },
93102 Handler : cache.ResourceEventHandlerFuncs {
94- AddFunc : func (obj interface {} ) { c .enqueueCRB (obj ) },
95- UpdateFunc : func (obj , _ interface {} ) { c .enqueueCRB (obj ) },
96- DeleteFunc : func (obj interface {} ) { c .enqueueCRB (obj ) },
103+ AddFunc : func (obj any ) { c .enqueueCRB (obj ) },
104+ UpdateFunc : func (obj , _ any ) { c .enqueueCRB (obj ) },
105+ DeleteFunc : func (obj any ) { c .enqueueCRB (obj ) },
97106 },
98107 }))
99108
@@ -114,7 +123,7 @@ type Controller struct {
114123 shardName string
115124}
116125
117- func (c * Controller ) enqueue (obj interface {} ) {
126+ func (c * Controller ) enqueue (obj any ) {
118127 key , err := kcpcache .DeletionHandlingMetaClusterNamespaceKeyFunc (obj )
119128 if err != nil {
120129 utilruntime .HandleError (err )
@@ -125,7 +134,7 @@ func (c *Controller) enqueue(obj interface{}) {
125134 c .queue .Add (key )
126135}
127136
128- func (c * Controller ) enqueueCRB (obj interface {} ) {
137+ func (c * Controller ) enqueueCRB (obj any ) {
129138 key , err := kcpcache .DeletionHandlingMetaClusterNamespaceKeyFunc (obj )
130139 if err != nil {
131140 utilruntime .HandleError (err )
@@ -195,14 +204,6 @@ func (c *Controller) process(ctx context.Context, key string) error {
195204 logicalCluster , err := c .logicalClusterLister .Cluster (clusterName ).Get (corev1alpha1 .LogicalClusterName )
196205 if err != nil {
197206 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 {
206207 logger .Error (err , "failed to get LogicalCluster from lister" , "cluster" , clusterName )
207208 }
208209 return nil
@@ -216,18 +217,6 @@ func (c *Controller) process(ctx context.Context, key string) error {
216217 return nil
217218 }
218219
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-
231220 // need to create ClusterRoleBinding for owner.
232221 ownerAnnotation := logicalCluster .Annotations [tenancyv1alpha1 .ExperimentalWorkspaceOwnerAnnotationKey ]
233222 // some older installations of kcp might have produced an annotation with empty value, so we should
@@ -279,3 +268,42 @@ func (c *Controller) process(ctx context.Context, key string) error {
279268 _ , err = c .kubeClusterClient .Cluster (clusterName .Path ()).RbacV1 ().ClusterRoleBindings ().Update (ctx , newBinding , metav1.UpdateOptions {})
280269 return err
281270}
271+
272+ func (c * Controller ) handleMetrics (obj any ) {
273+ logicalCluster , ok := obj .(* corev1alpha1.LogicalCluster )
274+ if ! ok {
275+ return
276+ }
277+
278+ if logicalCluster .Status .Phase == corev1alpha1 .LogicalClusterPhaseReady {
279+ c .mu .Lock ()
280+ clusterKey := string (logicalcluster .From (logicalCluster ))
281+ if ! c .countedClusters [clusterKey ] {
282+ c .countedClusters [clusterKey ] = true
283+ kcpmetrics .IncrementLogicalClusterCount (c .shardName )
284+ }
285+ c .mu .Unlock ()
286+ }
287+ }
288+
289+ func (c * Controller ) handleMetricsOnDelete (obj any ) {
290+ logicalCluster , ok := obj .(* corev1alpha1.LogicalCluster )
291+ if ! ok {
292+ if tombstone , ok := obj .(cache.DeletedFinalStateUnknown ); ok {
293+ logicalCluster , ok = tombstone .Obj .(* corev1alpha1.LogicalCluster )
294+ if ! ok {
295+ return
296+ }
297+ } else {
298+ return
299+ }
300+ }
301+
302+ c .mu .Lock ()
303+ clusterKey := string (logicalcluster .From (logicalCluster ))
304+ if c .countedClusters [clusterKey ] {
305+ delete (c .countedClusters , clusterKey )
306+ kcpmetrics .DecrementLogicalClusterCount (c .shardName )
307+ }
308+ c .mu .Unlock ()
309+ }
0 commit comments