Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add headlessService for grafana high availability
Browse files Browse the repository at this point in the history
synthe102 committed Jan 30, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ff3fb07 commit ed971a5
Showing 7 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions controllers/config/operator_constants.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ const (
GrafanaHttpPort int = 3000
GrafanaHttpPortName = "grafana"
GrafanaServerProtocol = "http"
GrafanaAlertPort int = 9094
GrafanaAlertPortName = "grafana-alert"

// Data storage
GrafanaProvisionPluginVolumeName = "grafana-provision-plugins"
12 changes: 12 additions & 0 deletions controllers/model/grafana_resources.go
Original file line number Diff line number Diff line change
@@ -81,6 +81,18 @@ func GetGrafanaService(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v1.S
return service
}

func GetGrafanaHeadlessService(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v1.Service {
service := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-alerting", cr.Name),
Namespace: cr.Namespace,
Labels: CommonLabels,
},
}
controllerutil.SetControllerReference(cr, service, scheme) //nolint:errcheck
return service
}

func GetGrafanaIngress(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v12.Ingress {
ingress := &v12.Ingress{
ObjectMeta: metav1.ObjectMeta{
16 changes: 16 additions & 0 deletions controllers/reconcilers/grafana/deployment_reconciler.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import (
"strings"

"github.com/grafana/grafana-operator/v5/api/v1beta1"
"github.com/grafana/grafana-operator/v5/controllers/config"
config2 "github.com/grafana/grafana-operator/v5/controllers/config"
"github.com/grafana/grafana-operator/v5/controllers/model"
"github.com/grafana/grafana-operator/v5/controllers/reconcilers"
@@ -184,6 +185,16 @@ func getContainers(cr *v1beta1.Grafana, scheme *runtime.Scheme, vars *v1beta1.Op
Value: config2.GrafanaDataPath,
})

// env var to get Pod IP from downward API for gossip (useful for unified alerting).
envVars = append(envVars, v1.EnvVar{
Name: "POD_IP",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "status.podIP",
},
},
})

containers = append(containers, v1.Container{
Name: "grafana",
Image: image,
@@ -195,6 +206,11 @@ func getContainers(cr *v1beta1.Grafana, scheme *runtime.Scheme, vars *v1beta1.Op
ContainerPort: int32(GetGrafanaPort(cr)), // #nosec G115
Protocol: "TCP",
},
{
Name: config.GrafanaAlertPortName,
ContainerPort: int32(config.GrafanaAlertPort),
Protocol: "TCP",
},
},
Env: envVars,
Resources: getResources(),
33 changes: 33 additions & 0 deletions controllers/reconcilers/grafana/grafana_service_reconciler.go
Original file line number Diff line number Diff line change
@@ -54,6 +54,24 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana,
int32(GetGrafanaPort(cr))) // #nosec G115
}

// Headless service for grafana unified alerting
headlessService := model.GetGrafanaHeadlessService(cr, scheme)
_, err = controllerutil.CreateOrUpdate(ctx, r.client, headlessService, func() error {
model.SetCommonLabels(service)
service.Spec = v1.ServiceSpec{
ClusterIP: "None",
Ports: getHeadlessServicePorts(cr),
Selector: map[string]string{
"app": cr.Name,
},
Type: v1.ServiceTypeClusterIP,
}
return nil
})
if err != nil {
return v1beta1.OperatorStageResultFailed, err
}

return v1beta1.OperatorStageResultSuccess, nil
}

@@ -95,3 +113,18 @@ func getServicePorts(cr *v1beta1.Grafana) []v1.ServicePort {

return defaultPorts
}

func getHeadlessServicePorts(_ *v1beta1.Grafana) []v1.ServicePort {
intPort := int32(config.GrafanaAlertPort)

defaultPorts := []v1.ServicePort{
{
Name: config.GrafanaAlertPortName,
Protocol: "TCP",
Port: intPort,
TargetPort: intstr.FromInt32(intPort),
},
}

return defaultPorts
}
9 changes: 9 additions & 0 deletions examples/multiple_replicas/resources.yaml
Original file line number Diff line number Diff line change
@@ -85,3 +85,12 @@ spec:
name: "grafana"
user: "grafana"
password: "grafana"
# Configure HA for Grafana alerting
# https://grafana.com/docs/grafana/latest/alerting/set-up/configure-high-availability/
unified_alerting:
enabled: true
ha_listen_address: "${POD_IP}:9094"
ha_peers: "grafana-alerting:9094"
ha_advertise_address: "${POD_IP}:9094"
ha_peer_timeout: 15s
ha_reconnect_timeout: 2m
10 changes: 10 additions & 0 deletions tests/e2e/examples/basic/assertions.yaml
Original file line number Diff line number Diff line change
@@ -19,6 +19,16 @@ metadata:
spec: {}
---
apiVersion: v1
kind: Service
metadata:
name: grafana-alerting
ownerReferences:
- apiVersion: grafana.integreatly.org/v1beta1
kind: Grafana
name: grafana
spec: {}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-ini
10 changes: 10 additions & 0 deletions tests/e2e/examples/crossnamespace/assertions.yaml
Original file line number Diff line number Diff line change
@@ -19,6 +19,16 @@ metadata:
spec: {}
---
apiVersion: v1
kind: Service
metadata:
name: grafana-alerting
ownerReferences:
- apiVersion: grafana.integreatly.org/v1beta1
kind: Grafana
name: grafana
spec: {}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-ini

0 comments on commit ed971a5

Please sign in to comment.