srv.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package discovery
  14. import (
  15. "fmt"
  16. "log"
  17. "net"
  18. "strings"
  19. "github.com/coreos/etcd/pkg/types"
  20. )
  21. var (
  22. // indirection for testing
  23. lookupSRV = net.LookupSRV
  24. )
  25. // TODO(barakmich): Currently ignores priority and weight (as they don't make as much sense for a bootstrap)
  26. // Also doesn't do any lookups for the token (though it could)
  27. // Also sees each entry as a separate instance.
  28. func SRVGetCluster(name, dns string, defaultToken string, apurls types.URLs) (string, string, error) {
  29. stringParts := make([]string, 0)
  30. tempName := int(0)
  31. tcpAPUrls := make([]string, 0)
  32. // First, resolve the apurls
  33. for _, url := range apurls {
  34. tcpAddr, err := net.ResolveTCPAddr("tcp", url.Host)
  35. if err != nil {
  36. log.Printf("discovery: Couldn't resolve host %s during SRV discovery", url.Host)
  37. return "", "", err
  38. }
  39. tcpAPUrls = append(tcpAPUrls, tcpAddr.String())
  40. }
  41. updateNodeMap := func(service, prefix string) error {
  42. _, addrs, err := lookupSRV(service, "tcp", dns)
  43. if err != nil {
  44. return err
  45. }
  46. for _, srv := range addrs {
  47. host := net.JoinHostPort(srv.Target, fmt.Sprintf("%d", srv.Port))
  48. tcpAddr, err := net.ResolveTCPAddr("tcp", host)
  49. if err != nil {
  50. log.Printf("discovery: Couldn't resolve host %s during SRV discovery", host)
  51. continue
  52. }
  53. n := ""
  54. for _, url := range tcpAPUrls {
  55. if url == tcpAddr.String() {
  56. n = name
  57. }
  58. }
  59. if n == "" {
  60. n = fmt.Sprintf("%d", tempName)
  61. tempName += 1
  62. }
  63. stringParts = append(stringParts, fmt.Sprintf("%s=%s%s", n, prefix, tcpAddr.String()))
  64. log.Printf("discovery: Got bootstrap from DNS for %s at host %s to %s%s", service, host, prefix, tcpAddr.String())
  65. }
  66. return nil
  67. }
  68. failCount := 0
  69. err := updateNodeMap("etcd-server-ssl", "https://")
  70. if err != nil {
  71. log.Printf("discovery: Error querying DNS SRV records for _etcd-server-ssl %s", err)
  72. failCount += 1
  73. }
  74. err = updateNodeMap("etcd-server", "http://")
  75. if err != nil {
  76. log.Printf("discovery: Error querying DNS SRV records for _etcd-server %s", err)
  77. failCount += 1
  78. }
  79. if failCount == 2 {
  80. log.Printf("discovery: SRV discovery failed: too many errors querying DNS SRV records")
  81. return "", "", err
  82. }
  83. return strings.Join(stringParts, ","), defaultToken, nil
  84. }