mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #372 from philips/bind-addr-ports
fix(server): override port of bind
This commit is contained in:
commit
1a7eb36b6f
@ -445,7 +445,7 @@ func (c *Config) PeerTLSConfig() (TLSConfig, error) {
|
|||||||
return c.PeerTLSInfo().Config()
|
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.
|
// attach a schema.
|
||||||
func sanitizeURL(host string, defaultScheme string) (string, error) {
|
func sanitizeURL(host string, defaultScheme string) (string, error) {
|
||||||
// Blank URLs are fine input, just return it
|
// Blank URLs are fine input, just return it
|
||||||
@ -476,15 +476,23 @@ func sanitizeBindAddr(bindAddr string, addr string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
ahost, aport, err := net.SplitHostPort(aurl.Host)
|
// 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the listen host isn't set use the advertised host
|
|
||||||
if bindAddr == "" {
|
|
||||||
bindAddr = ahost
|
|
||||||
}
|
|
||||||
|
|
||||||
return net.JoinHostPort(bindAddr, aport), nil
|
return net.JoinHostPort(bindAddr, aport), nil
|
||||||
}
|
}
|
||||||
|
@ -223,6 +223,29 @@ func TestConfigBindAddrFlag(t *testing.T) {
|
|||||||
assert.Equal(t, c.BindAddr, "127.0.0.1:4003", "")
|
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.
|
// Ensures that the peers can be parsed from the environment.
|
||||||
func TestConfigPeersEnv(t *testing.T) {
|
func TestConfigPeersEnv(t *testing.T) {
|
||||||
withEnv("ETCD_PEERS", "coreos.com:4001,coreos.com:4002", func(c *Config) {
|
withEnv("ETCD_PEERS", "coreos.com:4001,coreos.com:4002", func(c *Config) {
|
||||||
|
@ -31,18 +31,18 @@ Cluster Configuration Options:
|
|||||||
should match the peer's '-peer-addr' flag.
|
should match the peer's '-peer-addr' flag.
|
||||||
|
|
||||||
Client Communication Options:
|
Client Communication Options:
|
||||||
-addr=<host:port> The public host:port used for client communication.
|
-addr=<host:port> The public host:port used for client communication.
|
||||||
-bind-addr=<host> The listening hostname 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.
|
-ca-file=<path> Path to the client CA file.
|
||||||
-cert-file=<path> Path to the client cert file.
|
-cert-file=<path> Path to the client cert file.
|
||||||
-key-file=<path> Path to the client key file.
|
-key-file=<path> Path to the client key file.
|
||||||
|
|
||||||
Peer Communication Options:
|
Peer Communication Options:
|
||||||
-peer-addr=<host:port> The public host:port used for peer communication.
|
-peer-addr=<host:port> The public host:port used for peer communication.
|
||||||
-peer-bind-addr=<host> The listening hostname 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-ca-file=<path> Path to the peer CA file.
|
||||||
-peer-cert-file=<path> Path to the peer cert file.
|
-peer-cert-file=<path> Path to the peer cert file.
|
||||||
-peer-key-file=<path> Path to the peer key file.
|
-peer-key-file=<path> Path to the peer key file.
|
||||||
|
|
||||||
Other Options:
|
Other Options:
|
||||||
-max-result-buffer Max size of the result buffer.
|
-max-result-buffer Max size of the result buffer.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user