Skip to content

Commit

Permalink
Expose haproxy prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
indiebrain committed Jan 3, 2024
1 parent 2233e9c commit de71e28
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
1 change: 1 addition & 0 deletions operator/redisfailover/service/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package service
const (
exporterPort = 9121
sentinelExporterPort = 9355
haproxyExporterPort = 9405
exporterPortName = "http-metrics"
exporterContainerName = "redis-exporter"
sentinelExporterContainerName = "sentinel-exporter"
Expand Down
17 changes: 16 additions & 1 deletion operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
},
}

Expand Down
71 changes: 44 additions & 27 deletions operator/redisfailover/service/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -1330,26 +1328,33 @@ 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,
},
},
},
},
},
{
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{
{
Expand All @@ -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,
},
},
Expand All @@ -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{}

Expand Down

0 comments on commit de71e28

Please sign in to comment.