-
Notifications
You must be signed in to change notification settings - Fork 18
/
service.go
57 lines (45 loc) · 1.53 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
package main
import "strings"
type Service struct {
// A list of hosts that make up the service.
Hosts []Host `json:"hosts"`
}
type Host struct {
// The IP address of the upstream host.
IPAddress string `json:"ip_address"`
// The port of the upstream host.
Port int32 `json:"port"`
Tags *Tags `json:"tags,omitempty"`
}
type Tags struct {
// The optional zone of the upstream host. Envoy uses the zone
// for various statistics and load balancing tasks.
AZ string `json:"az,omitempty"`
// The optional canary status of the upstream host. Envoy uses
// the canary status for various statistics and load balancing
// tasks.
Canary bool `json:"canary,omitempty"`
// The optional load balancing weight of the upstream host, in
// the range 1 - 100. Envoy uses the load balancing weight in
// some of the built in load balancers.
LoadBalancingWeight int32 `json:"load_balancing_weight,omitempty"`
}
func getService(consul *Consul, serviceName string) (*Service, error) {
hosts := make([]Host, 0)
// extract service name from dns.
// for example: we will get "redis" from "redis.service.dc1.consul"
s := strings.Split(serviceName, ".")
// get service address, ports and tags from consul
consulService, err := consul.GetService(s[0])
if err != nil {
return &Service{Hosts: hosts}, err
}
if consulService == nil {
return &Service{Hosts: hosts}, nil
}
// create service hosts
for _, srv := range consulService {
hosts = append(hosts, Host{IPAddress: srv.Address, Port: int32(srv.Port)})
}
return &Service{Hosts: hosts}, nil
}