srv.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package discovery
  15. import (
  16. "fmt"
  17. "net"
  18. "net/url"
  19. "strings"
  20. "github.com/coreos/etcd/pkg/types"
  21. )
  22. var (
  23. // indirection for testing
  24. lookupSRV = net.LookupSRV
  25. resolveTCPAddr = net.ResolveTCPAddr
  26. )
  27. // SRVGetCluster gets the cluster information via DNS discovery.
  28. // TODO(barakmich): Currently ignores priority and weight (as they don't make as much sense for a bootstrap)
  29. // Also doesn't do any lookups for the token (though it could)
  30. // Also sees each entry as a separate instance.
  31. func SRVGetCluster(name, dns string, defaultToken string, apurls types.URLs) (string, string, error) {
  32. tempName := int(0)
  33. tcp2ap := make(map[string]url.URL)
  34. // First, resolve the apurls
  35. for _, url := range apurls {
  36. tcpAddr, err := resolveTCPAddr("tcp", url.Host)
  37. if err != nil {
  38. plog.Errorf("couldn't resolve host %s during SRV discovery", url.Host)
  39. return "", "", err
  40. }
  41. tcp2ap[tcpAddr.String()] = url
  42. }
  43. stringParts := []string{}
  44. updateNodeMap := func(service, scheme string) error {
  45. _, addrs, err := lookupSRV(service, "tcp", dns)
  46. if err != nil {
  47. return err
  48. }
  49. for _, srv := range addrs {
  50. port := fmt.Sprintf("%d", srv.Port)
  51. host := net.JoinHostPort(srv.Target, port)
  52. tcpAddr, err := resolveTCPAddr("tcp", host)
  53. if err != nil {
  54. plog.Warningf("couldn't resolve host %s during SRV discovery", host)
  55. continue
  56. }
  57. n := ""
  58. url, ok := tcp2ap[tcpAddr.String()]
  59. if ok {
  60. n = name
  61. }
  62. if n == "" {
  63. n = fmt.Sprintf("%d", tempName)
  64. tempName++
  65. }
  66. // SRV records have a trailing dot but URL shouldn't.
  67. shortHost := strings.TrimSuffix(srv.Target, ".")
  68. urlHost := net.JoinHostPort(shortHost, port)
  69. stringParts = append(stringParts, fmt.Sprintf("%s=%s://%s", n, scheme, urlHost))
  70. plog.Noticef("got bootstrap from DNS for %s at %s://%s", service, scheme, urlHost)
  71. if ok && url.Scheme != scheme {
  72. plog.Errorf("bootstrap at %s from DNS for %s has scheme mismatch with expected peer %s", scheme+"://"+urlHost, service, url.String())
  73. }
  74. }
  75. return nil
  76. }
  77. failCount := 0
  78. err := updateNodeMap("etcd-server-ssl", "https")
  79. srvErr := make([]string, 2)
  80. if err != nil {
  81. srvErr[0] = fmt.Sprintf("error querying DNS SRV records for _etcd-server-ssl %s", err)
  82. failCount++
  83. }
  84. err = updateNodeMap("etcd-server", "http")
  85. if err != nil {
  86. srvErr[1] = fmt.Sprintf("error querying DNS SRV records for _etcd-server %s", err)
  87. failCount++
  88. }
  89. if failCount == 2 {
  90. plog.Warningf(srvErr[0])
  91. plog.Warningf(srvErr[1])
  92. plog.Errorf("SRV discovery failed: too many errors querying DNS SRV records")
  93. return "", "", err
  94. }
  95. return strings.Join(stringParts, ","), defaultToken, nil
  96. }