From e58287f0262c46b2673e3d5d570ecb3578cdc882 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Fri, 16 Dec 2016 15:01:53 -0800 Subject: [PATCH] embed: only override default advertised client URL if the client listen URL is 0.0.0.0 --- embed/config.go | 18 ++++++++++++++++++ etcdmain/etcd.go | 7 +------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/embed/config.go b/embed/config.go index ca69b6393e0..b2c26365e93 100644 --- a/embed/config.go +++ b/embed/config.go @@ -356,6 +356,24 @@ func (cfg Config) IsDefaultHost() (string, error) { return "", defaultHostStatus } +// UpdateDefaultClusterFromName updates cluster advertise URLs with default host. +// TODO: check whether fields are set instead of whether fields have default value +func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) { + defaultHost, defaultHostErr := cfg.IsDefaultHost() + defaultHostOverride := defaultHost == "" || defaultHostErr == nil + if (defaultHostOverride || cfg.Name != DefaultName) && cfg.InitialCluster == defaultInitialCluster { + cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name) + ip, _, _ := net.SplitHostPort(cfg.LCUrls[0].Host) + // if client-listen-url is 0.0.0.0, just use detected default host + // otherwise, rewrite advertise-client-url with localhost + if ip != "0.0.0.0" { + _, acPort, _ := net.SplitHostPort(cfg.ACUrls[0].Host) + cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("localhost:%s", 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 { diff --git a/etcdmain/etcd.go b/etcdmain/etcd.go index fd990950a14..72653439101 100644 --- a/etcdmain/etcd.go +++ b/etcdmain/etcd.go @@ -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) - } + (&cfg.Config).UpdateDefaultClusterFromName(defaultInitialCluster) if cfg.Dir == "" { cfg.Dir = fmt.Sprintf("%v.etcd", cfg.Name)