...

Source file src/go.etcd.io/etcd/server/v3/etcdmain/util.go

Documentation: go.etcd.io/etcd/server/v3/etcdmain

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

View as plain text