-
Notifications
You must be signed in to change notification settings - Fork 372
/
grafana_service_reconciler.go
95 lines (78 loc) · 2.52 KB
/
grafana_service_reconciler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package grafana
import (
"context"
"fmt"
"strconv"
"github.com/grafana-operator/grafana-operator/api/v1beta1"
"github.com/grafana-operator/grafana-operator/controllers/config"
"github.com/grafana-operator/grafana-operator/controllers/model"
"github.com/grafana-operator/grafana-operator/controllers/reconcilers"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)
type ServiceReconciler struct {
client client.Client
}
func NewServiceReconciler(client client.Client) reconcilers.OperatorGrafanaReconciler {
return &ServiceReconciler{
client: client,
}
}
func (r *ServiceReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana, status *v1beta1.GrafanaStatus, vars *v1beta1.OperatorReconcileVars, scheme *runtime.Scheme) (v1beta1.OperatorStageStatus, error) {
_ = log.FromContext(ctx)
service := model.GetGrafanaService(cr, scheme)
_, err := controllerutil.CreateOrUpdate(ctx, r.client, service, func() error {
service.Spec = v1.ServiceSpec{
Ports: getServicePorts(cr),
Selector: map[string]string{
"app": cr.Name,
},
Type: v1.ServiceTypeClusterIP,
}
return v1beta1.Merge(service, cr.Spec.Service)
})
if err != nil {
return v1beta1.OperatorStageResultFailed, err
}
// try to assign the admin url
if !cr.PreferIngress() {
status.AdminUrl = fmt.Sprintf("%v://%v.%v:%d", getGrafanaServerProtocol(cr), service.Name, cr.Namespace,
int32(GetGrafanaPort(cr)))
}
return v1beta1.OperatorStageResultSuccess, nil
}
func getGrafanaServerProtocol(cr *v1beta1.Grafana) string {
if cr.Spec.Config != nil && cr.Spec.Config["server"] != nil && cr.Spec.Config["server"]["protocol"] != "" {
return cr.Spec.Config["server"]["protocol"]
}
return config.GrafanaServerProtocol
}
func GetGrafanaPort(cr *v1beta1.Grafana) int {
if cr.Spec.Config["server"] == nil {
return config.GrafanaHttpPort
}
if cr.Spec.Config["server"]["http_port"] == "" {
return config.GrafanaHttpPort
}
port, err := strconv.Atoi(cr.Spec.Config["server"]["http_port"])
if err != nil {
return config.GrafanaHttpPort
}
return port
}
func getServicePorts(cr *v1beta1.Grafana) []v1.ServicePort {
intPort := int32(GetGrafanaPort(cr))
defaultPorts := []v1.ServicePort{
{
Name: config.GrafanaHttpPortName,
Protocol: "TCP",
Port: intPort,
TargetPort: intstr.FromString("grafana-http"),
},
}
return defaultPorts
}