Skip to content

Commit

Permalink
embed: rewrite advertise url when listen-url is not 0.0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho committed Dec 16, 2016
1 parent 531c306 commit 5fa78a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@ func (cfg Config) IsDefaultHost() (string, error) {
return "", defaultHostStatus
}

// IsDefaultRoute returns true if listen URL is 0.0.0.0.
func (cfg Config) IsDefaultRoute() bool {
ip, _, _ := net.SplitHostPort(cfg.LPUrls[0].Host)
return ip == "0.0.0.0"
}

// UpdateHost updates advertise URLs.
func UpdateHost(cfg *Config, host string) {
_, acPort, _ := net.SplitHostPort(cfg.ACUrls[0].Host)
cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", host, acPort)}

_, apPort, _ := net.SplitHostPort(cfg.APUrls[0].Host)
cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", host, apPort)}

cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
}

// checkBindURLs returns an error if any URL uses a domain name.
// TODO: return error in 3.2.0
func checkBindURLs(urls []url.URL) error {
Expand Down
5 changes: 5 additions & 0 deletions etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func startEtcdOrProxyV2() {
defaultHostOverride := defaultHost == "" || defaultHostErr == nil
if (defaultHostOverride || cfg.Name != embed.DefaultName) && cfg.InitialCluster == defaultInitialCluster {
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
// if listen-url is 0.0.0.0, just use detected default host
// otherwise, rewrite advertise-url with localhost
if !cfg.IsDefaultRoute() {
embed.UpdateHost(&cfg.Config, "localhost")
}
}

if cfg.Dir == "" {
Expand Down

0 comments on commit 5fa78a3

Please sign in to comment.