-
Notifications
You must be signed in to change notification settings - Fork 8
/
discovery.go
463 lines (416 loc) · 11.7 KB
/
discovery.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package connector
import (
"context"
"fmt"
"strconv"
"strings"
"time"
consul "github.com/hashicorp/consul/api"
eureka "github.com/hudl/fargo"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
nacos "github.com/nacos-group/nacos-sdk-go/v2/model"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
ctv1 "github.com/flomesh-io/fsm/pkg/apis/connector/v1alpha1"
machinev1alpha1 "github.com/flomesh-io/fsm/pkg/apis/machine/v1alpha1"
)
const (
CONSUL_METADATA_GRPC_PORT = "gRPC.port="
)
const (
EUREKA_METADATA_GRPC_PORT = "gRPC__port"
EUREKA_METADATA_MGMT_PORT = "management.port"
)
const (
NACOS_METADATA_GRPC_PORT = "gRPC_port"
NACOS_DEFAULT_CLUSTER = "DEFAULT"
)
type MicroService struct {
Service string
Namespace string
}
type ServiceAddress struct {
HostName string
Port int32
}
// AgentCheck represents a check known to the agent
type AgentCheck struct {
CheckID string
ServiceID string
Name string
Namespace string
Type string
Status string
Output string
}
func (ac *AgentCheck) ToConsul() *consul.AgentCheck {
check := new(consul.AgentCheck)
check.CheckID = ac.CheckID
check.ServiceID = ac.ServiceID
check.Name = ac.Name
check.Namespace = ac.Namespace
check.Type = ac.Type
check.Status = ac.Status
check.Output = ac.Output
return check
}
type AgentWeights struct {
Passing int
Warning int
}
func (aw *AgentWeights) ToConsul() consul.AgentWeights {
return consul.AgentWeights{
Passing: aw.Passing,
Warning: aw.Warning,
}
}
func (aw *AgentWeights) FromConsul(w consul.AgentWeights) {
aw.Passing = w.Passing
aw.Warning = w.Warning
}
// AgentService represents a service known to the agent
type AgentService struct {
MicroService
ID string
InstanceId string
ClusterId string
Address string
HTTPPort int
GRPCPort int
ViaAddress string
ViaPort int
Weights AgentWeights
Tags []string
Meta map[string]interface{}
HealthCheck bool
}
func (as *AgentService) ToConsul() *consul.AgentService {
agentService := new(consul.AgentService)
agentService.ID = as.ID
agentService.Service = as.Service
agentService.Namespace = as.Namespace
agentService.Address = as.Address
agentService.Port = as.HTTPPort
agentService.Weights = as.Weights.ToConsul()
if len(as.Tags) > 0 {
agentService.Tags = append(agentService.Tags, as.Tags...)
}
agentService.Tags = append(agentService.Tags, "secure=false")
if as.GRPCPort > 0 {
agentService.Tags = append(agentService.Tags, fmt.Sprintf("%s%d", CONSUL_METADATA_GRPC_PORT, as.GRPCPort))
}
if len(as.Meta) > 0 {
agentService.Meta = make(map[string]string)
for k, v := range as.Meta {
agentService.Meta[k] = v.(string)
}
}
return agentService
}
func (as *AgentService) FromConsul(agentService *consul.AgentService) {
as.ID = agentService.ID
as.Service = agentService.Service
as.Namespace = agentService.Namespace
as.Address = agentService.Address
as.HTTPPort = agentService.Port
as.Weights.FromConsul(agentService.Weights)
if len(agentService.Tags) > 0 {
for _, tag := range agentService.Tags {
if strings.HasPrefix(tag, CONSUL_METADATA_GRPC_PORT) {
if segs := strings.Split(tag, "="); len(segs) == 2 {
if grpcPort, convErr := strconv.Atoi(segs[1]); convErr == nil {
as.GRPCPort = grpcPort
}
}
}
as.Tags = append(as.Tags, tag)
}
}
if len(agentService.Meta) > 0 {
as.Meta = make(map[string]interface{})
for k, v := range agentService.Meta {
as.Meta[k] = v
}
}
}
func (as *AgentService) FromEureka(ins *eureka.Instance) {
if ins == nil {
return
}
as.ID = ins.Id()
as.Service = strings.ToLower(ins.VipAddress)
as.InstanceId = ins.InstanceId
as.Address = ins.IPAddr
as.HTTPPort = ins.Port
metadata := ins.Metadata.GetMap()
if len(metadata) > 0 {
as.Meta = make(map[string]interface{})
for k, v := range metadata {
if strings.EqualFold(k, EUREKA_METADATA_GRPC_PORT) {
if grpcPort, ok := v.(float64); ok {
as.GRPCPort = int(grpcPort)
}
}
as.Meta[k] = v
}
}
}
func (as *AgentService) FromNacos(ins *nacos.Instance) {
if ins == nil {
return
}
as.ID = ins.InstanceId
as.Service = strings.ToLower(strings.Split(ins.ServiceName, constant.SERVICE_INFO_SPLITER)[1])
as.InstanceId = ins.InstanceId
as.Address = ins.Ip
as.HTTPPort = int(ins.Port)
if len(ins.Metadata) > 0 {
as.Meta = make(map[string]interface{})
for k, v := range ins.Metadata {
if strings.EqualFold(k, NACOS_METADATA_GRPC_PORT) {
if grpcPort, err := strconv.ParseInt(v, 10, 32); err == nil {
as.GRPCPort = int(grpcPort)
}
}
as.Meta[k] = v
}
}
}
func (as *AgentService) FromVM(vm machinev1alpha1.VirtualMachine, svc machinev1alpha1.ServiceSpec) {
as.ID = fmt.Sprintf("%s-%s", svc.ServiceName, vm.UID)
as.Service = svc.ServiceName
as.InstanceId = fmt.Sprintf("%s-%s-%s", vm.Name, svc.ServiceName, vm.UID)
as.Address = vm.Spec.MachineIP
as.HTTPPort = int(svc.Port)
metadata := vm.Labels
if len(metadata) > 0 {
as.Meta = make(map[string]interface{})
for k, v := range metadata {
as.Meta[k] = v
}
}
}
type CatalogDeregistration struct {
MicroService
Node string
ServiceID string
}
func (cdr *CatalogDeregistration) ToConsul() *consul.CatalogDeregistration {
r := new(consul.CatalogDeregistration)
r.Node = cdr.Node
r.ServiceID = cdr.ServiceID
r.Namespace = cdr.Namespace
return r
}
func (cdr *CatalogDeregistration) ToEureka() *eureka.Instance {
r := new(eureka.Instance)
r.InstanceId = cdr.ServiceID
r.App = strings.ToUpper(cdr.Service)
return r
}
func (cdr *CatalogDeregistration) ToNacos() *vo.DeregisterInstanceParam {
svcInfoSegs := strings.Split(cdr.ServiceID, constant.SERVICE_INFO_SPLITER)
if len(svcInfoSegs) < 4 {
return nil
}
r := new(vo.DeregisterInstanceParam)
r.ServiceName = svcInfoSegs[1]
insInfoSegs := strings.Split(svcInfoSegs[0], constant.NAMING_INSTANCE_ID_SPLITTER)
r.Ip = insInfoSegs[0]
r.Port, _ = strconv.ParseUint(insInfoSegs[1], 10, 64)
r.Cluster = insInfoSegs[2]
r.GroupName = insInfoSegs[3]
r.Ephemeral = true
return r
}
type CatalogRegistration struct {
Node string
Address string
NodeMeta map[string]string
Service *AgentService
Check *AgentCheck
SkipNodeUpdate bool
}
func (cr *CatalogRegistration) ToConsul() *consul.CatalogRegistration {
r := new(consul.CatalogRegistration)
r.Node = cr.Node
r.Address = cr.Address
if len(cr.NodeMeta) > 0 {
r.NodeMeta = make(map[string]string)
for k, v := range cr.NodeMeta {
r.NodeMeta[k] = v
}
}
if cr.Service != nil {
r.Service = cr.Service.ToConsul()
}
if cr.Check != nil {
r.Check = cr.Check.ToConsul()
}
r.SkipNodeUpdate = cr.SkipNodeUpdate
return r
}
func (cr *CatalogRegistration) ToEureka() *eureka.Instance {
r := new(eureka.Instance)
if len(cr.NodeMeta) > 0 {
for k, v := range cr.NodeMeta {
r.SetMetadataString(k, v)
}
}
if cr.Service != nil {
r.UniqueID = func(i eureka.Instance) string {
return cr.Service.ID
}
r.InstanceId = cr.Service.ID
r.HostName = cr.Service.Address
r.IPAddr = cr.Service.Address
r.App = strings.ToUpper(cr.Service.Service)
r.VipAddress = strings.ToUpper(cr.Service.Service)
r.SecureVipAddress = strings.ToUpper(cr.Service.Service)
r.Port = cr.Service.HTTPPort
r.PortEnabled = true
r.Status = eureka.UP
r.DataCenterInfo = eureka.DataCenterInfo{Name: eureka.MyOwn}
rMetadata := r.Metadata.GetMap()
if len(cr.Service.Meta) > 0 {
for k, v := range cr.Service.Meta {
rMetadata[k] = v
}
}
if cr.Service.GRPCPort > 0 {
rMetadata[EUREKA_METADATA_GRPC_PORT] = fmt.Sprintf("%d", cr.Service.GRPCPort)
rMetadata[EUREKA_METADATA_MGMT_PORT] = fmt.Sprintf("%d", cr.Service.HTTPPort)
}
//r.HomePageUrl = fmt.Sprintf("http://%s:%d/", cr.Service.Address, cr.Service.HTTPPort)
//r.StatusPageUrl = fmt.Sprintf("http://%s:%d/actuator/info", cr.Service.Address, cr.Service.HTTPPort)
//r.HealthCheckUrl = fmt.Sprintf("http://%s:%d/actuator/health", cr.Service.Address, cr.Service.HTTPPort)
}
return r
}
func (cr *CatalogRegistration) ToNacos(cluster, group string, weight float64) *vo.RegisterInstanceParam {
r := new(vo.RegisterInstanceParam)
r.Metadata = make(map[string]string)
if len(cr.NodeMeta) > 0 {
for k, v := range cr.NodeMeta {
r.Metadata[k] = v
}
}
if cr.Service != nil {
r.ClusterName = cluster
r.GroupName = group
r.ServiceName = strings.ToLower(cr.Service.Service)
r.Ip = cr.Service.Address
r.Port = uint64(cr.Service.HTTPPort)
r.Weight = weight
r.Enable = true
r.Healthy = true
r.Ephemeral = true
if len(cr.Service.Meta) > 0 {
for k, v := range cr.Service.Meta {
r.Metadata[k] = fmt.Sprintf("%v", v)
}
}
if cr.Service.GRPCPort > 0 {
r.Metadata[NACOS_METADATA_GRPC_PORT] = fmt.Sprintf("%d", cr.Service.GRPCPort)
}
}
return r
}
type CatalogService struct {
Node string
ServiceID string
ServiceName string
}
func (cs *CatalogService) FromConsul(svc *consul.CatalogService) {
if svc == nil {
return
}
cs.Node = svc.Node
cs.ServiceID = svc.ServiceID
cs.ServiceName = svc.ServiceName
}
func (cs *CatalogService) FromEureka(svc *eureka.Instance) {
if svc == nil {
return
}
cs.Node = svc.DataCenterInfo.Name
cs.ServiceID = svc.Id()
cs.ServiceName = strings.ToLower(svc.App)
}
func (cs *CatalogService) FromNacos(svc *nacos.Instance) {
if svc == nil {
return
}
cs.Node = svc.ClusterName
cs.ServiceID = svc.InstanceId
cs.ServiceName = strings.ToLower(strings.Split(svc.ServiceName, constant.SERVICE_INFO_SPLITER)[1])
}
// QueryOptions are used to parameterize a query
type QueryOptions struct {
// AllowStale allows any Consul server (non-leader) to service
// a read. This allows for lower latency and higher throughput
AllowStale bool
// Namespace overrides the `default` namespace
// Note: Namespaces are available only in Consul Enterprise
Namespace string
// WaitIndex is used to enable a blocking query. Waits
// until the timeout or the next index is reached
WaitIndex uint64
// WaitTime is used to bound the duration of a wait.
// Defaults to that of the Config, but can be overridden.
WaitTime time.Duration
// Providing a peer name in the query option
Peer string
// Filter requests filtering data prior to it being returned. The string
// is a go-bexpr compatible expression.
Filter string
// ctx is an optional context pass through to the underlying HTTP
// request layer. Use Context() and WithContext() to manage this.
ctx context.Context
}
func (o *QueryOptions) Context() context.Context {
if o != nil && o.ctx != nil {
return o.ctx
}
return context.Background()
}
func (o *QueryOptions) WithContext(ctx context.Context) *QueryOptions {
o2 := new(QueryOptions)
if o != nil {
*o2 = *o
}
o2.ctx = ctx
return o2
}
func (o *QueryOptions) ToConsul() *consul.QueryOptions {
opts := new(consul.QueryOptions)
opts.AllowStale = o.AllowStale
opts.Namespace = o.Namespace
opts.WaitIndex = o.WaitIndex
opts.WaitTime = o.WaitTime
opts.Peer = o.Peer
opts.Filter = o.Filter
opts.WithContext(o.Context())
return opts
}
type ServiceDiscoveryClient interface {
CatalogServices(q *QueryOptions) ([]MicroService, error)
CatalogInstances(service string, q *QueryOptions) ([]*AgentService, error)
RegisteredInstances(service string, q *QueryOptions) ([]*CatalogService, error)
RegisteredServices(q *QueryOptions) ([]MicroService, error)
Register(reg *CatalogRegistration) error
Deregister(dereg *CatalogDeregistration) error
EnableNamespaces() bool
EnsureNamespaceExists(ns string) (bool, error)
RegisteredNamespace(kubeNS string) string
MicroServiceProvider() ctv1.DiscoveryServiceProvider
IsInternalServices() bool
}
const (
// HealthAny is special, and is used as a wild card,
// not as a specific state.
HealthAny = "any"
HealthPassing = "passing"
HealthWarning = "warning"
HealthCritical = "critical"
HealthMaint = "maintenance"
)