/
service.go
279 lines (248 loc) · 8.93 KB
/
service.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2021-present, StarRocks Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resource_utils
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
srapi "github.com/StarRocks/starrocks-kubernetes-operator/pkg/apis/starrocks/v1"
"github.com/StarRocks/starrocks-kubernetes-operator/pkg/common/hash"
"github.com/StarRocks/starrocks-kubernetes-operator/pkg/k8sutils/templates/object"
"github.com/StarRocks/starrocks-kubernetes-operator/pkg/k8sutils/templates/service"
)
const (
FeHTTPPortName = "http"
FeRPCPortName = "rpc"
FeQueryPortName = "query"
FeEditLogPortName = "edit-log"
BePortName = "be"
BeWebserverPortName = "webserver"
BeHeartbeatPortName = "heartbeat"
BeBrpcPortName = "brpc"
CnThriftPortName = "thrift"
CnWebserverPortName = "webserver"
CnHeartbeatPortName = "heartbeat"
CnBrpcPortName = "brpc"
)
// HashService service hash components
type hashService struct {
name string
namespace string
finalizers []string
ports []corev1.ServicePort
selector map[string]string
serviceType corev1.ServiceType
labels map[string]string
annotations map[string]string
}
// BuildExternalService build the external service. not have selector
func BuildExternalService(object object.StarRocksObject, spec srapi.SpecInterface,
config map[string]interface{}, selector map[string]string, labels map[string]string) corev1.Service {
// the k8s service type.
var srPorts []srapi.StarRocksServicePort
svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: service.ExternalServiceName(object.AliasName, spec),
Namespace: object.GetNamespace(),
Labels: labels,
},
Spec: corev1.ServiceSpec{
Selector: selector,
},
}
setServiceType(spec.GetService(), &svc)
anno := getServiceAnnotations(spec.GetService())
switch spec.(type) {
case *srapi.StarRocksFeSpec:
srPorts = getFeServicePorts(config, spec.GetService())
case *srapi.StarRocksBeSpec:
srPorts = getBeServicePorts(config, spec.GetService())
case *srapi.StarRocksCnSpec:
srPorts = getCnServicePorts(config, spec.GetService())
case *srapi.StarRocksFeProxySpec:
srPorts = []srapi.StarRocksServicePort{
mergePort(spec.GetService(), srapi.StarRocksServicePort{
Name: FE_PORXY_HTTP_PORT_NAME,
Port: FE_PROXY_HTTP_PORT,
ContainerPort: FE_PROXY_HTTP_PORT,
}),
}
}
ref := metav1.NewControllerRef(object, object.GroupVersionKind())
svc.OwnerReferences = []metav1.OwnerReference{*ref}
var ports []corev1.ServicePort
for _, sp := range srPorts {
servicePort := corev1.ServicePort{
Name: sp.Name,
Port: sp.Port,
NodePort: sp.NodePort,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt(int(sp.ContainerPort)),
}
if servicePort.Name == FeQueryPortName {
servicePort.AppProtocol = func() *string {
protocol := "mysql"
return &protocol
}()
}
ports = append(ports, servicePort)
}
// set Ports field before calculate resource hash
svc.Spec.Ports = ports
svc.Annotations = anno
anno[srapi.ComponentResourceHash] = hash.HashObject(serviceHashObject(&svc))
svc.Annotations = anno
return svc
}
func getFeServicePorts(config map[string]interface{}, service *srapi.StarRocksService) (srPorts []srapi.StarRocksServicePort) {
httpPort := GetPort(config, HTTP_PORT)
rpcPort := GetPort(config, RPC_PORT)
queryPort := GetPort(config, QUERY_PORT)
editPort := GetPort(config, EDIT_LOG_PORT)
srPorts = append(srPorts, mergePort(service, srapi.StarRocksServicePort{
Port: httpPort, ContainerPort: httpPort, Name: FeHTTPPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: rpcPort, ContainerPort: rpcPort, Name: FeRPCPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: queryPort, ContainerPort: queryPort, Name: FeQueryPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: editPort, ContainerPort: editPort, Name: FeEditLogPortName,
}))
return srPorts
}
func getBeServicePorts(config map[string]interface{}, service *srapi.StarRocksService) (srPorts []srapi.StarRocksServicePort) {
bePort := GetPort(config, BE_PORT)
webserverPort := GetPort(config, WEBSERVER_PORT)
heartPort := GetPort(config, HEARTBEAT_SERVICE_PORT)
brpcPort := GetPort(config, BRPC_PORT)
srPorts = append(srPorts, mergePort(service, srapi.StarRocksServicePort{
Port: bePort, ContainerPort: bePort, Name: BePortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: webserverPort, ContainerPort: webserverPort, Name: BeWebserverPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: heartPort, ContainerPort: heartPort, Name: BeHeartbeatPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: brpcPort, ContainerPort: brpcPort, Name: BeBrpcPortName,
}))
return srPorts
}
func getCnServicePorts(config map[string]interface{}, service *srapi.StarRocksService) (srPorts []srapi.StarRocksServicePort) {
thriftPort := GetPort(config, THRIFT_PORT)
webserverPort := GetPort(config, WEBSERVER_PORT)
heartPort := GetPort(config, HEARTBEAT_SERVICE_PORT)
brpcPort := GetPort(config, BRPC_PORT)
srPorts = append(srPorts, mergePort(service, srapi.StarRocksServicePort{
Port: thriftPort, ContainerPort: thriftPort, Name: CnThriftPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: webserverPort, ContainerPort: webserverPort, Name: CnWebserverPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: heartPort, ContainerPort: heartPort, Name: CnHeartbeatPortName,
}), mergePort(service, srapi.StarRocksServicePort{
Port: brpcPort, ContainerPort: brpcPort, Name: CnBrpcPortName,
}))
return srPorts
}
func setServiceType(svc *srapi.StarRocksService, service *corev1.Service) {
service.Spec.Type = corev1.ServiceTypeClusterIP
if svc != nil && svc.Type != "" {
service.Spec.Type = svc.Type
}
if service.Spec.Type == corev1.ServiceTypeLoadBalancer && svc.LoadBalancerIP != "" {
service.Spec.LoadBalancerIP = svc.LoadBalancerIP
}
}
func mergePort(service *srapi.StarRocksService, defaultPort srapi.StarRocksServicePort) srapi.StarRocksServicePort {
if service == nil || service.Ports == nil {
return defaultPort
}
port := defaultPort
// If containerPort is match, use the nodePort
for _, sp := range service.Ports {
if sp.ContainerPort == defaultPort.ContainerPort {
if sp.NodePort != 0 {
port.NodePort = sp.NodePort
}
if sp.Port != 0 {
port.Port = sp.Port
}
return port
}
}
// If containerPort is not match, e.g. the value of containerPort is 0, use the name to match
for _, sp := range service.Ports {
if sp.Name == defaultPort.Name {
if sp.NodePort != 0 {
port.NodePort = sp.NodePort
}
if sp.Port != 0 {
port.Port = sp.Port
}
break
}
}
return port
}
func getServiceAnnotations(svc *srapi.StarRocksService) map[string]string {
if svc != nil && svc.Annotations != nil {
annotations := map[string]string{}
for key, val := range svc.Annotations {
annotations[key] = val
}
return annotations
}
return map[string]string{}
}
func ServiceDeepEqual(expectSvc, actualSvc *corev1.Service) bool {
var expectHashValue string
if _, ok := expectSvc.Annotations[srapi.ComponentResourceHash]; ok {
expectHashValue = expectSvc.Annotations[srapi.ComponentResourceHash]
} else {
expectHashValue = hash.HashObject(serviceHashObject(expectSvc))
}
// Because service annotations will be updated by k8s controller manager, so we should get hash value from
// srapi.ComponentResourceHash annotation.
var actualHashValue string
if _, ok := actualSvc.Annotations[srapi.ComponentResourceHash]; ok {
actualHashValue = actualSvc.Annotations[srapi.ComponentResourceHash]
} else {
actualHashValue = hash.HashObject(serviceHashObject(actualSvc))
}
return expectHashValue == actualHashValue && expectSvc.Namespace == actualSvc.Namespace
}
func serviceHashObject(svc *corev1.Service) hashService {
return hashService{
name: svc.Name,
namespace: svc.Namespace,
finalizers: svc.Finalizers,
ports: svc.Spec.Ports,
selector: svc.Spec.Selector,
serviceType: svc.Spec.Type,
labels: svc.Labels,
annotations: svc.Annotations,
}
}
func HaveEqualOwnerReference(svc1 *corev1.Service, svc2 *corev1.Service) bool {
set := make(map[string]bool)
for _, o := range svc1.OwnerReferences {
key := o.Kind + o.Name
set[key] = true
}
for _, o := range svc2.OwnerReferences {
key := o.Kind + o.Name
if _, ok := set[key]; ok {
return true
}
}
return false
}