Skip to content

Commit

Permalink
Add in HaProxy slave redis in bootstrap mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rurkss committed Jan 11, 2024
1 parent bd12c9a commit ca7fe72
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 9 deletions.
32 changes: 32 additions & 0 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ func generateHAProxyConfigmap(rf *redisfailoverv1.RedisFailover, labels map[stri
server-template redis %d _redis._tcp.%s.%s.svc.cluster.local:%d check inter 1s resolvers k8s init-addr none
`, port, rf.Spec.Redis.Replicas, redisName, namespace, port)

if rf.Spec.BootstrapNode != nil && rf.Spec.BootstrapNode.Port != rf.Spec.Redis.Port.ToString() {

portInt, _ := strconv.Atoi(rf.Spec.BootstrapNode.Port)
haproxyCfg += fmt.Sprintf(`
frontend redis-slave
bind *:%d
default_backend redis-slave
backend redis-slave
mode tcp
balance first
option tcp-check
tcp-check send info\ replication\r\n
tcp-check expect string role:slave
server-template redis %d _redis._tcp.%s.%s.svc.cluster.local:%d check inter 1s resolvers k8s init-addr none
`,
portInt, rf.Spec.Redis.Replicas, redisName, namespace, port)

}

if rf.Spec.Haproxy.CustomConfig != "" {
haproxyCfg = rf.Spec.Haproxy.CustomConfig
}
Expand Down Expand Up @@ -263,6 +283,18 @@ func generateHAProxyService(rf *redisfailoverv1.RedisFailover, labels map[string
},
}

if rf.Spec.BootstrapNode != nil && rf.Spec.BootstrapNode.Port != rf.Spec.Redis.Port.ToString() {

portInt, _ := strconv.Atoi(rf.Spec.BootstrapNode.Port)
additionalPort := corev1.ServicePort{
Name: "redis-slave",
Port: int32(portInt),
TargetPort: intstr.FromInt(int(portInt)),
Protocol: "TCP",
}
spec.Ports = append(spec.Ports, additionalPort)
}

serviceSettings := rf.Spec.Haproxy.Service
if serviceSettings != nil {
if serviceSettings.ClusterIP != "" {
Expand Down
94 changes: 85 additions & 9 deletions operator/redisfailover/service/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service_test

import (
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -1297,12 +1298,15 @@ func TestRedisService(t *testing.T) {
func TestHaproxyService(t *testing.T) {
haproxyName := "redis-haproxy"
portName := "redis-master"
defaultRedisPort := redisfailoverv1.Port(6379)
slaveRedisPortName := "redis-slave"
redisPort := 6379
redisSlavePort := 6389
defaultRedisPort := redisfailoverv1.Port(redisPort)
defaultRedisSlavePort := redisfailoverv1.Port(redisSlavePort)
customClusterIP := "10.1.1.100"
tests := []struct {
name string
rfName string
rfNamespace string
rfBootstrap redisfailoverv1.BootstrapSettings
rfLabels map[string]string
rfAnnotations map[string]string
rfRedisPort redisfailoverv1.Port
Expand Down Expand Up @@ -1374,6 +1378,79 @@ func TestHaproxyService(t *testing.T) {
},
},
},
}, {
name: "with bootstrapSettings without Port",
rfRedisPort: defaultRedisPort,
rfBootstrap: redisfailoverv1.BootstrapSettings{
Host: "redis",
Port: strconv.Itoa(redisPort),
},
expectedService: corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: haproxyName,
Namespace: namespace,
OwnerReferences: []metav1.OwnerReference{
{
Name: "testing",
},
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Selector: map[string]string{
"app.kubernetes.io/component": "redis",
"redisfailovers.databases.spotahome.com/component": "haproxy",
},
Ports: []corev1.ServicePort{
{
Name: portName,
Port: defaultRedisPort.ToInt32(),
TargetPort: intstr.FromInt(int(defaultRedisPort)),
Protocol: corev1.ProtocolTCP,
},
},
},
},
},
{
name: "with bootstrapSettings and with different Port",
rfRedisPort: defaultRedisPort,
rfBootstrap: redisfailoverv1.BootstrapSettings{
Host: "redis",
Port: defaultRedisSlavePort.ToString(),
},
expectedService: corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: haproxyName,
Namespace: namespace,
OwnerReferences: []metav1.OwnerReference{
{
Name: "testing",
},
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Selector: map[string]string{
"app.kubernetes.io/component": "redis",
"redisfailovers.databases.spotahome.com/component": "haproxy",
},
Ports: []corev1.ServicePort{
{
Name: portName,
Port: defaultRedisPort.ToInt32(),
TargetPort: intstr.FromInt(int(defaultRedisPort)),
Protocol: corev1.ProtocolTCP,
},
{
Name: slaveRedisPortName,
Port: defaultRedisSlavePort.ToInt32(),
TargetPort: intstr.FromInt(int(defaultRedisSlavePort)),
Protocol: corev1.ProtocolTCP,
},
},
},
},
},
}

Expand All @@ -1383,16 +1460,15 @@ func TestHaproxyService(t *testing.T) {

// Generate a default RedisFailover and attaching the required annotations
rf := generateRF()
if test.rfName != "" {
rf.Name = test.rfName
}
if test.rfNamespace != "" {
rf.Namespace = test.rfNamespace
}

rf.Spec.Redis.ServiceAnnotations = test.rfAnnotations
rf.Spec.Redis.Port = test.rfRedisPort
rf.Spec.Haproxy = &test.haproxy

if test.rfBootstrap.Host != "" {
rf.Spec.BootstrapNode = &test.rfBootstrap
}

generatedService := corev1.Service{}

ms := &mK8SService.Services{}
Expand Down

0 comments on commit ca7fe72

Please sign in to comment.