diff --git a/operator/redisfailover/service/constants.go b/operator/redisfailover/service/constants.go index ef320fd0f..bce35a0a9 100644 --- a/operator/redisfailover/service/constants.go +++ b/operator/redisfailover/service/constants.go @@ -4,6 +4,7 @@ package service const ( exporterPort = 9121 sentinelExporterPort = 9355 + haproxyExporterPort = 9405 exporterPortName = "http-metrics" exporterContainerName = "redis-exporter" sentinelExporterContainerName = "sentinel-exporter" diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index d7c1cbe5e..f7c67e48d 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -111,6 +111,10 @@ func generateHAProxyDeployment(rf *redisfailoverv1.RedisFailover, labels map[str { ContainerPort: rf.Spec.Redis.Port.ToInt32(), }, + { + Name: exporterPortName, + ContainerPort: haproxyExporterPort, + }, }, VolumeMounts: volumeMounts, Resources: rf.Spec.Haproxy.Resources, @@ -156,6 +160,11 @@ func generateHAProxyConfigmap(rf *redisfailoverv1.RedisFailover, labels map[stri bind :8080 default_backend stats + frontend prometheus + mode http + bind :%d + http-request use-service prometheus-exporter + backend stats mode http stats enable @@ -184,7 +193,7 @@ func generateHAProxyConfigmap(rf *redisfailoverv1.RedisFailover, labels map[stri tcp-check send info\ replication\r\n tcp-check expect string role:master 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) +`, haproxyExporterPort, port, rf.Spec.Redis.Replicas, redisName, namespace, port) if rf.Spec.Haproxy.CustomConfig != "" { haproxyCfg = rf.Spec.Haproxy.CustomConfig @@ -260,6 +269,12 @@ func generateHAProxyService(rf *redisfailoverv1.RedisFailover, labels map[string TargetPort: redisTargetPort, Protocol: "TCP", }, + { + Name: exporterPortName, + Port: haproxyExporterPort, + TargetPort: intstr.FromInt(haproxyExporterPort), + Protocol: "TCP", + }, }, } diff --git a/operator/redisfailover/service/generator_test.go b/operator/redisfailover/service/generator_test.go index bb89f5b12..b481fc8ff 100644 --- a/operator/redisfailover/service/generator_test.go +++ b/operator/redisfailover/service/generator_test.go @@ -1295,26 +1295,24 @@ func TestRedisService(t *testing.T) { } func TestHaproxyService(t *testing.T) { - haproxyName := "redis-haproxy" - portName := "redis-master" - defaultRedisPort := redisfailoverv1.Port(6379) - customClusterIP := "10.1.1.100" tests := []struct { - name string - rfName string - rfNamespace string - rfLabels map[string]string - rfAnnotations map[string]string - rfRedisPort redisfailoverv1.Port - haproxy redisfailoverv1.HaproxySettings - expectedService corev1.Service + name string + rfName string + rfNamespace string + rfLabels map[string]string + rfAnnotations map[string]string + rfRedisPort redisfailoverv1.Port + rfHaproxyName string + rfHaproxyClusterIP string + haproxy redisfailoverv1.HaproxySettings + expectedService corev1.Service }{ { name: "with defaults", - rfRedisPort: defaultRedisPort, + rfRedisPort: 6379, expectedService: corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: haproxyName, + Name: "redis-haproxy", Namespace: namespace, OwnerReferences: []metav1.OwnerReference{ { @@ -1330,9 +1328,15 @@ func TestHaproxyService(t *testing.T) { }, Ports: []corev1.ServicePort{ { - Name: portName, - Port: defaultRedisPort.ToInt32(), - TargetPort: intstr.FromInt(int(defaultRedisPort)), + Name: "redis-master", + Port: 6379, + TargetPort: intstr.FromInt(6379), + Protocol: corev1.ProtocolTCP, + }, + { + Name: "http-metrics", + Port: 9405, + TargetPort: intstr.FromInt(9405), Protocol: corev1.ProtocolTCP, }, }, @@ -1340,16 +1344,17 @@ func TestHaproxyService(t *testing.T) { }, }, { - name: "with custom ClusterIP", - rfRedisPort: defaultRedisPort, + name: "with custom ClusterIP", + rfHaproxyName: "redis-haproxy", + rfHaproxyClusterIP: "10.1.1.100", haproxy: redisfailoverv1.HaproxySettings{ Service: &redisfailoverv1.ServiceSettings{ - ClusterIP: customClusterIP, + ClusterIP: "10.1.1.100", }, }, expectedService: corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: haproxyName, + Name: "redis-haproxy", Namespace: namespace, OwnerReferences: []metav1.OwnerReference{ { @@ -1359,16 +1364,22 @@ func TestHaproxyService(t *testing.T) { }, Spec: corev1.ServiceSpec{ Type: corev1.ServiceTypeClusterIP, - ClusterIP: customClusterIP, + ClusterIP: "10.1.1.100", 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)), + Name: "redis-master", + Port: 6379, + TargetPort: intstr.FromInt(6379), + Protocol: corev1.ProtocolTCP, + }, + { + Name: "http-metrics", + Port: 9405, + TargetPort: intstr.FromInt(9405), Protocol: corev1.ProtocolTCP, }, }, @@ -1383,15 +1394,21 @@ func TestHaproxyService(t *testing.T) { // Generate a default RedisFailover and attaching the required annotations rf := generateRF() + rf.Spec.Haproxy = &test.haproxy if test.rfName != "" { rf.Name = test.rfName } if test.rfNamespace != "" { rf.Namespace = test.rfNamespace } + if test.rfRedisPort == 0 { + test.rfRedisPort = 6379 + } + rf.Spec.Redis.Port = redisfailoverv1.Port(6379) + if test.rfHaproxyName == "" { + rf.Spec.Haproxy.RedisHost = "redis-haproxy" + } rf.Spec.Redis.ServiceAnnotations = test.rfAnnotations - rf.Spec.Redis.Port = test.rfRedisPort - rf.Spec.Haproxy = &test.haproxy generatedService := corev1.Service{}