mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
add apurl checking and logging
This commit is contained in:
parent
04d9f848a7
commit
fc70aa27d2
@ -424,7 +424,7 @@ func setupCluster(apurls []url.URL) (*etcdserver.Cluster, error) {
|
|||||||
clusterStr := genClusterString(*name, apurls)
|
clusterStr := genClusterString(*name, apurls)
|
||||||
cls, err = etcdserver.NewClusterFromString(*durl, clusterStr)
|
cls, err = etcdserver.NewClusterFromString(*durl, clusterStr)
|
||||||
case set["dns-cluster-domain"]:
|
case set["dns-cluster-domain"]:
|
||||||
clusterStr, clusterToken, err := genDNSClusterString(*initialClusterToken)
|
clusterStr, clusterToken, err := genDNSClusterString(*initialClusterToken, apurls)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -448,9 +448,8 @@ func genClusterString(name string, urls types.URLs) string {
|
|||||||
|
|
||||||
// TODO(barakmich): Currently ignores priority and weight (as they don't make as much sense for a bootstrap)
|
// TODO(barakmich): Currently ignores priority and weight (as they don't make as much sense for a bootstrap)
|
||||||
// Also doesn't do any lookups for the token (though it could)
|
// Also doesn't do any lookups for the token (though it could)
|
||||||
// Also sees hostnames and IPs as separate -- use one or the other for consistency.
|
// Also sees each entry as a separate instance.
|
||||||
func genDNSClusterString(defaultToken string) (string, string, error) {
|
func genDNSClusterString(defaultToken string, apurls types.URLs) (string, string, error) {
|
||||||
targetName := make(map[string]int)
|
|
||||||
stringParts := make([]string, 0)
|
stringParts := make([]string, 0)
|
||||||
tempName := int(0)
|
tempName := int(0)
|
||||||
|
|
||||||
@ -460,14 +459,18 @@ func genDNSClusterString(defaultToken string) (string, string, error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, srv := range addrs {
|
for _, srv := range addrs {
|
||||||
var v int
|
n := ""
|
||||||
var ok bool
|
for _, url := range apurls {
|
||||||
if v, ok = targetName[srv.Target]; !ok {
|
if url.Host == fmt.Sprintf("%s:%d", srv.Target, srv.Port) {
|
||||||
v = tempName
|
n = *name
|
||||||
targetName[srv.Target] = v
|
}
|
||||||
|
}
|
||||||
|
if n == "" {
|
||||||
|
n = fmt.Sprintf("%d", tempName)
|
||||||
tempName += 1
|
tempName += 1
|
||||||
}
|
}
|
||||||
stringParts = append(stringParts, fmt.Sprintf("%d=%s%s:%d", v, prefix, srv.Target, srv.Port))
|
stringParts = append(stringParts, fmt.Sprintf("%s=%s%s:%d", n, prefix, srv.Target, srv.Port))
|
||||||
|
log.Printf("etcd: Got bootstrap from DNS for %s at %s%s:%d", service, prefix, srv.Target, srv.Port)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func mustNewURLs(t *testing.T, urls []string) []url.URL {
|
func mustNewURLs(t *testing.T, urls []string) []url.URL {
|
||||||
|
if urls == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
u, err := types.NewURLs(urls)
|
u, err := types.NewURLs(urls)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected new urls error: %v", err)
|
t.Fatalf("unexpected new urls error: %v", err)
|
||||||
@ -58,14 +61,19 @@ func TestGenClusterString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGenDNSClusterString(t *testing.T) {
|
func TestGenDNSClusterString(t *testing.T) {
|
||||||
|
oldname := *name
|
||||||
|
*name = "dnsClusterTest"
|
||||||
|
defer func() { *name = oldname }()
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
withSSL []*net.SRV
|
withSSL []*net.SRV
|
||||||
withoutSSL []*net.SRV
|
withoutSSL []*net.SRV
|
||||||
|
urls []string
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
[]*net.SRV{},
|
[]*net.SRV{},
|
||||||
[]*net.SRV{},
|
[]*net.SRV{},
|
||||||
|
nil,
|
||||||
"",
|
"",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -75,6 +83,7 @@ func TestGenDNSClusterString(t *testing.T) {
|
|||||||
&net.SRV{Target: "10.0.0.3", Port: 2480},
|
&net.SRV{Target: "10.0.0.3", Port: 2480},
|
||||||
},
|
},
|
||||||
[]*net.SRV{},
|
[]*net.SRV{},
|
||||||
|
nil,
|
||||||
"0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480",
|
"0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -86,7 +95,20 @@ func TestGenDNSClusterString(t *testing.T) {
|
|||||||
[]*net.SRV{
|
[]*net.SRV{
|
||||||
&net.SRV{Target: "10.0.0.1", Port: 7001},
|
&net.SRV{Target: "10.0.0.1", Port: 7001},
|
||||||
},
|
},
|
||||||
"0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480,0=http://10.0.0.1:7001",
|
nil,
|
||||||
|
"0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480,3=http://10.0.0.1:7001",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]*net.SRV{
|
||||||
|
&net.SRV{Target: "10.0.0.1", Port: 2480},
|
||||||
|
&net.SRV{Target: "10.0.0.2", Port: 2480},
|
||||||
|
&net.SRV{Target: "10.0.0.3", Port: 2480},
|
||||||
|
},
|
||||||
|
[]*net.SRV{
|
||||||
|
&net.SRV{Target: "10.0.0.1", Port: 7001},
|
||||||
|
},
|
||||||
|
[]string{"https://10.0.0.1:2480"},
|
||||||
|
"dnsClusterTest=https://10.0.0.1:2480,0=https://10.0.0.2:2480,1=https://10.0.0.3:2480,2=http://10.0.0.1:7001",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +123,8 @@ func TestGenDNSClusterString(t *testing.T) {
|
|||||||
return "", nil, errors.New("Unkown service in mock")
|
return "", nil, errors.New("Unkown service in mock")
|
||||||
}
|
}
|
||||||
defer func() { lookupSRV = net.LookupSRV }()
|
defer func() { lookupSRV = net.LookupSRV }()
|
||||||
str, token, err := genDNSClusterString("token")
|
urls := mustNewURLs(t, tt.urls)
|
||||||
|
str, token, err := genDNSClusterString("token", urls)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("%d: err: %#v", i, err)
|
t.Fatalf("%d: err: %#v", i, err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user