forked from grafana/grafana-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafanaService.go
158 lines (137 loc) · 3.82 KB
/
grafanaService.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package model
import (
"strconv"
"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 getServiceName(cr *v1alpha1.Grafana) string {
if cr.Spec.Service != nil && cr.Spec.Service.Name != "" {
return cr.Spec.Service.Name
}
return constants.GrafanaServiceName
}
func getServiceLabels(cr *v1alpha1.Grafana) map[string]string {
if cr.Spec.Service == nil {
return nil
}
return cr.Spec.Service.Labels
}
func getServiceAnnotations(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 getServiceType(cr *v1alpha1.Grafana) v1.ServiceType {
if cr.Spec.Service == nil {
return v1.ServiceTypeClusterIP
}
if cr.Spec.Service.Type == "" {
return v1.ServiceTypeClusterIP
}
return cr.Spec.Service.Type
}
func getClusterIP(cr *v1alpha1.Grafana) string {
if cr.Spec.Service == nil {
return ""
}
return cr.Spec.Service.ClusterIP
}
func GetGrafanaPort(cr *v1alpha1.Grafana) int {
if cr.Spec.Config.Server == nil {
return constants.GrafanaHttpPort
}
if cr.Spec.Config.Server.HttpPort == "" {
return constants.GrafanaHttpPort
}
port, err := strconv.Atoi(cr.Spec.Config.Server.HttpPort)
if err != nil {
return constants.GrafanaHttpPort
}
return port
}
func getServicePorts(cr *v1alpha1.Grafana, currentState *v1.Service) []v1.ServicePort {
intPort := int32(GetGrafanaPort(cr))
nodePort := int32(0)
namePort := constants.GrafanaHttpPortName
appProtcol := constants.GrafanaHttpPortAppProtocol
if cr.Spec.Service != nil {
for _, nPort := range cr.Spec.Service.Ports {
if nPort.Port == 3000 {
nodePort = nPort.NodePort
namePort = nPort.Name
}
}
}
defaultPorts := []v1.ServicePort{
{
Name: namePort,
Protocol: "TCP",
Port: intPort,
TargetPort: intstr.FromString("grafana-http"),
NodePort: nodePort,
AppProtocol: &appProtcol,
},
}
if cr.Spec.Service == nil {
return defaultPorts
}
// Re-assign existing node port
if cr.Spec.Service != nil &&
currentState != nil {
for _, port := range currentState.Spec.Ports {
if port.Name == constants.GrafanaHttpPortName {
defaultPorts[0].NodePort = port.NodePort
}
}
}
if cr.Spec.Service.Ports == nil {
return defaultPorts
}
// Don't allow overriding the default port but allow adding
// additional ports
for _, port := range cr.Spec.Service.Ports {
if port.Name == constants.GrafanaHttpPortName || port.Port == intPort {
continue
}
defaultPorts = append(defaultPorts, port)
}
return defaultPorts
}
func GrafanaService(cr *v1alpha1.Grafana) *v1.Service {
return &v1.Service{
ObjectMeta: v12.ObjectMeta{
Name: getServiceName(cr),
Namespace: cr.Namespace,
Labels: getServiceLabels(cr),
Annotations: getServiceAnnotations(cr, nil),
},
Spec: v1.ServiceSpec{
Ports: getServicePorts(cr, nil),
Selector: map[string]string{
"app": constants.GrafanaPodLabel,
},
ClusterIP: getClusterIP(cr),
Type: getServiceType(cr),
},
}
}
func GrafanaServiceReconciled(cr *v1alpha1.Grafana, currentState *v1.Service) *v1.Service {
reconciled := currentState.DeepCopy()
reconciled.Name = getServiceName(cr)
reconciled.Labels = getServiceLabels(cr)
reconciled.Annotations = getServiceAnnotations(cr, currentState.Annotations)
reconciled.Spec.Ports = getServicePorts(cr, currentState)
reconciled.Spec.Type = getServiceType(cr)
return reconciled
}
func GrafanaServiceSelector(cr *v1alpha1.Grafana) client.ObjectKey {
return client.ObjectKey{
Namespace: cr.Namespace,
Name: getServiceName(cr),
}
}