Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cluster): remove raleted metrics label when cluster is deleted #5866

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion 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 @@ -238,8 +239,13 @@ func (c *Controller) syncCluster(ctx context.Context, cluster *clusterv1alpha1.C
return c.ensureFinalizer(ctx, cluster)
}

func (c *Controller) removeCluster(ctx context.Context, cluster *clusterv1alpha1.Cluster) (controllerruntime.Result, error) {
func (c *Controller) removeCluster(ctx context.Context, cluster *clusterv1alpha1.Cluster) (ret controllerruntime.Result, retErr error) {
// add terminating taint before cluster is deleted
defer func() {
if retErr == nil {
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