blob: 2a5faa746c9a5477c1da8b44aa6eb87f3d3ba8df [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// 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 "net/url"
24 "os"
25 "runtime"
26 "strings"
27
28 "github.com/coreos/etcd/embed"
29 "github.com/coreos/etcd/pkg/flags"
30 "github.com/coreos/etcd/pkg/types"
31 "github.com/coreos/etcd/version"
32
33 "github.com/ghodss/yaml"
34)
35
36var (
37 proxyFlagOff = "off"
38 proxyFlagReadonly = "readonly"
39 proxyFlagOn = "on"
40
41 fallbackFlagExit = "exit"
42 fallbackFlagProxy = "proxy"
43
44 ignored = []string{
45 "cluster-active-size",
46 "cluster-remove-delay",
47 "cluster-sync-interval",
48 "config",
49 "force",
50 "max-result-buffer",
51 "max-retry-attempts",
52 "peer-heartbeat-interval",
53 "peer-election-timeout",
54 "retry-interval",
55 "snapshot",
56 "v",
57 "vv",
58 // for coverage testing
59 "test.coverprofile",
60 "test.outputdir",
61 }
62)
63
64type configProxy struct {
65 ProxyFailureWaitMs uint `json:"proxy-failure-wait"`
66 ProxyRefreshIntervalMs uint `json:"proxy-refresh-interval"`
67 ProxyDialTimeoutMs uint `json:"proxy-dial-timeout"`
68 ProxyWriteTimeoutMs uint `json:"proxy-write-timeout"`
69 ProxyReadTimeoutMs uint `json:"proxy-read-timeout"`
70 Fallback string
71 Proxy string
72 ProxyJSON string `json:"proxy"`
73 FallbackJSON string `json:"discovery-fallback"`
74}
75
76// config holds the config for a command line invocation of etcd
77type config struct {
78 ec embed.Config
79 cp configProxy
80 cf configFlags
81 configFile string
82 printVersion bool
83 ignored []string
84}
85
86// configFlags has the set of flags used for command line parsing a Config
87type configFlags struct {
88 flagSet *flag.FlagSet
89 clusterState *flags.StringsFlag
90 fallback *flags.StringsFlag
91 proxy *flags.StringsFlag
92}
93
94func newConfig() *config {
95 cfg := &config{
96 ec: *embed.NewConfig(),
97 cp: configProxy{
98 Proxy: proxyFlagOff,
99 ProxyFailureWaitMs: 5000,
100 ProxyRefreshIntervalMs: 30000,
101 ProxyDialTimeoutMs: 1000,
102 ProxyWriteTimeoutMs: 5000,
103 },
104 ignored: ignored,
105 }
106 cfg.cf = configFlags{
107 flagSet: flag.NewFlagSet("etcd", flag.ContinueOnError),
108 clusterState: flags.NewStringsFlag(
109 embed.ClusterStateFlagNew,
110 embed.ClusterStateFlagExisting,
111 ),
112 fallback: flags.NewStringsFlag(
113 fallbackFlagProxy,
114 fallbackFlagExit,
115 ),
116 proxy: flags.NewStringsFlag(
117 proxyFlagOff,
118 proxyFlagReadonly,
119 proxyFlagOn,
120 ),
121 }
122
123 fs := cfg.cf.flagSet
124 fs.Usage = func() {
125 fmt.Fprintln(os.Stderr, usageline)
126 }
127
128 fs.StringVar(&cfg.configFile, "config-file", "", "Path to the server configuration file")
129
130 // member
131 fs.Var(cfg.ec.CorsInfo, "cors", "Comma-separated white list of origins for CORS (cross-origin resource sharing).")
132 fs.StringVar(&cfg.ec.Dir, "data-dir", cfg.ec.Dir, "Path to the data directory.")
133 fs.StringVar(&cfg.ec.WalDir, "wal-dir", cfg.ec.WalDir, "Path to the dedicated wal directory.")
134 fs.Var(flags.NewURLsValue(embed.DefaultListenPeerURLs), "listen-peer-urls", "List of URLs to listen on for peer traffic.")
135 fs.Var(flags.NewURLsValue(embed.DefaultListenClientURLs), "listen-client-urls", "List of URLs to listen on for client traffic.")
136 fs.StringVar(&cfg.ec.ListenMetricsUrlsJSON, "listen-metrics-urls", "", "List of URLs to listen on for metrics.")
137 fs.UintVar(&cfg.ec.MaxSnapFiles, "max-snapshots", cfg.ec.MaxSnapFiles, "Maximum number of snapshot files to retain (0 is unlimited).")
138 fs.UintVar(&cfg.ec.MaxWalFiles, "max-wals", cfg.ec.MaxWalFiles, "Maximum number of wal files to retain (0 is unlimited).")
139 fs.StringVar(&cfg.ec.Name, "name", cfg.ec.Name, "Human-readable name for this member.")
140 fs.Uint64Var(&cfg.ec.SnapCount, "snapshot-count", cfg.ec.SnapCount, "Number of committed transactions to trigger a snapshot to disk.")
141 fs.UintVar(&cfg.ec.TickMs, "heartbeat-interval", cfg.ec.TickMs, "Time (in milliseconds) of a heartbeat interval.")
142 fs.UintVar(&cfg.ec.ElectionMs, "election-timeout", cfg.ec.ElectionMs, "Time (in milliseconds) for an election to timeout.")
143 fs.BoolVar(&cfg.ec.InitialElectionTickAdvance, "initial-election-tick-advance", cfg.ec.InitialElectionTickAdvance, "Whether to fast-forward initial election ticks on boot for faster election.")
144 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.")
145 fs.UintVar(&cfg.ec.MaxTxnOps, "max-txn-ops", cfg.ec.MaxTxnOps, "Maximum number of operations permitted in a transaction.")
146 fs.UintVar(&cfg.ec.MaxRequestBytes, "max-request-bytes", cfg.ec.MaxRequestBytes, "Maximum client request size in bytes the server will accept.")
147 fs.DurationVar(&cfg.ec.GRPCKeepAliveMinTime, "grpc-keepalive-min-time", cfg.ec.GRPCKeepAliveMinTime, "Minimum interval duration that a client should wait before pinging server.")
148 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).")
149 fs.DurationVar(&cfg.ec.GRPCKeepAliveTimeout, "grpc-keepalive-timeout", cfg.ec.GRPCKeepAliveTimeout, "Additional duration of wait before closing a non-responsive connection (0 to disable).")
150
151 // clustering
152 fs.Var(flags.NewURLsValue(embed.DefaultInitialAdvertisePeerURLs), "initial-advertise-peer-urls", "List of this member's peer URLs to advertise to the rest of the cluster.")
153 fs.Var(flags.NewURLsValue(embed.DefaultAdvertiseClientURLs), "advertise-client-urls", "List of this member's client URLs to advertise to the public.")
154 fs.StringVar(&cfg.ec.Durl, "discovery", cfg.ec.Durl, "Discovery URL used to bootstrap the cluster.")
155 fs.Var(cfg.cf.fallback, "discovery-fallback", fmt.Sprintf("Valid values include %s", strings.Join(cfg.cf.fallback.Values, ", ")))
156
157 fs.StringVar(&cfg.ec.Dproxy, "discovery-proxy", cfg.ec.Dproxy, "HTTP proxy to use for traffic to discovery service.")
158 fs.StringVar(&cfg.ec.DNSCluster, "discovery-srv", cfg.ec.DNSCluster, "DNS domain used to bootstrap initial cluster.")
159 fs.StringVar(&cfg.ec.InitialCluster, "initial-cluster", cfg.ec.InitialCluster, "Initial cluster configuration for bootstrapping.")
160 fs.StringVar(&cfg.ec.InitialClusterToken, "initial-cluster-token", cfg.ec.InitialClusterToken, "Initial cluster token for the etcd cluster during bootstrap.")
161 fs.Var(cfg.cf.clusterState, "initial-cluster-state", "Initial cluster state ('new' or 'existing').")
162
163 fs.BoolVar(&cfg.ec.StrictReconfigCheck, "strict-reconfig-check", cfg.ec.StrictReconfigCheck, "Reject reconfiguration requests that would cause quorum loss.")
164 fs.BoolVar(&cfg.ec.EnableV2, "enable-v2", cfg.ec.EnableV2, "Accept etcd V2 client requests.")
165 fs.StringVar(&cfg.ec.ExperimentalEnableV2V3, "experimental-enable-v2v3", cfg.ec.ExperimentalEnableV2V3, "v3 prefix for serving emulated v2 state.")
166
167 // proxy
168 fs.Var(cfg.cf.proxy, "proxy", fmt.Sprintf("Valid values include %s", strings.Join(cfg.cf.proxy.Values, ", ")))
169
170 fs.UintVar(&cfg.cp.ProxyFailureWaitMs, "proxy-failure-wait", cfg.cp.ProxyFailureWaitMs, "Time (in milliseconds) an endpoint will be held in a failed state.")
171 fs.UintVar(&cfg.cp.ProxyRefreshIntervalMs, "proxy-refresh-interval", cfg.cp.ProxyRefreshIntervalMs, "Time (in milliseconds) of the endpoints refresh interval.")
172 fs.UintVar(&cfg.cp.ProxyDialTimeoutMs, "proxy-dial-timeout", cfg.cp.ProxyDialTimeoutMs, "Time (in milliseconds) for a dial to timeout.")
173 fs.UintVar(&cfg.cp.ProxyWriteTimeoutMs, "proxy-write-timeout", cfg.cp.ProxyWriteTimeoutMs, "Time (in milliseconds) for a write to timeout.")
174 fs.UintVar(&cfg.cp.ProxyReadTimeoutMs, "proxy-read-timeout", cfg.cp.ProxyReadTimeoutMs, "Time (in milliseconds) for a read to timeout.")
175
176 // security
177 fs.StringVar(&cfg.ec.ClientTLSInfo.CAFile, "ca-file", "", "DEPRECATED: Path to the client server TLS CA file.")
178 fs.StringVar(&cfg.ec.ClientTLSInfo.CertFile, "cert-file", "", "Path to the client server TLS cert file.")
179 fs.StringVar(&cfg.ec.ClientTLSInfo.KeyFile, "key-file", "", "Path to the client server TLS key file.")
180 fs.BoolVar(&cfg.ec.ClientTLSInfo.ClientCertAuth, "client-cert-auth", false, "Enable client cert authentication.")
181 fs.StringVar(&cfg.ec.ClientTLSInfo.CRLFile, "client-crl-file", "", "Path to the client certificate revocation list file.")
182 fs.StringVar(&cfg.ec.ClientTLSInfo.TrustedCAFile, "trusted-ca-file", "", "Path to the client server TLS trusted CA cert file.")
183 fs.BoolVar(&cfg.ec.ClientAutoTLS, "auto-tls", false, "Client TLS using generated certificates")
184 fs.StringVar(&cfg.ec.PeerTLSInfo.CAFile, "peer-ca-file", "", "DEPRECATED: Path to the peer server TLS CA file.")
185 fs.StringVar(&cfg.ec.PeerTLSInfo.CertFile, "peer-cert-file", "", "Path to the peer server TLS cert file.")
186 fs.StringVar(&cfg.ec.PeerTLSInfo.KeyFile, "peer-key-file", "", "Path to the peer server TLS key file.")
187 fs.BoolVar(&cfg.ec.PeerTLSInfo.ClientCertAuth, "peer-client-cert-auth", false, "Enable peer client cert authentication.")
188 fs.StringVar(&cfg.ec.PeerTLSInfo.TrustedCAFile, "peer-trusted-ca-file", "", "Path to the peer server TLS trusted CA file.")
189 fs.BoolVar(&cfg.ec.PeerAutoTLS, "peer-auto-tls", false, "Peer TLS using generated certificates")
190 fs.StringVar(&cfg.ec.PeerTLSInfo.CRLFile, "peer-crl-file", "", "Path to the peer certificate revocation list file.")
191 fs.StringVar(&cfg.ec.PeerTLSInfo.AllowedCN, "peer-cert-allowed-cn", "", "Allowed CN for inter peer authentication.")
192
193 fs.Var(flags.NewStringsValueV2(""), "cipher-suites", "Comma-separated list of supported TLS cipher suites between client/server and peers (empty will be auto-populated by Go).")
194
195 // logging
196 fs.BoolVar(&cfg.ec.Debug, "debug", false, "Enable debug-level logging for etcd.")
197 fs.StringVar(&cfg.ec.LogPkgLevels, "log-package-levels", "", "Specify a particular log level for each etcd package (eg: 'etcdmain=CRITICAL,etcdserver=DEBUG').")
198 fs.StringVar(&cfg.ec.LogOutput, "log-output", embed.DefaultLogOutput, "Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd.")
199
200 // unsafe
201 fs.BoolVar(&cfg.ec.ForceNewCluster, "force-new-cluster", false, "Force to create a new one member cluster.")
202
203 // version
204 fs.BoolVar(&cfg.printVersion, "version", false, "Print the version and exit.")
205
206 fs.StringVar(&cfg.ec.AutoCompactionRetention, "auto-compaction-retention", "0", "Auto compaction retention for mvcc key value store. 0 means disable auto compaction.")
207 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.")
208
209 // pprof profiler via HTTP
210 fs.BoolVar(&cfg.ec.EnablePprof, "enable-pprof", false, "Enable runtime profiling data via HTTP server. Address is at client URL + \"/debug/pprof/\"")
211
212 // additional metrics
213 fs.StringVar(&cfg.ec.Metrics, "metrics", cfg.ec.Metrics, "Set level of detail for exported metrics, specify 'extensive' to include histogram metrics")
214
215 // auth
216 fs.StringVar(&cfg.ec.AuthToken, "auth-token", cfg.ec.AuthToken, "Specify auth token specific options.")
217
218 // experimental
219 fs.BoolVar(&cfg.ec.ExperimentalInitialCorruptCheck, "experimental-initial-corrupt-check", cfg.ec.ExperimentalInitialCorruptCheck, "Enable to check data corruption before serving any client/peer traffic.")
220 fs.DurationVar(&cfg.ec.ExperimentalCorruptCheckTime, "experimental-corrupt-check-time", cfg.ec.ExperimentalCorruptCheckTime, "Duration of time between cluster corruption check passes.")
221
222 // ignored
223 for _, f := range cfg.ignored {
224 fs.Var(&flags.IgnoredFlag{Name: f}, f, "")
225 }
226 return cfg
227}
228
229func (cfg *config) parse(arguments []string) error {
230 perr := cfg.cf.flagSet.Parse(arguments)
231 switch perr {
232 case nil:
233 case flag.ErrHelp:
234 fmt.Println(flagsline)
235 os.Exit(0)
236 default:
237 os.Exit(2)
238 }
239 if len(cfg.cf.flagSet.Args()) != 0 {
240 return fmt.Errorf("'%s' is not a valid flag", cfg.cf.flagSet.Arg(0))
241 }
242
243 if cfg.printVersion {
244 fmt.Printf("etcd Version: %s\n", version.Version)
245 fmt.Printf("Git SHA: %s\n", version.GitSHA)
246 fmt.Printf("Go Version: %s\n", runtime.Version())
247 fmt.Printf("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
248 os.Exit(0)
249 }
250
251 var err error
252 if cfg.configFile != "" {
253 plog.Infof("Loading server configuration from %q", cfg.configFile)
254 err = cfg.configFromFile(cfg.configFile)
255 } else {
256 err = cfg.configFromCmdLine()
257 }
258 return err
259}
260
261func (cfg *config) configFromCmdLine() error {
262 err := flags.SetFlagsFromEnv("ETCD", cfg.cf.flagSet)
263 if err != nil {
264 plog.Fatalf("%v", err)
265 }
266
267 cfg.ec.LPUrls = flags.URLsFromFlag(cfg.cf.flagSet, "listen-peer-urls")
268 cfg.ec.APUrls = flags.URLsFromFlag(cfg.cf.flagSet, "initial-advertise-peer-urls")
269 cfg.ec.LCUrls = flags.URLsFromFlag(cfg.cf.flagSet, "listen-client-urls")
270 cfg.ec.ACUrls = flags.URLsFromFlag(cfg.cf.flagSet, "advertise-client-urls")
271
272 if len(cfg.ec.ListenMetricsUrlsJSON) > 0 {
273 u, err := types.NewURLs(strings.Split(cfg.ec.ListenMetricsUrlsJSON, ","))
274 if err != nil {
275 plog.Fatalf("unexpected error setting up listen-metrics-urls: %v", err)
276 }
277 cfg.ec.ListenMetricsUrls = []url.URL(u)
278 }
279
280 cfg.ec.CipherSuites = flags.StringsFromFlagV2(cfg.cf.flagSet, "cipher-suites")
281
282 cfg.ec.ClusterState = cfg.cf.clusterState.String()
283 cfg.cp.Fallback = cfg.cf.fallback.String()
284 cfg.cp.Proxy = cfg.cf.proxy.String()
285
286 // disable default advertise-client-urls if lcurls is set
287 missingAC := flags.IsSet(cfg.cf.flagSet, "listen-client-urls") && !flags.IsSet(cfg.cf.flagSet, "advertise-client-urls")
288 if !cfg.mayBeProxy() && missingAC {
289 cfg.ec.ACUrls = nil
290 }
291
292 // disable default initial-cluster if discovery is set
293 if (cfg.ec.Durl != "" || cfg.ec.DNSCluster != "") && !flags.IsSet(cfg.cf.flagSet, "initial-cluster") {
294 cfg.ec.InitialCluster = ""
295 }
296
297 return cfg.validate()
298}
299
300func (cfg *config) configFromFile(path string) error {
301 eCfg, err := embed.ConfigFromFile(path)
302 if err != nil {
303 return err
304 }
305 cfg.ec = *eCfg
306
307 // load extra config information
308 b, rerr := ioutil.ReadFile(path)
309 if rerr != nil {
310 return rerr
311 }
312 if yerr := yaml.Unmarshal(b, &cfg.cp); yerr != nil {
313 return yerr
314 }
315 if cfg.cp.FallbackJSON != "" {
316 if err := cfg.cf.fallback.Set(cfg.cp.FallbackJSON); err != nil {
317 plog.Panicf("unexpected error setting up discovery-fallback flag: %v", err)
318 }
319 cfg.cp.Fallback = cfg.cf.fallback.String()
320 }
321 if cfg.cp.ProxyJSON != "" {
322 if err := cfg.cf.proxy.Set(cfg.cp.ProxyJSON); err != nil {
323 plog.Panicf("unexpected error setting up proxyFlag: %v", err)
324 }
325 cfg.cp.Proxy = cfg.cf.proxy.String()
326 }
327 return nil
328}
329
330func (cfg *config) mayBeProxy() bool {
331 mayFallbackToProxy := cfg.ec.Durl != "" && cfg.cp.Fallback == fallbackFlagProxy
332 return cfg.cp.Proxy != proxyFlagOff || mayFallbackToProxy
333}
334
335func (cfg *config) validate() error {
336 err := cfg.ec.Validate()
337 // TODO(yichengq): check this for joining through discovery service case
338 if err == embed.ErrUnsetAdvertiseClientURLsFlag && cfg.mayBeProxy() {
339 return nil
340 }
341 return err
342}
343
344func (cfg config) isProxy() bool { return cfg.cf.proxy.String() != proxyFlagOff }
345func (cfg config) isReadonlyProxy() bool { return cfg.cf.proxy.String() == proxyFlagReadonly }
346func (cfg config) shouldFallbackToProxy() bool { return cfg.cf.fallback.String() == fallbackFlagProxy }