2019-10-02 20:56:53 +03:00
|
|
|
// Package etcd provides an etcd service registry
|
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-10-03 18:16:25 +03:00
|
|
|
"net"
|
2019-10-02 20:56:53 +03:00
|
|
|
"path"
|
2019-10-10 21:16:31 +03:00
|
|
|
"sort"
|
2019-10-02 20:56:53 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/clientv3"
|
|
|
|
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
|
2020-06-19 16:58:16 +03:00
|
|
|
"github.com/coreos/etcd/mvcc/mvccpb"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/logger"
|
|
|
|
"github.com/micro/go-micro/v3/registry"
|
2019-10-02 20:56:53 +03:00
|
|
|
hash "github.com/mitchellh/hashstructure"
|
2019-12-13 06:22:05 +03:00
|
|
|
"go.uber.org/zap"
|
2019-10-02 20:56:53 +03:00
|
|
|
)
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
const (
|
|
|
|
prefix = "/micro/registry/"
|
|
|
|
defaultDomain = "micro"
|
2019-10-02 20:56:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type etcdRegistry struct {
|
|
|
|
client *clientv3.Client
|
|
|
|
options registry.Options
|
2019-10-06 11:54:26 +03:00
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
// register and leases are grouped by domain
|
2019-10-06 11:54:26 +03:00
|
|
|
sync.RWMutex
|
2020-06-19 16:58:16 +03:00
|
|
|
register map[string]register
|
|
|
|
leases map[string]leases
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
type register map[string]uint64
|
|
|
|
type leases map[string]clientv3.LeaseID
|
|
|
|
|
|
|
|
// NewRegistry returns an initialized etcd registry
|
2019-10-02 20:56:53 +03:00
|
|
|
func NewRegistry(opts ...registry.Option) registry.Registry {
|
|
|
|
e := &etcdRegistry{
|
|
|
|
options: registry.Options{},
|
2020-06-19 16:58:16 +03:00
|
|
|
register: make(map[string]register),
|
|
|
|
leases: make(map[string]leases),
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
configure(e, opts...)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2020-08-08 03:40:41 +03:00
|
|
|
func newClient(e *etcdRegistry) (*clientv3.Client, error) {
|
2019-10-02 20:56:53 +03:00
|
|
|
config := clientv3.Config{
|
|
|
|
Endpoints: []string{"127.0.0.1:2379"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.options.Timeout == 0 {
|
|
|
|
e.options.Timeout = 5 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.options.Secure || e.options.TLSConfig != nil {
|
|
|
|
tlsConfig := e.options.TLSConfig
|
|
|
|
if tlsConfig == nil {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config.TLS = tlsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.options.Context != nil {
|
|
|
|
u, ok := e.options.Context.Value(authKey{}).(*authCreds)
|
|
|
|
if ok {
|
|
|
|
config.Username = u.Username
|
|
|
|
config.Password = u.Password
|
|
|
|
}
|
2019-12-13 06:22:05 +03:00
|
|
|
cfg, ok := e.options.Context.Value(logConfigKey{}).(*zap.Config)
|
|
|
|
if ok && cfg != nil {
|
|
|
|
config.LogConfig = cfg
|
|
|
|
}
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cAddrs []string
|
|
|
|
|
2019-10-03 18:16:25 +03:00
|
|
|
for _, address := range e.options.Addrs {
|
|
|
|
if len(address) == 0 {
|
2019-10-02 20:56:53 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-03 18:16:25 +03:00
|
|
|
addr, port, err := net.SplitHostPort(address)
|
|
|
|
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
|
|
|
|
port = "2379"
|
|
|
|
addr = address
|
|
|
|
cAddrs = append(cAddrs, net.JoinHostPort(addr, port))
|
|
|
|
} else if err == nil {
|
|
|
|
cAddrs = append(cAddrs, net.JoinHostPort(addr, port))
|
|
|
|
}
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we got addrs then we'll update
|
|
|
|
if len(cAddrs) > 0 {
|
|
|
|
config.Endpoints = cAddrs
|
|
|
|
}
|
|
|
|
|
2020-08-08 00:46:05 +03:00
|
|
|
// check if the endpoints have https://
|
|
|
|
if config.TLS != nil {
|
|
|
|
for i, ep := range config.Endpoints {
|
|
|
|
if !strings.HasPrefix(ep, "https://") {
|
|
|
|
config.Endpoints[i] = "https://" + ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
cli, err := clientv3.New(config)
|
2020-08-08 03:40:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure will setup the registry with new options
|
|
|
|
func configure(e *etcdRegistry, opts ...registry.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&e.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the client
|
|
|
|
cli, err := newClient(e)
|
2019-10-02 20:56:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-08 01:09:06 +03:00
|
|
|
|
|
|
|
if e.client != nil {
|
|
|
|
e.client.Close()
|
|
|
|
}
|
|
|
|
|
2020-08-08 03:40:41 +03:00
|
|
|
// setup new client
|
2019-10-02 20:56:53 +03:00
|
|
|
e.client = cli
|
2020-08-08 03:40:41 +03:00
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 23:58:35 +03:00
|
|
|
// getName returns the domain and name
|
|
|
|
// it returns false if there's an issue
|
2020-08-06 14:42:14 +03:00
|
|
|
// the key is a path of /prefix/domain/name/id e.g /micro/registry/domain/service/uuid
|
2020-08-10 23:58:35 +03:00
|
|
|
func getName(key, prefix string) (string, string, bool) {
|
2020-08-06 14:42:14 +03:00
|
|
|
// strip the prefix from keys
|
|
|
|
key = strings.TrimPrefix(key, prefix)
|
|
|
|
|
|
|
|
// split the key so we remove domain
|
|
|
|
parts := strings.Split(key, "/")
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
2020-08-10 23:58:35 +03:00
|
|
|
return "", "", false
|
2020-08-06 14:42:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts[0]) == 0 {
|
|
|
|
parts = parts[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// we expect a domain and then name domain/service
|
|
|
|
if len(parts) < 2 {
|
2020-08-10 23:58:35 +03:00
|
|
|
return "", "", false
|
2020-08-06 14:42:14 +03:00
|
|
|
}
|
|
|
|
|
2020-08-10 23:58:35 +03:00
|
|
|
// return name, domain
|
|
|
|
return parts[0], parts[1], true
|
2020-08-06 14:42:14 +03:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
func encode(s *registry.Service) string {
|
|
|
|
b, _ := json.Marshal(s)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decode(ds []byte) *registry.Service {
|
|
|
|
var s *registry.Service
|
|
|
|
json.Unmarshal(ds, &s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
func nodePath(domain, s, id string) string {
|
2019-10-02 20:56:53 +03:00
|
|
|
service := strings.Replace(s, "/", "-", -1)
|
|
|
|
node := strings.Replace(id, "/", "-", -1)
|
2020-06-19 16:58:16 +03:00
|
|
|
return path.Join(prefixWithDomain(domain), service, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func servicePath(domain, s string) string {
|
|
|
|
return path.Join(prefixWithDomain(domain), serializeServiceName(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializeServiceName(s string) string {
|
|
|
|
return strings.ReplaceAll(s, "/", "-")
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
func prefixWithDomain(domain string) string {
|
|
|
|
return path.Join(prefix, domain)
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) Init(opts ...registry.Option) error {
|
|
|
|
return configure(e, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) Options() registry.Options {
|
|
|
|
return e.options
|
|
|
|
}
|
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
func (e *etcdRegistry) registerNode(s *registry.Service, node *registry.Node, opts ...registry.RegisterOption) error {
|
2019-10-02 20:56:53 +03:00
|
|
|
if len(s.Nodes) == 0 {
|
|
|
|
return errors.New("Require at least one node")
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
// parse the options
|
|
|
|
var options registry.RegisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
|
|
|
}
|
|
|
|
|
2020-06-29 18:37:45 +03:00
|
|
|
if s.Metadata == nil {
|
2020-08-10 23:58:35 +03:00
|
|
|
s.Metadata = map[string]string{}
|
2020-06-29 18:37:45 +03:00
|
|
|
}
|
2020-08-10 23:58:35 +03:00
|
|
|
if node.Metadata == nil {
|
|
|
|
node.Metadata = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the domain in metadata so it can be retrieved by wildcard queries
|
|
|
|
s.Metadata["domain"] = options.Domain
|
|
|
|
node.Metadata["domain"] = options.Domain
|
2020-06-29 18:37:45 +03:00
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
e.Lock()
|
|
|
|
// ensure the leases and registers are setup for this domain
|
|
|
|
if _, ok := e.leases[options.Domain]; !ok {
|
|
|
|
e.leases[options.Domain] = make(leases)
|
|
|
|
}
|
|
|
|
if _, ok := e.register[options.Domain]; !ok {
|
|
|
|
e.register[options.Domain] = make(register)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if we already have a lease cached
|
|
|
|
leaseID, ok := e.leases[options.Domain][s.Name+node.Id]
|
|
|
|
e.Unlock()
|
2019-10-02 20:56:53 +03:00
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
if !ok {
|
|
|
|
// missing lease, check if the key exists
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), e.options.Timeout)
|
|
|
|
defer cancel()
|
2019-10-02 20:56:53 +03:00
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
// look for the existing key
|
2020-06-19 16:58:16 +03:00
|
|
|
key := nodePath(options.Domain, s.Name, node.Id)
|
|
|
|
rsp, err := e.client.Get(ctx, key, clientv3.WithSerializable())
|
2019-10-02 20:56:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
// get the existing lease
|
|
|
|
for _, kv := range rsp.Kvs {
|
|
|
|
if kv.Lease > 0 {
|
|
|
|
leaseID = clientv3.LeaseID(kv.Lease)
|
2019-10-06 15:43:41 +03:00
|
|
|
|
|
|
|
// decode the existing node
|
|
|
|
srv := decode(kv.Value)
|
|
|
|
if srv == nil || len(srv.Nodes) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
// create hash of service; uint64
|
2019-10-06 15:43:41 +03:00
|
|
|
h, err := hash.Hash(srv.Nodes[0], nil)
|
2019-10-06 11:54:26 +03:00
|
|
|
if err != nil {
|
2019-10-06 15:43:41 +03:00
|
|
|
continue
|
2019-10-06 11:54:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the info
|
|
|
|
e.Lock()
|
2020-06-19 16:58:16 +03:00
|
|
|
e.leases[options.Domain][s.Name+node.Id] = leaseID
|
|
|
|
e.register[options.Domain][s.Name+node.Id] = h
|
2019-10-06 11:54:26 +03:00
|
|
|
e.Unlock()
|
2019-10-06 15:43:41 +03:00
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var leaseNotFound bool
|
2019-10-06 11:54:26 +03:00
|
|
|
|
|
|
|
// renew the lease if it exists
|
|
|
|
if leaseID > 0 {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("Renewing existing lease for %s %d", s.Name, leaseID)
|
|
|
|
}
|
2020-06-19 16:58:16 +03:00
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
if _, err := e.client.KeepAliveOnce(context.TODO(), leaseID); err != nil {
|
|
|
|
if err != rpctypes.ErrLeaseNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("Lease not found for %s %d", s.Name, leaseID)
|
|
|
|
}
|
2020-06-19 16:58:16 +03:00
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
// lease not found do register
|
|
|
|
leaseNotFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create hash of service; uint64
|
2019-10-06 11:54:26 +03:00
|
|
|
h, err := hash.Hash(node, nil)
|
2019-10-02 20:56:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
// get existing hash for the service node
|
2020-06-19 16:58:16 +03:00
|
|
|
e.RLock()
|
|
|
|
v, ok := e.register[options.Domain][s.Name+node.Id]
|
|
|
|
e.RUnlock()
|
2019-10-02 20:56:53 +03:00
|
|
|
|
|
|
|
// the service is unchanged, skip registering
|
|
|
|
if ok && v == h && !leaseNotFound {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("Service %s node %s unchanged skipping registration", s.Name, node.Id)
|
|
|
|
}
|
2019-10-02 20:56:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
// add domain to the service metadata so it can be determined when doing wildcard queries
|
|
|
|
if s.Metadata == nil {
|
|
|
|
s.Metadata = map[string]string{"domain": options.Domain}
|
|
|
|
} else {
|
|
|
|
s.Metadata["domain"] = options.Domain
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
service := ®istry.Service{
|
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Metadata: s.Metadata,
|
|
|
|
Endpoints: s.Endpoints,
|
2019-10-06 11:54:26 +03:00
|
|
|
Nodes: []*registry.Node{node},
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), e.options.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var lgr *clientv3.LeaseGrantResponse
|
|
|
|
if options.TTL.Seconds() > 0 {
|
2019-10-06 11:54:26 +03:00
|
|
|
// get a lease used to expire keys since we have a ttl
|
2019-10-02 20:56:53 +03:00
|
|
|
lgr, err = e.client.Grant(ctx, int64(options.TTL.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
// create an entry for the node
|
2020-06-19 16:58:16 +03:00
|
|
|
var putOpts []clientv3.OpOption
|
2019-10-06 11:54:26 +03:00
|
|
|
if lgr != nil {
|
2020-06-19 16:58:16 +03:00
|
|
|
putOpts = append(putOpts, clientv3.WithLease(lgr.ID))
|
2020-07-31 16:40:11 +03:00
|
|
|
|
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
2020-07-31 17:05:32 +03:00
|
|
|
logger.Tracef("Registering %s id %s with lease %v and leaseID %v and ttl %v", service.Name, node.Id, lgr, lgr.ID, options.TTL)
|
2020-07-31 16:40:11 +03:00
|
|
|
}
|
|
|
|
} else if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
2020-07-31 17:05:32 +03:00
|
|
|
logger.Tracef("Registering %s id %s without lease", service.Name, node.Id)
|
2019-10-06 11:54:26 +03:00
|
|
|
}
|
2020-06-19 16:58:16 +03:00
|
|
|
|
|
|
|
key := nodePath(options.Domain, s.Name, node.Id)
|
|
|
|
if _, err = e.client.Put(ctx, key, encode(service), putOpts...); err != nil {
|
2019-10-06 11:54:26 +03:00
|
|
|
return err
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
e.Lock()
|
|
|
|
// save our hash of the service
|
2020-06-19 16:58:16 +03:00
|
|
|
e.register[options.Domain][s.Name+node.Id] = h
|
2019-10-02 20:56:53 +03:00
|
|
|
// save our leaseID of the service
|
|
|
|
if lgr != nil {
|
2020-06-19 16:58:16 +03:00
|
|
|
e.leases[options.Domain][s.Name+node.Id] = lgr.ID
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
e.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:32:59 +03:00
|
|
|
func (e *etcdRegistry) Deregister(s *registry.Service, opts ...registry.DeregisterOption) error {
|
2019-10-06 11:54:26 +03:00
|
|
|
if len(s.Nodes) == 0 {
|
|
|
|
return errors.New("Require at least one node")
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
// parse the options
|
|
|
|
var options registry.DeregisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
|
|
|
}
|
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
for _, node := range s.Nodes {
|
|
|
|
e.Lock()
|
|
|
|
// delete our hash of the service
|
2020-08-07 21:58:25 +03:00
|
|
|
nodes, ok := e.register[options.Domain]
|
|
|
|
if ok {
|
|
|
|
delete(nodes, s.Name+node.Id)
|
|
|
|
e.register[options.Domain] = nodes
|
|
|
|
}
|
|
|
|
|
2019-10-06 11:54:26 +03:00
|
|
|
// delete our lease of the service
|
2020-08-07 21:58:25 +03:00
|
|
|
leases, ok := e.leases[options.Domain]
|
|
|
|
if ok {
|
|
|
|
delete(leases, s.Name+node.Id)
|
|
|
|
e.leases[options.Domain] = leases
|
|
|
|
}
|
2019-10-06 11:54:26 +03:00
|
|
|
e.Unlock()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), e.options.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("Deregistering %s id %s", s.Name, node.Id)
|
|
|
|
}
|
2020-06-19 16:58:16 +03:00
|
|
|
|
|
|
|
if _, err := e.client.Delete(ctx, nodePath(options.Domain, s.Name, node.Id)); err != nil {
|
2019-10-06 11:54:26 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
|
|
|
if len(s.Nodes) == 0 {
|
|
|
|
return errors.New("Require at least one node")
|
|
|
|
}
|
|
|
|
|
|
|
|
var gerr error
|
|
|
|
|
|
|
|
// register each node individually
|
|
|
|
for _, node := range s.Nodes {
|
2020-06-19 16:58:16 +03:00
|
|
|
if err := e.registerNode(s, node, opts...); err != nil {
|
2019-10-06 11:54:26 +03:00
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gerr
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:32:59 +03:00
|
|
|
func (e *etcdRegistry) GetService(name string, opts ...registry.GetOption) ([]*registry.Service, error) {
|
2019-10-02 20:56:53 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), e.options.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
// parse the options and fallback to the default domain
|
|
|
|
var options registry.GetOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
var results []*mvccpb.KeyValue
|
2020-08-06 14:42:14 +03:00
|
|
|
|
|
|
|
// TODO: refactorout wildcard, this is an incredibly expensive operation
|
2020-06-19 16:58:16 +03:00
|
|
|
if options.Domain == registry.WildcardDomain {
|
|
|
|
rsp, err := e.client.Get(ctx, prefix, clientv3.WithPrefix(), clientv3.WithSerializable())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:42:14 +03:00
|
|
|
// filter the results for the key we care about
|
2020-06-19 16:58:16 +03:00
|
|
|
for _, kv := range rsp.Kvs {
|
2020-08-06 14:42:14 +03:00
|
|
|
// if the key does not contain the name then pass
|
2020-08-10 23:58:35 +03:00
|
|
|
_, service, ok := getName(string(kv.Key), prefix)
|
|
|
|
if !ok || service != name {
|
2020-08-06 14:42:14 +03:00
|
|
|
continue
|
2020-06-19 16:58:16 +03:00
|
|
|
}
|
2020-08-06 14:42:14 +03:00
|
|
|
|
|
|
|
// save the result if its what we expect
|
|
|
|
results = append(results, kv)
|
2020-06-19 16:58:16 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prefix := servicePath(options.Domain, name) + "/"
|
|
|
|
rsp, err := e.client.Get(ctx, prefix, clientv3.WithPrefix(), clientv3.WithSerializable())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results = rsp.Kvs
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(results) == 0 {
|
2019-10-02 20:56:53 +03:00
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
versions := make(map[string]*registry.Service)
|
|
|
|
|
|
|
|
for _, n := range results {
|
2020-08-06 14:42:14 +03:00
|
|
|
// only process the things we care about
|
2020-08-10 23:58:35 +03:00
|
|
|
domain, service, ok := getName(string(n.Key), prefix)
|
|
|
|
if !ok || service != name {
|
2020-08-06 14:42:14 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-02 20:56:53 +03:00
|
|
|
|
|
|
|
if sn := decode(n.Value); sn != nil {
|
2020-08-10 23:58:35 +03:00
|
|
|
// compose a key of name/version/domain
|
|
|
|
key := sn.Name + sn.Version + domain
|
|
|
|
|
|
|
|
s, ok := versions[key]
|
2019-10-02 20:56:53 +03:00
|
|
|
if !ok {
|
|
|
|
s = ®istry.Service{
|
|
|
|
Name: sn.Name,
|
|
|
|
Version: sn.Version,
|
|
|
|
Metadata: sn.Metadata,
|
|
|
|
Endpoints: sn.Endpoints,
|
|
|
|
}
|
2020-08-10 23:58:35 +03:00
|
|
|
versions[key] = s
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
2019-12-03 22:59:44 +03:00
|
|
|
s.Nodes = append(s.Nodes, sn.Nodes...)
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
services := make([]*registry.Service, 0, len(versions))
|
2020-08-06 14:42:14 +03:00
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
for _, service := range versions {
|
2019-10-02 20:56:53 +03:00
|
|
|
services = append(services, service)
|
|
|
|
}
|
2019-10-10 21:16:31 +03:00
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:32:59 +03:00
|
|
|
func (e *etcdRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Service, error) {
|
2020-06-19 16:58:16 +03:00
|
|
|
// parse the options
|
|
|
|
var options registry.ListOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine the prefix
|
|
|
|
var p string
|
|
|
|
if options.Domain == registry.WildcardDomain {
|
|
|
|
p = prefix
|
|
|
|
} else {
|
|
|
|
p = prefixWithDomain(options.Domain)
|
|
|
|
}
|
2019-10-02 20:56:53 +03:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), e.options.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
rsp, err := e.client.Get(ctx, p, clientv3.WithPrefix(), clientv3.WithSerializable())
|
2019-10-02 20:56:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(rsp.Kvs) == 0 {
|
|
|
|
return []*registry.Service{}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:58:16 +03:00
|
|
|
versions := make(map[string]*registry.Service)
|
2019-10-02 20:56:53 +03:00
|
|
|
for _, n := range rsp.Kvs {
|
2020-08-10 23:58:35 +03:00
|
|
|
domain, service, ok := getName(string(n.Key), prefix)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-19 16:58:16 +03:00
|
|
|
|
2020-08-10 23:58:35 +03:00
|
|
|
sn := decode(n.Value)
|
|
|
|
if sn == nil || sn.Name != service {
|
2019-10-07 18:11:52 +03:00
|
|
|
continue
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
2020-06-19 16:58:16 +03:00
|
|
|
|
2020-08-10 23:58:35 +03:00
|
|
|
// key based on name/version/domain
|
|
|
|
key := sn.Name + sn.Version + domain
|
|
|
|
|
|
|
|
v, ok := versions[key]
|
2019-10-07 18:11:52 +03:00
|
|
|
if !ok {
|
2020-08-10 23:58:35 +03:00
|
|
|
versions[key] = sn
|
2019-10-07 18:11:52 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-06-19 16:58:16 +03:00
|
|
|
|
2019-10-07 18:11:52 +03:00
|
|
|
// append to service:version nodes
|
|
|
|
v.Nodes = append(v.Nodes, sn.Nodes...)
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
2019-10-07 18:11:52 +03:00
|
|
|
|
2019-11-11 03:03:51 +03:00
|
|
|
services := make([]*registry.Service, 0, len(versions))
|
2019-10-07 18:11:52 +03:00
|
|
|
for _, service := range versions {
|
2019-10-02 20:56:53 +03:00
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
|
2019-10-10 21:16:31 +03:00
|
|
|
// sort the services
|
|
|
|
sort.Slice(services, func(i, j int) bool { return services[i].Name < services[j].Name })
|
|
|
|
|
2019-10-02 20:56:53 +03:00
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
2020-08-08 03:40:41 +03:00
|
|
|
cli, err := newClient(e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newEtcdWatcher(cli, e.options.Timeout, opts...)
|
2019-10-02 20:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) String() string {
|
|
|
|
return "etcd"
|
|
|
|
}
|