Skip to content

Commit

Permalink
Merge pull request #868 from celestialorb/bugfix/unified-alerting-ser…
Browse files Browse the repository at this point in the history
…vice

Fix Unified Alerting Service Configuration
  • Loading branch information
pb82 authored Mar 7, 2023
2 parents d159ebf + 669ba94 commit a33ef1d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
22 changes: 21 additions & 1 deletion controllers/common/clusterState.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

type ClusterState struct {
GrafanaAlertService *v1.Service
GrafanaService *v1.Service
GrafanaDataPersistentVolumeClaim *v1.PersistentVolumeClaim
GrafanaServiceAccount *v1.ServiceAccount
Expand All @@ -34,7 +35,12 @@ func (i *ClusterState) Read(ctx context.Context, cr *v1alpha1.Grafana, client cl
cfg := config.GetControllerConfig()
isOpenshift := cfg.GetConfigBool(config.ConfigOpenshift, false)

err := i.readGrafanaService(ctx, cr, client)
err := i.readGrafanaAlertService(ctx, cr, client)
if err != nil {
return err
}

err = i.readGrafanaService(ctx, cr, client)
if err != nil {
return err
}
Expand Down Expand Up @@ -94,6 +100,20 @@ func (i *ClusterState) readGrafanaService(ctx context.Context, cr *v1alpha1.Graf
return nil
}

func (i *ClusterState) readGrafanaAlertService(ctx context.Context, cr *v1alpha1.Grafana, client client.Client) error {
currentState := &v1.Service{}
selector := model.GrafanaAlertServiceSelector(cr)
err := client.Get(ctx, selector, currentState)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
i.GrafanaAlertService = currentState.DeepCopy()
return nil
}

func (i *ClusterState) readGrafanaDataPVC(ctx context.Context, cr *v1alpha1.Grafana, client client.Client) error {
currentState := &v1.PersistentVolumeClaim{}
selector := model.GrafanaDataStorageSelector(cr)
Expand Down
4 changes: 4 additions & 0 deletions controllers/config/grafanaIni.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,10 @@ func (i *GrafanaIni) cfgUnifiedAlerting(config map[string][]string) map[string][
items = appendStr(items, "evaluation_timeout", i.cfg.UnifiedAlerting.EvaluationTimeout)
items = appendInt(items, "max_attempts", i.cfg.UnifiedAlerting.MaxAttempts)
items = appendStr(items, "min_interval", i.cfg.UnifiedAlerting.MinInterval)

items = appendStr(items, "ha_advertise_address", "${POD_IP}:9094")
items = appendStr(items, "ha_listen_address", "${POD_IP}:9094")
items = appendStr(items, "ha_peers", "grafana-alert:9094")
config["unified_alerting"] = items

return config
Expand Down
6 changes: 6 additions & 0 deletions controllers/config/grafanaIni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ static_root_path = /
enabled = true
evaluation_timeout = 3s
execute_alerts = true
ha_advertise_address = ${POD_IP}:9094
ha_listen_address = ${POD_IP}:9094
ha_peers = grafana-alert:9094
max_attempts = 2
min_interval = 1m
Expand Down Expand Up @@ -387,6 +390,9 @@ func TestCfgUnifiedAlerting(t *testing.T) {
"evaluation_timeout = 3s",
"max_attempts = 2",
"min_interval = 1m",
"ha_advertise_address = ${POD_IP}:9094",
"ha_listen_address = ${POD_IP}:9094",
"ha_peers = grafana-alert:9094",
},
}
require.Equal(t, config, testConfig)
Expand Down
14 changes: 14 additions & 0 deletions controllers/grafana/grafana_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (i *GrafanaReconciler) Reconcile(state *common.ClusterState, cr *v1alpha1.G

desired = desired.AddAction(i.getGrafanaAdminUserSecretDesiredState(state, cr))
desired = desired.AddAction(i.getGrafanaServiceDesiredState(state, cr))
desired = desired.AddAction(i.getGrafanaAlertServiceDesiredState(state, cr))

if cr.UsedPersistentVolume() {
desired = desired.AddAction(i.getGrafanaDataPvcDesiredState(state, cr))
Expand Down Expand Up @@ -118,6 +119,19 @@ func (i *GrafanaReconciler) getGrafanaServiceDesiredState(state *common.ClusterS
}
}

func (i *GrafanaReconciler) getGrafanaAlertServiceDesiredState(state *common.ClusterState, cr *v1alpha1.Grafana) common.ClusterAction {
if state.GrafanaAlertService == nil {
return common.GenericCreateAction{
Ref: model.GrafanaAlertService(cr),
Msg: "create grafana alert service",
}
}
return common.GenericUpdateAction{
Ref: model.GrafanaAlertServiceReconciled(cr, state.GrafanaAlertService),
Msg: "update grafana alert service",
}
}

func (i *GrafanaReconciler) getGrafanaDataPvcDesiredState(state *common.ClusterState, cr *v1alpha1.Grafana) common.ClusterAction {
if state.GrafanaDataPersistentVolumeClaim == nil {
return common.GenericCreateAction{
Expand Down
66 changes: 66 additions & 0 deletions controllers/model/grafanaAlertService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package model

import (
"github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
"github.com/grafana-operator/grafana-operator/v4/controllers/constants"
v1 "k8s.io/api/core/v1"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func getAlertServiceName() string {
return "grafana-alert"
}

func getAlertServiceLabels(cr *v1alpha1.Grafana) map[string]string {
if cr.Spec.Service == nil {
return nil
}
return cr.Spec.Service.Labels
}

func getAlertServiceAnnotations(cr *v1alpha1.Grafana, existing map[string]string) map[string]string {
if cr.Spec.Service == nil {
return existing
}

return MergeAnnotations(cr.Spec.Service.Annotations, existing)
}

func GrafanaAlertService(cr *v1alpha1.Grafana) *v1.Service {
return &v1.Service{
ObjectMeta: v12.ObjectMeta{
Name: getAlertServiceName(),
Namespace: cr.Namespace,
Labels: getAlertServiceLabels(cr),
Annotations: getAlertServiceAnnotations(cr, nil),
},
Spec: v1.ServiceSpec{
ClusterIP: "None",
Ports: []v1.ServicePort{
{
Name: "grafana-alert",
Port: 9094,
TargetPort: intstr.FromString("grafana-alert"),
},
},
Selector: map[string]string{
"app": constants.GrafanaPodLabel,
},
Type: v1.ServiceTypeClusterIP,
},
}
}

func GrafanaAlertServiceReconciled(cr *v1alpha1.Grafana, currentState *v1.Service) *v1.Service {
reconciled := currentState.DeepCopy()
return reconciled
}

func GrafanaAlertServiceSelector(cr *v1alpha1.Grafana) client.ObjectKey {
return client.ObjectKey{
Namespace: cr.Namespace,
Name: getAlertServiceName(),
}
}
10 changes: 10 additions & 0 deletions controllers/model/grafanaDeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,16 @@ func getContainers(cr *v1alpha1.Grafana, configHash, dsHash, credentialsHash str
ContainerPort: int32(GetGrafanaPort(cr)),
Protocol: "TCP",
},
{
Name: "grafana-alert",
ContainerPort: 9094,
Protocol: "TCP",
},
{
Name: "grafana-alert-u",
ContainerPort: 9094,
Protocol: "UDP",
},
},
Env: envVars,
EnvFrom: getEnvFrom(cr),
Expand Down

0 comments on commit a33ef1d

Please sign in to comment.