srv.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "log"
  18. "net"
  19. "strings"
  20. "github.com/coreos/etcd/pkg/types"
  21. )
  22. var (
  23. // indirection for testing
  24. lookupSRV = net.LookupSRV
  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 := net.ResolveTCPAddr("tcp", url.Host)
  36. if err != nil {
  37. log.Printf("discovery: 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. host := net.JoinHostPort(srv.Target, fmt.Sprintf("%d", srv.Port))
  49. tcpAddr, err := net.ResolveTCPAddr("tcp", host)
  50. if err != nil {
  51. log.Printf("discovery: Couldn't resolve host %s during SRV discovery", host)
  52. continue
  53. }
  54. n := ""
  55. for _, url := range tcpAPUrls {
  56. if url == tcpAddr.String() {
  57. n = name
  58. }
  59. }
  60. if n == "" {
  61. n = fmt.Sprintf("%d", tempName)
  62. tempName += 1
  63. }
  64. stringParts = append(stringParts, fmt.Sprintf("%s=%s%s", n, prefix, tcpAddr.String()))
  65. log.Printf("discovery: Got bootstrap from DNS for %s at host %s to %s%s", service, host, prefix, tcpAddr.String())
  66. }
  67. return nil
  68. }
  69. failCount := 0
  70. err := updateNodeMap("etcd-server-ssl", "https://")
  71. if err != nil {
  72. log.Printf("discovery: Error querying DNS SRV records for _etcd-server-ssl %s", err)
  73. failCount += 1
  74. }
  75. err = updateNodeMap("etcd-server", "http://")
  76. if err != nil {
  77. log.Printf("discovery: Error querying DNS SRV records for _etcd-server %s", err)
  78. failCount += 1
  79. }
  80. if failCount == 2 {
  81. log.Printf("discovery: SRV discovery failed: too many errors querying DNS SRV records")
  82. return "", "", err
  83. }
  84. return strings.Join(stringParts, ","), defaultToken, nil
  85. }