Skip to content

Commit

Permalink
embed: only override default advertised client URL if the client list…
Browse files Browse the repository at this point in the history
…en URL is 0.0.0.0
  • Loading branch information
gyuho committed Dec 17, 2016
1 parent af3451b commit 6cac0ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 25 additions & 0 deletions embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,31 @@ func (cfg Config) IsDefaultHost() (string, error) {
return "", defaultHostStatus
}

// TODO: check whether fields are set instead of whether fields have default value
func UpdateDefaultClusterFromName(cfg *Config, defaultInitialCluster string) {
defaultHost, defaultHostErr := cfg.IsDefaultHost()
defaultHostOverride := defaultHost == "" || defaultHostErr == nil
if (defaultHostOverride || cfg.Name != DefaultName) && cfg.InitialCluster == defaultInitialCluster {
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
// if client-listen-url is 0.0.0.0, just use detected default host
// otherwise, rewrite advertise-client-url with localhost
if !isDefaultRoute(cfg.LCUrls[0].Host) {
cfg.updateACURLHost("localhost")
}
}
}

func isDefaultRoute(host string) bool {
ip, _, _ := net.SplitHostPort(host)
return ip == "0.0.0.0"
}

func (cfg *Config) updateACURLHost(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)}
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
7 changes: 1 addition & 6 deletions etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,7 @@ func startEtcdOrProxyV2() {
GoMaxProcs := runtime.GOMAXPROCS(0)
plog.Infof("setting maximum number of CPUs to %d, total number of available CPUs is %d", GoMaxProcs, runtime.NumCPU())

// TODO: check whether fields are set instead of whether fields have default value
defaultHost, defaultHostErr := cfg.IsDefaultHost()
defaultHostOverride := defaultHost == "" || defaultHostErr == nil
if (defaultHostOverride || cfg.Name != embed.DefaultName) && cfg.InitialCluster == defaultInitialCluster {
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
}
embed.UpdateDefaultClusterFromName(&cfg.Config, defaultInitialCluster)

if cfg.Dir == "" {
cfg.Dir = fmt.Sprintf("%v.etcd", cfg.Name)
Expand Down

0 comments on commit 6cac0ca

Please sign in to comment.