Skip to content

Commit

Permalink
fix(cluster): remove raleted metrics label when cluster is deleted
Browse files Browse the repository at this point in the history
Signed-off-by: chang.qiangqiang <[email protected]>
  • Loading branch information
CharlesQQ committed Nov 23, 2024
1 parent 88c3e44 commit cadc62a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pkg/metrics/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit cadc62a

Please sign in to comment.