srv.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. stringParts := make([]string, 0)
  32. tempName := int(0)
  33. tcpAPUrls := make([]string, 0)
  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. tcpAPUrls = append(tcpAPUrls, tcpAddr.String())
  42. }
  43. updateNodeMap := func(service, prefix string) error {
  44. _, addrs, err := lookupSRV(service, "tcp", dns)
  45. if err != nil {
  46. return err
  47. }
  48. for _, srv := range addrs {
  49. port := fmt.Sprintf("%d", srv.Port)
  50. host := net.JoinHostPort(srv.Target, port)
  51. tcpAddr, err := resolveTCPAddr("tcp", host)
  52. if err != nil {
  53. plog.Warningf("couldn't resolve host %s during SRV discovery", host)
  54. continue
  55. }
  56. n := ""
  57. for _, url := range tcpAPUrls {
  58. if url == tcpAddr.String() {
  59. n = name
  60. }
  61. }
  62. if n == "" {
  63. n = fmt.Sprintf("%d", tempName)
  64. tempName += 1
  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, prefix, urlHost))
  70. plog.Noticef("got bootstrap from DNS for %s at %s%s", service, prefix, urlHost)
  71. }
  72. return nil
  73. }
  74. failCount := 0
  75. err := updateNodeMap("etcd-server-ssl", "https://")
  76. srvErr := make([]string, 2)
  77. if err != nil {
  78. srvErr[0] = fmt.Sprintf("error querying DNS SRV records for _etcd-server-ssl %s", err)
  79. failCount += 1
  80. }
  81. err = updateNodeMap("etcd-server", "http://")
  82. if err != nil {
  83. srvErr[1] = fmt.Sprintf("error querying DNS SRV records for _etcd-server %s", err)
  84. failCount += 1
  85. }
  86. if failCount == 2 {
  87. plog.Warningf(srvErr[0])
  88. plog.Warningf(srvErr[1])
  89. plog.Errorf("SRV discovery failed: too many errors querying DNS SRV records")
  90. return "", "", err
  91. }
  92. return strings.Join(stringParts, ","), defaultToken, nil
  93. }