Browse Source

fix(server): override port of bind

Allow people to specify ports on the `-bind-addr` arguments so that they
can use randomly assigned port numbers in containers.
Brandon Philips 12 years ago
parent
commit
9db521ca04
3 changed files with 48 additions and 17 deletions
  1. 15 7
      server/config.go
  2. 23 0
      server/config_test.go
  3. 10 10
      server/usage.go

+ 15 - 7
server/config.go

@@ -435,7 +435,7 @@ func (c *Config) PeerTLSConfig() (TLSConfig, error) {
 	return c.PeerTLSInfo().Config()
 }
 
-// sanitizeURL will cleanup a host string in the format hostname:port and
+// sanitizeURL will cleanup a host string in the format hostname[:port] and
 // attach a schema.
 func sanitizeURL(host string, defaultScheme string) (string, error) {
 	// Blank URLs are fine input, just return it
@@ -466,14 +466,22 @@ func sanitizeBindAddr(bindAddr string, addr string) (string, error) {
 		return "", err
 	}
 
-	ahost, aport, err := net.SplitHostPort(aurl.Host)
-	if err != nil {
-		return "", err
+	// If it is a valid host:port simply return with no further checks.
+	bhost, bport, err := net.SplitHostPort(bindAddr)
+	if err == nil && bhost != "" {
+		return bindAddr, nil
+	}
+
+	// SplitHostPort makes the host optional, but we don't want that.
+	if bhost == "" && bport != "" {
+		return "", fmt.Errorf("IP required can't use a port only")
 	}
 
-	// If the listen host isn't set use the advertised host
-	if bindAddr == "" {
-		bindAddr = ahost
+	// bindAddr doesn't have a port if we reach here so take the port from the
+	// advertised URL.
+	_, aport, err := net.SplitHostPort(aurl.Host)
+	if err != nil {
+		return "", err
 	}
 
 	return net.JoinHostPort(bindAddr, aport), nil

+ 23 - 0
server/config_test.go

@@ -223,6 +223,29 @@ func TestConfigBindAddrFlag(t *testing.T) {
 	assert.Equal(t, c.BindAddr, "127.0.0.1:4003", "")
 }
 
+// Ensures that a the Listen Host port overrides the advertised port
+func TestConfigBindAddrOverride(t *testing.T) {
+	c := NewConfig()
+	assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", "127.0.0.1:4010"}), "")
+	assert.Nil(t, c.Sanitize())
+	assert.Equal(t, c.BindAddr, "127.0.0.1:4010", "")
+}
+
+// Ensures that a the Listen Host inherits its port from the advertised addr
+func TestConfigBindAddrInheritPort(t *testing.T) {
+	c := NewConfig()
+	assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", "127.0.0.1"}), "")
+	assert.Nil(t, c.Sanitize())
+	assert.Equal(t, c.BindAddr, "127.0.0.1:4009", "")
+}
+
+// Ensures that a port only argument errors out
+func TestConfigBindAddrErrorOnNoHost(t *testing.T) {
+	c := NewConfig()
+	assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", ":4010"}), "")
+	assert.Error(t, c.Sanitize())
+}
+
 // Ensures that the peers can be parsed from the environment.
 func TestConfigPeersEnv(t *testing.T) {
 	withEnv("ETCD_PEERS", "coreos.com:4001,coreos.com:4002", func(c *Config) {

+ 10 - 10
server/usage.go

@@ -31,18 +31,18 @@ Cluster Configuration Options:
                                   should match the peer's '-peer-addr' flag.
 
 Client Communication Options:
-  -addr=<host:port>   The public host:port used for client communication.
-  -bind-addr=<host>   The listening hostname used for client communication.
-  -ca-file=<path>     Path to the client CA file.
-  -cert-file=<path>   Path to the client cert file.
-  -key-file=<path>    Path to the client key file.
+  -addr=<host:port>         The public host:port used for client communication.
+  -bind-addr=<host[:port]>  The listening host:port used for client communication.
+  -ca-file=<path>           Path to the client CA file.
+  -cert-file=<path>         Path to the client cert file.
+  -key-file=<path>          Path to the client key file.
 
 Peer Communication Options:
-  -peer-addr=<host:port>  The public host:port used for peer communication.
-  -peer-bind-addr=<host>  The listening hostname used for peer communication.
-  -peer-ca-file=<path>    Path to the peer CA file.
-  -peer-cert-file=<path>  Path to the peer cert file.
-  -peer-key-file=<path>   Path to the peer key file.
+  -peer-addr=<host:port>         The public host:port used for peer communication.
+  -peer-bind-addr=<host[:port]>  The listening host:port used for peer communication.
+  -peer-ca-file=<path>           Path to the peer CA file.
+  -peer-cert-file=<path>         Path to the peer cert file.
+  -peer-key-file=<path>          Path to the peer key file.
 
 Other Options:
   -max-result-buffer   Max size of the result buffer.