srv.go 2.8 KB

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