Browse Source

Remove metrics when a resource has been deleted (#2576)

Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>
Shuhei Kitagawa 2 years ago
parent
commit
6afdba88c9

+ 12 - 0
pkg/controllers/clusterexternalsecret/cesmetrics/cesmetrics.go

@@ -110,3 +110,15 @@ func UpdateClusterExternalSecretCondition(ces *esv1beta1.ClusterExternalSecret,
 			}))
 	}
 }
+
+// RemoveMetrics deletes all metrics published by the resource.
+func RemoveMetrics(namespace, name string) {
+	for _, gaugeVecMetric := range gaugeVecMetrics {
+		gaugeVecMetric.DeletePartialMatch(
+			map[string]string{
+				"namespace": namespace,
+				"name":      name,
+			},
+		)
+	}
+}

+ 1 - 0
pkg/controllers/clusterexternalsecret/clusterexternalsecret_controller.go

@@ -73,6 +73,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
 	err := r.Get(ctx, req.NamespacedName, &clusterExternalSecret)
 	if err != nil {
 		if apierrors.IsNotFound(err) {
+			cesmetrics.RemoveMetrics(req.Namespace, req.Name)
 			return ctrl.Result{}, nil
 		}
 

+ 1 - 0
pkg/controllers/secretstore/clustersecretstore_controller.go

@@ -54,6 +54,7 @@ func (r *ClusterStoreReconciler) Reconcile(ctx context.Context, req ctrl.Request
 	var css esapi.ClusterSecretStore
 	err := r.Get(ctx, req.NamespacedName, &css)
 	if apierrors.IsNotFound(err) {
+		cssmetrics.RemoveMetrics(req.Namespace, req.Name)
 		return ctrl.Result{}, nil
 	} else if err != nil {
 		log.Error(err, "unable to get ClusterSecretStore")

+ 12 - 0
pkg/controllers/secretstore/cssmetrics/cssmetrics.go

@@ -55,3 +55,15 @@ func SetUpMetrics() {
 func GetGaugeVec(key string) *prometheus.GaugeVec {
 	return gaugeVecMetrics[key]
 }
+
+// RemoveMetrics deletes all metrics published by the resource.
+func RemoveMetrics(namespace, name string) {
+	for _, gaugeVecMetric := range gaugeVecMetrics {
+		gaugeVecMetric.DeletePartialMatch(
+			map[string]string{
+				"namespace": namespace,
+				"name":      name,
+			},
+		)
+	}
+}

+ 1 - 0
pkg/controllers/secretstore/secretstore_controller.go

@@ -55,6 +55,7 @@ func (r *StoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
 	var ss esapi.SecretStore
 	err := r.Get(ctx, req.NamespacedName, &ss)
 	if apierrors.IsNotFound(err) {
+		ssmetrics.RemoveMetrics(req.Namespace, req.Name)
 		return ctrl.Result{}, nil
 	} else if err != nil {
 		log.Error(err, "unable to get SecretStore")

+ 12 - 0
pkg/controllers/secretstore/ssmetrics/ssmetrics.go

@@ -55,3 +55,15 @@ func SetUpMetrics() {
 func GetGaugeVec(key string) *prometheus.GaugeVec {
 	return gaugeVecMetrics[key]
 }
+
+// RemoveMetrics deletes all metrics published by the resource.
+func RemoveMetrics(namespace, name string) {
+	for _, gaugeVecMetric := range gaugeVecMetrics {
+		gaugeVecMetric.DeletePartialMatch(
+			map[string]string{
+				"namespace": namespace,
+				"name":      name,
+			},
+		)
+	}
+}