Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add headlessService for grafana high availability #1840

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions controllers/config/operator_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
GrafanaHttpPort int = 3000
GrafanaHttpPortName = "grafana"
GrafanaServerProtocol = "http"
GrafanaAlertPort int = 9094
GrafanaAlertPortName = "grafana-alert"

// Data storage
GrafanaProvisionPluginVolumeName = "grafana-provision-plugins"
Expand Down
12 changes: 12 additions & 0 deletions controllers/model/grafana_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
16 changes: 16 additions & 0 deletions controllers/reconcilers/grafana/deployment_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
33 changes: 33 additions & 0 deletions controllers/reconcilers/grafana/grafana_service_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Member

@theSuess theSuess Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only called on update, which is why the e2e tests are currently failing. You'll need to specify the ports in GetGrafanaHeadlessService instead

Selector: map[string]string{
"app": cr.Name,
},
Type: v1.ServiceTypeClusterIP,
}
return nil
})
if err != nil {
return v1beta1.OperatorStageResultFailed, err
}

return v1beta1.OperatorStageResultSuccess, nil
}

Expand Down Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/examples/crossnamespace/assertions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading