Skip to content

Commit

Permalink
[DEVOPS-824] Delete metric if redis-failover no longer exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio Chana committed Sep 14, 2018
1 parent f82dce7 commit 11a3584
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions metrics/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
6 changes: 6 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions operator/redisfailover/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redisfailover
import (
"context"
"fmt"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 11a3584

Please sign in to comment.