Skip to content

Commit

Permalink
Merge pull request #92 from spotahome/devops-824-limit-name-length
Browse files Browse the repository at this point in the history
[DEVOPS-824] Limit name length
  • Loading branch information
jchanam authored Sep 17, 2018
2 parents 13d3c8d + b4a1429 commit 198ad91
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := 0.5.2
VERSION := 0.5.3

# Name of this service/application
SERVICE_NAME := redis-operator
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This redis-failover will be managed by the operator, resulting in the following
* `rfs-<NAME>`: Sentinel service

**NOTE**: `NAME` is the named provided when creating the RedisFailover.
**IMPORTANT**: the name of the redis-failover to be created cannot be longer that 48 characters, due to prepend of redis/sentinel identification and statefulset limitation.

### Persistance
The operator has the ability of add persistance to Redis data. By default an `emptyDir` will be used, so the data is not saved.
Expand Down
9 changes: 9 additions & 0 deletions api/redisfailover/v1alpha2/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ package v1alpha2

import (
"errors"
"fmt"
)

const (
maxNameLength = 48
)

// Validate set the values by default if not defined and checks if the values given are valid
func (r *RedisFailover) Validate() error {
if len(r.Name) > maxNameLength {
return fmt.Errorf("name length can't be higher than %d", maxNameLength)
}

if r.Spec.Redis.Replicas == 0 {
r.Spec.Redis.Replicas = defaultRedisNumber
} else if r.Spec.Redis.Replicas < defaultRedisNumber {
Expand Down
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
2 changes: 2 additions & 0 deletions operator/redisfailover/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (r *RedisFailoverHandler) CheckAndHeal(rf *redisfailoverv1alpha2.RedisFailo
r.logger.Debugf("Time %.f more than expected. Not even one master, fixing...", minTime.Round(time.Second).Seconds())
// We can consider there's an error
if err2 := r.rfHealer.SetRandomMaster(rf); err2 != nil {
r.mClient.SetClusterError(rf.Namespace, rf.Name)
return err2
}
} else {
Expand Down Expand Up @@ -86,6 +87,7 @@ func (r *RedisFailoverHandler) CheckAndHeal(rf *redisfailoverv1alpha2.RedisFailo
if err := r.rfChecker.CheckSentinelMonitor(sip, master); err != nil {
r.logger.Debug("Sentinel is not monitoring the correct master")
if err := r.rfHealer.NewSentinelMonitor(sip, master, rf); err != nil {
r.mClient.SetClusterError(rf.Namespace, rf.Name)
return err
}
}
Expand Down
6 changes: 6 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 @@ -63,6 +64,7 @@ func (r *RedisFailoverHandler) Add(_ context.Context, obj runtime.Object) error
}

if err := rf.Validate(); err != nil {
r.mClient.SetClusterError(rf.Namespace, rf.Name)
return err
}

Expand All @@ -82,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
2 changes: 1 addition & 1 deletion operator/redisfailover/service/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
sentinelConfigFileName = "sentinel.conf"
redisConfigFileName = "redis.conf"
redisName = "r"
redisShutdownName = "r-shutdown"
redisShutdownName = "r-s"
redisRoleName = "redis"
redisGroupName = "mymaster"
appLabel = "redis-failover"
Expand Down

0 comments on commit 198ad91

Please sign in to comment.