@@ -38,6 +38,8 @@ import (
38
38
"k8s.io/klog/v2"
39
39
40
40
fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"
41
+ "github.com/kubewharf/kubeadmiral/pkg/stats"
42
+ clusterutil "github.com/kubewharf/kubeadmiral/pkg/util/cluster"
41
43
)
42
44
43
45
const (
@@ -63,6 +65,10 @@ func (c *FederatedClusterController) collectIndividualClusterStatus(
63
65
ctx context.Context ,
64
66
cluster * fedcorev1a1.FederatedCluster ,
65
67
) (retryAfter time.Duration , err error ) {
68
+ startTime := time .Now ()
69
+ defer func () {
70
+ c .recordClusterStatus (cluster , startTime )
71
+ }()
66
72
logger := klog .FromContext (ctx )
67
73
68
74
clusterKubeClient , exists := c .federatedInformerManager .GetClusterKubeClient (cluster .Name )
@@ -308,3 +314,57 @@ func shouldCollectClusterStatus(cluster *fedcorev1a1.FederatedCluster, collectIn
308
314
nextCollectTime := readyCond .LastProbeTime .Time .Add (collectInterval )
309
315
return time .Now ().After (nextCollectTime )
310
316
}
317
+
318
+ func (c * FederatedClusterController ) recordClusterStatus (cluster * fedcorev1a1.FederatedCluster , startTime time.Time ) {
319
+ if clusterutil .IsClusterReady (& cluster .Status ) {
320
+ c .metrics .Store ("cluster_ready_state" ,
321
+ 1 ,
322
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
323
+ } else {
324
+ c .metrics .Store ("cluster_ready_state" ,
325
+ 0 ,
326
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
327
+ }
328
+ if clusterutil .IsClusterOffline (& cluster .Status ) {
329
+ c .metrics .Store ("cluster_offline_state" ,
330
+ 1 ,
331
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
332
+ } else {
333
+ c .metrics .Store ("cluster_offline_state" ,
334
+ 0 ,
335
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
336
+ }
337
+ if clusterutil .IsClusterJoined (& cluster .Status ) {
338
+ c .metrics .Store ("cluster_joined_state" ,
339
+ 1 ,
340
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
341
+ } else {
342
+ c .metrics .Store ("cluster_joined_state" ,
343
+ 0 ,
344
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
345
+ }
346
+ c .metrics .Duration ("cluster_sync_status_duration" ,
347
+ startTime ,
348
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
349
+ if cluster .Status .Resources .Allocatable != nil {
350
+ c .metrics .Store ("cluster_memory_allocatable_bytes" ,
351
+ cluster .Status .Resources .Allocatable .Memory ().AsApproximateFloat64 (),
352
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
353
+ c .metrics .Store ("cluster_cpu_allocatable_number" ,
354
+ cluster .Status .Resources .Allocatable .Cpu ().AsApproximateFloat64 (),
355
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
356
+ }
357
+ if cluster .Status .Resources .Available != nil {
358
+ c .metrics .Store ("cluster_memory_available_bytes" ,
359
+ cluster .Status .Resources .Available .Memory ().AsApproximateFloat64 (),
360
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
361
+ c .metrics .Store ("cluster_cpu_available_number" ,
362
+ cluster .Status .Resources .Available .Cpu ().AsApproximateFloat64 (),
363
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
364
+ }
365
+ if cluster .Status .Resources .SchedulableNodes != nil {
366
+ c .metrics .Store ("cluster_schedulable_nodes_total" ,
367
+ * cluster .Status .Resources .SchedulableNodes ,
368
+ stats.Tag {Name : "cluster_name" , Value : cluster .Name })
369
+ }
370
+ }
0 commit comments