Skip to content

Commit

Permalink
Merge pull request #7882 from heyitsanthony/srv-priority
Browse files Browse the repository at this point in the history
gateway: DNS SRV priority
  • Loading branch information
Anthony Romano authored May 8, 2017
2 parents 3a2e765 + c232814 commit aac2292
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 237 deletions.
19 changes: 19 additions & 0 deletions client/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,27 @@

package client

import (
"github.com/coreos/etcd/pkg/srv"
)

// Discoverer is an interface that wraps the Discover method.
type Discoverer interface {
// Discover looks up the etcd servers for the domain.
Discover(domain string) ([]string, error)
}

type srvDiscover struct{}

// NewSRVDiscover constructs a new Discoverer that uses the stdlib to lookup SRV records.
func NewSRVDiscover() Discoverer {
return &srvDiscover{}
}

func (d *srvDiscover) Discover(domain string) ([]string, error) {
srvs, err := srv.GetClient("etcd-client", domain)
if err != nil {
return nil, err
}
return srvs.Endpoints, nil
}
65 changes: 0 additions & 65 deletions client/srv.go

This file was deleted.

102 changes: 0 additions & 102 deletions client/srv_test.go

This file was deleted.

14 changes: 9 additions & 5 deletions embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"net/url"
"strings"

"github.com/coreos/etcd/discovery"
"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/pkg/cors"
"github.com/coreos/etcd/pkg/netutil"
"github.com/coreos/etcd/pkg/srv"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/etcd/pkg/types"

Expand Down Expand Up @@ -321,11 +321,15 @@ func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, tok
urlsmap[cfg.Name] = cfg.APUrls
token = cfg.Durl
case cfg.DNSCluster != "":
var clusterStr string
clusterStr, err = discovery.SRVGetCluster(cfg.Name, cfg.DNSCluster, cfg.APUrls)
if err != nil {
return nil, "", err
clusterStrs, cerr := srv.GetCluster("etcd-server", cfg.Name, cfg.DNSCluster, cfg.APUrls)
if cerr != nil {
plog.Errorf("couldn't resolve during SRV discovery (%v)", cerr)
return nil, "", cerr
}
for _, s := range clusterStrs {
plog.Noticef("got bootstrap from DNS for etcd-server at %s", s)
}
clusterStr := strings.Join(clusterStrs, ",")
if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.CAFile == "" {
cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
}
Expand Down
27 changes: 19 additions & 8 deletions etcdmain/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,28 @@ func stripSchema(eps []string) []string {

return endpoints
}
func startGateway(cmd *cobra.Command, args []string) {
endpoints := gatewayEndpoints

if eps := discoverEndpoints(gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery); len(eps) != 0 {
endpoints = eps
func startGateway(cmd *cobra.Command, args []string) {
srvs := discoverEndpoints(gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery)
if len(srvs.Endpoints) == 0 {
// no endpoints discovered, fall back to provided endpoints
srvs.Endpoints = gatewayEndpoints
}

// Strip the schema from the endpoints because we start just a TCP proxy
endpoints = stripSchema(endpoints)
srvs.Endpoints = stripSchema(srvs.Endpoints)
if len(srvs.SRVs) == 0 {
for _, ep := range srvs.Endpoints {
h, p, err := net.SplitHostPort(ep)
if err != nil {
plog.Fatalf("error parsing endpoint %q", ep)
}
var port uint16
fmt.Sscanf(p, "%d", &port)
srvs.SRVs = append(srvs.SRVs, &net.SRV{Target: h, Port: port})
}
}

if len(endpoints) == 0 {
if len(srvs.Endpoints) == 0 {
plog.Fatalf("no endpoints found")
}

Expand All @@ -113,7 +124,7 @@ func startGateway(cmd *cobra.Command, args []string) {

tp := tcpproxy.TCPProxy{
Listener: l,
Endpoints: endpoints,
Endpoints: srvs.SRVs,
MonitorInterval: getewayRetryDelay,
}

Expand Down
5 changes: 3 additions & 2 deletions etcdmain/grpc_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {
os.Exit(1)
}

if eps := discoverEndpoints(grpcProxyDNSCluster, grpcProxyCA, grpcProxyInsecureDiscovery); len(eps) != 0 {
grpcProxyEndpoints = eps
srvs := discoverEndpoints(grpcProxyDNSCluster, grpcProxyCA, grpcProxyInsecureDiscovery)
if len(srvs.Endpoints) != 0 {
grpcProxyEndpoints = srvs.Endpoints
}

l, err := net.Listen("tcp", grpcProxyListenAddr)
Expand Down
27 changes: 21 additions & 6 deletions etcdmain/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ import (
"fmt"
"os"

"github.com/coreos/etcd/client"
"github.com/coreos/etcd/pkg/srv"
"github.com/coreos/etcd/pkg/transport"
)

func discoverEndpoints(dns string, ca string, insecure bool) (endpoints []string) {
func discoverEndpoints(dns string, ca string, insecure bool) (s srv.SRVClients) {
if dns == "" {
return nil
return s
}
endpoints, err := client.NewSRVDiscover().Discover(dns)
srvs, err := srv.GetClient("etcd-client", dns)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
endpoints := srvs.Endpoints
plog.Infof("discovered the cluster %s from %s", endpoints, dns)
if insecure {
return endpoints
return *srvs
}
// confirm TLS connections are good
tlsInfo := transport.TLSInfo{
Expand All @@ -46,5 +47,19 @@ func discoverEndpoints(dns string, ca string, insecure bool) (endpoints []string
plog.Warningf("%v", err)
}
plog.Infof("using discovered endpoints %v", endpoints)
return endpoints

// map endpoints back to SRVClients struct with SRV data
eps := make(map[string]struct{})
for _, ep := range endpoints {
eps[ep] = struct{}{}
}
for i := range srvs.Endpoints {
if _, ok := eps[srvs.Endpoints[i]]; !ok {
continue
}
s.Endpoints = append(s.Endpoints, srvs.Endpoints[i])
s.SRVs = append(s.SRVs, srvs.SRVs[i])
}

return s
}
Loading

0 comments on commit aac2292

Please sign in to comment.