srv.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. port := fmt.Sprintf("%d", srv.Port)
  52. host := net.JoinHostPort(srv.Target, 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. // SRV records have a trailing dot but URL shouldn't.
  69. shortHost := strings.TrimSuffix(srv.Target, ".")
  70. urlHost := net.JoinHostPort(shortHost, port)
  71. stringParts = append(stringParts, fmt.Sprintf("%s=%s%s", n, prefix, urlHost))
  72. plog.Noticef("got bootstrap from DNS for %s at %s%s", service, prefix, urlHost)
  73. }
  74. return nil
  75. }
  76. failCount := 0
  77. err := updateNodeMap("etcd-server-ssl", "https://")
  78. srvErr := make([]string, 2)
  79. if err != nil {
  80. srvErr[0] = fmt.Sprintf("error querying DNS SRV records for _etcd-server-ssl %s", err)
  81. failCount++
  82. }
  83. err = updateNodeMap("etcd-server", "http://")
  84. if err != nil {
  85. srvErr[1] = fmt.Sprintf("error querying DNS SRV records for _etcd-server %s", err)
  86. failCount++
  87. }
  88. if failCount == 2 {
  89. plog.Warningf(srvErr[0])
  90. plog.Warningf(srvErr[1])
  91. plog.Errorf("SRV discovery failed: too many errors querying DNS SRV records")
  92. return "", "", err
  93. }
  94. return strings.Join(stringParts, ","), defaultToken, nil
  95. }