srv.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "strings"
  19. "github.com/coreos/etcd/pkg/types"
  20. )
  21. var (
  22. // indirection for testing
  23. lookupSRV = net.LookupSRV
  24. resolveTCPAddr = net.ResolveTCPAddr
  25. )
  26. // SRVGetCluster gets the cluster information via DNS discovery.
  27. // TODO(barakmich): Currently ignores priority and weight (as they don't make as much sense for a bootstrap)
  28. // Also doesn't do any lookups for the token (though it could)
  29. // Also sees each entry as a separate instance.
  30. func SRVGetCluster(name, dns string, defaultToken string, apurls types.URLs) (string, string, error) {
  31. var (
  32. stringParts []string
  33. tcpAPUrls []string
  34. )
  35. tempName := int(0)
  36. // First, resolve the apurls
  37. for _, url := range apurls {
  38. tcpAddr, err := resolveTCPAddr("tcp", url.Host)
  39. if err != nil {
  40. plog.Errorf("couldn't resolve host %s during SRV discovery", url.Host)
  41. return "", "", err
  42. }
  43. tcpAPUrls = append(tcpAPUrls, tcpAddr.String())
  44. }
  45. updateNodeMap := func(service, prefix string) error {
  46. _, addrs, err := lookupSRV(service, "tcp", dns)
  47. if err != nil {
  48. return err
  49. }
  50. for _, srv := range addrs {
  51. target := strings.TrimSuffix(srv.Target, ".")
  52. host := net.JoinHostPort(target, fmt.Sprintf("%d", srv.Port))
  53. tcpAddr, err := resolveTCPAddr("tcp", host)
  54. if err != nil {
  55. plog.Warningf("couldn't resolve host %s during SRV discovery", host)
  56. continue
  57. }
  58. n := ""
  59. for _, url := range tcpAPUrls {
  60. if url == tcpAddr.String() {
  61. n = name
  62. }
  63. }
  64. if n == "" {
  65. n = fmt.Sprintf("%d", tempName)
  66. tempName++
  67. }
  68. stringParts = append(stringParts, fmt.Sprintf("%s=%s%s", n, prefix, host))
  69. plog.Noticef("got bootstrap from DNS for %s at %s%s", service, prefix, host)
  70. }
  71. return nil
  72. }
  73. failCount := 0
  74. err := updateNodeMap("etcd-server-ssl", "https://")
  75. srvErr := make([]string, 2)
  76. if err != nil {
  77. srvErr[0] = fmt.Sprintf("error querying DNS SRV records for _etcd-server-ssl %s", err)
  78. failCount++
  79. }
  80. err = updateNodeMap("etcd-server", "http://")
  81. if err != nil {
  82. srvErr[1] = fmt.Sprintf("error querying DNS SRV records for _etcd-server %s", err)
  83. failCount++
  84. }
  85. if failCount == 2 {
  86. plog.Warningf(srvErr[0])
  87. plog.Warningf(srvErr[1])
  88. plog.Errorf("SRV discovery failed: too many errors querying DNS SRV records")
  89. return "", "", err
  90. }
  91. return strings.Join(stringParts, ","), defaultToken, nil
  92. }