util.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2017 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 etcdmain
  15. import (
  16. "fmt"
  17. "os"
  18. "go.etcd.io/etcd/pkg/srv"
  19. "go.etcd.io/etcd/pkg/transport"
  20. "go.uber.org/zap"
  21. )
  22. func discoverEndpoints(lg *zap.Logger, dns string, ca string, insecure bool, serviceName string) (s srv.SRVClients) {
  23. if dns == "" {
  24. return s
  25. }
  26. srvs, err := srv.GetClient("etcd-client", dns, serviceName)
  27. if err != nil {
  28. fmt.Fprintln(os.Stderr, err)
  29. os.Exit(1)
  30. }
  31. endpoints := srvs.Endpoints
  32. if lg != nil {
  33. lg.Info(
  34. "discovered cluster from SRV",
  35. zap.String("srv-server", dns),
  36. zap.Strings("endpoints", endpoints),
  37. )
  38. } else {
  39. plog.Infof("discovered the cluster %s from %s", endpoints, dns)
  40. }
  41. if insecure {
  42. return *srvs
  43. }
  44. // confirm TLS connections are good
  45. tlsInfo := transport.TLSInfo{
  46. TrustedCAFile: ca,
  47. ServerName: dns,
  48. }
  49. if lg != nil {
  50. lg.Info(
  51. "validating discovered SRV endpoints",
  52. zap.String("srv-server", dns),
  53. zap.Strings("endpoints", endpoints),
  54. )
  55. } else {
  56. plog.Infof("validating discovered endpoints %v", endpoints)
  57. }
  58. endpoints, err = transport.ValidateSecureEndpoints(tlsInfo, endpoints)
  59. if err != nil {
  60. if lg != nil {
  61. lg.Warn(
  62. "failed to validate discovered endpoints",
  63. zap.String("srv-server", dns),
  64. zap.Strings("endpoints", endpoints),
  65. zap.Error(err),
  66. )
  67. } else {
  68. plog.Warningf("%v", err)
  69. }
  70. } else {
  71. if lg != nil {
  72. lg.Info(
  73. "using validated discovered SRV endpoints",
  74. zap.String("srv-server", dns),
  75. zap.Strings("endpoints", endpoints),
  76. )
  77. }
  78. }
  79. if lg == nil {
  80. plog.Infof("using discovered endpoints %v", endpoints)
  81. }
  82. // map endpoints back to SRVClients struct with SRV data
  83. eps := make(map[string]struct{})
  84. for _, ep := range endpoints {
  85. eps[ep] = struct{}{}
  86. }
  87. for i := range srvs.Endpoints {
  88. if _, ok := eps[srvs.Endpoints[i]]; !ok {
  89. continue
  90. }
  91. s.Endpoints = append(s.Endpoints, srvs.Endpoints[i])
  92. s.SRVs = append(s.SRVs, srvs.SRVs[i])
  93. }
  94. return s
  95. }