From 58887bc0782d706237ae5da197ca8000fa14b165 Mon Sep 17 00:00:00 2001 From: "chang.qiangqiang" Date: Fri, 14 Feb 2025 10:17:34 +0800 Subject: [PATCH] fix(cluster): remove related metrics label after cluster is deleted Signed-off-by: chang.qiangqiang --- .../status/cluster_status_controller.go | 1 + pkg/metrics/cluster.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/controllers/status/cluster_status_controller.go b/pkg/controllers/status/cluster_status_controller.go index 8ee541659..e3e361723 100644 --- a/pkg/controllers/status/cluster_status_controller.go +++ b/pkg/controllers/status/cluster_status_controller.go @@ -135,6 +135,7 @@ func (c *ClusterStatusController) Reconcile(ctx context.Context, req controllerr c.GenericInformerManager.Stop(req.NamespacedName.Name) c.TypedInformerManager.Stop(req.NamespacedName.Name) c.clusterConditionCache.delete(req.NamespacedName.Name) + metrics.CleanupMetricsForCluster(req.NamespacedName.Name) // stop lease controller after the cluster is gone. // only used for clusters in Pull mode because no need to set up lease syncing for Push clusters. diff --git a/pkg/metrics/cluster.go b/pkg/metrics/cluster.go index 3078d7147..50fe02c69 100644 --- a/pkg/metrics/cluster.go +++ b/pkg/metrics/cluster.go @@ -135,6 +135,20 @@ func RecordClusterSyncStatusDuration(cluster *v1alpha1.Cluster, startTime time.T clusterSyncStatusDuration.WithLabelValues(cluster.Name).Observe(utilmetrics.DurationInSeconds(startTime)) } +// CleanupMetricsForCluster removes the cluster status metrics after the cluster is deleted. +func CleanupMetricsForCluster(clusterName string) { + clusterReadyGauge.DeleteLabelValues(clusterName) + clusterTotalNodeNumberGauge.DeleteLabelValues(clusterName) + clusterReadyNodeNumberGauge.DeleteLabelValues(clusterName) + clusterMemoryAllocatableGauge.DeleteLabelValues(clusterName) + clusterCPUAllocatableGauge.DeleteLabelValues(clusterName) + clusterPodAllocatableGauge.DeleteLabelValues(clusterName) + clusterMemoryAllocatedGauge.DeleteLabelValues(clusterName) + clusterCPUAllocatedGauge.DeleteLabelValues(clusterName) + clusterPodAllocatedGauge.DeleteLabelValues(clusterName) + clusterSyncStatusDuration.DeleteLabelValues(clusterName) +} + // ClusterCollectors returns the collectors about clusters. func ClusterCollectors() []prometheus.Collector { return []prometheus.Collector{