From e8594b60b180eae2158dadf6b9a5d5452a0fe18b Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Fri, 12 Aug 2016 19:55:55 -0700 Subject: [PATCH] embed: use default route IP for default advertise URL Fixes #2858 --- embed/config.go | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/embed/config.go b/embed/config.go index cb1ff1147..94e95902c 100644 --- a/embed/config.go +++ b/embed/config.go @@ -24,6 +24,7 @@ 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" @@ -33,13 +34,9 @@ const ( ClusterStateFlagNew = "new" ClusterStateFlagExisting = "existing" - DefaultName = "default" - DefaultInitialAdvertisePeerURLs = "http://localhost:2380" - DefaultAdvertiseClientURLs = "http://localhost:2379" - DefaultListenPeerURLs = "http://localhost:2380" - DefaultListenClientURLs = "http://localhost:2379" - DefaultMaxSnapshots = 5 - DefaultMaxWALs = 5 + DefaultName = "default" + DefaultMaxSnapshots = 5 + DefaultMaxWALs = 5 // maxElectionMs specifies the maximum value of election timeout. // More details are listed in ../Documentation/tuning.md#time-parameters. @@ -50,8 +47,28 @@ var ( ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " + "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"") ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly") + + DefaultListenPeerURLs = "http://localhost:2380" + DefaultListenClientURLs = "http://localhost:2379" + 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 @@ -317,3 +334,15 @@ 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 + } + if len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs { + return defaultHostname, defaultHostStatus + } + return "", defaultHostStatus +}