Skip to content

Commit aac2292

Browse files
author
Anthony Romano
authored
Merge pull request #7882 from heyitsanthony/srv-priority
gateway: DNS SRV priority
2 parents 3a2e765 + c232814 commit aac2292

File tree

11 files changed

+284
-237
lines changed

11 files changed

+284
-237
lines changed

client/discover.go

+19
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,27 @@
1414

1515
package client
1616

17+
import (
18+
"github.com/coreos/etcd/pkg/srv"
19+
)
20+
1721
// Discoverer is an interface that wraps the Discover method.
1822
type Discoverer interface {
1923
// Discover looks up the etcd servers for the domain.
2024
Discover(domain string) ([]string, error)
2125
}
26+
27+
type srvDiscover struct{}
28+
29+
// NewSRVDiscover constructs a new Discoverer that uses the stdlib to lookup SRV records.
30+
func NewSRVDiscover() Discoverer {
31+
return &srvDiscover{}
32+
}
33+
34+
func (d *srvDiscover) Discover(domain string) ([]string, error) {
35+
srvs, err := srv.GetClient("etcd-client", domain)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return srvs.Endpoints, nil
40+
}

client/srv.go

-65
This file was deleted.

client/srv_test.go

-102
This file was deleted.

embed/config.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"net/url"
2323
"strings"
2424

25-
"github.com/coreos/etcd/discovery"
2625
"github.com/coreos/etcd/etcdserver"
2726
"github.com/coreos/etcd/pkg/cors"
2827
"github.com/coreos/etcd/pkg/netutil"
28+
"github.com/coreos/etcd/pkg/srv"
2929
"github.com/coreos/etcd/pkg/transport"
3030
"github.com/coreos/etcd/pkg/types"
3131

@@ -321,11 +321,15 @@ func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, tok
321321
urlsmap[cfg.Name] = cfg.APUrls
322322
token = cfg.Durl
323323
case cfg.DNSCluster != "":
324-
var clusterStr string
325-
clusterStr, err = discovery.SRVGetCluster(cfg.Name, cfg.DNSCluster, cfg.APUrls)
326-
if err != nil {
327-
return nil, "", err
324+
clusterStrs, cerr := srv.GetCluster("etcd-server", cfg.Name, cfg.DNSCluster, cfg.APUrls)
325+
if cerr != nil {
326+
plog.Errorf("couldn't resolve during SRV discovery (%v)", cerr)
327+
return nil, "", cerr
328+
}
329+
for _, s := range clusterStrs {
330+
plog.Noticef("got bootstrap from DNS for etcd-server at %s", s)
328331
}
332+
clusterStr := strings.Join(clusterStrs, ",")
329333
if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.CAFile == "" {
330334
cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
331335
}

etcdmain/gateway.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,28 @@ func stripSchema(eps []string) []string {
9191

9292
return endpoints
9393
}
94-
func startGateway(cmd *cobra.Command, args []string) {
95-
endpoints := gatewayEndpoints
9694

97-
if eps := discoverEndpoints(gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery); len(eps) != 0 {
98-
endpoints = eps
95+
func startGateway(cmd *cobra.Command, args []string) {
96+
srvs := discoverEndpoints(gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery)
97+
if len(srvs.Endpoints) == 0 {
98+
// no endpoints discovered, fall back to provided endpoints
99+
srvs.Endpoints = gatewayEndpoints
99100
}
100-
101101
// Strip the schema from the endpoints because we start just a TCP proxy
102-
endpoints = stripSchema(endpoints)
102+
srvs.Endpoints = stripSchema(srvs.Endpoints)
103+
if len(srvs.SRVs) == 0 {
104+
for _, ep := range srvs.Endpoints {
105+
h, p, err := net.SplitHostPort(ep)
106+
if err != nil {
107+
plog.Fatalf("error parsing endpoint %q", ep)
108+
}
109+
var port uint16
110+
fmt.Sscanf(p, "%d", &port)
111+
srvs.SRVs = append(srvs.SRVs, &net.SRV{Target: h, Port: port})
112+
}
113+
}
103114

104-
if len(endpoints) == 0 {
115+
if len(srvs.Endpoints) == 0 {
105116
plog.Fatalf("no endpoints found")
106117
}
107118

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

114125
tp := tcpproxy.TCPProxy{
115126
Listener: l,
116-
Endpoints: endpoints,
127+
Endpoints: srvs.SRVs,
117128
MonitorInterval: getewayRetryDelay,
118129
}
119130

etcdmain/grpc_proxy.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {
106106
os.Exit(1)
107107
}
108108

109-
if eps := discoverEndpoints(grpcProxyDNSCluster, grpcProxyCA, grpcProxyInsecureDiscovery); len(eps) != 0 {
110-
grpcProxyEndpoints = eps
109+
srvs := discoverEndpoints(grpcProxyDNSCluster, grpcProxyCA, grpcProxyInsecureDiscovery)
110+
if len(srvs.Endpoints) != 0 {
111+
grpcProxyEndpoints = srvs.Endpoints
111112
}
112113

113114
l, err := net.Listen("tcp", grpcProxyListenAddr)

etcdmain/util.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@ import (
1818
"fmt"
1919
"os"
2020

21-
"github.com/coreos/etcd/client"
21+
"github.com/coreos/etcd/pkg/srv"
2222
"github.com/coreos/etcd/pkg/transport"
2323
)
2424

25-
func discoverEndpoints(dns string, ca string, insecure bool) (endpoints []string) {
25+
func discoverEndpoints(dns string, ca string, insecure bool) (s srv.SRVClients) {
2626
if dns == "" {
27-
return nil
27+
return s
2828
}
29-
endpoints, err := client.NewSRVDiscover().Discover(dns)
29+
srvs, err := srv.GetClient("etcd-client", dns)
3030
if err != nil {
3131
fmt.Fprintln(os.Stderr, err)
3232
os.Exit(1)
3333
}
34+
endpoints := srvs.Endpoints
3435
plog.Infof("discovered the cluster %s from %s", endpoints, dns)
3536
if insecure {
36-
return endpoints
37+
return *srvs
3738
}
3839
// confirm TLS connections are good
3940
tlsInfo := transport.TLSInfo{
@@ -46,5 +47,19 @@ func discoverEndpoints(dns string, ca string, insecure bool) (endpoints []string
4647
plog.Warningf("%v", err)
4748
}
4849
plog.Infof("using discovered endpoints %v", endpoints)
49-
return 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
5065
}

0 commit comments

Comments
 (0)