Browse Source

Merge pull request #8415 from heyitsanthony/fix-resolv-unix

netutil: don't resolve unix socket URLs when comparing URLs
Anthony Romano 8 years ago
parent
commit
2321835c47
2 changed files with 41 additions and 8 deletions
  1. 29 0
      e2e/etcd_config_test.go
  2. 12 8
      pkg/netutil/netutil.go

+ 29 - 0
e2e/etcd_config_test.go

@@ -84,3 +84,32 @@ func TestEtcdMultiPeer(t *testing.T) {
 		}
 	}
 }
+
+// TestEtcdUnixPeers checks that etcd will boot with unix socket peers.
+func TestEtcdUnixPeers(t *testing.T) {
+	d, err := ioutil.TempDir("", "e1.etcd")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer os.RemoveAll(d)
+	proc, err := spawnCmd(
+		[]string{
+			binDir + "/etcd",
+			"--data-dir", d,
+			"--name", "e1",
+			"--listen-peer-urls", "unix://etcd.unix:1",
+			"--initial-advertise-peer-urls", "unix://etcd.unix:1",
+			"--initial-cluster", "e1=unix://etcd.unix:1",
+		},
+	)
+	defer os.Remove("etcd.unix:1")
+	if err != nil {
+		t.Fatal(err)
+	}
+	if err = waitReadyExpectProc(proc, etcdServerReadyLines); err != nil {
+		t.Fatal(err)
+	}
+	if err = proc.Stop(); err != nil {
+		t.Fatal(err)
+	}
+}

+ 12 - 8
pkg/netutil/netutil.go

@@ -92,15 +92,19 @@ func resolveTCPAddrs(ctx context.Context, urls [][]url.URL) ([][]url.URL, error)
 }
 
 func resolveURL(ctx context.Context, u url.URL) (string, error) {
+	if u.Scheme == "unix" || u.Scheme == "unixs" {
+		// unix sockets don't resolve over TCP
+		return "", nil
+	}
+	host, _, err := net.SplitHostPort(u.Host)
+	if err != nil {
+		plog.Errorf("could not parse url %s during tcp resolving", u.Host)
+		return "", err
+	}
+	if host == "localhost" || net.ParseIP(host) != nil {
+		return "", nil
+	}
 	for ctx.Err() == nil {
-		host, _, err := net.SplitHostPort(u.Host)
-		if err != nil {
-			plog.Errorf("could not parse url %s during tcp resolving", u.Host)
-			return "", err
-		}
-		if host == "localhost" || net.ParseIP(host) != nil {
-			return "", nil
-		}
 		tcpAddr, err := resolveTCPAddr(ctx, u.Host)
 		if err == nil {
 			plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())