config.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package embed
  15. import (
  16. "crypto/tls"
  17. "fmt"
  18. "io/ioutil"
  19. "net"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "path/filepath"
  24. "strings"
  25. "time"
  26. "github.com/coreos/etcd/compactor"
  27. "github.com/coreos/etcd/etcdserver"
  28. "github.com/coreos/etcd/pkg/cors"
  29. "github.com/coreos/etcd/pkg/netutil"
  30. "github.com/coreos/etcd/pkg/srv"
  31. "github.com/coreos/etcd/pkg/transport"
  32. "github.com/coreos/etcd/pkg/types"
  33. "github.com/coreos/pkg/capnslog"
  34. "github.com/ghodss/yaml"
  35. "google.golang.org/grpc"
  36. "google.golang.org/grpc/grpclog"
  37. )
  38. const (
  39. ClusterStateFlagNew = "new"
  40. ClusterStateFlagExisting = "existing"
  41. DefaultName = "default"
  42. DefaultMaxSnapshots = 5
  43. DefaultMaxWALs = 5
  44. DefaultMaxTxnOps = uint(128)
  45. DefaultMaxRequestBytes = 1.5 * 1024 * 1024
  46. DefaultGRPCKeepAliveMinTime = 5 * time.Second
  47. DefaultGRPCKeepAliveInterval = 2 * time.Hour
  48. DefaultGRPCKeepAliveTimeout = 20 * time.Second
  49. DefaultListenPeerURLs = "http://localhost:2380"
  50. DefaultListenClientURLs = "http://localhost:2379"
  51. DefaultLogOutput = "default"
  52. // DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag.
  53. // It's enabled by default.
  54. DefaultStrictReconfigCheck = true
  55. // DefaultEnableV2 is the default value for "--enable-v2" flag.
  56. // v2 is enabled by default.
  57. // TODO: disable v2 when deprecated.
  58. DefaultEnableV2 = true
  59. // maxElectionMs specifies the maximum value of election timeout.
  60. // More details are listed in ../Documentation/tuning.md#time-parameters.
  61. maxElectionMs = 50000
  62. )
  63. var (
  64. ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
  65. "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"")
  66. ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")
  67. DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
  68. DefaultAdvertiseClientURLs = "http://localhost:2379"
  69. defaultHostname string
  70. defaultHostStatus error
  71. )
  72. func init() {
  73. defaultHostname, defaultHostStatus = netutil.GetDefaultHost()
  74. }
  75. // Config holds the arguments for configuring an etcd server.
  76. type Config struct {
  77. // member
  78. CorsInfo *cors.CORSInfo
  79. LPUrls, LCUrls []url.URL
  80. Dir string `json:"data-dir"`
  81. WalDir string `json:"wal-dir"`
  82. MaxSnapFiles uint `json:"max-snapshots"`
  83. MaxWalFiles uint `json:"max-wals"`
  84. Name string `json:"name"`
  85. SnapCount uint64 `json:"snapshot-count"`
  86. // AutoCompactionMode is either 'periodic' or 'revision'.
  87. AutoCompactionMode string `json:"auto-compaction-mode"`
  88. // AutoCompactionRetention is either duration string with time unit
  89. // (e.g. '5m' for 5-minute), or revision unit (e.g. '5000').
  90. // If no time unit is provided and compaction mode is 'periodic',
  91. // the unit defaults to hour. For example, '5' translates into 5-hour.
  92. AutoCompactionRetention string `json:"auto-compaction-retention"`
  93. // TickMs is the number of milliseconds between heartbeat ticks.
  94. // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
  95. // make ticks a cluster wide configuration.
  96. TickMs uint `json:"heartbeat-interval"`
  97. ElectionMs uint `json:"election-timeout"`
  98. // InitialElectionTickAdvance is true, then local member fast-forwards
  99. // election ticks to speed up "initial" leader election trigger. This
  100. // benefits the case of larger election ticks. For instance, cross
  101. // datacenter deployment may require longer election timeout of 10-second.
  102. // If true, local node does not need wait up to 10-second. Instead,
  103. // forwards its election ticks to 8-second, and have only 2-second left
  104. // before leader election.
  105. //
  106. // Major assumptions are that:
  107. // - cluster has no active leader thus advancing ticks enables faster
  108. // leader election, or
  109. // - cluster already has an established leader, and rejoining follower
  110. // is likely to receive heartbeats from the leader after tick advance
  111. // and before election timeout.
  112. //
  113. // However, when network from leader to rejoining follower is congested,
  114. // and the follower does not receive leader heartbeat within left election
  115. // ticks, disruptive election has to happen thus affecting cluster
  116. // availabilities.
  117. //
  118. // Disabling this would slow down initial bootstrap process for cross
  119. // datacenter deployments. Make your own tradeoffs by configuring
  120. // --initial-election-tick-advance at the cost of slow initial bootstrap.
  121. //
  122. // If single-node, it advances ticks regardless.
  123. //
  124. // See https://github.com/coreos/etcd/issues/9333 for more detail.
  125. InitialElectionTickAdvance bool `json:"initial-election-tick-advance"`
  126. QuotaBackendBytes int64 `json:"quota-backend-bytes"`
  127. MaxTxnOps uint `json:"max-txn-ops"`
  128. MaxRequestBytes uint `json:"max-request-bytes"`
  129. // gRPC server options
  130. // GRPCKeepAliveMinTime is the minimum interval that a client should
  131. // wait before pinging server. When client pings "too fast", server
  132. // sends goaway and closes the connection (errors: too_many_pings,
  133. // http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
  134. // Server expects client pings only when there is any active streams
  135. // (PermitWithoutStream is set false).
  136. GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
  137. // GRPCKeepAliveInterval is the frequency of server-to-client ping
  138. // to check if a connection is alive. Close a non-responsive connection
  139. // after an additional duration of Timeout. 0 to disable.
  140. GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
  141. // GRPCKeepAliveTimeout is the additional duration of wait
  142. // before closing a non-responsive connection. 0 to disable.
  143. GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`
  144. // clustering
  145. APUrls, ACUrls []url.URL
  146. ClusterState string `json:"initial-cluster-state"`
  147. DNSCluster string `json:"discovery-srv"`
  148. Dproxy string `json:"discovery-proxy"`
  149. Durl string `json:"discovery"`
  150. InitialCluster string `json:"initial-cluster"`
  151. InitialClusterToken string `json:"initial-cluster-token"`
  152. StrictReconfigCheck bool `json:"strict-reconfig-check"`
  153. EnableV2 bool `json:"enable-v2"`
  154. // security
  155. ClientTLSInfo transport.TLSInfo
  156. ClientAutoTLS bool
  157. PeerTLSInfo transport.TLSInfo
  158. PeerAutoTLS bool
  159. // debug
  160. Debug bool `json:"debug"`
  161. LogPkgLevels string `json:"log-package-levels"`
  162. LogOutput string `json:"log-output"`
  163. EnablePprof bool `json:"enable-pprof"`
  164. Metrics string `json:"metrics"`
  165. ListenMetricsUrls []url.URL
  166. ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`
  167. // ForceNewCluster starts a new cluster even if previously started; unsafe.
  168. ForceNewCluster bool `json:"force-new-cluster"`
  169. // UserHandlers is for registering users handlers and only used for
  170. // embedding etcd into other applications.
  171. // The map key is the route path for the handler, and
  172. // you must ensure it can't be conflicted with etcd's.
  173. UserHandlers map[string]http.Handler `json:"-"`
  174. // ServiceRegister is for registering users' gRPC services. A simple usage example:
  175. // cfg := embed.NewConfig()
  176. // cfg.ServerRegister = func(s *grpc.Server) {
  177. // pb.RegisterFooServer(s, &fooServer{})
  178. // pb.RegisterBarServer(s, &barServer{})
  179. // }
  180. // embed.StartEtcd(cfg)
  181. ServiceRegister func(*grpc.Server) `json:"-"`
  182. // auth
  183. AuthToken string `json:"auth-token"`
  184. // Experimental flags
  185. ExperimentalInitialCorruptCheck bool `json:"experimental-initial-corrupt-check"`
  186. ExperimentalCorruptCheckTime time.Duration `json:"experimental-corrupt-check-time"`
  187. ExperimentalEnableV2V3 string `json:"experimental-enable-v2v3"`
  188. }
  189. // configYAML holds the config suitable for yaml parsing
  190. type configYAML struct {
  191. Config
  192. configJSON
  193. }
  194. // configJSON has file options that are translated into Config options
  195. type configJSON struct {
  196. LPUrlsJSON string `json:"listen-peer-urls"`
  197. LCUrlsJSON string `json:"listen-client-urls"`
  198. CorsJSON string `json:"cors"`
  199. APUrlsJSON string `json:"initial-advertise-peer-urls"`
  200. ACUrlsJSON string `json:"advertise-client-urls"`
  201. ClientSecurityJSON securityConfig `json:"client-transport-security"`
  202. PeerSecurityJSON securityConfig `json:"peer-transport-security"`
  203. }
  204. type securityConfig struct {
  205. CAFile string `json:"ca-file"`
  206. CertFile string `json:"cert-file"`
  207. KeyFile string `json:"key-file"`
  208. CertAuth bool `json:"client-cert-auth"`
  209. TrustedCAFile string `json:"trusted-ca-file"`
  210. AutoTLS bool `json:"auto-tls"`
  211. }
  212. // NewConfig creates a new Config populated with default values.
  213. func NewConfig() *Config {
  214. lpurl, _ := url.Parse(DefaultListenPeerURLs)
  215. apurl, _ := url.Parse(DefaultInitialAdvertisePeerURLs)
  216. lcurl, _ := url.Parse(DefaultListenClientURLs)
  217. acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
  218. cfg := &Config{
  219. CorsInfo: &cors.CORSInfo{},
  220. MaxSnapFiles: DefaultMaxSnapshots,
  221. MaxWalFiles: DefaultMaxWALs,
  222. Name: DefaultName,
  223. SnapCount: etcdserver.DefaultSnapCount,
  224. MaxTxnOps: DefaultMaxTxnOps,
  225. MaxRequestBytes: DefaultMaxRequestBytes,
  226. GRPCKeepAliveMinTime: DefaultGRPCKeepAliveMinTime,
  227. GRPCKeepAliveInterval: DefaultGRPCKeepAliveInterval,
  228. GRPCKeepAliveTimeout: DefaultGRPCKeepAliveTimeout,
  229. TickMs: 100,
  230. ElectionMs: 1000,
  231. InitialElectionTickAdvance: true,
  232. LPUrls: []url.URL{*lpurl},
  233. LCUrls: []url.URL{*lcurl},
  234. APUrls: []url.URL{*apurl},
  235. ACUrls: []url.URL{*acurl},
  236. ClusterState: ClusterStateFlagNew,
  237. InitialClusterToken: "etcd-cluster",
  238. StrictReconfigCheck: DefaultStrictReconfigCheck,
  239. LogOutput: DefaultLogOutput,
  240. Metrics: "basic",
  241. EnableV2: DefaultEnableV2,
  242. AuthToken: "simple",
  243. }
  244. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  245. return cfg
  246. }
  247. func logTLSHandshakeFailure(conn *tls.Conn, err error) {
  248. state := conn.ConnectionState()
  249. remoteAddr := conn.RemoteAddr().String()
  250. serverName := state.ServerName
  251. if len(state.PeerCertificates) > 0 {
  252. cert := state.PeerCertificates[0]
  253. ips, dns := cert.IPAddresses, cert.DNSNames
  254. plog.Infof("rejected connection from %q (error %q, ServerName %q, IPAddresses %q, DNSNames %q)", remoteAddr, err.Error(), serverName, ips, dns)
  255. } else {
  256. plog.Infof("rejected connection from %q (error %q, ServerName %q)", remoteAddr, err.Error(), serverName)
  257. }
  258. }
  259. // SetupLogging initializes etcd logging.
  260. // Must be called after flag parsing.
  261. func (cfg *Config) SetupLogging() {
  262. cfg.ClientTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  263. cfg.PeerTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  264. capnslog.SetGlobalLogLevel(capnslog.INFO)
  265. if cfg.Debug {
  266. capnslog.SetGlobalLogLevel(capnslog.DEBUG)
  267. grpc.EnableTracing = true
  268. // enable info, warning, error
  269. grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
  270. } else {
  271. // only discard info
  272. grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, os.Stderr, os.Stderr))
  273. }
  274. if cfg.LogPkgLevels != "" {
  275. repoLog := capnslog.MustRepoLogger("github.com/coreos/etcd")
  276. settings, err := repoLog.ParseLogLevelConfig(cfg.LogPkgLevels)
  277. if err != nil {
  278. plog.Warningf("couldn't parse log level string: %s, continuing with default levels", err.Error())
  279. return
  280. }
  281. repoLog.SetLogLevel(settings)
  282. }
  283. // capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr))
  284. // where NewDefaultFormatter returns NewJournaldFormatter when syscall.Getppid() == 1
  285. // specify 'stdout' or 'stderr' to skip journald logging even when running under systemd
  286. switch cfg.LogOutput {
  287. case "stdout":
  288. capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug))
  289. case "stderr":
  290. capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stderr, cfg.Debug))
  291. case DefaultLogOutput:
  292. default:
  293. plog.Panicf(`unknown log-output %q (only supports %q, "stdout", "stderr")`, cfg.LogOutput, DefaultLogOutput)
  294. }
  295. }
  296. func ConfigFromFile(path string) (*Config, error) {
  297. cfg := &configYAML{Config: *NewConfig()}
  298. if err := cfg.configFromFile(path); err != nil {
  299. return nil, err
  300. }
  301. return &cfg.Config, nil
  302. }
  303. func (cfg *configYAML) configFromFile(path string) error {
  304. b, err := ioutil.ReadFile(path)
  305. if err != nil {
  306. return err
  307. }
  308. defaultInitialCluster := cfg.InitialCluster
  309. err = yaml.Unmarshal(b, cfg)
  310. if err != nil {
  311. return err
  312. }
  313. if cfg.LPUrlsJSON != "" {
  314. u, err := types.NewURLs(strings.Split(cfg.LPUrlsJSON, ","))
  315. if err != nil {
  316. plog.Fatalf("unexpected error setting up listen-peer-urls: %v", err)
  317. }
  318. cfg.LPUrls = []url.URL(u)
  319. }
  320. if cfg.LCUrlsJSON != "" {
  321. u, err := types.NewURLs(strings.Split(cfg.LCUrlsJSON, ","))
  322. if err != nil {
  323. plog.Fatalf("unexpected error setting up listen-client-urls: %v", err)
  324. }
  325. cfg.LCUrls = []url.URL(u)
  326. }
  327. if cfg.CorsJSON != "" {
  328. if err := cfg.CorsInfo.Set(cfg.CorsJSON); err != nil {
  329. plog.Panicf("unexpected error setting up cors: %v", err)
  330. }
  331. }
  332. if cfg.APUrlsJSON != "" {
  333. u, err := types.NewURLs(strings.Split(cfg.APUrlsJSON, ","))
  334. if err != nil {
  335. plog.Fatalf("unexpected error setting up initial-advertise-peer-urls: %v", err)
  336. }
  337. cfg.APUrls = []url.URL(u)
  338. }
  339. if cfg.ACUrlsJSON != "" {
  340. u, err := types.NewURLs(strings.Split(cfg.ACUrlsJSON, ","))
  341. if err != nil {
  342. plog.Fatalf("unexpected error setting up advertise-peer-urls: %v", err)
  343. }
  344. cfg.ACUrls = []url.URL(u)
  345. }
  346. if cfg.ListenMetricsUrlsJSON != "" {
  347. u, err := types.NewURLs(strings.Split(cfg.ListenMetricsUrlsJSON, ","))
  348. if err != nil {
  349. plog.Fatalf("unexpected error setting up listen-metrics-urls: %v", err)
  350. }
  351. cfg.ListenMetricsUrls = []url.URL(u)
  352. }
  353. // If a discovery flag is set, clear default initial cluster set by InitialClusterFromName
  354. if (cfg.Durl != "" || cfg.DNSCluster != "") && cfg.InitialCluster == defaultInitialCluster {
  355. cfg.InitialCluster = ""
  356. }
  357. if cfg.ClusterState == "" {
  358. cfg.ClusterState = ClusterStateFlagNew
  359. }
  360. copySecurityDetails := func(tls *transport.TLSInfo, ysc *securityConfig) {
  361. tls.CAFile = ysc.CAFile
  362. tls.CertFile = ysc.CertFile
  363. tls.KeyFile = ysc.KeyFile
  364. tls.ClientCertAuth = ysc.CertAuth
  365. tls.TrustedCAFile = ysc.TrustedCAFile
  366. }
  367. copySecurityDetails(&cfg.ClientTLSInfo, &cfg.ClientSecurityJSON)
  368. copySecurityDetails(&cfg.PeerTLSInfo, &cfg.PeerSecurityJSON)
  369. cfg.ClientAutoTLS = cfg.ClientSecurityJSON.AutoTLS
  370. cfg.PeerAutoTLS = cfg.PeerSecurityJSON.AutoTLS
  371. return cfg.Validate()
  372. }
  373. // Validate ensures that '*embed.Config' fields are properly configured.
  374. func (cfg *Config) Validate() error {
  375. if err := checkBindURLs(cfg.LPUrls); err != nil {
  376. return err
  377. }
  378. if err := checkBindURLs(cfg.LCUrls); err != nil {
  379. return err
  380. }
  381. if err := checkBindURLs(cfg.ListenMetricsUrls); err != nil {
  382. return err
  383. }
  384. if err := checkHostURLs(cfg.APUrls); err != nil {
  385. // TODO: return err in v3.4
  386. addrs := make([]string, len(cfg.APUrls))
  387. for i := range cfg.APUrls {
  388. addrs[i] = cfg.APUrls[i].String()
  389. }
  390. plog.Warningf("advertise-peer-urls %q is deprecated (%v)", strings.Join(addrs, ","), err)
  391. }
  392. if err := checkHostURLs(cfg.ACUrls); err != nil {
  393. // TODO: return err in v3.4
  394. addrs := make([]string, len(cfg.ACUrls))
  395. for i := range cfg.ACUrls {
  396. addrs[i] = cfg.ACUrls[i].String()
  397. }
  398. plog.Warningf("advertise-client-urls %q is deprecated (%v)", strings.Join(addrs, ","), err)
  399. }
  400. // Check if conflicting flags are passed.
  401. nSet := 0
  402. for _, v := range []bool{cfg.Durl != "", cfg.InitialCluster != "", cfg.DNSCluster != ""} {
  403. if v {
  404. nSet++
  405. }
  406. }
  407. if cfg.ClusterState != ClusterStateFlagNew && cfg.ClusterState != ClusterStateFlagExisting {
  408. return fmt.Errorf("unexpected clusterState %q", cfg.ClusterState)
  409. }
  410. if nSet > 1 {
  411. return ErrConflictBootstrapFlags
  412. }
  413. if cfg.TickMs <= 0 {
  414. return fmt.Errorf("--heartbeat-interval must be >0 (set to %dms)", cfg.TickMs)
  415. }
  416. if cfg.ElectionMs <= 0 {
  417. return fmt.Errorf("--election-timeout must be >0 (set to %dms)", cfg.ElectionMs)
  418. }
  419. if 5*cfg.TickMs > cfg.ElectionMs {
  420. return fmt.Errorf("--election-timeout[%vms] should be at least as 5 times as --heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
  421. }
  422. if cfg.ElectionMs > maxElectionMs {
  423. return fmt.Errorf("--election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs)
  424. }
  425. // check this last since proxying in etcdmain may make this OK
  426. if cfg.LCUrls != nil && cfg.ACUrls == nil {
  427. return ErrUnsetAdvertiseClientURLsFlag
  428. }
  429. switch cfg.AutoCompactionMode {
  430. case "":
  431. case compactor.ModeRevision, compactor.ModePeriodic:
  432. default:
  433. return fmt.Errorf("unknown auto-compaction-mode %q", cfg.AutoCompactionMode)
  434. }
  435. return nil
  436. }
  437. // PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.
  438. func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error) {
  439. token = cfg.InitialClusterToken
  440. switch {
  441. case cfg.Durl != "":
  442. urlsmap = types.URLsMap{}
  443. // If using discovery, generate a temporary cluster based on
  444. // self's advertised peer URLs
  445. urlsmap[cfg.Name] = cfg.APUrls
  446. token = cfg.Durl
  447. case cfg.DNSCluster != "":
  448. clusterStrs, cerr := srv.GetCluster("etcd-server", cfg.Name, cfg.DNSCluster, cfg.APUrls)
  449. if cerr != nil {
  450. plog.Errorf("couldn't resolve during SRV discovery (%v)", cerr)
  451. return nil, "", cerr
  452. }
  453. for _, s := range clusterStrs {
  454. plog.Noticef("got bootstrap from DNS for etcd-server at %s", s)
  455. }
  456. clusterStr := strings.Join(clusterStrs, ",")
  457. if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.CAFile == "" {
  458. cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
  459. }
  460. urlsmap, err = types.NewURLsMap(clusterStr)
  461. // only etcd member must belong to the discovered cluster.
  462. // proxy does not need to belong to the discovered cluster.
  463. if which == "etcd" {
  464. if _, ok := urlsmap[cfg.Name]; !ok {
  465. return nil, "", fmt.Errorf("cannot find local etcd member %q in SRV records", cfg.Name)
  466. }
  467. }
  468. default:
  469. // We're statically configured, and cluster has appropriately been set.
  470. urlsmap, err = types.NewURLsMap(cfg.InitialCluster)
  471. }
  472. return urlsmap, token, err
  473. }
  474. func (cfg Config) InitialClusterFromName(name string) (ret string) {
  475. if len(cfg.APUrls) == 0 {
  476. return ""
  477. }
  478. n := name
  479. if name == "" {
  480. n = DefaultName
  481. }
  482. for i := range cfg.APUrls {
  483. ret = ret + "," + n + "=" + cfg.APUrls[i].String()
  484. }
  485. return ret[1:]
  486. }
  487. func (cfg Config) IsNewCluster() bool { return cfg.ClusterState == ClusterStateFlagNew }
  488. func (cfg Config) ElectionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) }
  489. func (cfg Config) defaultPeerHost() bool {
  490. return len(cfg.APUrls) == 1 && cfg.APUrls[0].String() == DefaultInitialAdvertisePeerURLs
  491. }
  492. func (cfg Config) defaultClientHost() bool {
  493. return len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs
  494. }
  495. func (cfg *Config) ClientSelfCert() (err error) {
  496. if cfg.ClientAutoTLS && cfg.ClientTLSInfo.Empty() {
  497. chosts := make([]string, len(cfg.LCUrls))
  498. for i, u := range cfg.LCUrls {
  499. chosts[i] = u.Host
  500. }
  501. cfg.ClientTLSInfo, err = transport.SelfCert(filepath.Join(cfg.Dir, "fixtures", "client"), chosts)
  502. return err
  503. } else if cfg.ClientAutoTLS {
  504. plog.Warningf("ignoring client auto TLS since certs given")
  505. }
  506. return nil
  507. }
  508. func (cfg *Config) PeerSelfCert() (err error) {
  509. if cfg.PeerAutoTLS && cfg.PeerTLSInfo.Empty() {
  510. phosts := make([]string, len(cfg.LPUrls))
  511. for i, u := range cfg.LPUrls {
  512. phosts[i] = u.Host
  513. }
  514. cfg.PeerTLSInfo, err = transport.SelfCert(filepath.Join(cfg.Dir, "fixtures", "peer"), phosts)
  515. return err
  516. } else if cfg.PeerAutoTLS {
  517. plog.Warningf("ignoring peer auto TLS since certs given")
  518. }
  519. return nil
  520. }
  521. // UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host,
  522. // if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
  523. // e.g. advertise peer URL localhost:2380 or listen peer URL 0.0.0.0:2380
  524. // then the advertise peer host would be updated with machine's default host,
  525. // while keeping the listen URL's port.
  526. // User can work around this by explicitly setting URL with 127.0.0.1.
  527. // It returns the default hostname, if used, and the error, if any, from getting the machine's default host.
  528. // TODO: check whether fields are set instead of whether fields have default value
  529. func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error) {
  530. if defaultHostname == "" || defaultHostStatus != nil {
  531. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  532. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  533. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  534. }
  535. return "", defaultHostStatus
  536. }
  537. used := false
  538. pip, pport := cfg.LPUrls[0].Hostname(), cfg.LPUrls[0].Port()
  539. if cfg.defaultPeerHost() && pip == "0.0.0.0" {
  540. cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, pport)}
  541. used = true
  542. }
  543. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  544. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  545. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  546. }
  547. cip, cport := cfg.LCUrls[0].Hostname(), cfg.LCUrls[0].Port()
  548. if cfg.defaultClientHost() && cip == "0.0.0.0" {
  549. cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, cport)}
  550. used = true
  551. }
  552. dhost := defaultHostname
  553. if !used {
  554. dhost = ""
  555. }
  556. return dhost, defaultHostStatus
  557. }
  558. // checkBindURLs returns an error if any URL uses a domain name.
  559. func checkBindURLs(urls []url.URL) error {
  560. for _, url := range urls {
  561. if url.Scheme == "unix" || url.Scheme == "unixs" {
  562. continue
  563. }
  564. host, _, err := net.SplitHostPort(url.Host)
  565. if err != nil {
  566. return err
  567. }
  568. if host == "localhost" {
  569. // special case for local address
  570. // TODO: support /etc/hosts ?
  571. continue
  572. }
  573. if net.ParseIP(host) == nil {
  574. return fmt.Errorf("expected IP in URL for binding (%s)", url.String())
  575. }
  576. }
  577. return nil
  578. }
  579. func checkHostURLs(urls []url.URL) error {
  580. for _, url := range urls {
  581. host, _, err := net.SplitHostPort(url.Host)
  582. if err != nil {
  583. return err
  584. }
  585. if host == "" {
  586. return fmt.Errorf("unexpected empty host (%s)", url.String())
  587. }
  588. }
  589. return nil
  590. }