2016-07-10 21:06:08 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package embed
|
|
|
|
|
|
|
|
import (
|
2017-11-02 22:49:18 +03:00
|
|
|
"crypto/tls"
|
2016-07-10 21:06:08 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-09-13 05:49:22 +03:00
|
|
|
"net"
|
2016-07-28 08:39:06 +03:00
|
|
|
"net/http"
|
2016-07-10 21:06:08 +03:00
|
|
|
"net/url"
|
2017-11-02 22:49:18 +03:00
|
|
|
"os"
|
2017-07-18 00:34:59 +03:00
|
|
|
"path/filepath"
|
2016-07-10 21:06:08 +03:00
|
|
|
"strings"
|
2018-04-15 08:52:39 +03:00
|
|
|
"sync"
|
2017-08-19 12:40:15 +03:00
|
|
|
"time"
|
2016-07-10 21:06:08 +03:00
|
|
|
|
2018-02-26 21:32:24 +03:00
|
|
|
"github.com/coreos/etcd/compactor"
|
2016-07-10 21:06:08 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver"
|
2018-03-26 23:16:08 +03:00
|
|
|
"github.com/coreos/etcd/pkg/flags"
|
2016-08-13 05:55:55 +03:00
|
|
|
"github.com/coreos/etcd/pkg/netutil"
|
2017-05-05 06:15:17 +03:00
|
|
|
"github.com/coreos/etcd/pkg/srv"
|
2016-07-10 21:06:08 +03:00
|
|
|
"github.com/coreos/etcd/pkg/transport"
|
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2017-01-23 05:43:03 +03:00
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
"github.com/ghodss/yaml"
|
2018-04-02 23:40:10 +03:00
|
|
|
"go.uber.org/zap"
|
2018-04-25 01:25:20 +03:00
|
|
|
"go.uber.org/zap/zapcore"
|
2017-01-22 13:21:19 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-07-10 21:06:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ClusterStateFlagNew = "new"
|
|
|
|
ClusterStateFlagExisting = "existing"
|
|
|
|
|
2017-08-25 23:26:28 +03:00
|
|
|
DefaultName = "default"
|
|
|
|
DefaultMaxSnapshots = 5
|
|
|
|
DefaultMaxWALs = 5
|
|
|
|
DefaultMaxTxnOps = uint(128)
|
|
|
|
DefaultMaxRequestBytes = 1.5 * 1024 * 1024
|
|
|
|
DefaultGRPCKeepAliveMinTime = 5 * time.Second
|
|
|
|
DefaultGRPCKeepAliveInterval = 2 * time.Hour
|
|
|
|
DefaultGRPCKeepAliveTimeout = 20 * time.Second
|
2016-07-10 21:06:08 +03:00
|
|
|
|
2016-11-11 15:33:31 +03:00
|
|
|
DefaultListenPeerURLs = "http://localhost:2380"
|
|
|
|
DefaultListenClientURLs = "http://localhost:2379"
|
|
|
|
|
2017-11-03 00:32:20 +03:00
|
|
|
DefaultLogOutput = "default"
|
|
|
|
|
2017-12-14 19:30:28 +03:00
|
|
|
// DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag.
|
|
|
|
// It's enabled by default.
|
|
|
|
DefaultStrictReconfigCheck = true
|
|
|
|
// DefaultEnableV2 is the default value for "--enable-v2" flag.
|
|
|
|
// v2 is enabled by default.
|
|
|
|
// TODO: disable v2 when deprecated.
|
|
|
|
DefaultEnableV2 = true
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
// maxElectionMs specifies the maximum value of election timeout.
|
|
|
|
// More details are listed in ../Documentation/tuning.md#time-parameters.
|
|
|
|
maxElectionMs = 50000
|
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2016-08-13 05:55:55 +03:00
|
|
|
|
|
|
|
DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
|
|
|
|
DefaultAdvertiseClientURLs = "http://localhost:2379"
|
|
|
|
|
2018-03-26 23:16:08 +03:00
|
|
|
defaultHostname string
|
|
|
|
defaultHostStatus error
|
2016-07-10 21:06:08 +03:00
|
|
|
)
|
|
|
|
|
2018-01-30 02:28:40 +03:00
|
|
|
var (
|
|
|
|
// CompactorModePeriodic is periodic compaction mode
|
|
|
|
// for "Config.AutoCompactionMode" field.
|
|
|
|
// If "AutoCompactionMode" is CompactorModePeriodic and
|
|
|
|
// "AutoCompactionRetention" is "1h", it automatically compacts
|
|
|
|
// compacts storage every hour.
|
|
|
|
CompactorModePeriodic = compactor.ModePeriodic
|
|
|
|
|
|
|
|
// CompactorModeRevision is revision-based compaction mode
|
|
|
|
// for "Config.AutoCompactionMode" field.
|
|
|
|
// If "AutoCompactionMode" is CompactorModeRevision and
|
|
|
|
// "AutoCompactionRetention" is "1000", it compacts log on
|
|
|
|
// revision 5000 when the current revision is 6000.
|
|
|
|
// This runs every 5-minute if enough of logs have proceeded.
|
|
|
|
CompactorModeRevision = compactor.ModeRevision
|
|
|
|
)
|
|
|
|
|
2016-08-13 05:55:55 +03:00
|
|
|
func init() {
|
2017-03-04 00:32:46 +03:00
|
|
|
defaultHostname, defaultHostStatus = netutil.GetDefaultHost()
|
2016-08-13 05:55:55 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
// Config holds the arguments for configuring an etcd server.
|
|
|
|
type Config struct {
|
2018-04-15 08:52:39 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
Dir string `json:"data-dir"`
|
|
|
|
WalDir string `json:"wal-dir"`
|
|
|
|
SnapCount uint64 `json:"snapshot-count"`
|
|
|
|
MaxSnapFiles uint `json:"max-snapshots"`
|
|
|
|
MaxWalFiles uint `json:"max-wals"`
|
2016-07-10 21:06:08 +03:00
|
|
|
|
|
|
|
// TickMs is the number of milliseconds between heartbeat ticks.
|
|
|
|
// TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
|
|
|
|
// make ticks a cluster wide configuration.
|
2018-04-20 00:03:17 +03:00
|
|
|
TickMs uint `json:"heartbeat-interval"`
|
|
|
|
ElectionMs uint `json:"election-timeout"`
|
|
|
|
|
|
|
|
// InitialElectionTickAdvance is true, then local member fast-forwards
|
|
|
|
// election ticks to speed up "initial" leader election trigger. This
|
|
|
|
// benefits the case of larger election ticks. For instance, cross
|
|
|
|
// datacenter deployment may require longer election timeout of 10-second.
|
|
|
|
// If true, local node does not need wait up to 10-second. Instead,
|
|
|
|
// forwards its election ticks to 8-second, and have only 2-second left
|
|
|
|
// before leader election.
|
|
|
|
//
|
|
|
|
// Major assumptions are that:
|
|
|
|
// - cluster has no active leader thus advancing ticks enables faster
|
|
|
|
// leader election, or
|
|
|
|
// - cluster already has an established leader, and rejoining follower
|
|
|
|
// is likely to receive heartbeats from the leader after tick advance
|
|
|
|
// and before election timeout.
|
|
|
|
//
|
|
|
|
// However, when network from leader to rejoining follower is congested,
|
|
|
|
// and the follower does not receive leader heartbeat within left election
|
|
|
|
// ticks, disruptive election has to happen thus affecting cluster
|
|
|
|
// availabilities.
|
|
|
|
//
|
|
|
|
// Disabling this would slow down initial bootstrap process for cross
|
|
|
|
// datacenter deployments. Make your own tradeoffs by configuring
|
|
|
|
// --initial-election-tick-advance at the cost of slow initial bootstrap.
|
|
|
|
//
|
|
|
|
// If single-node, it advances ticks regardless.
|
|
|
|
//
|
|
|
|
// See https://github.com/coreos/etcd/issues/9333 for more detail.
|
|
|
|
InitialElectionTickAdvance bool `json:"initial-election-tick-advance"`
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
QuotaBackendBytes int64 `json:"quota-backend-bytes"`
|
2017-05-24 03:50:20 +03:00
|
|
|
MaxTxnOps uint `json:"max-txn-ops"`
|
2017-05-23 02:54:50 +03:00
|
|
|
MaxRequestBytes uint `json:"max-request-bytes"`
|
2016-07-10 21:06:08 +03:00
|
|
|
|
2018-04-15 08:52:39 +03:00
|
|
|
LPUrls, LCUrls []url.URL
|
|
|
|
APUrls, ACUrls []url.URL
|
|
|
|
ClientTLSInfo transport.TLSInfo
|
|
|
|
ClientAutoTLS bool
|
|
|
|
PeerTLSInfo transport.TLSInfo
|
|
|
|
PeerAutoTLS bool
|
|
|
|
|
|
|
|
ClusterState string `json:"initial-cluster-state"`
|
|
|
|
DNSCluster string `json:"discovery-srv"`
|
|
|
|
DNSClusterServiceName string `json:"discovery-srv-name"`
|
|
|
|
Dproxy string `json:"discovery-proxy"`
|
|
|
|
Durl string `json:"discovery"`
|
|
|
|
InitialCluster string `json:"initial-cluster"`
|
|
|
|
InitialClusterToken string `json:"initial-cluster-token"`
|
|
|
|
StrictReconfigCheck bool `json:"strict-reconfig-check"`
|
|
|
|
EnableV2 bool `json:"enable-v2"`
|
|
|
|
|
|
|
|
// AutoCompactionMode is either 'periodic' or 'revision'.
|
|
|
|
AutoCompactionMode string `json:"auto-compaction-mode"`
|
|
|
|
// AutoCompactionRetention is either duration string with time unit
|
|
|
|
// (e.g. '5m' for 5-minute), or revision unit (e.g. '5000').
|
|
|
|
// If no time unit is provided and compaction mode is 'periodic',
|
|
|
|
// the unit defaults to hour. For example, '5' translates into 5-hour.
|
|
|
|
AutoCompactionRetention string `json:"auto-compaction-retention"`
|
|
|
|
|
2017-08-25 23:26:28 +03:00
|
|
|
// GRPCKeepAliveMinTime is the minimum interval that a client should
|
|
|
|
// wait before pinging server. When client pings "too fast", server
|
|
|
|
// sends goaway and closes the connection (errors: too_many_pings,
|
|
|
|
// http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
|
|
|
|
// Server expects client pings only when there is any active streams
|
|
|
|
// (PermitWithoutStream is set false).
|
|
|
|
GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
|
|
|
|
// GRPCKeepAliveInterval is the frequency of server-to-client ping
|
|
|
|
// to check if a connection is alive. Close a non-responsive connection
|
|
|
|
// after an additional duration of Timeout. 0 to disable.
|
|
|
|
GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
|
|
|
|
// GRPCKeepAliveTimeout is the additional duration of wait
|
|
|
|
// before closing a non-responsive connection. 0 to disable.
|
|
|
|
GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`
|
|
|
|
|
2018-03-18 06:19:18 +03:00
|
|
|
// PreVote is true to enable Raft Pre-Vote.
|
|
|
|
// If enabled, Raft runs an additional election phase
|
|
|
|
// to check whether it would get enough votes to win
|
|
|
|
// an election, thus minimizing disruptions.
|
|
|
|
// TODO: enable by default in 3.5.
|
|
|
|
PreVote bool `json:"pre-vote"`
|
2016-07-10 21:06:08 +03:00
|
|
|
|
2018-03-27 03:11:27 +03:00
|
|
|
CORS map[string]struct{}
|
2018-03-26 23:16:08 +03:00
|
|
|
|
2018-02-27 02:14:18 +03:00
|
|
|
// HostWhitelist lists acceptable hostnames from HTTP client requests.
|
|
|
|
// Client origin policy protects against "DNS Rebinding" attacks
|
|
|
|
// to insecure etcd servers. That is, any website can simply create
|
|
|
|
// an authorized DNS name, and direct DNS to "localhost" (or any
|
|
|
|
// other address). Then, all HTTP endpoints of etcd server listening
|
|
|
|
// on "localhost" becomes accessible, thus vulnerable to DNS rebinding
|
|
|
|
// attacks. See "CVE-2018-5702" for more detail.
|
|
|
|
//
|
|
|
|
// 1. If client connection is secure via HTTPS, allow any hostnames.
|
|
|
|
// 2. If client connection is not secure and "HostWhitelist" is not empty,
|
|
|
|
// only allow HTTP requests whose Host field is listed in whitelist.
|
|
|
|
//
|
|
|
|
// Note that the client origin policy is enforced whether authentication
|
|
|
|
// is enabled or not, for tighter controls.
|
|
|
|
//
|
2018-03-27 04:25:47 +03:00
|
|
|
// By default, "HostWhitelist" is "*", which allows any hostnames.
|
2018-02-27 02:14:18 +03:00
|
|
|
// Note that when specifying hostnames, loopback addresses are not added
|
2018-03-27 04:25:47 +03:00
|
|
|
// automatically. To allow loopback interfaces, leave it empty or set it "*",
|
|
|
|
// or add them to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
|
2018-02-27 02:14:18 +03:00
|
|
|
//
|
|
|
|
// CVE-2018-5702 reference:
|
|
|
|
// - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
|
|
|
|
// - https://github.com/transmission/transmission/pull/468
|
|
|
|
// - https://github.com/coreos/etcd/issues/9353
|
2018-03-26 23:16:08 +03:00
|
|
|
HostWhitelist map[string]struct{}
|
2018-02-27 02:14:18 +03:00
|
|
|
|
2016-07-28 08:39:06 +03:00
|
|
|
// UserHandlers is for registering users handlers and only used for
|
|
|
|
// embedding etcd into other applications.
|
|
|
|
// The map key is the route path for the handler, and
|
|
|
|
// you must ensure it can't be conflicted with etcd's.
|
|
|
|
UserHandlers map[string]http.Handler `json:"-"`
|
2017-01-23 05:43:03 +03:00
|
|
|
// ServiceRegister is for registering users' gRPC services. A simple usage example:
|
|
|
|
// cfg := embed.NewConfig()
|
|
|
|
// cfg.ServerRegister = func(s *grpc.Server) {
|
|
|
|
// pb.RegisterFooServer(s, &fooServer{})
|
|
|
|
// pb.RegisterBarServer(s, &barServer{})
|
|
|
|
// }
|
|
|
|
// embed.StartEtcd(cfg)
|
2017-01-22 13:21:19 +03:00
|
|
|
ServiceRegister func(*grpc.Server) `json:"-"`
|
2016-07-21 08:13:57 +03:00
|
|
|
|
|
|
|
AuthToken string `json:"auth-token"`
|
2017-08-19 12:40:15 +03:00
|
|
|
|
2017-11-22 00:17:59 +03:00
|
|
|
ExperimentalInitialCorruptCheck bool `json:"experimental-initial-corrupt-check"`
|
|
|
|
ExperimentalCorruptCheckTime time.Duration `json:"experimental-corrupt-check-time"`
|
|
|
|
ExperimentalEnableV2V3 string `json:"experimental-enable-v2v3"`
|
2018-03-23 15:24:20 +03:00
|
|
|
|
|
|
|
// ForceNewCluster starts a new cluster even if previously started; unsafe.
|
|
|
|
ForceNewCluster bool `json:"force-new-cluster"`
|
2018-04-15 08:52:39 +03:00
|
|
|
|
|
|
|
EnablePprof bool `json:"enable-pprof"`
|
|
|
|
Metrics string `json:"metrics"`
|
|
|
|
ListenMetricsUrls []url.URL
|
|
|
|
ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`
|
|
|
|
|
|
|
|
// Logger is logger options: "zap", "capnslog".
|
|
|
|
// WARN: "capnslog" is being deprecated in v3.5.
|
|
|
|
Logger string `json:"logger"`
|
|
|
|
|
2018-04-25 21:38:11 +03:00
|
|
|
// DeprecatedLogOutput is to be deprecated in v3.5.
|
|
|
|
// Just here for safe migration in v3.4.
|
|
|
|
DeprecatedLogOutput []string `json:"log-output"`
|
|
|
|
|
2018-04-25 20:28:13 +03:00
|
|
|
// LogOutputs is either:
|
2018-04-17 21:50:48 +03:00
|
|
|
// - "default" as os.Stderr,
|
|
|
|
// - "stderr" as os.Stderr,
|
|
|
|
// - "stdout" as os.Stdout,
|
|
|
|
// - file path to append server logs to.
|
|
|
|
// It can be multiple when "Logger" is zap.
|
2018-04-25 21:38:11 +03:00
|
|
|
LogOutputs []string `json:"log-outputs"`
|
2018-04-27 00:22:33 +03:00
|
|
|
|
2018-04-15 08:52:39 +03:00
|
|
|
// Debug is true, to enable debug level logging.
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
|
2018-04-25 01:40:52 +03:00
|
|
|
// logger logs server-side operations. The default is nil,
|
|
|
|
// and "setupLogging" must be called before starting server.
|
|
|
|
// Do not set logger directly.
|
|
|
|
loggerMu *sync.RWMutex
|
|
|
|
logger *zap.Logger
|
|
|
|
|
|
|
|
// loggerConfig is server logger configuration for Raft logger.
|
|
|
|
// Must be either: "loggerConfig != nil" or "loggerCore != nil && loggerWriteSyncer != nil".
|
|
|
|
loggerConfig *zap.Config
|
|
|
|
// loggerCore is "zapcore.Core" for raft logger.
|
|
|
|
// Must be either: "loggerConfig != nil" or "loggerCore != nil && loggerWriteSyncer != nil".
|
|
|
|
loggerCore zapcore.Core
|
|
|
|
loggerWriteSyncer zapcore.WriteSyncer
|
|
|
|
|
|
|
|
// TO BE DEPRECATED
|
|
|
|
|
2018-04-15 08:52:39 +03:00
|
|
|
// LogPkgLevels is being deprecated in v3.5.
|
|
|
|
// Only valid if "logger" option is "capnslog".
|
|
|
|
// WARN: DO NOT USE THIS!
|
|
|
|
LogPkgLevels string `json:"log-package-levels"`
|
2016-07-10 21:06:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// configYAML holds the config suitable for yaml parsing
|
|
|
|
type configYAML struct {
|
|
|
|
Config
|
|
|
|
configJSON
|
|
|
|
}
|
|
|
|
|
|
|
|
// configJSON has file options that are translated into Config options
|
|
|
|
type configJSON struct {
|
2018-03-26 23:16:08 +03:00
|
|
|
LPUrlsJSON string `json:"listen-peer-urls"`
|
|
|
|
LCUrlsJSON string `json:"listen-client-urls"`
|
|
|
|
APUrlsJSON string `json:"initial-advertise-peer-urls"`
|
|
|
|
ACUrlsJSON string `json:"advertise-client-urls"`
|
|
|
|
|
2018-03-27 03:11:27 +03:00
|
|
|
CORSJSON string `json:"cors"`
|
2018-03-26 23:16:08 +03:00
|
|
|
HostWhitelistJSON string `json:"host-whitelist"`
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
ClientSecurityJSON securityConfig `json:"client-transport-security"`
|
|
|
|
PeerSecurityJSON securityConfig `json:"peer-transport-security"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type securityConfig struct {
|
|
|
|
CertFile string `json:"cert-file"`
|
|
|
|
KeyFile string `json:"key-file"`
|
|
|
|
CertAuth bool `json:"client-cert-auth"`
|
|
|
|
TrustedCAFile string `json:"trusted-ca-file"`
|
|
|
|
AutoTLS bool `json:"auto-tls"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig creates a new Config populated with default values.
|
|
|
|
func NewConfig() *Config {
|
2016-07-25 21:06:03 +03:00
|
|
|
lpurl, _ := url.Parse(DefaultListenPeerURLs)
|
2016-07-10 21:06:08 +03:00
|
|
|
apurl, _ := url.Parse(DefaultInitialAdvertisePeerURLs)
|
2016-07-25 21:06:03 +03:00
|
|
|
lcurl, _ := url.Parse(DefaultListenClientURLs)
|
2016-07-10 21:06:08 +03:00
|
|
|
acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
|
|
|
|
cfg := &Config{
|
2018-04-17 23:27:55 +03:00
|
|
|
MaxSnapFiles: DefaultMaxSnapshots,
|
|
|
|
MaxWalFiles: DefaultMaxWALs,
|
|
|
|
|
|
|
|
Name: DefaultName,
|
|
|
|
|
|
|
|
SnapCount: etcdserver.DefaultSnapCount,
|
|
|
|
MaxTxnOps: DefaultMaxTxnOps,
|
|
|
|
MaxRequestBytes: DefaultMaxRequestBytes,
|
|
|
|
|
2017-08-25 23:26:28 +03:00
|
|
|
GRPCKeepAliveMinTime: DefaultGRPCKeepAliveMinTime,
|
|
|
|
GRPCKeepAliveInterval: DefaultGRPCKeepAliveInterval,
|
|
|
|
GRPCKeepAliveTimeout: DefaultGRPCKeepAliveTimeout,
|
2018-04-17 23:27:55 +03:00
|
|
|
|
2018-04-20 00:03:17 +03:00
|
|
|
TickMs: 100,
|
|
|
|
ElectionMs: 1000,
|
|
|
|
InitialElectionTickAdvance: true,
|
2018-04-17 23:27:55 +03:00
|
|
|
|
|
|
|
LPUrls: []url.URL{*lpurl},
|
|
|
|
LCUrls: []url.URL{*lcurl},
|
|
|
|
APUrls: []url.URL{*apurl},
|
|
|
|
ACUrls: []url.URL{*acurl},
|
|
|
|
|
|
|
|
ClusterState: ClusterStateFlagNew,
|
|
|
|
InitialClusterToken: "etcd-cluster",
|
|
|
|
|
|
|
|
StrictReconfigCheck: DefaultStrictReconfigCheck,
|
|
|
|
Metrics: "basic",
|
|
|
|
EnableV2: DefaultEnableV2,
|
|
|
|
|
|
|
|
CORS: map[string]struct{}{"*": {}},
|
|
|
|
HostWhitelist: map[string]struct{}{"*": {}},
|
|
|
|
|
|
|
|
AuthToken: "simple",
|
|
|
|
|
|
|
|
PreVote: false, // TODO: enable by default in v3.5
|
2018-04-15 08:52:39 +03:00
|
|
|
|
2018-04-25 21:38:11 +03:00
|
|
|
loggerMu: new(sync.RWMutex),
|
|
|
|
logger: nil,
|
|
|
|
Logger: "capnslog",
|
|
|
|
DeprecatedLogOutput: []string{DefaultLogOutput},
|
|
|
|
LogOutputs: []string{DefaultLogOutput},
|
|
|
|
Debug: false,
|
|
|
|
LogPkgLevels: "",
|
2016-07-10 21:06:08 +03:00
|
|
|
}
|
|
|
|
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2017-12-01 20:25:39 +03:00
|
|
|
func logTLSHandshakeFailure(conn *tls.Conn, err error) {
|
|
|
|
state := conn.ConnectionState()
|
|
|
|
remoteAddr := conn.RemoteAddr().String()
|
|
|
|
serverName := state.ServerName
|
|
|
|
if len(state.PeerCertificates) > 0 {
|
|
|
|
cert := state.PeerCertificates[0]
|
|
|
|
ips, dns := cert.IPAddresses, cert.DNSNames
|
|
|
|
plog.Infof("rejected connection from %q (error %q, ServerName %q, IPAddresses %q, DNSNames %q)", remoteAddr, err.Error(), serverName, ips, dns)
|
|
|
|
} else {
|
|
|
|
plog.Infof("rejected connection from %q (error %q, ServerName %q)", remoteAddr, err.Error(), serverName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
func ConfigFromFile(path string) (*Config, error) {
|
2016-08-10 19:44:50 +03:00
|
|
|
cfg := &configYAML{Config: *NewConfig()}
|
2016-07-10 21:06:08 +03:00
|
|
|
if err := cfg.configFromFile(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cfg.Config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *configYAML) configFromFile(path string) error {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-28 08:26:02 +03:00
|
|
|
defaultInitialCluster := cfg.InitialCluster
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
err = yaml.Unmarshal(b, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.LPUrlsJSON != "" {
|
|
|
|
u, err := types.NewURLs(strings.Split(cfg.LPUrlsJSON, ","))
|
|
|
|
if err != nil {
|
2018-04-15 08:52:39 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "unexpected error setting up listen-peer-urls: %v\n", err)
|
|
|
|
os.Exit(1)
|
2016-07-10 21:06:08 +03:00
|
|
|
}
|
|
|
|
cfg.LPUrls = []url.URL(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.LCUrlsJSON != "" {
|
|
|
|
u, err := types.NewURLs(strings.Split(cfg.LCUrlsJSON, ","))
|
|
|
|
if err != nil {
|
2018-04-15 08:52:39 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "unexpected error setting up listen-client-urls: %v\n", err)
|
|
|
|
os.Exit(1)
|
2016-07-10 21:06:08 +03:00
|
|
|
}
|
|
|
|
cfg.LCUrls = []url.URL(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.APUrlsJSON != "" {
|
|
|
|
u, err := types.NewURLs(strings.Split(cfg.APUrlsJSON, ","))
|
|
|
|
if err != nil {
|
2018-04-15 08:52:39 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "unexpected error setting up initial-advertise-peer-urls: %v\n", err)
|
|
|
|
os.Exit(1)
|
2016-07-10 21:06:08 +03:00
|
|
|
}
|
|
|
|
cfg.APUrls = []url.URL(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ACUrlsJSON != "" {
|
|
|
|
u, err := types.NewURLs(strings.Split(cfg.ACUrlsJSON, ","))
|
|
|
|
if err != nil {
|
2018-04-15 08:52:39 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "unexpected error setting up advertise-peer-urls: %v\n", err)
|
|
|
|
os.Exit(1)
|
2016-07-10 21:06:08 +03:00
|
|
|
}
|
|
|
|
cfg.ACUrls = []url.URL(u)
|
|
|
|
}
|
|
|
|
|
2017-07-12 20:05:16 +03:00
|
|
|
if cfg.ListenMetricsUrlsJSON != "" {
|
|
|
|
u, err := types.NewURLs(strings.Split(cfg.ListenMetricsUrlsJSON, ","))
|
|
|
|
if err != nil {
|
2018-04-15 08:52:39 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "unexpected error setting up listen-metrics-urls: %v\n", err)
|
|
|
|
os.Exit(1)
|
2017-07-12 20:05:16 +03:00
|
|
|
}
|
|
|
|
cfg.ListenMetricsUrls = []url.URL(u)
|
|
|
|
}
|
|
|
|
|
2018-03-27 03:11:27 +03:00
|
|
|
if cfg.CORSJSON != "" {
|
|
|
|
uv := flags.NewUniqueURLsWithExceptions(cfg.CORSJSON, "*")
|
|
|
|
cfg.CORS = uv.Values
|
2018-03-26 23:16:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.HostWhitelistJSON != "" {
|
|
|
|
uv := flags.NewUniqueStringsValue(cfg.HostWhitelistJSON)
|
|
|
|
cfg.HostWhitelist = uv.Values
|
|
|
|
}
|
|
|
|
|
2017-03-28 08:26:02 +03:00
|
|
|
// If a discovery flag is set, clear default initial cluster set by InitialClusterFromName
|
|
|
|
if (cfg.Durl != "" || cfg.DNSCluster != "") && cfg.InitialCluster == defaultInitialCluster {
|
2017-03-16 05:46:44 +03:00
|
|
|
cfg.InitialCluster = ""
|
|
|
|
}
|
2016-07-10 21:06:08 +03:00
|
|
|
if cfg.ClusterState == "" {
|
|
|
|
cfg.ClusterState = ClusterStateFlagNew
|
|
|
|
}
|
|
|
|
|
|
|
|
copySecurityDetails := func(tls *transport.TLSInfo, ysc *securityConfig) {
|
|
|
|
tls.CertFile = ysc.CertFile
|
|
|
|
tls.KeyFile = ysc.KeyFile
|
|
|
|
tls.ClientCertAuth = ysc.CertAuth
|
|
|
|
tls.TrustedCAFile = ysc.TrustedCAFile
|
|
|
|
}
|
|
|
|
copySecurityDetails(&cfg.ClientTLSInfo, &cfg.ClientSecurityJSON)
|
|
|
|
copySecurityDetails(&cfg.PeerTLSInfo, &cfg.PeerSecurityJSON)
|
|
|
|
cfg.ClientAutoTLS = cfg.ClientSecurityJSON.AutoTLS
|
|
|
|
cfg.PeerAutoTLS = cfg.PeerSecurityJSON.AutoTLS
|
|
|
|
|
|
|
|
return cfg.Validate()
|
|
|
|
}
|
|
|
|
|
2018-02-21 04:18:48 +03:00
|
|
|
// Validate ensures that '*embed.Config' fields are properly configured.
|
2016-07-10 21:06:08 +03:00
|
|
|
func (cfg *Config) Validate() error {
|
2018-04-15 08:52:39 +03:00
|
|
|
if err := cfg.setupLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-13 05:49:22 +03:00
|
|
|
if err := checkBindURLs(cfg.LPUrls); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := checkBindURLs(cfg.LCUrls); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-12 20:05:16 +03:00
|
|
|
if err := checkBindURLs(cfg.ListenMetricsUrls); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-09 23:35:09 +03:00
|
|
|
if err := checkHostURLs(cfg.APUrls); err != nil {
|
2018-04-17 23:27:55 +03:00
|
|
|
addrs := cfg.getAPURLs()
|
2017-10-28 12:27:31 +03:00
|
|
|
return fmt.Errorf(`--initial-advertise-peer-urls %q must be "host:port" (%v)`, strings.Join(addrs, ","), err)
|
2017-08-09 23:35:09 +03:00
|
|
|
}
|
|
|
|
if err := checkHostURLs(cfg.ACUrls); err != nil {
|
2018-04-17 23:27:55 +03:00
|
|
|
addrs := cfg.getACURLs()
|
2017-10-28 12:27:31 +03:00
|
|
|
return fmt.Errorf(`--advertise-client-urls %q must be "host:port" (%v)`, strings.Join(addrs, ","), err)
|
2017-08-09 23:35:09 +03:00
|
|
|
}
|
2016-07-10 21:06:08 +03:00
|
|
|
// Check if conflicting flags are passed.
|
|
|
|
nSet := 0
|
|
|
|
for _, v := range []bool{cfg.Durl != "", cfg.InitialCluster != "", cfg.DNSCluster != ""} {
|
|
|
|
if v {
|
|
|
|
nSet++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ClusterState != ClusterStateFlagNew && cfg.ClusterState != ClusterStateFlagExisting {
|
|
|
|
return fmt.Errorf("unexpected clusterState %q", cfg.ClusterState)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nSet > 1 {
|
|
|
|
return ErrConflictBootstrapFlags
|
|
|
|
}
|
|
|
|
|
2017-11-28 20:34:03 +03:00
|
|
|
if cfg.TickMs <= 0 {
|
|
|
|
return fmt.Errorf("--heartbeat-interval must be >0 (set to %dms)", cfg.TickMs)
|
|
|
|
}
|
|
|
|
if cfg.ElectionMs <= 0 {
|
|
|
|
return fmt.Errorf("--election-timeout must be >0 (set to %dms)", cfg.ElectionMs)
|
|
|
|
}
|
2016-07-10 21:06:08 +03:00
|
|
|
if 5*cfg.TickMs > cfg.ElectionMs {
|
|
|
|
return fmt.Errorf("--election-timeout[%vms] should be at least as 5 times as --heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
|
|
|
|
}
|
|
|
|
if cfg.ElectionMs > maxElectionMs {
|
|
|
|
return fmt.Errorf("--election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check this last since proxying in etcdmain may make this OK
|
|
|
|
if cfg.LCUrls != nil && cfg.ACUrls == nil {
|
|
|
|
return ErrUnsetAdvertiseClientURLsFlag
|
|
|
|
}
|
|
|
|
|
2018-02-21 04:18:48 +03:00
|
|
|
switch cfg.AutoCompactionMode {
|
|
|
|
case "":
|
|
|
|
case CompactorModeRevision, CompactorModePeriodic:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown auto-compaction-mode %q", cfg.AutoCompactionMode)
|
|
|
|
}
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.
|
|
|
|
func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error) {
|
2017-04-13 02:49:44 +03:00
|
|
|
token = cfg.InitialClusterToken
|
2016-07-10 21:06:08 +03:00
|
|
|
switch {
|
|
|
|
case cfg.Durl != "":
|
|
|
|
urlsmap = types.URLsMap{}
|
|
|
|
// If using discovery, generate a temporary cluster based on
|
|
|
|
// self's advertised peer URLs
|
|
|
|
urlsmap[cfg.Name] = cfg.APUrls
|
|
|
|
token = cfg.Durl
|
2018-04-27 00:22:33 +03:00
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
case cfg.DNSCluster != "":
|
2017-06-14 19:48:23 +03:00
|
|
|
clusterStrs, cerr := cfg.GetDNSClusterNames()
|
2018-04-15 08:52:39 +03:00
|
|
|
lg := cfg.logger
|
2017-05-05 06:15:17 +03:00
|
|
|
if cerr != nil {
|
2018-04-15 08:52:39 +03:00
|
|
|
if lg != nil {
|
2018-04-27 00:22:33 +03:00
|
|
|
lg.Warn("failed to resolve during SRV discovery", zap.Error(cerr))
|
2018-04-15 08:52:39 +03:00
|
|
|
} else {
|
|
|
|
plog.Errorf("couldn't resolve during SRV discovery (%v)", cerr)
|
|
|
|
}
|
2017-05-05 06:15:17 +03:00
|
|
|
return nil, "", cerr
|
|
|
|
}
|
|
|
|
for _, s := range clusterStrs {
|
2018-04-15 08:52:39 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info("got bootstrap from DNS for etcd-server", zap.String("node", s))
|
|
|
|
} else {
|
|
|
|
plog.Noticef("got bootstrap from DNS for etcd-server at %s", s)
|
|
|
|
}
|
2016-07-10 21:06:08 +03:00
|
|
|
}
|
2017-05-05 06:15:17 +03:00
|
|
|
clusterStr := strings.Join(clusterStrs, ",")
|
2018-03-21 01:15:22 +03:00
|
|
|
if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.TrustedCAFile == "" {
|
2017-11-15 21:14:21 +03:00
|
|
|
cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
|
2016-08-03 03:10:05 +03:00
|
|
|
}
|
2016-07-10 21:06:08 +03:00
|
|
|
urlsmap, err = types.NewURLsMap(clusterStr)
|
|
|
|
// only etcd member must belong to the discovered cluster.
|
|
|
|
// proxy does not need to belong to the discovered cluster.
|
|
|
|
if which == "etcd" {
|
|
|
|
if _, ok := urlsmap[cfg.Name]; !ok {
|
|
|
|
return nil, "", fmt.Errorf("cannot find local etcd member %q in SRV records", cfg.Name)
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 00:22:33 +03:00
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
default:
|
|
|
|
// We're statically configured, and cluster has appropriately been set.
|
|
|
|
urlsmap, err = types.NewURLsMap(cfg.InitialCluster)
|
|
|
|
}
|
|
|
|
return urlsmap, token, err
|
|
|
|
}
|
|
|
|
|
2017-06-14 19:48:23 +03:00
|
|
|
// GetDNSClusterNames uses DNS SRV records to get a list of initial nodes for cluster bootstrapping.
|
|
|
|
func (cfg *Config) GetDNSClusterNames() ([]string, error) {
|
|
|
|
var (
|
|
|
|
clusterStrs []string
|
|
|
|
cerr error
|
|
|
|
serviceNameSuffix string
|
|
|
|
)
|
|
|
|
if cfg.DNSClusterServiceName != "" {
|
|
|
|
serviceNameSuffix = "-" + cfg.DNSClusterServiceName
|
|
|
|
}
|
2018-04-27 00:22:33 +03:00
|
|
|
|
|
|
|
lg := cfg.GetLogger()
|
|
|
|
|
|
|
|
// Use both etcd-server-ssl and etcd-server for discovery.
|
|
|
|
// Combine the results if both are available.
|
2017-06-14 19:48:23 +03:00
|
|
|
clusterStrs, cerr = srv.GetCluster("https", "etcd-server-ssl"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.APUrls)
|
|
|
|
if cerr != nil {
|
|
|
|
clusterStrs = make([]string, 0)
|
|
|
|
}
|
2018-04-27 00:22:33 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"get cluster for etcd-server-ssl SRV",
|
|
|
|
zap.String("service-scheme", "https"),
|
|
|
|
zap.String("service-name", "etcd-server-ssl"+serviceNameSuffix),
|
|
|
|
zap.String("server-name", cfg.Name),
|
|
|
|
zap.String("discovery-srv", cfg.DNSCluster),
|
|
|
|
zap.Strings("advertise-peer-urls", cfg.getAPURLs()),
|
|
|
|
zap.Strings("found-cluster", clusterStrs),
|
|
|
|
zap.Error(cerr),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultHTTPClusterStrs, httpCerr := srv.GetCluster("http", "etcd-server"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.APUrls)
|
2017-06-14 19:48:23 +03:00
|
|
|
if httpCerr != nil {
|
|
|
|
clusterStrs = append(clusterStrs, defaultHTTPClusterStrs...)
|
|
|
|
}
|
2018-04-27 00:22:33 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"get cluster for etcd-server SRV",
|
|
|
|
zap.String("service-scheme", "http"),
|
|
|
|
zap.String("service-name", "etcd-server"+serviceNameSuffix),
|
|
|
|
zap.String("server-name", cfg.Name),
|
|
|
|
zap.String("discovery-srv", cfg.DNSCluster),
|
|
|
|
zap.Strings("advertise-peer-urls", cfg.getAPURLs()),
|
|
|
|
zap.Strings("found-cluster", clusterStrs),
|
|
|
|
zap.Error(httpCerr),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-06-14 19:48:23 +03:00
|
|
|
return clusterStrs, cerr
|
|
|
|
}
|
|
|
|
|
2016-07-10 21:06:08 +03:00
|
|
|
func (cfg Config) InitialClusterFromName(name string) (ret string) {
|
|
|
|
if len(cfg.APUrls) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
n := name
|
|
|
|
if name == "" {
|
|
|
|
n = DefaultName
|
|
|
|
}
|
|
|
|
for i := range cfg.APUrls {
|
|
|
|
ret = ret + "," + n + "=" + cfg.APUrls[i].String()
|
|
|
|
}
|
|
|
|
return ret[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg Config) IsNewCluster() bool { return cfg.ClusterState == ClusterStateFlagNew }
|
|
|
|
func (cfg Config) ElectionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) }
|
2016-08-13 05:55:55 +03:00
|
|
|
|
2017-03-04 00:32:46 +03:00
|
|
|
func (cfg Config) defaultPeerHost() bool {
|
|
|
|
return len(cfg.APUrls) == 1 && cfg.APUrls[0].String() == DefaultInitialAdvertisePeerURLs
|
2016-08-13 05:55:55 +03:00
|
|
|
}
|
2016-09-13 05:49:22 +03:00
|
|
|
|
2017-03-04 00:32:46 +03:00
|
|
|
func (cfg Config) defaultClientHost() bool {
|
|
|
|
return len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs
|
|
|
|
}
|
|
|
|
|
2017-07-18 00:34:59 +03:00
|
|
|
func (cfg *Config) ClientSelfCert() (err error) {
|
|
|
|
if cfg.ClientAutoTLS && cfg.ClientTLSInfo.Empty() {
|
|
|
|
chosts := make([]string, len(cfg.LCUrls))
|
|
|
|
for i, u := range cfg.LCUrls {
|
|
|
|
chosts[i] = u.Host
|
|
|
|
}
|
2018-04-15 08:52:39 +03:00
|
|
|
cfg.ClientTLSInfo, err = transport.SelfCert(cfg.logger, filepath.Join(cfg.Dir, "fixtures", "client"), chosts)
|
2017-07-18 00:34:59 +03:00
|
|
|
return err
|
|
|
|
} else if cfg.ClientAutoTLS {
|
2018-04-15 08:52:39 +03:00
|
|
|
if cfg.logger != nil {
|
|
|
|
cfg.logger.Warn("ignoring client auto TLS since certs given")
|
|
|
|
} else {
|
|
|
|
plog.Warningf("ignoring client auto TLS since certs given")
|
|
|
|
}
|
2017-07-18 00:34:59 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) PeerSelfCert() (err error) {
|
|
|
|
if cfg.PeerAutoTLS && cfg.PeerTLSInfo.Empty() {
|
|
|
|
phosts := make([]string, len(cfg.LPUrls))
|
|
|
|
for i, u := range cfg.LPUrls {
|
|
|
|
phosts[i] = u.Host
|
|
|
|
}
|
2018-04-15 08:52:39 +03:00
|
|
|
cfg.PeerTLSInfo, err = transport.SelfCert(cfg.logger, filepath.Join(cfg.Dir, "fixtures", "peer"), phosts)
|
2017-07-18 00:34:59 +03:00
|
|
|
return err
|
|
|
|
} else if cfg.PeerAutoTLS {
|
2018-04-15 08:52:39 +03:00
|
|
|
if cfg.logger != nil {
|
|
|
|
cfg.logger.Warn("ignoring peer auto TLS since certs given")
|
|
|
|
} else {
|
|
|
|
plog.Warningf("ignoring peer auto TLS since certs given")
|
|
|
|
}
|
2017-07-18 00:34:59 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-04 00:32:46 +03:00
|
|
|
// UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host,
|
|
|
|
// if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
|
|
|
|
// e.g. advertise peer URL localhost:2380 or listen peer URL 0.0.0.0:2380
|
|
|
|
// then the advertise peer host would be updated with machine's default host,
|
|
|
|
// while keeping the listen URL's port.
|
|
|
|
// User can work around this by explicitly setting URL with 127.0.0.1.
|
|
|
|
// It returns the default hostname, if used, and the error, if any, from getting the machine's default host.
|
2016-12-17 02:01:53 +03:00
|
|
|
// TODO: check whether fields are set instead of whether fields have default value
|
2017-03-04 00:32:46 +03:00
|
|
|
func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error) {
|
|
|
|
if defaultHostname == "" || defaultHostStatus != nil {
|
|
|
|
// update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
|
|
|
|
if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
|
2016-12-17 02:01:53 +03:00
|
|
|
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
|
|
|
|
}
|
2017-03-04 00:32:46 +03:00
|
|
|
return "", defaultHostStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
used := false
|
2017-04-18 22:08:59 +03:00
|
|
|
pip, pport := cfg.LPUrls[0].Hostname(), cfg.LPUrls[0].Port()
|
2017-03-04 00:32:46 +03:00
|
|
|
if cfg.defaultPeerHost() && pip == "0.0.0.0" {
|
|
|
|
cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, pport)}
|
|
|
|
used = true
|
|
|
|
}
|
|
|
|
// update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
|
|
|
|
if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
|
|
|
|
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
|
|
|
|
}
|
|
|
|
|
2017-04-18 22:08:59 +03:00
|
|
|
cip, cport := cfg.LCUrls[0].Hostname(), cfg.LCUrls[0].Port()
|
2017-03-04 00:32:46 +03:00
|
|
|
if cfg.defaultClientHost() && cip == "0.0.0.0" {
|
|
|
|
cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, cport)}
|
|
|
|
used = true
|
|
|
|
}
|
|
|
|
dhost := defaultHostname
|
|
|
|
if !used {
|
|
|
|
dhost = ""
|
2016-12-17 02:01:53 +03:00
|
|
|
}
|
2017-03-04 00:32:46 +03:00
|
|
|
return dhost, defaultHostStatus
|
2016-12-17 02:01:53 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 05:49:22 +03:00
|
|
|
// checkBindURLs returns an error if any URL uses a domain name.
|
|
|
|
func checkBindURLs(urls []url.URL) error {
|
|
|
|
for _, url := range urls {
|
|
|
|
if url.Scheme == "unix" || url.Scheme == "unixs" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
host, _, err := net.SplitHostPort(url.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if host == "localhost" {
|
|
|
|
// special case for local address
|
|
|
|
// TODO: support /etc/hosts ?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if net.ParseIP(host) == nil {
|
2017-01-26 06:23:10 +03:00
|
|
|
return fmt.Errorf("expected IP in URL for binding (%s)", url.String())
|
2016-09-13 05:49:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-09 23:35:09 +03:00
|
|
|
|
|
|
|
func checkHostURLs(urls []url.URL) error {
|
|
|
|
for _, url := range urls {
|
|
|
|
host, _, err := net.SplitHostPort(url.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if host == "" {
|
|
|
|
return fmt.Errorf("unexpected empty host (%s)", url.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-17 23:27:55 +03:00
|
|
|
|
|
|
|
func (cfg *Config) getAPURLs() (ss []string) {
|
|
|
|
ss = make([]string, len(cfg.APUrls))
|
|
|
|
for i := range cfg.APUrls {
|
|
|
|
ss[i] = cfg.APUrls[i].String()
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) getLPURLs() (ss []string) {
|
|
|
|
ss = make([]string, len(cfg.LPUrls))
|
|
|
|
for i := range cfg.LPUrls {
|
|
|
|
ss[i] = cfg.LPUrls[i].String()
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) getACURLs() (ss []string) {
|
|
|
|
ss = make([]string, len(cfg.ACUrls))
|
|
|
|
for i := range cfg.ACUrls {
|
|
|
|
ss[i] = cfg.ACUrls[i].String()
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) getLCURLs() (ss []string) {
|
|
|
|
ss = make([]string, len(cfg.LCUrls))
|
|
|
|
for i := range cfg.LCUrls {
|
|
|
|
ss[i] = cfg.LCUrls[i].String()
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) getMetricsURLs() (ss []string) {
|
|
|
|
ss = make([]string, len(cfg.ListenMetricsUrls))
|
|
|
|
for i := range cfg.ListenMetricsUrls {
|
|
|
|
ss[i] = cfg.ListenMetricsUrls[i].String()
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|