Browse Source

add apurl checking and logging

Barak Michener 11 years ago
parent
commit
fc70aa27d2
2 changed files with 38 additions and 12 deletions
  1. 13 10
      etcdmain/etcd.go
  2. 25 2
      etcdmain/etcd_test.go

+ 13 - 10
etcdmain/etcd.go

@@ -424,7 +424,7 @@ func setupCluster(apurls []url.URL) (*etcdserver.Cluster, error) {
 		clusterStr := genClusterString(*name, apurls)
 		cls, err = etcdserver.NewClusterFromString(*durl, clusterStr)
 	case set["dns-cluster-domain"]:
-		clusterStr, clusterToken, err := genDNSClusterString(*initialClusterToken)
+		clusterStr, clusterToken, err := genDNSClusterString(*initialClusterToken, apurls)
 		if err != nil {
 			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)
 // 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.
-func genDNSClusterString(defaultToken string) (string, string, error) {
-	targetName := make(map[string]int)
+// Also sees each entry as a separate instance.
+func genDNSClusterString(defaultToken string, apurls types.URLs) (string, string, error) {
 	stringParts := make([]string, 0)
 	tempName := int(0)
 
@@ -460,14 +459,18 @@ func genDNSClusterString(defaultToken string) (string, string, error) {
 			return err
 		}
 		for _, srv := range addrs {
-			var v int
-			var ok bool
-			if v, ok = targetName[srv.Target]; !ok {
-				v = tempName
-				targetName[srv.Target] = v
+			n := ""
+			for _, url := range apurls {
+				if url.Host == fmt.Sprintf("%s:%d", srv.Target, srv.Port) {
+					n = *name
+				}
+			}
+			if n == "" {
+				n = fmt.Sprintf("%d", tempName)
 				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
 	}

+ 25 - 2
etcdmain/etcd_test.go

@@ -26,6 +26,9 @@ import (
 )
 
 func mustNewURLs(t *testing.T, urls []string) []url.URL {
+	if urls == nil {
+		return nil
+	}
 	u, err := types.NewURLs(urls)
 	if err != nil {
 		t.Fatalf("unexpected new urls error: %v", err)
@@ -58,14 +61,19 @@ func TestGenClusterString(t *testing.T) {
 }
 
 func TestGenDNSClusterString(t *testing.T) {
+	oldname := *name
+	*name = "dnsClusterTest"
+	defer func() { *name = oldname }()
 	tests := []struct {
 		withSSL    []*net.SRV
 		withoutSSL []*net.SRV
+		urls       []string
 		expected   string
 	}{
 		{
 			[]*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{},
+			nil,
 			"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{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")
 		}
 		defer func() { lookupSRV = net.LookupSRV }()
-		str, token, err := genDNSClusterString("token")
+		urls := mustNewURLs(t, tt.urls)
+		str, token, err := genDNSClusterString("token", urls)
 		if err != nil {
 			t.Fatalf("%d: err: %#v", i, err)
 		}