Skip to content
/ etcd Public
forked from etcd-io/etcd

Commit

Permalink
embed: overwrite advertise URLs only when IP is 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 d9e928d commit 40aecf0
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,9 @@ var (
DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
DefaultAdvertiseClientURLs = "http://localhost:2379"

defaultHostname string = "localhost"
defaultHostStatus error
defaultHostname string = "localhost"
)

func init() {
ip, err := netutil.GetDefaultHost()
if err != nil {
defaultHostStatus = err
return
}
// found default host, advertise on it
DefaultInitialAdvertisePeerURLs = "http://" + ip + ":2380"
DefaultAdvertiseClientURLs = "http://" + ip + ":2379"
defaultHostname = ip
}

// Config holds the arguments for configuring an etcd server.
type Config struct {
// member
Expand Down Expand Up @@ -346,14 +333,27 @@ func (cfg Config) ElectionTicks() int { return int(cfg.ElectionMs / cfg.TickMs)

// IsDefaultHost returns the default hostname, if used, and the error, if any,
// from getting the machine's default host.
func (cfg Config) IsDefaultHost() (string, error) {
if len(cfg.APUrls) == 1 && cfg.APUrls[0].String() == DefaultInitialAdvertisePeerURLs {
return defaultHostname, defaultHostStatus
func (cfg *Config) IsDefaultHost() (string, error) {
h, herr := netutil.GetDefaultHost()
if len(cfg.APUrls) == 1 {
ip, port, err := net.SplitHostPort(cfg.APUrls[0].Host)
if err != nil {
return "", err
}
if ip == "0.0.0.0" {
cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", h, port)}
}
}
if len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs {
return defaultHostname, defaultHostStatus
if len(cfg.ACUrls) == 1 {
ip, port, err := net.SplitHostPort(cfg.ACUrls[0].Host)
if err != nil {
return "", err
}
if ip == "0.0.0.0" {
cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", h, port)}
}
}
return "", defaultHostStatus
return h, herr
}

// checkBindURLs returns an error if any URL uses a domain name.
Expand Down

0 comments on commit 40aecf0

Please sign in to comment.