From cadc62a2ebd14fc9775e92cf5df647155a5b3ac8 Mon Sep 17 00:00:00 2001 From: "chang.qiangqiang" Date: Sat, 23 Nov 2024 23:55:57 +0800 Subject: [PATCH] fix(cluster): remove raleted metrics label when cluster is deleted Signed-off-by: chang.qiangqiang --- pkg/controllers/cluster/cluster_controller.go | 4 ++++ pkg/metrics/cluster.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/controllers/cluster/cluster_controller.go b/pkg/controllers/cluster/cluster_controller.go index 0c0b4bdf6f2f..218bbf918c47 100644 --- a/pkg/controllers/cluster/cluster_controller.go +++ b/pkg/controllers/cluster/cluster_controller.go @@ -45,6 +45,7 @@ import ( workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" "github.com/karmada-io/karmada/pkg/events" "github.com/karmada-io/karmada/pkg/features" + "github.com/karmada-io/karmada/pkg/metrics" "github.com/karmada-io/karmada/pkg/util" utilhelper "github.com/karmada-io/karmada/pkg/util/helper" "github.com/karmada-io/karmada/pkg/util/names" @@ -240,6 +241,9 @@ func (c *Controller) syncCluster(ctx context.Context, cluster *clusterv1alpha1.C func (c *Controller) removeCluster(ctx context.Context, cluster *clusterv1alpha1.Cluster) (controllerruntime.Result, error) { // add terminating taint before cluster is deleted + defer func() { + metrics.RemoveClusterStatus(cluster) + }() if err := c.updateClusterTaints(ctx, []*corev1.Taint{TerminatingTaintTemplate}, nil, cluster); err != nil { klog.ErrorS(err, "Failed to update terminating taint", "cluster", cluster.Name) return controllerruntime.Result{}, err diff --git a/pkg/metrics/cluster.go b/pkg/metrics/cluster.go index 3078d71476dd..7fe3766ee0b5 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)) } +// RemoveClusterStatus removes the cluster status metrics when the cluster is deleted. +func RemoveClusterStatus(cluster *v1alpha1.Cluster) { + clusterReadyGauge.DeleteLabelValues(cluster.Name) + clusterTotalNodeNumberGauge.DeleteLabelValues(cluster.Name) + clusterReadyNodeNumberGauge.DeleteLabelValues(cluster.Name) + clusterMemoryAllocatableGauge.DeleteLabelValues(cluster.Name) + clusterCPUAllocatableGauge.DeleteLabelValues(cluster.Name) + clusterPodAllocatableGauge.DeleteLabelValues(cluster.Name) + clusterMemoryAllocatedGauge.DeleteLabelValues(cluster.Name) + clusterCPUAllocatedGauge.DeleteLabelValues(cluster.Name) + clusterPodAllocatedGauge.DeleteLabelValues(cluster.Name) + clusterSyncStatusDuration.DeleteLabelValues(cluster.Name) +} + // ClusterCollectors returns the collectors about clusters. func ClusterCollectors() []prometheus.Collector { return []prometheus.Collector{