2015-01-13 23:31:27 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2016-01-16 20:25:18 +00:00
|
|
|
"crypto/tls"
|
2015-05-25 22:14:28 +01:00
|
|
|
"encoding/json"
|
2015-01-13 23:31:27 +00:00
|
|
|
"errors"
|
2015-11-16 12:11:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-01-16 20:25:18 +00:00
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2015-01-19 19:37:28 +00:00
|
|
|
consul "github.com/hashicorp/consul/api"
|
2015-01-13 23:31:27 +00:00
|
|
|
)
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
type consulRegistry struct {
|
2015-02-14 23:00:47 +00:00
|
|
|
Address string
|
|
|
|
Client *consul.Client
|
2015-12-19 18:28:08 +00:00
|
|
|
Options Options
|
2015-05-25 22:14:28 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 23:39:47 +00:00
|
|
|
func newTransport(config *tls.Config) *http.Transport {
|
|
|
|
if config == nil {
|
|
|
|
config = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 20:25:18 +00:00
|
|
|
t := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
2016-01-16 23:39:47 +00:00
|
|
|
TLSClientConfig: config,
|
2016-01-16 20:25:18 +00:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(&t, func(tr **http.Transport) {
|
|
|
|
(*tr).CloseIdleConnections()
|
|
|
|
})
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2015-10-11 12:05:20 +01:00
|
|
|
func encodeEndpoints(en []*Endpoint) []string {
|
|
|
|
var tags []string
|
|
|
|
for _, e := range en {
|
|
|
|
if b, err := json.Marshal(e); err == nil {
|
|
|
|
tags = append(tags, "e="+string(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeEndpoints(tags []string) []*Endpoint {
|
|
|
|
var en []*Endpoint
|
|
|
|
for _, tag := range tags {
|
|
|
|
if len(tag) == 0 || tag[0] != 'e' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Endpoint
|
|
|
|
if err := json.Unmarshal([]byte(tag[2:]), &e); err == nil {
|
|
|
|
en = append(en, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return en
|
|
|
|
}
|
|
|
|
|
2015-05-26 22:39:48 +01:00
|
|
|
func encodeMetadata(md map[string]string) []string {
|
2015-05-25 22:14:28 +01:00
|
|
|
var tags []string
|
|
|
|
for k, v := range md {
|
|
|
|
if b, err := json.Marshal(map[string]string{
|
|
|
|
k: v,
|
|
|
|
}); err == nil {
|
2015-10-11 12:05:20 +01:00
|
|
|
tags = append(tags, "t="+string(b))
|
2015-05-25 22:14:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
2015-05-26 22:39:48 +01:00
|
|
|
func decodeMetadata(tags []string) map[string]string {
|
2015-05-25 22:14:28 +01:00
|
|
|
md := make(map[string]string)
|
|
|
|
for _, tag := range tags {
|
2015-10-11 12:05:20 +01:00
|
|
|
if len(tag) == 0 || tag[0] != 't' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
var kv map[string]string
|
2015-10-11 12:05:20 +01:00
|
|
|
if err := json.Unmarshal([]byte(tag[2:]), &kv); err == nil {
|
2015-05-25 22:14:28 +01:00
|
|
|
for k, v := range kv {
|
|
|
|
md[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return md
|
2015-02-14 23:00:47 +00:00
|
|
|
}
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2016-01-26 20:30:05 +00:00
|
|
|
func encodeVersion(v string) string {
|
|
|
|
return "v=" + v
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeVersion(tags []string) (string, bool) {
|
|
|
|
for _, tag := range tags {
|
|
|
|
if len(tag) == 0 || tag[0] != 'v' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return tag[2:], true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func newConsulRegistry(addrs []string, opts ...Option) Registry {
|
2015-12-19 18:28:08 +00:00
|
|
|
var opt Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// use default config
|
2015-05-23 20:04:16 +01:00
|
|
|
config := consul.DefaultConfig()
|
2015-12-19 18:28:08 +00:00
|
|
|
|
|
|
|
// set timeout
|
|
|
|
if opt.Timeout > 0 {
|
|
|
|
config.HttpClient.Timeout = opt.Timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there are any addrs
|
2015-05-23 20:04:16 +01:00
|
|
|
if len(addrs) > 0 {
|
2015-11-16 12:11:03 +00:00
|
|
|
addr, port, err := net.SplitHostPort(addrs[0])
|
|
|
|
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
|
|
|
|
port = "8500"
|
2016-01-26 20:30:05 +00:00
|
|
|
addr = addrs[0]
|
2015-11-16 12:11:03 +00:00
|
|
|
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
|
|
|
} else if err == nil {
|
|
|
|
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
|
|
|
}
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|
2015-12-19 18:28:08 +00:00
|
|
|
|
2016-01-16 20:25:18 +00:00
|
|
|
// requires secure connection?
|
2016-01-17 00:33:07 +00:00
|
|
|
if opt.Secure || opt.TLSConfig != nil {
|
2016-01-16 20:25:18 +00:00
|
|
|
config.Scheme = "https"
|
|
|
|
// We're going to support InsecureSkipVerify
|
2016-01-16 23:39:47 +00:00
|
|
|
config.HttpClient.Transport = newTransport(opt.TLSConfig)
|
2016-01-16 20:25:18 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 18:28:08 +00:00
|
|
|
// create the client
|
2015-10-26 20:23:57 +00:00
|
|
|
client, _ := consul.NewClient(config)
|
2015-05-23 20:04:16 +01:00
|
|
|
|
|
|
|
cr := &consulRegistry{
|
2015-12-05 01:12:29 +00:00
|
|
|
Address: config.Address,
|
|
|
|
Client: client,
|
2015-12-19 18:28:08 +00:00
|
|
|
Options: opt,
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return cr
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
func (c *consulRegistry) Deregister(s *Service) error {
|
|
|
|
if len(s.Nodes) == 0 {
|
2015-01-13 23:31:27 +00:00
|
|
|
return errors.New("Require at least one node")
|
|
|
|
}
|
2016-01-26 20:44:29 +00:00
|
|
|
|
|
|
|
node := s.Nodes[0]
|
2016-01-26 23:32:27 +00:00
|
|
|
return c.Client.Agent().ServiceDeregister(node.Id)
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 23:32:27 +00:00
|
|
|
func (c *consulRegistry) Register(s *Service, opts ...RegisterOption) error {
|
2015-05-25 22:14:28 +01:00
|
|
|
if len(s.Nodes) == 0 {
|
2015-01-13 23:31:27 +00:00
|
|
|
return errors.New("Require at least one node")
|
|
|
|
}
|
|
|
|
|
2016-01-26 23:32:27 +00:00
|
|
|
var options RegisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
node := s.Nodes[0]
|
|
|
|
|
2015-05-26 22:39:48 +01:00
|
|
|
tags := encodeMetadata(node.Metadata)
|
2015-10-11 12:05:20 +01:00
|
|
|
tags = append(tags, encodeEndpoints(s.Endpoints)...)
|
2016-01-26 20:30:05 +00:00
|
|
|
tags = append(tags, encodeVersion(s.Version))
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2016-01-27 00:32:16 +00:00
|
|
|
var check *consul.AgentServiceCheck
|
|
|
|
|
|
|
|
if options.TTL > time.Duration(0) {
|
|
|
|
check = &consul.AgentServiceCheck{
|
|
|
|
TTL: fmt.Sprintf("%v", options.TTL),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:15:46 +00:00
|
|
|
if err := c.Client.Agent().ServiceRegister(&consul.AgentServiceRegistration{
|
|
|
|
ID: node.Id,
|
|
|
|
Name: s.Name,
|
|
|
|
Tags: tags,
|
|
|
|
Port: node.Port,
|
2015-05-25 22:14:28 +01:00
|
|
|
Address: node.Address,
|
2016-01-27 00:32:16 +00:00
|
|
|
Check: check,
|
2016-01-27 00:15:46 +00:00
|
|
|
}); err != nil {
|
2016-01-26 20:30:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2016-01-27 00:32:16 +00:00
|
|
|
if options.TTL == time.Duration(0) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:15:46 +00:00
|
|
|
return c.Client.Agent().PassTTL("service:"+node.Id, "")
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 01:48:48 +00:00
|
|
|
func (c *consulRegistry) GetService(name string) ([]*Service, error) {
|
2016-01-26 23:32:27 +00:00
|
|
|
rsp, _, err := c.Client.Health().Service(name, "", true, nil)
|
2015-01-13 23:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-08 01:48:48 +00:00
|
|
|
serviceMap := map[string]*Service{}
|
2015-01-13 23:31:27 +00:00
|
|
|
|
|
|
|
for _, s := range rsp {
|
2016-01-26 23:32:27 +00:00
|
|
|
if s.Service.Service != name {
|
2015-01-13 23:31:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-26 20:30:05 +00:00
|
|
|
// version is now a tag
|
2016-01-26 23:32:27 +00:00
|
|
|
version, found := decodeVersion(s.Service.Tags)
|
2016-01-26 20:30:05 +00:00
|
|
|
// service ID is now the node id
|
2016-01-26 23:32:27 +00:00
|
|
|
id := s.Service.ID
|
2016-01-26 20:30:05 +00:00
|
|
|
// key is always the version
|
|
|
|
key := version
|
|
|
|
// address is service address
|
2016-01-26 23:32:27 +00:00
|
|
|
address := s.Service.Address
|
2016-01-26 20:30:05 +00:00
|
|
|
|
|
|
|
// if we can't get the new type of version
|
|
|
|
// use old the old ways
|
|
|
|
if !found {
|
|
|
|
// id was set as node
|
2016-01-26 23:32:27 +00:00
|
|
|
id = s.Node.Node
|
2016-01-26 20:30:05 +00:00
|
|
|
// key was service id
|
2016-01-26 23:32:27 +00:00
|
|
|
key = s.Service.ID
|
2016-01-26 20:30:05 +00:00
|
|
|
// version was service id
|
2016-01-26 23:32:27 +00:00
|
|
|
version = s.Service.ID
|
2016-01-26 20:30:05 +00:00
|
|
|
// address was address
|
2016-01-26 23:32:27 +00:00
|
|
|
address = s.Node.Address
|
2015-11-08 01:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
svc, ok := serviceMap[key]
|
|
|
|
if !ok {
|
|
|
|
svc = &Service{
|
2016-01-26 23:32:27 +00:00
|
|
|
Endpoints: decodeEndpoints(s.Service.Tags),
|
|
|
|
Name: s.Service.Service,
|
2015-11-08 01:48:48 +00:00
|
|
|
Version: version,
|
|
|
|
}
|
|
|
|
serviceMap[key] = svc
|
|
|
|
}
|
|
|
|
|
|
|
|
svc.Nodes = append(svc.Nodes, &Node{
|
|
|
|
Id: id,
|
2016-01-26 20:30:05 +00:00
|
|
|
Address: address,
|
2016-01-26 23:32:27 +00:00
|
|
|
Port: s.Service.Port,
|
|
|
|
Metadata: decodeMetadata(s.Service.Tags),
|
2015-01-13 23:31:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-08 01:48:48 +00:00
|
|
|
var services []*Service
|
|
|
|
for _, service := range serviceMap {
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
return services, nil
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
func (c *consulRegistry) ListServices() ([]*Service, error) {
|
2015-12-19 18:28:08 +00:00
|
|
|
rsp, _, err := c.Client.Catalog().Services(nil)
|
2015-04-03 00:52:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-05 01:12:29 +00:00
|
|
|
var services []*Service
|
|
|
|
|
2015-04-03 00:52:49 +02:00
|
|
|
for service, _ := range rsp {
|
2015-05-25 22:14:28 +01:00
|
|
|
services = append(services, &Service{Name: service})
|
2015-04-03 00:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
func (c *consulRegistry) Watch() (Watcher, error) {
|
|
|
|
return newConsulWatcher(c)
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
2015-12-19 21:56:14 +00:00
|
|
|
|
|
|
|
func (c *consulRegistry) String() string {
|
|
|
|
return "consul"
|
|
|
|
}
|