diff --git a/metrics/dummy.go b/metrics/dummy.go index b9f42e637..49480e5e9 100644 --- a/metrics/dummy.go +++ b/metrics/dummy.go @@ -8,3 +8,4 @@ type dummy struct{} func (d *dummy) SetClusterOK(namespace string, name string) {} func (d *dummy) SetClusterError(namespace string, name string) {} +func (d *dummy) DeleteCluster(namespace string, name string) {} diff --git a/metrics/metrics.go b/metrics/metrics.go index 67e808cf4..78e5549a5 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -15,6 +15,7 @@ const ( type Instrumenter interface { SetClusterOK(namespace string, name string) SetClusterError(namespace string, name string) + DeleteCluster(namespace string, name string) } // PromMetrics implements the instrumenter so the metrics can be managed by Prometheus. @@ -71,3 +72,8 @@ func (p *PromMetrics) SetClusterOK(namespace string, name string) { func (p *PromMetrics) SetClusterError(namespace string, name string) { p.clusterOK.WithLabelValues(namespace, name).Set(0) } + +// DeleteCluster set the cluster status to Error +func (p *PromMetrics) DeleteCluster(namespace string, name string) { + p.clusterOK.DeleteLabelValues(namespace, name) +} diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index fb34fa4fa..3c08cc6a6 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -87,6 +87,27 @@ func TestPrometheusMetrics(t *testing.T) { }, expCode: http.StatusOK, }, + { + name: "Deleting a cluster should remove it", + addMetrics: func(pm *metrics.PromMetrics) { + pm.SetClusterOK("testns1", "test") + pm.DeleteCluster("testns1", "test") + }, + expMetrics: []string{}, + expCode: http.StatusOK, + }, + { + name: "Deleting a cluster should remove only the desired one", + addMetrics: func(pm *metrics.PromMetrics) { + pm.SetClusterOK("testns1", "test") + pm.SetClusterOK("testns2", "test") + pm.DeleteCluster("testns1", "test") + }, + expMetrics: []string{ + `my_metrics_controller_cluster_ok{name="test",namespace="testns2"} 1`, + }, + expCode: http.StatusOK, + }, } for _, test := range tests { diff --git a/operator/redisfailover/handler.go b/operator/redisfailover/handler.go index 085d4e400..cf0329b30 100644 --- a/operator/redisfailover/handler.go +++ b/operator/redisfailover/handler.go @@ -3,6 +3,7 @@ package redisfailover import ( "context" "fmt" + "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -83,6 +84,10 @@ func (r *RedisFailoverHandler) Add(_ context.Context, obj runtime.Object) error // Delete handles the deletion of a RF. func (r *RedisFailoverHandler) Delete(_ context.Context, name string) error { + n := strings.Split(name, "/") + if len(n) >= 2 { + r.mClient.DeleteCluster(n[0], n[1]) + } // No need to do anything, it will be handled by the owner reference done // on the creation. r.logger.Debugf("ignoring, kubernetes GCs all using the objects OwnerReference metadata")