srv.go 2.9 KB

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