1// Copyright 2015 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 15// Every change should be reflected on help.go as well. 16 17package etcdmain 18 19import ( 20 "flag" 21 "fmt" 22 "io/ioutil" 23 "log" 24 "os" 25 "runtime" 26 27 "go.etcd.io/etcd/api/v3/version" 28 "go.etcd.io/etcd/client/pkg/v3/logutil" 29 "go.etcd.io/etcd/pkg/v3/flags" 30 cconfig "go.etcd.io/etcd/server/v3/config" 31 "go.etcd.io/etcd/server/v3/embed" 32 "go.etcd.io/etcd/server/v3/etcdserver/api/rafthttp" 33 34 "go.uber.org/zap" 35 "sigs.k8s.io/yaml" 36) 37 38var ( 39 proxyFlagOff = "off" 40 proxyFlagReadonly = "readonly" 41 proxyFlagOn = "on" 42 43 fallbackFlagExit = "exit" 44 fallbackFlagProxy = "proxy" 45 46 ignored = []string{ 47 "cluster-active-size", 48 "cluster-remove-delay", 49 "cluster-sync-interval", 50 "config", 51 "force", 52 "max-result-buffer", 53 "max-retry-attempts", 54 "peer-heartbeat-interval", 55 "peer-election-timeout", 56 "retry-interval", 57 "snapshot", 58 "v", 59 "vv", 60 // for coverage testing 61 "test.coverprofile", 62 "test.outputdir", 63 } 64) 65 66type configProxy struct { 67 ProxyFailureWaitMs uint `json:"proxy-failure-wait"` 68 ProxyRefreshIntervalMs uint `json:"proxy-refresh-interval"` 69 ProxyDialTimeoutMs uint `json:"proxy-dial-timeout"` 70 ProxyWriteTimeoutMs uint `json:"proxy-write-timeout"` 71 ProxyReadTimeoutMs uint `json:"proxy-read-timeout"` 72 Fallback string 73 Proxy string 74 ProxyJSON string `json:"proxy"` 75 FallbackJSON string `json:"discovery-fallback"` 76} 77 78// config holds the config for a command line invocation of etcd 79type config struct { 80 ec embed.Config 81 cp configProxy 82 cf configFlags 83 configFile string 84 printVersion bool 85 ignored []string 86} 87 88// configFlags has the set of flags used for command line parsing a Config 89type configFlags struct { 90 flagSet *flag.FlagSet 91 clusterState *flags.SelectiveStringValue 92 fallback *flags.SelectiveStringValue 93 proxy *flags.SelectiveStringValue 94 v2deprecation *flags.SelectiveStringsValue 95} 96 97func newConfig() *config { 98 cfg := &config{ 99 ec: *embed.NewConfig(), 100 cp: configProxy{ 101 Proxy: proxyFlagOff, 102 ProxyFailureWaitMs: 5000, 103 ProxyRefreshIntervalMs: 30000, 104 ProxyDialTimeoutMs: 1000, 105 ProxyWriteTimeoutMs: 5000, 106 }, 107 ignored: ignored, 108 } 109 cfg.cf = configFlags{ 110 flagSet: flag.NewFlagSet("etcd", flag.ContinueOnError), 111 clusterState: flags.NewSelectiveStringValue( 112 embed.ClusterStateFlagNew, 113 embed.ClusterStateFlagExisting, 114 ), 115 fallback: flags.NewSelectiveStringValue( 116 fallbackFlagProxy, 117 fallbackFlagExit, 118 ), 119 proxy: flags.NewSelectiveStringValue( 120 proxyFlagOff, 121 proxyFlagReadonly, 122 proxyFlagOn, 123 ), 124 v2deprecation: flags.NewSelectiveStringsValue( 125 string(cconfig.V2_DEPR_0_NOT_YET), 126 string(cconfig.V2_DEPR_1_WRITE_ONLY), 127 string(cconfig.V2_DEPR_1_WRITE_ONLY_DROP), 128 string(cconfig.V2_DEPR_2_GONE)), 129 } 130 131 fs := cfg.cf.flagSet 132 fs.Usage = func() { 133 fmt.Fprintln(os.Stderr, usageline) 134 } 135 136 fs.StringVar(&cfg.configFile, "config-file", "", "Path to the server configuration file. Note that if a configuration file is provided, other command line flags and environment variables will be ignored.") 137 138 // member 139 fs.StringVar(&cfg.ec.Dir, "data-dir", cfg.ec.Dir, "Path to the data directory.") 140 fs.StringVar(&cfg.ec.WalDir, "wal-dir", cfg.ec.WalDir, "Path to the dedicated wal directory.") 141 fs.Var( 142 flags.NewUniqueURLsWithExceptions(embed.DefaultListenPeerURLs, ""), 143 "listen-peer-urls", 144 "List of URLs to listen on for peer traffic.", 145 ) 146 fs.Var( 147 flags.NewUniqueURLsWithExceptions(embed.DefaultListenClientURLs, ""), "listen-client-urls", 148 "List of URLs to listen on for client traffic.", 149 ) 150 fs.Var( 151 flags.NewUniqueURLsWithExceptions("", ""), 152 "listen-metrics-urls", 153 "List of URLs to listen on for the metrics and health endpoints.", 154 ) 155 fs.UintVar(&cfg.ec.MaxSnapFiles, "max-snapshots", cfg.ec.MaxSnapFiles, "Maximum number of snapshot files to retain (0 is unlimited).") 156 fs.UintVar(&cfg.ec.MaxWalFiles, "max-wals", cfg.ec.MaxWalFiles, "Maximum number of wal files to retain (0 is unlimited).") 157 fs.StringVar(&cfg.ec.Name, "name", cfg.ec.Name, "Human-readable name for this member.") 158 fs.Uint64Var(&cfg.ec.SnapshotCount, "snapshot-count", cfg.ec.SnapshotCount, "Number of committed transactions to trigger a snapshot to disk.") 159 fs.UintVar(&cfg.ec.TickMs, "heartbeat-interval", cfg.ec.TickMs, "Time (in milliseconds) of a heartbeat interval.") 160 fs.UintVar(&cfg.ec.ElectionMs, "election-timeout", cfg.ec.ElectionMs, "Time (in milliseconds) for an election to timeout.") 161 fs.BoolVar(&cfg.ec.InitialElectionTickAdvance, "initial-election-tick-advance", cfg.ec.InitialElectionTickAdvance, "Whether to fast-forward initial election ticks on boot for faster election.") 162 fs.Int64Var(&cfg.ec.QuotaBackendBytes, "quota-backend-bytes", cfg.ec.QuotaBackendBytes, "Raise alarms when backend size exceeds the given quota. 0 means use the default quota.") 163 fs.StringVar(&cfg.ec.BackendFreelistType, "backend-bbolt-freelist-type", cfg.ec.BackendFreelistType, "BackendFreelistType specifies the type of freelist that boltdb backend uses(array and map are supported types)") 164 fs.DurationVar(&cfg.ec.BackendBatchInterval, "backend-batch-interval", cfg.ec.BackendBatchInterval, "BackendBatchInterval is the maximum time before commit the backend transaction.") 165 fs.IntVar(&cfg.ec.BackendBatchLimit, "backend-batch-limit", cfg.ec.BackendBatchLimit, "BackendBatchLimit is the maximum operations before commit the backend transaction.") 166 fs.UintVar(&cfg.ec.MaxTxnOps, "max-txn-ops", cfg.ec.MaxTxnOps, "Maximum number of operations permitted in a transaction.") 167 fs.UintVar(&cfg.ec.MaxRequestBytes, "max-request-bytes", cfg.ec.MaxRequestBytes, "Maximum client request size in bytes the server will accept.") 168 fs.DurationVar(&cfg.ec.GRPCKeepAliveMinTime, "grpc-keepalive-min-time", cfg.ec.GRPCKeepAliveMinTime, "Minimum interval duration that a client should wait before pinging server.") 169 fs.DurationVar(&cfg.ec.GRPCKeepAliveInterval, "grpc-keepalive-interval", cfg.ec.GRPCKeepAliveInterval, "Frequency duration of server-to-client ping to check if a connection is alive (0 to disable).") 170 fs.DurationVar(&cfg.ec.GRPCKeepAliveTimeout, "grpc-keepalive-timeout", cfg.ec.GRPCKeepAliveTimeout, "Additional duration of wait before closing a non-responsive connection (0 to disable).") 171 fs.BoolVar(&cfg.ec.SocketOpts.ReusePort, "socket-reuse-port", cfg.ec.SocketOpts.ReusePort, "Enable to set socket option SO_REUSEPORT on listeners allowing rebinding of a port already in use.") 172 fs.BoolVar(&cfg.ec.SocketOpts.ReuseAddress, "socket-reuse-address", cfg.ec.SocketOpts.ReuseAddress, "Enable to set socket option SO_REUSEADDR on listeners allowing binding to an address in `TIME_WAIT` state.") 173 174 // raft connection timeouts 175 fs.DurationVar(&rafthttp.ConnReadTimeout, "raft-read-timeout", rafthttp.DefaultConnReadTimeout, "Read timeout set on each rafthttp connection") 176 fs.DurationVar(&rafthttp.ConnWriteTimeout, "raft-write-timeout", rafthttp.DefaultConnWriteTimeout, "Write timeout set on each rafthttp connection") 177 178 // clustering 179 fs.Var( 180 flags.NewUniqueURLsWithExceptions(embed.DefaultInitialAdvertisePeerURLs, ""), 181 "initial-advertise-peer-urls", 182 "List of this member's peer URLs to advertise to the rest of the cluster.", 183 ) 184 fs.Var( 185 flags.NewUniqueURLsWithExceptions(embed.DefaultAdvertiseClientURLs, ""), 186 "advertise-client-urls", 187 "List of this member's client URLs to advertise to the public.", 188 ) 189 fs.StringVar(&cfg.ec.Durl, "discovery", cfg.ec.Durl, "Discovery URL used to bootstrap the cluster.") 190 fs.Var(cfg.cf.fallback, "discovery-fallback", fmt.Sprintf("Valid values include %q", cfg.cf.fallback.Valids())) 191 192 fs.StringVar(&cfg.ec.Dproxy, "discovery-proxy", cfg.ec.Dproxy, "HTTP proxy to use for traffic to discovery service.") 193 fs.StringVar(&cfg.ec.DNSCluster, "discovery-srv", cfg.ec.DNSCluster, "DNS domain used to bootstrap initial cluster.") 194 fs.StringVar(&cfg.ec.DNSClusterServiceName, "discovery-srv-name", cfg.ec.DNSClusterServiceName, "Service name to query when using DNS discovery.") 195 fs.StringVar(&cfg.ec.InitialCluster, "initial-cluster", cfg.ec.InitialCluster, "Initial cluster configuration for bootstrapping.") 196 fs.StringVar(&cfg.ec.InitialClusterToken, "initial-cluster-token", cfg.ec.InitialClusterToken, "Initial cluster token for the etcd cluster during bootstrap.") 197 fs.Var(cfg.cf.clusterState, "initial-cluster-state", "Initial cluster state ('new' or 'existing').") 198 199 fs.BoolVar(&cfg.ec.StrictReconfigCheck, "strict-reconfig-check", cfg.ec.StrictReconfigCheck, "Reject reconfiguration requests that would cause quorum loss.") 200 201 fs.BoolVar(&cfg.ec.PreVote, "pre-vote", cfg.ec.PreVote, "Enable to run an additional Raft election phase.") 202 203 fs.BoolVar(&cfg.ec.EnableV2, "enable-v2", cfg.ec.EnableV2, "Accept etcd V2 client requests. Deprecated in v3.5. Will be decommission in v3.6.") 204 fs.StringVar(&cfg.ec.ExperimentalEnableV2V3, "experimental-enable-v2v3", cfg.ec.ExperimentalEnableV2V3, "v3 prefix for serving emulated v2 state. Deprecated in 3.5. Will be decomissioned in 3.6.") 205 fs.Var(cfg.cf.v2deprecation, "v2-deprecation", fmt.Sprintf("v2store deprecation stage: %q. ", cfg.cf.proxy.Valids())) 206 207 // proxy 208 fs.Var(cfg.cf.proxy, "proxy", fmt.Sprintf("Valid values include %q", cfg.cf.proxy.Valids())) 209 fs.UintVar(&cfg.cp.ProxyFailureWaitMs, "proxy-failure-wait", cfg.cp.ProxyFailureWaitMs, "Time (in milliseconds) an endpoint will be held in a failed state.") 210 fs.UintVar(&cfg.cp.ProxyRefreshIntervalMs, "proxy-refresh-interval", cfg.cp.ProxyRefreshIntervalMs, "Time (in milliseconds) of the endpoints refresh interval.") 211 fs.UintVar(&cfg.cp.ProxyDialTimeoutMs, "proxy-dial-timeout", cfg.cp.ProxyDialTimeoutMs, "Time (in milliseconds) for a dial to timeout.") 212 fs.UintVar(&cfg.cp.ProxyWriteTimeoutMs, "proxy-write-timeout", cfg.cp.ProxyWriteTimeoutMs, "Time (in milliseconds) for a write to timeout.") 213 fs.UintVar(&cfg.cp.ProxyReadTimeoutMs, "proxy-read-timeout", cfg.cp.ProxyReadTimeoutMs, "Time (in milliseconds) for a read to timeout.") 214 215 // security 216 fs.StringVar(&cfg.ec.ClientTLSInfo.CertFile, "cert-file", "", "Path to the client server TLS cert file.") 217 fs.StringVar(&cfg.ec.ClientTLSInfo.KeyFile, "key-file", "", "Path to the client server TLS key file.") 218 fs.StringVar(&cfg.ec.ClientTLSInfo.ClientCertFile, "client-cert-file", "", "Path to an explicit peer client TLS cert file otherwise cert file will be used when client auth is required.") 219 fs.StringVar(&cfg.ec.ClientTLSInfo.ClientKeyFile, "client-key-file", "", "Path to an explicit peer client TLS key file otherwise key file will be used when client auth is required.") 220 fs.BoolVar(&cfg.ec.ClientTLSInfo.ClientCertAuth, "client-cert-auth", false, "Enable client cert authentication.") 221 fs.StringVar(&cfg.ec.ClientTLSInfo.CRLFile, "client-crl-file", "", "Path to the client certificate revocation list file.") 222 fs.StringVar(&cfg.ec.ClientTLSInfo.AllowedHostname, "client-cert-allowed-hostname", "", "Allowed TLS hostname for client cert authentication.") 223 fs.StringVar(&cfg.ec.ClientTLSInfo.TrustedCAFile, "trusted-ca-file", "", "Path to the client server TLS trusted CA cert file.") 224 fs.BoolVar(&cfg.ec.ClientAutoTLS, "auto-tls", false, "Client TLS using generated certificates") 225 fs.StringVar(&cfg.ec.PeerTLSInfo.CertFile, "peer-cert-file", "", "Path to the peer server TLS cert file.") 226 fs.StringVar(&cfg.ec.PeerTLSInfo.KeyFile, "peer-key-file", "", "Path to the peer server TLS key file.") 227 fs.StringVar(&cfg.ec.PeerTLSInfo.ClientCertFile, "peer-client-cert-file", "", "Path to an explicit peer client TLS cert file otherwise peer cert file will be used when client auth is required.") 228 fs.StringVar(&cfg.ec.PeerTLSInfo.ClientKeyFile, "peer-client-key-file", "", "Path to an explicit peer client TLS key file otherwise peer key file will be used when client auth is required.") 229 fs.BoolVar(&cfg.ec.PeerTLSInfo.ClientCertAuth, "peer-client-cert-auth", false, "Enable peer client cert authentication.") 230 fs.StringVar(&cfg.ec.PeerTLSInfo.TrustedCAFile, "peer-trusted-ca-file", "", "Path to the peer server TLS trusted CA file.") 231 fs.BoolVar(&cfg.ec.PeerAutoTLS, "peer-auto-tls", false, "Peer TLS using generated certificates") 232 fs.UintVar(&cfg.ec.SelfSignedCertValidity, "self-signed-cert-validity", 1, "The validity period of the client and peer certificates, unit is year") 233 fs.StringVar(&cfg.ec.PeerTLSInfo.CRLFile, "peer-crl-file", "", "Path to the peer certificate revocation list file.") 234 fs.StringVar(&cfg.ec.PeerTLSInfo.AllowedCN, "peer-cert-allowed-cn", "", "Allowed CN for inter peer authentication.") 235 fs.StringVar(&cfg.ec.PeerTLSInfo.AllowedHostname, "peer-cert-allowed-hostname", "", "Allowed TLS hostname for inter peer authentication.") 236 fs.Var(flags.NewStringsValue(""), "cipher-suites", "Comma-separated list of supported TLS cipher suites between client/server and peers (empty will be auto-populated by Go).") 237 fs.BoolVar(&cfg.ec.PeerTLSInfo.SkipClientSANVerify, "experimental-peer-skip-client-san-verification", false, "Skip verification of SAN field in client certificate for peer connections.") 238 239 fs.Var( 240 flags.NewUniqueURLsWithExceptions("*", "*"), 241 "cors", 242 "Comma-separated white list of origins for CORS, or cross-origin resource sharing, (empty or * means allow all)", 243 ) 244 fs.Var(flags.NewUniqueStringsValue("*"), "host-whitelist", "Comma-separated acceptable hostnames from HTTP client requests, if server is not secure (empty means allow all).") 245 246 // logging 247 fs.StringVar(&cfg.ec.Logger, "logger", "zap", "Currently only supports 'zap' for structured logging.") 248 fs.Var(flags.NewUniqueStringsValue(embed.DefaultLogOutput), "log-outputs", "Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd, or list of comma separated output targets.") 249 fs.StringVar(&cfg.ec.LogLevel, "log-level", logutil.DefaultLogLevel, "Configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'.") 250 fs.BoolVar(&cfg.ec.EnableLogRotation, "enable-log-rotation", false, "Enable log rotation of a single log-outputs file target.") 251 fs.StringVar(&cfg.ec.LogRotationConfigJSON, "log-rotation-config-json", embed.DefaultLogRotationConfig, "Configures log rotation if enabled with a JSON logger config. Default: MaxSize=100(MB), MaxAge=0(days,no limit), MaxBackups=0(no limit), LocalTime=false(UTC), Compress=false(gzip)") 252 253 // version 254 fs.BoolVar(&cfg.printVersion, "version", false, "Print the version and exit.") 255 256 fs.StringVar(&cfg.ec.AutoCompactionRetention, "auto-compaction-retention", "0", "Auto compaction retention for mvcc key value store. 0 means disable auto compaction.") 257 fs.StringVar(&cfg.ec.AutoCompactionMode, "auto-compaction-mode", "periodic", "interpret 'auto-compaction-retention' one of: periodic|revision. 'periodic' for duration based retention, defaulting to hours if no time unit is provided (e.g. '5m'). 'revision' for revision number based retention.") 258 259 // pprof profiler via HTTP 260 fs.BoolVar(&cfg.ec.EnablePprof, "enable-pprof", false, "Enable runtime profiling data via HTTP server. Address is at client URL + \"/debug/pprof/\"") 261 262 // additional metrics 263 fs.StringVar(&cfg.ec.Metrics, "metrics", cfg.ec.Metrics, "Set level of detail for exported metrics, specify 'extensive' to include server side grpc histogram metrics") 264 265 // experimental distributed tracing 266 fs.BoolVar(&cfg.ec.ExperimentalEnableDistributedTracing, "experimental-enable-distributed-tracing", false, "Enable experimental distributed tracing using OpenTelemetry Tracing.") 267 fs.StringVar(&cfg.ec.ExperimentalDistributedTracingAddress, "experimental-distributed-tracing-address", embed.ExperimentalDistributedTracingAddress, "Address for distributed tracing used for OpenTelemetry Tracing (if enabled with experimental-enable-distributed-tracing flag).") 268 fs.StringVar(&cfg.ec.ExperimentalDistributedTracingServiceName, "experimental-distributed-tracing-service-name", embed.ExperimentalDistributedTracingServiceName, "Configures service name for distributed tracing to be used to define service name for OpenTelemetry Tracing (if enabled with experimental-enable-distributed-tracing flag). 'etcd' is the default service name. Use the same service name for all instances of etcd.") 269 fs.StringVar(&cfg.ec.ExperimentalDistributedTracingServiceInstanceID, "experimental-distributed-tracing-instance-id", "", "Configures service instance ID for distributed tracing to be used to define service instance ID key for OpenTelemetry Tracing (if enabled with experimental-enable-distributed-tracing flag). There is no default value set. This ID must be unique per etcd instance.") 270 271 // auth 272 fs.StringVar(&cfg.ec.AuthToken, "auth-token", cfg.ec.AuthToken, "Specify auth token specific options.") 273 fs.UintVar(&cfg.ec.BcryptCost, "bcrypt-cost", cfg.ec.BcryptCost, "Specify bcrypt algorithm cost factor for auth password hashing.") 274 fs.UintVar(&cfg.ec.AuthTokenTTL, "auth-token-ttl", cfg.ec.AuthTokenTTL, "The lifetime in seconds of the auth token.") 275 276 // gateway 277 fs.BoolVar(&cfg.ec.EnableGRPCGateway, "enable-grpc-gateway", cfg.ec.EnableGRPCGateway, "Enable GRPC gateway.") 278 279 // experimental 280 fs.BoolVar(&cfg.ec.ExperimentalInitialCorruptCheck, "experimental-initial-corrupt-check", cfg.ec.ExperimentalInitialCorruptCheck, "Enable to check data corruption before serving any client/peer traffic.") 281 fs.DurationVar(&cfg.ec.ExperimentalCorruptCheckTime, "experimental-corrupt-check-time", cfg.ec.ExperimentalCorruptCheckTime, "Duration of time between cluster corruption check passes.") 282 283 fs.BoolVar(&cfg.ec.ExperimentalEnableLeaseCheckpoint, "experimental-enable-lease-checkpoint", false, "Enable to persist lease remaining TTL to prevent indefinite auto-renewal of long lived leases.") 284 fs.IntVar(&cfg.ec.ExperimentalCompactionBatchLimit, "experimental-compaction-batch-limit", cfg.ec.ExperimentalCompactionBatchLimit, "Sets the maximum revisions deleted in each compaction batch.") 285 fs.DurationVar(&cfg.ec.ExperimentalWatchProgressNotifyInterval, "experimental-watch-progress-notify-interval", cfg.ec.ExperimentalWatchProgressNotifyInterval, "Duration of periodic watch progress notifications.") 286 fs.DurationVar(&cfg.ec.ExperimentalDowngradeCheckTime, "experimental-downgrade-check-time", cfg.ec.ExperimentalDowngradeCheckTime, "Duration of time between two downgrade status check.") 287 fs.DurationVar(&cfg.ec.ExperimentalWarningApplyDuration, "experimental-warning-apply-duration", cfg.ec.ExperimentalWarningApplyDuration, "Time duration after which a warning is generated if request takes more time.") 288 fs.BoolVar(&cfg.ec.ExperimentalMemoryMlock, "experimental-memory-mlock", cfg.ec.ExperimentalMemoryMlock, "Enable to enforce etcd pages (in particular bbolt) to stay in RAM.") 289 fs.BoolVar(&cfg.ec.ExperimentalTxnModeWriteWithSharedBuffer, "experimental-txn-mode-write-with-shared-buffer", true, "Enable the write transaction to use a shared buffer in its readonly check operations.") 290 fs.UintVar(&cfg.ec.ExperimentalBootstrapDefragThresholdMegabytes, "experimental-bootstrap-defrag-threshold-megabytes", 0, "Enable the defrag during etcd server bootstrap on condition that it will free at least the provided threshold of disk space. Needs to be set to non-zero value to take effect.") 291 292 // unsafe 293 fs.BoolVar(&cfg.ec.UnsafeNoFsync, "unsafe-no-fsync", false, "Disables fsync, unsafe, will cause data loss.") 294 fs.BoolVar(&cfg.ec.ForceNewCluster, "force-new-cluster", false, "Force to create a new one member cluster.") 295 296 // ignored 297 for _, f := range cfg.ignored { 298 fs.Var(&flags.IgnoredFlag{Name: f}, f, "") 299 } 300 return cfg 301} 302 303func (cfg *config) parse(arguments []string) error { 304 perr := cfg.cf.flagSet.Parse(arguments) 305 switch perr { 306 case nil: 307 case flag.ErrHelp: 308 fmt.Println(flagsline) 309 os.Exit(0) 310 default: 311 os.Exit(2) 312 } 313 if len(cfg.cf.flagSet.Args()) != 0 { 314 return fmt.Errorf("'%s' is not a valid flag", cfg.cf.flagSet.Arg(0)) 315 } 316 317 if cfg.printVersion { 318 fmt.Printf("etcd Version: %s\n", version.Version) 319 fmt.Printf("Git SHA: %s\n", version.GitSHA) 320 fmt.Printf("Go Version: %s\n", runtime.Version()) 321 fmt.Printf("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH) 322 os.Exit(0) 323 } 324 325 var err error 326 327 // This env variable must be parsed separately 328 // because we need to determine whether to use or 329 // ignore the env variables based on if the config file is set. 330 if cfg.configFile == "" { 331 cfg.configFile = os.Getenv(flags.FlagToEnv("ETCD", "config-file")) 332 } 333 334 if cfg.configFile != "" { 335 err = cfg.configFromFile(cfg.configFile) 336 if lg := cfg.ec.GetLogger(); lg != nil { 337 lg.Info( 338 "loaded server configuration, other configuration command line flags and environment variables will be ignored if provided", 339 zap.String("path", cfg.configFile), 340 ) 341 } 342 } else { 343 err = cfg.configFromCmdLine() 344 } 345 346 if cfg.ec.V2Deprecation == "" { 347 cfg.ec.V2Deprecation = cconfig.V2_DEPR_DEFAULT 348 } 349 350 // now logger is set up 351 return err 352} 353 354func (cfg *config) configFromCmdLine() error { 355 // user-specified logger is not setup yet, use this logger during flag parsing 356 lg, err := zap.NewProduction() 357 if err != nil { 358 return err 359 } 360 361 verKey := "ETCD_VERSION" 362 if verVal := os.Getenv(verKey); verVal != "" { 363 // unset to avoid any possible side-effect. 364 os.Unsetenv(verKey) 365 366 lg.Warn( 367 "cannot set special environment variable", 368 zap.String("key", verKey), 369 zap.String("value", verVal), 370 ) 371 } 372 373 err = flags.SetFlagsFromEnv(lg, "ETCD", cfg.cf.flagSet) 374 if err != nil { 375 return err 376 } 377 378 if rafthttp.ConnReadTimeout < rafthttp.DefaultConnReadTimeout { 379 rafthttp.ConnReadTimeout = rafthttp.DefaultConnReadTimeout 380 lg.Info(fmt.Sprintf("raft-read-timeout increased to minimum value: %v", rafthttp.DefaultConnReadTimeout)) 381 } 382 if rafthttp.ConnWriteTimeout < rafthttp.DefaultConnWriteTimeout { 383 rafthttp.ConnWriteTimeout = rafthttp.DefaultConnWriteTimeout 384 lg.Info(fmt.Sprintf("raft-write-timeout increased to minimum value: %v", rafthttp.DefaultConnWriteTimeout)) 385 } 386 387 cfg.ec.LPUrls = flags.UniqueURLsFromFlag(cfg.cf.flagSet, "listen-peer-urls") 388 cfg.ec.APUrls = flags.UniqueURLsFromFlag(cfg.cf.flagSet, "initial-advertise-peer-urls") 389 cfg.ec.LCUrls = flags.UniqueURLsFromFlag(cfg.cf.flagSet, "listen-client-urls") 390 cfg.ec.ACUrls = flags.UniqueURLsFromFlag(cfg.cf.flagSet, "advertise-client-urls") 391 cfg.ec.ListenMetricsUrls = flags.UniqueURLsFromFlag(cfg.cf.flagSet, "listen-metrics-urls") 392 393 cfg.ec.CORS = flags.UniqueURLsMapFromFlag(cfg.cf.flagSet, "cors") 394 cfg.ec.HostWhitelist = flags.UniqueStringsMapFromFlag(cfg.cf.flagSet, "host-whitelist") 395 396 cfg.ec.CipherSuites = flags.StringsFromFlag(cfg.cf.flagSet, "cipher-suites") 397 398 cfg.ec.LogOutputs = flags.UniqueStringsFromFlag(cfg.cf.flagSet, "log-outputs") 399 400 cfg.ec.ClusterState = cfg.cf.clusterState.String() 401 cfg.cp.Fallback = cfg.cf.fallback.String() 402 cfg.cp.Proxy = cfg.cf.proxy.String() 403 404 cfg.ec.V2Deprecation = cconfig.V2DeprecationEnum(cfg.cf.v2deprecation.String()) 405 406 // disable default advertise-client-urls if lcurls is set 407 missingAC := flags.IsSet(cfg.cf.flagSet, "listen-client-urls") && !flags.IsSet(cfg.cf.flagSet, "advertise-client-urls") 408 if !cfg.mayBeProxy() && missingAC { 409 cfg.ec.ACUrls = nil 410 } 411 412 // disable default initial-cluster if discovery is set 413 if (cfg.ec.Durl != "" || cfg.ec.DNSCluster != "" || cfg.ec.DNSClusterServiceName != "") && !flags.IsSet(cfg.cf.flagSet, "initial-cluster") { 414 cfg.ec.InitialCluster = "" 415 } 416 417 return cfg.validate() 418} 419 420func (cfg *config) configFromFile(path string) error { 421 eCfg, err := embed.ConfigFromFile(path) 422 if err != nil { 423 return err 424 } 425 cfg.ec = *eCfg 426 427 // load extra config information 428 b, rerr := ioutil.ReadFile(path) 429 if rerr != nil { 430 return rerr 431 } 432 if yerr := yaml.Unmarshal(b, &cfg.cp); yerr != nil { 433 return yerr 434 } 435 436 if cfg.cp.FallbackJSON != "" { 437 if err := cfg.cf.fallback.Set(cfg.cp.FallbackJSON); err != nil { 438 log.Fatalf("unexpected error setting up discovery-fallback flag: %v", err) 439 } 440 cfg.cp.Fallback = cfg.cf.fallback.String() 441 } 442 443 if cfg.cp.ProxyJSON != "" { 444 if err := cfg.cf.proxy.Set(cfg.cp.ProxyJSON); err != nil { 445 log.Fatalf("unexpected error setting up proxyFlag: %v", err) 446 } 447 cfg.cp.Proxy = cfg.cf.proxy.String() 448 } 449 return nil 450} 451 452func (cfg *config) mayBeProxy() bool { 453 mayFallbackToProxy := cfg.ec.Durl != "" && cfg.cp.Fallback == fallbackFlagProxy 454 return cfg.cp.Proxy != proxyFlagOff || mayFallbackToProxy 455} 456 457func (cfg *config) validate() error { 458 err := cfg.ec.Validate() 459 // TODO(yichengq): check this for joining through discovery service case 460 if err == embed.ErrUnsetAdvertiseClientURLsFlag && cfg.mayBeProxy() { 461 return nil 462 } 463 return err 464} 465 466func (cfg config) isProxy() bool { return cfg.cf.proxy.String() != proxyFlagOff } 467func (cfg config) isReadonlyProxy() bool { return cfg.cf.proxy.String() == proxyFlagReadonly } 468func (cfg config) shouldFallbackToProxy() bool { return cfg.cf.fallback.String() == fallbackFlagProxy } 469