blob: 9657271d53a5f691ef15d2908be3c861d8f9c5c8 [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// 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
15package etcdmain
16
17import (
18 "fmt"
19 "os"
20
21 "github.com/coreos/etcd/pkg/srv"
22 "github.com/coreos/etcd/pkg/transport"
23)
24
25func discoverEndpoints(dns string, ca string, insecure bool) (s srv.SRVClients) {
26 if dns == "" {
27 return s
28 }
29 srvs, err := srv.GetClient("etcd-client", dns)
30 if err != nil {
31 fmt.Fprintln(os.Stderr, err)
32 os.Exit(1)
33 }
34 endpoints := srvs.Endpoints
35 plog.Infof("discovered the cluster %s from %s", endpoints, dns)
36 if insecure {
37 return *srvs
38 }
39 // confirm TLS connections are good
40 tlsInfo := transport.TLSInfo{
41 TrustedCAFile: ca,
42 ServerName: dns,
43 }
44 plog.Infof("validating discovered endpoints %v", endpoints)
45 endpoints, err = transport.ValidateSecureEndpoints(tlsInfo, endpoints)
46 if err != nil {
47 plog.Warningf("%v", err)
48 }
49 plog.Infof("using discovered endpoints %v", endpoints)
50
51 // map endpoints back to SRVClients struct with SRV data
52 eps := make(map[string]struct{})
53 for _, ep := range endpoints {
54 eps[ep] = struct{}{}
55 }
56 for i := range srvs.Endpoints {
57 if _, ok := eps[srvs.Endpoints[i]]; !ok {
58 continue
59 }
60 s.Endpoints = append(s.Endpoints, srvs.Endpoints[i])
61 s.SRVs = append(s.SRVs, srvs.SRVs[i])
62 }
63
64 return s
65}