forked from gliderlabs/registrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsul.go
155 lines (141 loc) · 4.43 KB
/
consul.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
package consul
import (
"fmt"
"log"
"net/url"
"strings"
"os"
"github.com/gliderlabs/registrator/bridge"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/go-cleanhttp"
)
const DefaultInterval = "10s"
func init() {
f := new(Factory)
bridge.Register(f, "consul")
bridge.Register(f, "consul-tls")
bridge.Register(f, "consul-unix")
}
func (r *ConsulAdapter) interpolateService(script string, service *bridge.Service) string {
withIp := strings.Replace(script, "$SERVICE_IP", service.Origin.HostIP, -1)
withPort := strings.Replace(withIp, "$SERVICE_PORT", service.Origin.HostPort, -1)
return withPort
}
type Factory struct{}
func (f *Factory) New(uri *url.URL) bridge.RegistryAdapter {
config := consulapi.DefaultConfig()
if uri.Scheme == "consul-unix" {
config.Address = strings.TrimPrefix(uri.String(), "consul-")
} else if uri.Scheme == "consul-tls" {
tlsConfigDesc := &consulapi.TLSConfig {
Address: uri.Host,
CAFile: os.Getenv("CONSUL_CACERT"),
CertFile: os.Getenv("CONSUL_TLSCERT"),
KeyFile: os.Getenv("CONSUL_TLSKEY"),
InsecureSkipVerify: false,
}
tlsConfig, err := consulapi.SetupTLSConfig(tlsConfigDesc)
if err != nil {
log.Fatal("Cannot set up Consul TLSConfig", err)
}
config.Scheme = "https"
transport := cleanhttp.DefaultPooledTransport()
transport.TLSClientConfig = tlsConfig
config.HttpClient.Transport = transport
config.Address = uri.Host
} else if uri.Host != "" {
config.Address = uri.Host
}
client, err := consulapi.NewClient(config)
if err != nil {
log.Fatal("consul: ", uri.Scheme)
}
return &ConsulAdapter{client: client}
}
type ConsulAdapter struct {
client *consulapi.Client
}
// Ping will try to connect to consul by attempting to retrieve the current leader.
func (r *ConsulAdapter) Ping() error {
status := r.client.Status()
leader, err := status.Leader()
if err != nil {
return err
}
log.Println("consul: current leader ", leader)
return nil
}
func (r *ConsulAdapter) Register(service *bridge.Service) error {
registration := new(consulapi.AgentServiceRegistration)
registration.ID = service.ID
registration.Name = service.Name
registration.Port = service.Port
registration.Tags = service.Tags
registration.Address = service.IP
registration.Check = r.buildCheck(service)
return r.client.Agent().ServiceRegister(registration)
}
func (r *ConsulAdapter) buildCheck(service *bridge.Service) *consulapi.AgentServiceCheck {
check := new(consulapi.AgentServiceCheck)
if status := service.Attrs["check_initial_status"]; status != "" {
check.Status = status
}
if path := service.Attrs["check_http"]; path != "" {
check.HTTP = fmt.Sprintf("http://%s:%d%s", service.IP, service.Port, path)
if timeout := service.Attrs["check_timeout"]; timeout != "" {
check.Timeout = timeout
}
} else if path := service.Attrs["check_https"]; path != "" {
check.HTTP = fmt.Sprintf("https://%s:%d%s", service.IP, service.Port, path)
if timeout := service.Attrs["check_timeout"]; timeout != "" {
check.Timeout = timeout
}
} else if cmd := service.Attrs["check_cmd"]; cmd != "" {
check.Script = fmt.Sprintf("check-cmd %s %s %s", service.Origin.ContainerID[:12], service.Origin.ExposedPort, cmd)
} else if script := service.Attrs["check_script"]; script != "" {
check.Script = r.interpolateService(script, service)
} else if ttl := service.Attrs["check_ttl"]; ttl != "" {
check.TTL = ttl
} else if tcp := service.Attrs["check_tcp"]; tcp != "" {
check.TCP = fmt.Sprintf("%s:%d", service.IP, service.Port)
if timeout := service.Attrs["check_timeout"]; timeout != "" {
check.Timeout = timeout
}
} else {
return nil
}
if check.Script != "" || check.HTTP != "" || check.TCP != "" {
if interval := service.Attrs["check_interval"]; interval != "" {
check.Interval = interval
} else {
check.Interval = DefaultInterval
}
}
return check
}
func (r *ConsulAdapter) Deregister(service *bridge.Service) error {
return r.client.Agent().ServiceDeregister(service.ID)
}
func (r *ConsulAdapter) Refresh(service *bridge.Service) error {
return nil
}
func (r *ConsulAdapter) Services() ([]*bridge.Service, error) {
services, err := r.client.Agent().Services()
if err != nil {
return []*bridge.Service{}, err
}
out := make([]*bridge.Service, len(services))
i := 0
for _, v := range services {
s := &bridge.Service{
ID: v.ID,
Name: v.Service,
Port: v.Port,
Tags: v.Tags,
IP: v.Address,
}
out[i] = s
i++
}
return out, nil
}