Skip to content

Commit 198ad91

Browse files
authored
Merge pull request #92 from spotahome/devops-824-limit-name-length
[DEVOPS-824] Limit name length
2 parents 13d3c8d + b4a1429 commit 198ad91

File tree

9 files changed

+48
-2
lines changed

9 files changed

+48
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION := 0.5.2
1+
VERSION := 0.5.3
22

33
# Name of this service/application
44
SERVICE_NAME := redis-operator

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This redis-failover will be managed by the operator, resulting in the following
5050
* `rfs-<NAME>`: Sentinel service
5151

5252
**NOTE**: `NAME` is the named provided when creating the RedisFailover.
53+
**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.
5354

5455
### Persistance
5556
The operator has the ability of add persistance to Redis data. By default an `emptyDir` will be used, so the data is not saved.

api/redisfailover/v1alpha2/validate.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ package v1alpha2
22

33
import (
44
"errors"
5+
"fmt"
6+
)
7+
8+
const (
9+
maxNameLength = 48
510
)
611

712
// Validate set the values by default if not defined and checks if the values given are valid
813
func (r *RedisFailover) Validate() error {
14+
if len(r.Name) > maxNameLength {
15+
return fmt.Errorf("name length can't be higher than %d", maxNameLength)
16+
}
17+
918
if r.Spec.Redis.Replicas == 0 {
1019
r.Spec.Redis.Replicas = defaultRedisNumber
1120
} else if r.Spec.Redis.Replicas < defaultRedisNumber {

metrics/dummy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ type dummy struct{}
88

99
func (d *dummy) SetClusterOK(namespace string, name string) {}
1010
func (d *dummy) SetClusterError(namespace string, name string) {}
11+
func (d *dummy) DeleteCluster(namespace string, name string) {}

metrics/metrics.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
type Instrumenter interface {
1616
SetClusterOK(namespace string, name string)
1717
SetClusterError(namespace string, name string)
18+
DeleteCluster(namespace string, name string)
1819
}
1920

2021
// PromMetrics implements the instrumenter so the metrics can be managed by Prometheus.
@@ -71,3 +72,8 @@ func (p *PromMetrics) SetClusterOK(namespace string, name string) {
7172
func (p *PromMetrics) SetClusterError(namespace string, name string) {
7273
p.clusterOK.WithLabelValues(namespace, name).Set(0)
7374
}
75+
76+
// DeleteCluster set the cluster status to Error
77+
func (p *PromMetrics) DeleteCluster(namespace string, name string) {
78+
p.clusterOK.DeleteLabelValues(namespace, name)
79+
}

metrics/metrics_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ func TestPrometheusMetrics(t *testing.T) {
8787
},
8888
expCode: http.StatusOK,
8989
},
90+
{
91+
name: "Deleting a cluster should remove it",
92+
addMetrics: func(pm *metrics.PromMetrics) {
93+
pm.SetClusterOK("testns1", "test")
94+
pm.DeleteCluster("testns1", "test")
95+
},
96+
expMetrics: []string{},
97+
expCode: http.StatusOK,
98+
},
99+
{
100+
name: "Deleting a cluster should remove only the desired one",
101+
addMetrics: func(pm *metrics.PromMetrics) {
102+
pm.SetClusterOK("testns1", "test")
103+
pm.SetClusterOK("testns2", "test")
104+
pm.DeleteCluster("testns1", "test")
105+
},
106+
expMetrics: []string{
107+
`my_metrics_controller_cluster_ok{name="test",namespace="testns2"} 1`,
108+
},
109+
expCode: http.StatusOK,
110+
},
90111
}
91112

92113
for _, test := range tests {

operator/redisfailover/checker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (r *RedisFailoverHandler) CheckAndHeal(rf *redisfailoverv1alpha2.RedisFailo
4343
r.logger.Debugf("Time %.f more than expected. Not even one master, fixing...", minTime.Round(time.Second).Seconds())
4444
// We can consider there's an error
4545
if err2 := r.rfHealer.SetRandomMaster(rf); err2 != nil {
46+
r.mClient.SetClusterError(rf.Namespace, rf.Name)
4647
return err2
4748
}
4849
} else {
@@ -86,6 +87,7 @@ func (r *RedisFailoverHandler) CheckAndHeal(rf *redisfailoverv1alpha2.RedisFailo
8687
if err := r.rfChecker.CheckSentinelMonitor(sip, master); err != nil {
8788
r.logger.Debug("Sentinel is not monitoring the correct master")
8889
if err := r.rfHealer.NewSentinelMonitor(sip, master, rf); err != nil {
90+
r.mClient.SetClusterError(rf.Namespace, rf.Name)
8991
return err
9092
}
9193
}

operator/redisfailover/handler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redisfailover
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
"k8s.io/apimachinery/pkg/runtime"
@@ -63,6 +64,7 @@ func (r *RedisFailoverHandler) Add(_ context.Context, obj runtime.Object) error
6364
}
6465

6566
if err := rf.Validate(); err != nil {
67+
r.mClient.SetClusterError(rf.Namespace, rf.Name)
6668
return err
6769
}
6870

@@ -82,6 +84,10 @@ func (r *RedisFailoverHandler) Add(_ context.Context, obj runtime.Object) error
8284

8385
// Delete handles the deletion of a RF.
8486
func (r *RedisFailoverHandler) Delete(_ context.Context, name string) error {
87+
n := strings.Split(name, "/")
88+
if len(n) >= 2 {
89+
r.mClient.DeleteCluster(n[0], n[1])
90+
}
8591
// No need to do anything, it will be handled by the owner reference done
8692
// on the creation.
8793
r.logger.Debugf("ignoring, kubernetes GCs all using the objects OwnerReference metadata")

operator/redisfailover/service/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
sentinelConfigFileName = "sentinel.conf"
2626
redisConfigFileName = "redis.conf"
2727
redisName = "r"
28-
redisShutdownName = "r-shutdown"
28+
redisShutdownName = "r-s"
2929
redisRoleName = "redis"
3030
redisGroupName = "mymaster"
3131
appLabel = "redis-failover"

0 commit comments

Comments
 (0)