backends.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package discovery
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "time"
  7. "github.com/Sirupsen/logrus"
  8. )
  9. var (
  10. // Backends is a global map of discovery backends indexed by their
  11. // associated scheme.
  12. backends = make(map[string]Backend)
  13. )
  14. // Register makes a discovery backend available by the provided scheme.
  15. // If Register is called twice with the same scheme an error is returned.
  16. func Register(scheme string, d Backend) error {
  17. if _, exists := backends[scheme]; exists {
  18. return fmt.Errorf("scheme already registered %s", scheme)
  19. }
  20. logrus.WithField("name", scheme).Debugf("Registering discovery service")
  21. backends[scheme] = d
  22. return nil
  23. }
  24. func parse(rawurl string) (string, string) {
  25. parts := strings.SplitN(rawurl, "://", 2)
  26. // nodes:port,node2:port => nodes://node1:port,node2:port
  27. if len(parts) == 1 {
  28. return "nodes", parts[0]
  29. }
  30. return parts[0], parts[1]
  31. }
  32. // ParseAdvertise parses the --cluster-advertise daemon config which accepts
  33. // <ip-address>:<port> or <interface-name>:<port>
  34. func ParseAdvertise(advertise string) (string, error) {
  35. var (
  36. iface *net.Interface
  37. addrs []net.Addr
  38. err error
  39. )
  40. addr, port, err := net.SplitHostPort(advertise)
  41. if err != nil {
  42. return "", fmt.Errorf("invalid --cluster-advertise configuration: %s: %v", advertise, err)
  43. }
  44. ip := net.ParseIP(addr)
  45. // If it is a valid ip-address, use it as is
  46. if ip != nil {
  47. return advertise, nil
  48. }
  49. // If advertise is a valid interface name, get the valid IPv4 address and use it to advertise
  50. ifaceName := addr
  51. iface, err = net.InterfaceByName(ifaceName)
  52. if err != nil {
  53. return "", fmt.Errorf("invalid cluster advertise IP address or interface name (%s) : %v", advertise, err)
  54. }
  55. addrs, err = iface.Addrs()
  56. if err != nil {
  57. return "", fmt.Errorf("unable to get advertise IP address from interface (%s) : %v", advertise, err)
  58. }
  59. if len(addrs) == 0 {
  60. return "", fmt.Errorf("no available advertise IP address in interface (%s)", advertise)
  61. }
  62. addr = ""
  63. for _, a := range addrs {
  64. ip, _, err := net.ParseCIDR(a.String())
  65. if err != nil {
  66. return "", fmt.Errorf("error deriving advertise ip-address in interface (%s) : %v", advertise, err)
  67. }
  68. if ip.To4() == nil || ip.IsLoopback() {
  69. continue
  70. }
  71. addr = ip.String()
  72. break
  73. }
  74. if addr == "" {
  75. return "", fmt.Errorf("could not find a valid ip-address in interface %s", advertise)
  76. }
  77. addr = net.JoinHostPort(addr, port)
  78. return addr, nil
  79. }
  80. // New returns a new Discovery given a URL, heartbeat and ttl settings.
  81. // Returns an error if the URL scheme is not supported.
  82. func New(rawurl string, heartbeat time.Duration, ttl time.Duration, clusterOpts map[string]string) (Backend, error) {
  83. scheme, uri := parse(rawurl)
  84. if backend, exists := backends[scheme]; exists {
  85. logrus.WithFields(logrus.Fields{"name": scheme, "uri": uri}).Debugf("Initializing discovery service")
  86. err := backend.Initialize(uri, heartbeat, ttl, clusterOpts)
  87. return backend, err
  88. }
  89. return nil, ErrNotSupported
  90. }