Skip to content

Commit

Permalink
embed: overwrite advertise URLs when listen-url 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 531c306 commit 746c018
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
41 changes: 17 additions & 24 deletions embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/coreos/etcd/discovery"
"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/pkg/cors"
"github.com/coreos/etcd/pkg/netutil"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/etcd/pkg/types"
"github.com/ghodss/yaml"
Expand Down Expand Up @@ -54,23 +53,8 @@ var (

DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
DefaultAdvertiseClientURLs = "http://localhost:2379"

defaultHostname string = "localhost"
defaultHostStatus error
)

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 @@ -344,16 +328,25 @@ func (cfg Config) InitialClusterFromName(name string) (ret string) {
func (cfg Config) IsNewCluster() bool { return cfg.ClusterState == ClusterStateFlagNew }
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
// IsDefaultHost returns true if listen URL is 0.0.0.0 and needs detect host name.
func (cfg Config) IsDefaultHost() bool {
if len(cfg.LPUrls) == 1 {
ip, _, _ := net.SplitHostPort(cfg.LPUrls[0].Host)
if ip == "0.0.0.0" {
if len(cfg.APUrls) == 1 && cfg.APUrls[0].String() == DefaultInitialAdvertisePeerURLs {
return true
}
}
}
if len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs {
return defaultHostname, defaultHostStatus
if len(cfg.LCUrls) == 1 {
ip, _, _ := net.SplitHostPort(cfg.LCUrls[0].Host)
if ip == "0.0.0.0" {
if len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs {
return true
}
}
}
return "", defaultHostStatus
return false
}

// checkBindURLs returns an error if any URL uses a domain name.
Expand Down
31 changes: 18 additions & 13 deletions etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"reflect"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/coreos/etcd/pkg/cors"
"github.com/coreos/etcd/pkg/fileutil"
pkgioutil "github.com/coreos/etcd/pkg/ioutil"
"github.com/coreos/etcd/pkg/netutil"
"github.com/coreos/etcd/pkg/osutil"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/etcd/pkg/types"
Expand Down Expand Up @@ -85,10 +87,22 @@ func startEtcdOrProxyV2() {
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)
needDefaultHost := cfg.IsDefaultHost()
fmt.Println(needDefaultHost, cfg.InitialCluster, defaultInitialCluster)
if (needDefaultHost || cfg.Name != embed.DefaultName) && cfg.InitialCluster == defaultInitialCluster {
defaultHost, dhErr := netutil.GetDefaultHost()
if defaultHost != "" {
if dhErr == nil {
_, apPort, _ := net.SplitHostPort(cfg.APUrls[0].Host)
cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHost, apPort)}
_, acPort, _ := net.SplitHostPort(cfg.ACUrls[0].Host)
cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHost, acPort)}
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
plog.Infof("advertising using detected default host %q", defaultHost)
} else {
plog.Noticef("failed to detect default host, advertise falling back to %q (%v)", defaultHost, dhErr)
}
}
}

if cfg.Dir == "" {
Expand Down Expand Up @@ -188,15 +202,6 @@ func startEtcdOrProxyV2() {

// startEtcd runs StartEtcd in addition to hooks needed for standalone etcd.
func startEtcd(cfg *embed.Config) (<-chan struct{}, <-chan error, error) {
defaultHost, dhErr := cfg.IsDefaultHost()
if defaultHost != "" {
if dhErr == nil {
plog.Infof("advertising using detected default host %q", defaultHost)
} else {
plog.Noticef("failed to detect default host, advertise falling back to %q (%v)", defaultHost, dhErr)
}
}

e, err := embed.StartEtcd(cfg)
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit 746c018

Please sign in to comment.