blob: 8f4a6aaa8cae62ba6348728369758ecae3adbd5a [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// 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
15package embed
16
17import (
18 "crypto/tls"
19 "errors"
20 "flag"
21 "fmt"
22 "math"
23 "net"
24 "net/http"
25 "net/netip"
26 "net/url"
27 "os"
28 "path/filepath"
29 "strings"
30 "sync"
31 "time"
32
33 "go.uber.org/zap"
34 "golang.org/x/crypto/bcrypt"
35 "google.golang.org/grpc"
36 "sigs.k8s.io/yaml"
37
38 bolt "go.etcd.io/bbolt"
39 "go.etcd.io/etcd/client/pkg/v3/logutil"
40 "go.etcd.io/etcd/client/pkg/v3/srv"
41 "go.etcd.io/etcd/client/pkg/v3/tlsutil"
42 "go.etcd.io/etcd/client/pkg/v3/transport"
43 "go.etcd.io/etcd/client/pkg/v3/types"
44 clientv3 "go.etcd.io/etcd/client/v3"
45 "go.etcd.io/etcd/pkg/v3/featuregate"
46 "go.etcd.io/etcd/pkg/v3/flags"
47 "go.etcd.io/etcd/pkg/v3/netutil"
48 "go.etcd.io/etcd/server/v3/config"
49 "go.etcd.io/etcd/server/v3/etcdserver"
50 "go.etcd.io/etcd/server/v3/etcdserver/api/membership"
51 "go.etcd.io/etcd/server/v3/etcdserver/api/rafthttp"
52 "go.etcd.io/etcd/server/v3/etcdserver/api/v3compactor"
53 "go.etcd.io/etcd/server/v3/etcdserver/api/v3discovery"
54 "go.etcd.io/etcd/server/v3/features"
55)
56
57const (
58 ClusterStateFlagNew = "new"
59 ClusterStateFlagExisting = "existing"
60
61 DefaultName = "default"
62 DefaultMaxSnapshots = 5
63 DefaultMaxWALs = 5
64 DefaultMaxTxnOps = uint(128)
65 DefaultWarningApplyDuration = 100 * time.Millisecond
66 DefaultWarningUnaryRequestDuration = 300 * time.Millisecond
67 DefaultMaxRequestBytes = 1.5 * 1024 * 1024
68 DefaultMaxConcurrentStreams = math.MaxUint32
69 DefaultGRPCKeepAliveMinTime = 5 * time.Second
70 DefaultGRPCKeepAliveInterval = 2 * time.Hour
71 DefaultGRPCKeepAliveTimeout = 20 * time.Second
72 DefaultDowngradeCheckTime = 5 * time.Second
73 DefaultAutoCompactionMode = "periodic"
74 DefaultAutoCompactionRetention = "0"
75 DefaultAuthToken = "simple"
76 DefaultCompactHashCheckTime = time.Minute
77 DefaultLoggingFormat = "json"
78
79 DefaultDiscoveryDialTimeout = 2 * time.Second
80 DefaultDiscoveryRequestTimeOut = 5 * time.Second
81 DefaultDiscoveryKeepAliveTime = 2 * time.Second
82 DefaultDiscoveryKeepAliveTimeOut = 6 * time.Second
83 DefaultDiscoveryInsecureTransport = true
84 DefaultSelfSignedCertValidity = 1
85 DefaultTLSMinVersion = string(tlsutil.TLSVersion12)
86
87 DefaultListenPeerURLs = "http://localhost:2380"
88 DefaultListenClientURLs = "http://localhost:2379"
89
90 DefaultLogOutput = "default"
91 JournalLogOutput = "systemd/journal"
92 StdErrLogOutput = "stderr"
93 StdOutLogOutput = "stdout"
94
95 // DefaultLogRotationConfig is the default configuration used for log rotation.
96 // Log rotation is disabled by default.
97 // MaxSize = 100 // MB
98 // MaxAge = 0 // days (no limit)
99 // MaxBackups = 0 // no limit
100 // LocalTime = false // use computers local time, UTC by default
101 // Compress = false // compress the rotated log in gzip format
102 DefaultLogRotationConfig = `{"maxsize": 100, "maxage": 0, "maxbackups": 0, "localtime": false, "compress": false}`
103
104 // ExperimentalDistributedTracingAddress is the default collector address.
105 // TODO: delete in v3.7
106 // Deprecated: Use DefaultDistributedTracingAddress instead. Will be decommissioned in v3.7.
107 ExperimentalDistributedTracingAddress = "localhost:4317"
108 // DefaultDistributedTracingAddress is the default collector address.
109 DefaultDistributedTracingAddress = "localhost:4317"
110 // ExperimentalDistributedTracingServiceName is the default etcd service name.
111 // TODO: delete in v3.7
112 // Deprecated: Use DefaultDistributedTracingServiceName instead. Will be decommissioned in v3.7.
113 ExperimentalDistributedTracingServiceName = "etcd"
114 // DefaultDistributedTracingServiceName is the default etcd service name.
115 DefaultDistributedTracingServiceName = "etcd"
116
117 DefaultExperimentalTxnModeWriteWithSharedBuffer = true
118
119 // DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag.
120 // It's enabled by default.
121 DefaultStrictReconfigCheck = true
122
123 // maxElectionMs specifies the maximum value of election timeout.
124 // More details are listed on etcd.io/docs > version > tuning/#time-parameters
125 maxElectionMs = 50000
126 // backend freelist map type
127 freelistArrayType = "array"
128
129 ServerFeatureGateFlagName = "feature-gates"
130)
131
132var (
133 ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
134 "Choose one of \"initial-cluster\", \"discovery\", \"discovery-endpoints\" or \"discovery-srv\"")
135 ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")
136 ErrLogRotationInvalidLogOutput = fmt.Errorf("--log-outputs requires a single file path when --log-rotate-config-json is defined")
137
138 DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
139 DefaultAdvertiseClientURLs = "http://localhost:2379"
140
141 defaultHostname string
142 defaultHostStatus error
143
144 // indirection for testing
145 getCluster = srv.GetCluster
146
147 // in 3.6, we are migration all the --experimental flags to feature gate and flags without the prefix.
148 // This is the mapping from the non boolean `experimental-` to the new flags.
149 // TODO: delete in v3.7
150 experimentalFlagMigrationMap = map[string]string{
151 "experimental-compact-hash-check-time": "compact-hash-check-time",
152 "experimental-corrupt-check-time": "corrupt-check-time",
153 "experimental-compaction-batch-limit": "compaction-batch-limit",
154 "experimental-watch-progress-notify-interval": "watch-progress-notify-interval",
155 "experimental-warning-apply-duration": "warning-apply-duration",
156 "experimental-bootstrap-defrag-threshold-megabytes": "bootstrap-defrag-threshold-megabytes",
157 "experimental-memory-mlock": "memory-mlock",
158 "experimental-snapshot-catchup-entries": "snapshot-catchup-entries",
159 "experimental-compaction-sleep-interval": "compaction-sleep-interval",
160 "experimental-downgrade-check-time": "downgrade-check-time",
161 "experimental-peer-skip-client-san-verification": "peer-skip-client-san-verification",
162 "experimental-enable-distributed-tracing": "enable-distributed-tracing",
163 "experimental-distributed-tracing-address": "distributed-tracing-address",
164 "experimental-distributed-tracing-service-name": "distributed-tracing-service-name",
165 "experimental-distributed-tracing-instance-id": "distributed-tracing-instance-id",
166 "experimental-distributed-tracing-sampling-rate": "distributed-tracing-sampling-rate",
167 }
168)
169
170var (
171 // CompactorModePeriodic is periodic compaction mode
172 // for "Config.AutoCompactionMode" field.
173 // If "AutoCompactionMode" is CompactorModePeriodic and
174 // "AutoCompactionRetention" is "1h", it automatically compacts
175 // compacts storage every hour.
176 CompactorModePeriodic = v3compactor.ModePeriodic
177
178 // CompactorModeRevision is revision-based compaction mode
179 // for "Config.AutoCompactionMode" field.
180 // If "AutoCompactionMode" is CompactorModeRevision and
181 // "AutoCompactionRetention" is "1000", it compacts log on
182 // revision 5000 when the current revision is 6000.
183 // This runs every 5-minute if enough of logs have proceeded.
184 CompactorModeRevision = v3compactor.ModeRevision
185)
186
187func init() {
188 defaultHostname, defaultHostStatus = netutil.GetDefaultHost()
189}
190
191// Config holds the arguments for configuring an etcd server.
192type Config struct {
193 Name string `json:"name"`
194 Dir string `json:"data-dir"`
195 //revive:disable-next-line:var-naming
196 WalDir string `json:"wal-dir"`
197
198 // SnapshotCount is the number of committed transactions that trigger a snapshot to disk.
199 // TODO: remove it in 3.7.
200 // Deprecated: Will be decommissioned in v3.7.
201 SnapshotCount uint64 `json:"snapshot-count"`
202
203 // ExperimentalSnapshotCatchUpEntries is the number of entries for a slow follower
204 // to catch-up after compacting the raft storage entries.
205 // We expect the follower has a millisecond level latency with the leader.
206 // The max throughput is around 10K. Keep a 5K entries is enough for helping
207 // follower to catch up.
208 // TODO: remove in v3.7.
209 // Note we made a mistake in https://github.com/etcd-io/etcd/pull/15033. The json tag
210 // `*-catch-up-*` isn't consistent with the command line flag `*-catchup-*`.
211 // Deprecated: Use SnapshotCatchUpEntries instead. Will be removed in v3.7.
212 ExperimentalSnapshotCatchUpEntries uint64 `json:"experimental-snapshot-catch-up-entries"`
213
214 // SnapshotCatchUpEntries is the number of entires for a slow follower
215 // to catch-up after compacting the raft storage entries.
216 // We expect the follower has a millisecond level latency with the leader.
217 // The max throughput is around 10K. Keep a 5K entries is enough for helping
218 // follower to catch up.
219 SnapshotCatchUpEntries uint64 `json:"snapshot-catchup-entries"`
220
221 // MaxSnapFiles is the maximum number of snapshot files.
222 // TODO: remove it in 3.7.
223 // Deprecated: Will be removed in v3.7.
224 MaxSnapFiles uint `json:"max-snapshots"`
225 //revive:disable-next-line:var-naming
226 MaxWalFiles uint `json:"max-wals"`
227
228 // TickMs is the number of milliseconds between heartbeat ticks.
229 // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
230 // make ticks a cluster wide configuration.
231 TickMs uint `json:"heartbeat-interval"`
232 ElectionMs uint `json:"election-timeout"`
233
234 // InitialElectionTickAdvance is true, then local member fast-forwards
235 // election ticks to speed up "initial" leader election trigger. This
236 // benefits the case of larger election ticks. For instance, cross
237 // datacenter deployment may require longer election timeout of 10-second.
238 // If true, local node does not need wait up to 10-second. Instead,
239 // forwards its election ticks to 8-second, and have only 2-second left
240 // before leader election.
241 //
242 // Major assumptions are that:
243 // - cluster has no active leader thus advancing ticks enables faster
244 // leader election, or
245 // - cluster already has an established leader, and rejoining follower
246 // is likely to receive heartbeats from the leader after tick advance
247 // and before election timeout.
248 //
249 // However, when network from leader to rejoining follower is congested,
250 // and the follower does not receive leader heartbeat within left election
251 // ticks, disruptive election has to happen thus affecting cluster
252 // availabilities.
253 //
254 // Disabling this would slow down initial bootstrap process for cross
255 // datacenter deployments. Make your own tradeoffs by configuring
256 // --initial-election-tick-advance at the cost of slow initial bootstrap.
257 //
258 // If single-node, it advances ticks regardless.
259 //
260 // See https://github.com/etcd-io/etcd/issues/9333 for more detail.
261 InitialElectionTickAdvance bool `json:"initial-election-tick-advance"`
262
263 // BackendBatchInterval is the maximum time before commit the backend transaction.
264 BackendBatchInterval time.Duration `json:"backend-batch-interval"`
265 // BackendBatchLimit is the maximum operations before commit the backend transaction.
266 BackendBatchLimit int `json:"backend-batch-limit"`
267 // BackendFreelistType specifies the type of freelist that boltdb backend uses (array and map are supported types).
268 BackendFreelistType string `json:"backend-bbolt-freelist-type"`
269 QuotaBackendBytes int64 `json:"quota-backend-bytes"`
270 MaxTxnOps uint `json:"max-txn-ops"`
271 MaxRequestBytes uint `json:"max-request-bytes"`
272
273 // MaxConcurrentStreams specifies the maximum number of concurrent
274 // streams that each client can open at a time.
275 MaxConcurrentStreams uint32 `json:"max-concurrent-streams"`
276
277 //revive:disable:var-naming
278 ListenPeerUrls, ListenClientUrls, ListenClientHttpUrls []url.URL
279 AdvertisePeerUrls, AdvertiseClientUrls []url.URL
280 //revive:enable:var-naming
281
282 ClientTLSInfo transport.TLSInfo
283 ClientAutoTLS bool
284 PeerTLSInfo transport.TLSInfo
285 PeerAutoTLS bool
286
287 // SelfSignedCertValidity specifies the validity period of the client and peer certificates
288 // that are automatically generated by etcd when you specify ClientAutoTLS and PeerAutoTLS,
289 // the unit is year, and the default is 1
290 SelfSignedCertValidity uint `json:"self-signed-cert-validity"`
291
292 // CipherSuites is a list of supported TLS cipher suites between
293 // client/server and peers. If empty, Go auto-populates the list.
294 // Note that cipher suites are prioritized in the given order.
295 CipherSuites []string `json:"cipher-suites"`
296
297 // TlsMinVersion is the minimum accepted TLS version between client/server and peers.
298 //revive:disable-next-line:var-naming
299 TlsMinVersion string `json:"tls-min-version"`
300
301 // TlsMaxVersion is the maximum accepted TLS version between client/server and peers.
302 //revive:disable-next-line:var-naming
303 TlsMaxVersion string `json:"tls-max-version"`
304
305 ClusterState string `json:"initial-cluster-state"`
306 DNSCluster string `json:"discovery-srv"`
307 DNSClusterServiceName string `json:"discovery-srv-name"`
308 Dproxy string `json:"discovery-proxy"`
309
310 Durl string `json:"discovery"`
311 DiscoveryCfg v3discovery.DiscoveryConfig `json:"discovery-config"`
312
313 InitialCluster string `json:"initial-cluster"`
314 InitialClusterToken string `json:"initial-cluster-token"`
315 StrictReconfigCheck bool `json:"strict-reconfig-check"`
316
317 // AutoCompactionMode is either 'periodic' or 'revision'.
318 AutoCompactionMode string `json:"auto-compaction-mode"`
319 // AutoCompactionRetention is either duration string with time unit
320 // (e.g. '5m' for 5-minute), or revision unit (e.g. '5000').
321 // If no time unit is provided and compaction mode is 'periodic',
322 // the unit defaults to hour. For example, '5' translates into 5-hour.
323 AutoCompactionRetention string `json:"auto-compaction-retention"`
324
325 // GRPCKeepAliveMinTime is the minimum interval that a client should
326 // wait before pinging server. When client pings "too fast", server
327 // sends goaway and closes the connection (errors: too_many_pings,
328 // http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
329 // Server expects client pings only when there is any active streams
330 // (PermitWithoutStream is set false).
331 GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
332 // GRPCKeepAliveInterval is the frequency of server-to-client ping
333 // to check if a connection is alive. Close a non-responsive connection
334 // after an additional duration of Timeout. 0 to disable.
335 GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
336 // GRPCKeepAliveTimeout is the additional duration of wait
337 // before closing a non-responsive connection. 0 to disable.
338 GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`
339
340 // GRPCAdditionalServerOptions is the additional server option hook
341 // for changing the default internal gRPC configuration. Note these
342 // additional configurations take precedence over the existing individual
343 // configurations if present. Please refer to
344 // https://github.com/etcd-io/etcd/pull/14066#issuecomment-1248682996
345 GRPCAdditionalServerOptions []grpc.ServerOption `json:"grpc-additional-server-options"`
346
347 // SocketOpts are socket options passed to listener config.
348 SocketOpts transport.SocketOpts `json:"socket-options"`
349
350 // PreVote is true to enable Raft Pre-Vote.
351 // If enabled, Raft runs an additional election phase
352 // to check whether it would get enough votes to win
353 // an election, thus minimizing disruptions.
354 PreVote bool `json:"pre-vote"`
355
356 CORS map[string]struct{}
357
358 // HostWhitelist lists acceptable hostnames from HTTP client requests.
359 // Client origin policy protects against "DNS Rebinding" attacks
360 // to insecure etcd servers. That is, any website can simply create
361 // an authorized DNS name, and direct DNS to "localhost" (or any
362 // other address). Then, all HTTP endpoints of etcd server listening
363 // on "localhost" becomes accessible, thus vulnerable to DNS rebinding
364 // attacks. See "CVE-2018-5702" for more detail.
365 //
366 // 1. If client connection is secure via HTTPS, allow any hostnames.
367 // 2. If client connection is not secure and "HostWhitelist" is not empty,
368 // only allow HTTP requests whose Host field is listed in whitelist.
369 //
370 // Note that the client origin policy is enforced whether authentication
371 // is enabled or not, for tighter controls.
372 //
373 // By default, "HostWhitelist" is "*", which allows any hostnames.
374 // Note that when specifying hostnames, loopback addresses are not added
375 // automatically. To allow loopback interfaces, leave it empty or set it "*",
376 // or add them to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
377 //
378 // CVE-2018-5702 reference:
379 // - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
380 // - https://github.com/transmission/transmission/pull/468
381 // - https://github.com/etcd-io/etcd/issues/9353
382 HostWhitelist map[string]struct{}
383
384 // UserHandlers is for registering users handlers and only used for
385 // embedding etcd into other applications.
386 // The map key is the route path for the handler, and
387 // you must ensure it can't be conflicted with etcd's.
388 UserHandlers map[string]http.Handler `json:"-"`
389 // ServiceRegister is for registering users' gRPC services. A simple usage example:
390 // cfg := embed.NewConfig()
391 // cfg.ServiceRegister = func(s *grpc.Server) {
392 // pb.RegisterFooServer(s, &fooServer{})
393 // pb.RegisterBarServer(s, &barServer{})
394 // }
395 // embed.StartEtcd(cfg)
396 ServiceRegister func(*grpc.Server) `json:"-"`
397
398 AuthToken string `json:"auth-token"`
399 BcryptCost uint `json:"bcrypt-cost"`
400
401 // AuthTokenTTL in seconds of the simple token
402 AuthTokenTTL uint `json:"auth-token-ttl"`
403
404 // ExperimentalInitialCorruptCheck defines to check data corrution on boot.
405 // TODO: delete in v3.7
406 // Deprecated: Use InitialCorruptCheck Feature Gate instead. Will be decommissioned in v3.7.
407 ExperimentalInitialCorruptCheck bool `json:"experimental-initial-corrupt-check"`
408 // ExperimentalCorruptCheckTime is the duration of time between cluster corruption check passes.
409 // TODO: delete in v3.7
410 // Deprecated: Use CorruptCheckTime instead. Will be decommissioned in v3.7.
411 ExperimentalCorruptCheckTime time.Duration `json:"experimental-corrupt-check-time"`
412 // CorruptCheckTime is the duration of time between cluster corruption check passes.
413 CorruptCheckTime time.Duration `json:"corrupt-check-time"`
414 // ExperimentalCompactHashCheckEnabled enables leader to periodically check followers compaction hashes.
415 // TODO: delete in v3.7
416 // Deprecated: Use CompactHashCheck Feature Gate. Will be decommissioned in v3.7.
417 ExperimentalCompactHashCheckEnabled bool `json:"experimental-compact-hash-check-enabled"`
418 // ExperimentalCompactHashCheckTime is the duration of time between leader checks followers compaction hashes.
419 // TODO: delete in v3.7
420 // Deprecated: Use CompactHashCheckTime instead. Will be decommissioned in v3.7.
421 ExperimentalCompactHashCheckTime time.Duration `json:"experimental-compact-hash-check-time"`
422 // CompactHashCheckTime is the duration of time between leader checks followers compaction hashes.
423 CompactHashCheckTime time.Duration `json:"compact-hash-check-time"`
424
425 // ExperimentalEnableLeaseCheckpoint enables leader to send regular checkpoints to other members to prevent reset of remaining TTL on leader change.
426 ExperimentalEnableLeaseCheckpoint bool `json:"experimental-enable-lease-checkpoint"`
427 // ExperimentalEnableLeaseCheckpointPersist enables persisting remainingTTL to prevent indefinite auto-renewal of long lived leases. Always enabled in v3.6. Should be used to ensure smooth upgrade from v3.5 clusters with this feature enabled.
428 // Requires experimental-enable-lease-checkpoint to be enabled.
429 // TODO: Delete in v3.7
430 // Deprecated: To be decommissioned in v3.7.
431 ExperimentalEnableLeaseCheckpointPersist bool `json:"experimental-enable-lease-checkpoint-persist"`
432 // ExperimentalCompactionBatchLimit Sets the maximum revisions deleted in each compaction batch.
433 // TODO: Delete in v3.7
434 // Deprecated: Use CompactionBatchLimit instead. Will be decommissioned in v3.7.
435 ExperimentalCompactionBatchLimit int `json:"experimental-compaction-batch-limit"`
436 // CompactionBatchLimit Sets the maximum revisions deleted in each compaction batch.
437 CompactionBatchLimit int `json:"compaction-batch-limit"`
438 // ExperimentalCompactionSleepInterval is the sleep interval between every etcd compaction loop.
439 // TODO: Delete in v3.7
440 // Deprecated: Use CompactionSleepInterval instead. Will be decommissioned in v3.7.
441 ExperimentalCompactionSleepInterval time.Duration `json:"experimental-compaction-sleep-interval"`
442 // CompactionSleepInterval is the sleep interval between every etcd compaction loop.
443 CompactionSleepInterval time.Duration `json:"compaction-sleep-interval"`
444 // ExperimentalWatchProgressNotifyInterval is the time duration of periodic watch progress notifications.
445 // TODO: Delete in v3.7
446 // Deprecated: Use WatchProgressNotifyInterval instead. Will be decommissioned in v3.7.
447 ExperimentalWatchProgressNotifyInterval time.Duration `json:"experimental-watch-progress-notify-interval"`
448 // WatchProgressNotifyInterval is the time duration of periodic watch progress notifications.
449 WatchProgressNotifyInterval time.Duration `json:"watch-progress-notify-interval"`
450 // ExperimentalWarningApplyDuration is the time duration after which a warning is generated if applying request
451 // takes more time than this value.
452 // TODO: Delete in v3.7
453 // Deprecated: Use WarningApplyDuration instead. Will be decommissioned in v3.7.
454 ExperimentalWarningApplyDuration time.Duration `json:"experimental-warning-apply-duration"`
455 // WarningApplyDuration is the time duration after which a warning is generated if applying request
456 WarningApplyDuration time.Duration `json:"warning-apply-duration"`
457 // ExperimentalBootstrapDefragThresholdMegabytes is the minimum number of megabytes needed to be freed for etcd server to
458 // consider running defrag during bootstrap. Needs to be set to non-zero value to take effect.
459 // TODO: Delete in v3.7
460 // Deprecated: Use BootstrapDefragThresholdMegabytes instead. Will be decommissioned in v3.7.
461 ExperimentalBootstrapDefragThresholdMegabytes uint `json:"experimental-bootstrap-defrag-threshold-megabytes"`
462 // BootstrapDefragThresholdMegabytes is the minimum number of megabytes needed to be freed for etcd server to
463 BootstrapDefragThresholdMegabytes uint `json:"bootstrap-defrag-threshold-megabytes"`
464 // WarningUnaryRequestDuration is the time duration after which a warning is generated if applying
465 // unary request takes more time than this value.
466 WarningUnaryRequestDuration time.Duration `json:"warning-unary-request-duration"`
467 // ExperimentalWarningUnaryRequestDuration is the time duration after which a warning is generated if applying
468 // TODO: Delete in v3.7
469 // Deprecated: Use WarningUnaryRequestDuration. Will be decommissioned in v3.7.
470 ExperimentalWarningUnaryRequestDuration time.Duration `json:"experimental-warning-unary-request-duration"`
471 // MaxLearners sets a limit to the number of learner members that can exist in the cluster membership.
472 MaxLearners int `json:"max-learners"`
473
474 // ForceNewCluster starts a new cluster even if previously started; unsafe.
475 ForceNewCluster bool `json:"force-new-cluster"`
476
477 EnablePprof bool `json:"enable-pprof"`
478 Metrics string `json:"metrics"`
479 ListenMetricsUrls []url.URL
480 ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`
481
482 // ExperimentalEnableDistributedTracing indicates if experimental tracing using OpenTelemetry is enabled.
483 // TODO: delete in v3.7
484 // Deprecated: Use EnableDistributedTracing instead. Will be decommissioned in v3.7.
485 ExperimentalEnableDistributedTracing bool `json:"experimental-enable-distributed-tracing"`
486 // EnableDistributedTracing indicates if tracing using OpenTelemetry is enabled.
487 EnableDistributedTracing bool `json:"enable-distributed-tracing"`
488 // ExperimentalDistributedTracingAddress is the address of the OpenTelemetry Collector.
489 // Can only be set if ExperimentalEnableDistributedTracing is true.
490 // TODO: delete in v3.7
491 // Deprecated: Use DistributedTracingAddress instead. Will be decommissioned in v3.7.
492 ExperimentalDistributedTracingAddress string `json:"experimental-distributed-tracing-address"`
493 // DistributedTracingAddress is the address of the OpenTelemetry Collector.
494 // Can only be set if EnableDistributedTracing is true.
495 DistributedTracingAddress string `json:"distributed-tracing-address"`
496 // ExperimentalDistributedTracingServiceName is the name of the service.
497 // Can only be used if ExperimentalEnableDistributedTracing is true.
498 // TODO: delete in v3.7
499 // Deprecated: Use DistributedTracingServiceName instead. Will be decommissioned in v3.7.
500 ExperimentalDistributedTracingServiceName string `json:"experimental-distributed-tracing-service-name"`
501 // DistributedTracingServiceName is the name of the service.
502 // Can only be used if EnableDistributedTracing is true.
503 DistributedTracingServiceName string `json:"distributed-tracing-service-name"`
504 // ExperimentalDistributedTracingServiceInstanceID is the ID key of the service.
505 // This ID must be unique, as helps to distinguish instances of the same service
506 // that exist at the same time.
507 // Can only be used if ExperimentalEnableDistributedTracing is true.
508 // TODO: delete in v3.7
509 // Deprecated: Use DistributedTracingServiceInstanceID instead. Will be decommissioned in v3.7.
510 ExperimentalDistributedTracingServiceInstanceID string `json:"experimental-distributed-tracing-instance-id"`
511 // DistributedTracingServiceInstanceID is the ID key of the service.
512 // This ID must be unique, as helps to distinguish instances of the same service
513 // that exist at the same time.
514 // Can only be used if EnableDistributedTracing is true.
515 DistributedTracingServiceInstanceID string `json:"distributed-tracing-instance-id"`
516 // ExperimentalDistributedTracingSamplingRatePerMillion is the number of samples to collect per million spans.
517 // Defaults to 0.
518 // TODO: delete in v3.7
519 // Deprecated: Use DistributedTracingSamplingRatePerMillion instead. Will be decommissioned in v3.7.
520 ExperimentalDistributedTracingSamplingRatePerMillion int `json:"experimental-distributed-tracing-sampling-rate"`
521 // DistributedTracingSamplingRatePerMillion is the number of samples to collect per million spans.
522 // Defaults to 0.
523 DistributedTracingSamplingRatePerMillion int `json:"distributed-tracing-sampling-rate"`
524
525 // ExperimentalPeerSkipClientSanVerification determines whether to skip verification of SAN field
526 // in client certificate for peer connections.
527 // TODO: Delete in v3.7
528 // Deprecated: Use `peer-skip-client-san-verification` instead. Will be decommissioned in v3.7.
529 ExperimentalPeerSkipClientSanVerification bool `json:"experimental-peer-skip-client-san-verification"`
530
531 // Logger is logger options: currently only supports "zap".
532 // "capnslog" is removed in v3.5.
533 Logger string `json:"logger"`
534 // LogLevel configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'.
535 LogLevel string `json:"log-level"`
536 // LogFormat set log encoding. Only supports json, console. Default is 'json'.
537 LogFormat string `json:"log-format"`
538 // LogOutputs is either:
539 // - "default" as os.Stderr,
540 // - "stderr" as os.Stderr,
541 // - "stdout" as os.Stdout,
542 // - file path to append server logs to.
543 // It can be multiple when "Logger" is zap.
544 LogOutputs []string `json:"log-outputs"`
545 // EnableLogRotation enables log rotation of a single LogOutputs file target.
546 EnableLogRotation bool `json:"enable-log-rotation"`
547 // LogRotationConfigJSON is a passthrough allowing a log rotation JSON config to be passed directly.
548 LogRotationConfigJSON string `json:"log-rotation-config-json"`
549 // ZapLoggerBuilder is used to build the zap logger.
550 ZapLoggerBuilder func(*Config) error
551
552 // logger logs server-side operations. The default is nil,
553 // and "setupLogging" must be called before starting server.
554 // Do not set logger directly.
555 loggerMu *sync.RWMutex
556 logger *zap.Logger
557 // EnableGRPCGateway enables grpc gateway.
558 // The gateway translates a RESTful HTTP API into gRPC.
559 EnableGRPCGateway bool `json:"enable-grpc-gateway"`
560
561 // UnsafeNoFsync disables all uses of fsync.
562 // Setting this is unsafe and will cause data loss.
563 UnsafeNoFsync bool `json:"unsafe-no-fsync"`
564
565 // ExperimentalDowngradeCheckTime is the duration between two downgrade status checks (in seconds).
566 // TODO: Delete `ExperimentalDowngradeCheckTime` in v3.7.
567 // Deprecated: Use DowngradeCheckTime instead. Will be decommissioned in v3.7.
568 ExperimentalDowngradeCheckTime time.Duration `json:"experimental-downgrade-check-time"`
569 // DowngradeCheckTime is the duration between two downgrade status checks (in seconds).
570 DowngradeCheckTime time.Duration `json:"downgrade-check-time"`
571
572 // MemoryMlock enables mlocking of etcd owned memory pages.
573 // The setting improves etcd tail latency in environments were:
574 // - memory pressure might lead to swapping pages to disk
575 // - disk latency might be unstable
576 // Currently all etcd memory gets mlocked, but in future the flag can
577 // be refined to mlock in-use area of bbolt only.
578 MemoryMlock bool `json:"memory-mlock"`
579
580 // ExperimentalMemoryMlock enables mlocking of etcd owned memory pages.
581 // TODO: Delete in v3.7
582 // Deprecated: Use MemoryMlock instad. To be decommissioned in v3.7.
583 ExperimentalMemoryMlock bool `json:"experimental-memory-mlock"`
584
585 // ExperimentalTxnModeWriteWithSharedBuffer enables write transaction to use a shared buffer in its readonly check operations.
586 // TODO: Delete in v3.7
587 // Deprecated: Use TxnModeWriteWithSharedBuffer Feature Flag. Will be decommissioned in v3.7.
588 ExperimentalTxnModeWriteWithSharedBuffer bool `json:"experimental-txn-mode-write-with-shared-buffer"`
589
590 // ExperimentalStopGRPCServiceOnDefrag enables etcd gRPC service to stop serving client requests on defragmentation.
591 // TODO: Delete in v3.7
592 // Deprecated: Use StopGRPCServiceOnDefrag Feature Flag. Will be decommissioned in v3.7.
593 ExperimentalStopGRPCServiceOnDefrag bool `json:"experimental-stop-grpc-service-on-defrag"`
594
595 // V2Deprecation describes phase of API & Storage V2 support.
596 // Do not set this field for embedded use cases, as it has no effect. However, setting it will not cause any harm.
597 // TODO: Delete in v3.8
598 // Deprecated: The default value is enforced, to be removed in v3.8.
599 V2Deprecation config.V2DeprecationEnum `json:"v2-deprecation"`
600
601 // ServerFeatureGate is a server level feature gate
602 ServerFeatureGate featuregate.FeatureGate
603 // FlagsExplicitlySet stores if a flag is explicitly set from the cmd line or config file.
604 FlagsExplicitlySet map[string]bool
605}
606
607// configYAML holds the config suitable for yaml parsing
608type configYAML struct {
609 Config
610 configJSON
611}
612
613// configJSON has file options that are translated into Config options
614type configJSON struct {
615 ListenPeerURLs string `json:"listen-peer-urls"`
616 ListenClientURLs string `json:"listen-client-urls"`
617 ListenClientHTTPURLs string `json:"listen-client-http-urls"`
618 AdvertisePeerURLs string `json:"initial-advertise-peer-urls"`
619 AdvertiseClientURLs string `json:"advertise-client-urls"`
620
621 CORSJSON string `json:"cors"`
622 HostWhitelistJSON string `json:"host-whitelist"`
623
624 ClientSecurityJSON securityConfig `json:"client-transport-security"`
625 PeerSecurityJSON securityConfig `json:"peer-transport-security"`
626
627 ServerFeatureGatesJSON string `json:"feature-gates"`
628}
629
630type securityConfig struct {
631 CertFile string `json:"cert-file"`
632 KeyFile string `json:"key-file"`
633 ClientCertFile string `json:"client-cert-file"`
634 ClientKeyFile string `json:"client-key-file"`
635 CertAuth bool `json:"client-cert-auth"`
636 TrustedCAFile string `json:"trusted-ca-file"`
637 AutoTLS bool `json:"auto-tls"`
638 AllowedCNs []string `json:"allowed-cn"`
639 AllowedHostnames []string `json:"allowed-hostname"`
640 SkipClientSANVerify bool `json:"skip-client-san-verification,omitempty"`
641}
642
643// NewConfig creates a new Config populated with default values.
644func NewConfig() *Config {
645 lpurl, _ := url.Parse(DefaultListenPeerURLs)
646 apurl, _ := url.Parse(DefaultInitialAdvertisePeerURLs)
647 lcurl, _ := url.Parse(DefaultListenClientURLs)
648 acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
649 cfg := &Config{
650 MaxSnapFiles: DefaultMaxSnapshots,
651 MaxWalFiles: DefaultMaxWALs,
652
653 Name: DefaultName,
654
655 SnapshotCount: etcdserver.DefaultSnapshotCount,
656 ExperimentalSnapshotCatchUpEntries: etcdserver.DefaultSnapshotCatchUpEntries,
657 SnapshotCatchUpEntries: etcdserver.DefaultSnapshotCatchUpEntries,
658
659 MaxTxnOps: DefaultMaxTxnOps,
660 MaxRequestBytes: DefaultMaxRequestBytes,
661 MaxConcurrentStreams: DefaultMaxConcurrentStreams,
662 WarningApplyDuration: DefaultWarningApplyDuration,
663
664 GRPCKeepAliveMinTime: DefaultGRPCKeepAliveMinTime,
665 GRPCKeepAliveInterval: DefaultGRPCKeepAliveInterval,
666 GRPCKeepAliveTimeout: DefaultGRPCKeepAliveTimeout,
667
668 SocketOpts: transport.SocketOpts{
669 ReusePort: false,
670 ReuseAddress: false,
671 },
672
673 TickMs: 100,
674 ElectionMs: 1000,
675 InitialElectionTickAdvance: true,
676
677 ListenPeerUrls: []url.URL{*lpurl},
678 ListenClientUrls: []url.URL{*lcurl},
679 AdvertisePeerUrls: []url.URL{*apurl},
680 AdvertiseClientUrls: []url.URL{*acurl},
681
682 ClusterState: ClusterStateFlagNew,
683 InitialClusterToken: "etcd-cluster",
684
685 StrictReconfigCheck: DefaultStrictReconfigCheck,
686 Metrics: "basic",
687
688 CORS: map[string]struct{}{"*": {}},
689 HostWhitelist: map[string]struct{}{"*": {}},
690
691 AuthToken: DefaultAuthToken,
692 BcryptCost: uint(bcrypt.DefaultCost),
693 AuthTokenTTL: 300,
694 SelfSignedCertValidity: DefaultSelfSignedCertValidity,
695 TlsMinVersion: DefaultTLSMinVersion,
696
697 PreVote: true,
698
699 loggerMu: new(sync.RWMutex),
700 logger: nil,
701 Logger: "zap",
702 LogFormat: DefaultLoggingFormat,
703 LogOutputs: []string{DefaultLogOutput},
704 LogLevel: logutil.DefaultLogLevel,
705 EnableLogRotation: false,
706 LogRotationConfigJSON: DefaultLogRotationConfig,
707 EnableGRPCGateway: true,
708
709 ExperimentalDowngradeCheckTime: DefaultDowngradeCheckTime,
710 DowngradeCheckTime: DefaultDowngradeCheckTime,
711 MemoryMlock: false,
712 // TODO: delete in v3.7
713 ExperimentalMemoryMlock: false,
714 ExperimentalStopGRPCServiceOnDefrag: false,
715 MaxLearners: membership.DefaultMaxLearners,
716
717 ExperimentalTxnModeWriteWithSharedBuffer: DefaultExperimentalTxnModeWriteWithSharedBuffer,
718 ExperimentalDistributedTracingAddress: DefaultDistributedTracingAddress,
719 DistributedTracingAddress: DefaultDistributedTracingAddress,
720 ExperimentalDistributedTracingServiceName: DefaultDistributedTracingServiceName,
721 DistributedTracingServiceName: DefaultDistributedTracingServiceName,
722
723 CompactHashCheckTime: DefaultCompactHashCheckTime,
724 // TODO: delete in v3.7
725 ExperimentalCompactHashCheckTime: DefaultCompactHashCheckTime,
726
727 V2Deprecation: config.V2DeprDefault,
728
729 DiscoveryCfg: v3discovery.DiscoveryConfig{
730 ConfigSpec: clientv3.ConfigSpec{
731 DialTimeout: DefaultDiscoveryDialTimeout,
732 RequestTimeout: DefaultDiscoveryRequestTimeOut,
733 KeepAliveTime: DefaultDiscoveryKeepAliveTime,
734 KeepAliveTimeout: DefaultDiscoveryKeepAliveTimeOut,
735
736 Secure: &clientv3.SecureConfig{
737 InsecureTransport: true,
738 },
739 Auth: &clientv3.AuthConfig{},
740 },
741 },
742
743 AutoCompactionMode: DefaultAutoCompactionMode,
744 AutoCompactionRetention: DefaultAutoCompactionRetention,
745 ServerFeatureGate: features.NewDefaultServerFeatureGate(DefaultName, nil),
746 FlagsExplicitlySet: map[string]bool{},
747 }
748 cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
749 return cfg
750}
751
752func (cfg *Config) AddFlags(fs *flag.FlagSet) {
753 // member
754 fs.StringVar(&cfg.Dir, "data-dir", cfg.Dir, "Path to the data directory.")
755 fs.StringVar(&cfg.WalDir, "wal-dir", cfg.WalDir, "Path to the dedicated wal directory.")
756 fs.Var(
757 flags.NewUniqueURLsWithExceptions(DefaultListenPeerURLs, ""),
758 "listen-peer-urls",
759 "List of URLs to listen on for peer traffic.",
760 )
761 fs.Var(
762 flags.NewUniqueURLsWithExceptions(DefaultListenClientURLs, ""), "listen-client-urls",
763 "List of URLs to listen on for client grpc traffic and http as long as --listen-client-http-urls is not specified.",
764 )
765 fs.Var(
766 flags.NewUniqueURLsWithExceptions("", ""), "listen-client-http-urls",
767 "List of URLs to listen on for http only client traffic. Enabling this flag removes http services from --listen-client-urls.",
768 )
769 fs.Var(
770 flags.NewUniqueURLsWithExceptions("", ""),
771 "listen-metrics-urls",
772 "List of URLs to listen on for the metrics and health endpoints.",
773 )
774 fs.UintVar(&cfg.MaxSnapFiles, "max-snapshots", cfg.MaxSnapFiles, "Maximum number of snapshot files to retain (0 is unlimited). Deprecated in v3.6 and will be decommissioned in v3.7.")
775 fs.UintVar(&cfg.MaxWalFiles, "max-wals", cfg.MaxWalFiles, "Maximum number of wal files to retain (0 is unlimited).")
776 fs.StringVar(&cfg.Name, "name", cfg.Name, "Human-readable name for this member.")
777 fs.Uint64Var(&cfg.SnapshotCount, "snapshot-count", cfg.SnapshotCount, "Number of committed transactions to trigger a snapshot to disk. Deprecated in v3.6 and will be decommissioned in v3.7.")
778 fs.UintVar(&cfg.TickMs, "heartbeat-interval", cfg.TickMs, "Time (in milliseconds) of a heartbeat interval.")
779 fs.UintVar(&cfg.ElectionMs, "election-timeout", cfg.ElectionMs, "Time (in milliseconds) for an election to timeout.")
780 fs.BoolVar(&cfg.InitialElectionTickAdvance, "initial-election-tick-advance", cfg.InitialElectionTickAdvance, "Whether to fast-forward initial election ticks on boot for faster election.")
781 fs.Int64Var(&cfg.QuotaBackendBytes, "quota-backend-bytes", cfg.QuotaBackendBytes, "Sets the maximum size (in bytes) that the etcd backend database may consume. Exceeding this triggers an alarm and puts etcd in read-only mode. Set to 0 to use the default 2GiB limit.")
782 fs.StringVar(&cfg.BackendFreelistType, "backend-bbolt-freelist-type", cfg.BackendFreelistType, "BackendFreelistType specifies the type of freelist that boltdb backend uses(array and map are supported types)")
783 fs.DurationVar(&cfg.BackendBatchInterval, "backend-batch-interval", cfg.BackendBatchInterval, "BackendBatchInterval is the maximum time before commit the backend transaction.")
784 fs.IntVar(&cfg.BackendBatchLimit, "backend-batch-limit", cfg.BackendBatchLimit, "BackendBatchLimit is the maximum operations before commit the backend transaction.")
785 fs.UintVar(&cfg.MaxTxnOps, "max-txn-ops", cfg.MaxTxnOps, "Maximum number of operations permitted in a transaction.")
786 fs.UintVar(&cfg.MaxRequestBytes, "max-request-bytes", cfg.MaxRequestBytes, "Maximum client request size in bytes the server will accept.")
787 fs.DurationVar(&cfg.GRPCKeepAliveMinTime, "grpc-keepalive-min-time", cfg.GRPCKeepAliveMinTime, "Minimum interval duration that a client should wait before pinging server.")
788 fs.DurationVar(&cfg.GRPCKeepAliveInterval, "grpc-keepalive-interval", cfg.GRPCKeepAliveInterval, "Frequency duration of server-to-client ping to check if a connection is alive (0 to disable).")
789 fs.DurationVar(&cfg.GRPCKeepAliveTimeout, "grpc-keepalive-timeout", cfg.GRPCKeepAliveTimeout, "Additional duration of wait before closing a non-responsive connection (0 to disable).")
790 fs.BoolVar(&cfg.SocketOpts.ReusePort, "socket-reuse-port", cfg.SocketOpts.ReusePort, "Enable to set socket option SO_REUSEPORT on listeners allowing rebinding of a port already in use.")
791 fs.BoolVar(&cfg.SocketOpts.ReuseAddress, "socket-reuse-address", cfg.SocketOpts.ReuseAddress, "Enable to set socket option SO_REUSEADDR on listeners allowing binding to an address in `TIME_WAIT` state.")
792
793 fs.Var(flags.NewUint32Value(cfg.MaxConcurrentStreams), "max-concurrent-streams", "Maximum concurrent streams that each client can open at a time.")
794
795 // raft connection timeouts
796 fs.DurationVar(&rafthttp.ConnReadTimeout, "raft-read-timeout", rafthttp.DefaultConnReadTimeout, "Read timeout set on each rafthttp connection")
797 fs.DurationVar(&rafthttp.ConnWriteTimeout, "raft-write-timeout", rafthttp.DefaultConnWriteTimeout, "Write timeout set on each rafthttp connection")
798
799 // clustering
800 fs.Var(
801 flags.NewUniqueURLsWithExceptions(DefaultInitialAdvertisePeerURLs, ""),
802 "initial-advertise-peer-urls",
803 "List of this member's peer URLs to advertise to the rest of the cluster.",
804 )
805
806 fs.Var(
807 flags.NewUniqueURLsWithExceptions(DefaultAdvertiseClientURLs, ""),
808 "advertise-client-urls",
809 "List of this member's client URLs to advertise to the public.",
810 )
811
812 fs.StringVar(&cfg.Durl, "discovery", cfg.Durl, "Discovery URL used to bootstrap the cluster for v2 discovery. Will be deprecated in v3.7, and be decommissioned in v3.8.")
813
814 fs.Var(
815 flags.NewUniqueStringsValue(""),
816 "discovery-endpoints",
817 "V3 discovery: List of gRPC endpoints of the discovery service.",
818 )
819 fs.StringVar(&cfg.DiscoveryCfg.Token, "discovery-token", "", "V3 discovery: discovery token for the etcd cluster to be bootstrapped.")
820 fs.DurationVar(&cfg.DiscoveryCfg.DialTimeout, "discovery-dial-timeout", cfg.DiscoveryCfg.DialTimeout, "V3 discovery: dial timeout for client connections.")
821 fs.DurationVar(&cfg.DiscoveryCfg.RequestTimeout, "discovery-request-timeout", cfg.DiscoveryCfg.RequestTimeout, "V3 discovery: timeout for discovery requests (excluding dial timeout).")
822 fs.DurationVar(&cfg.DiscoveryCfg.KeepAliveTime, "discovery-keepalive-time", cfg.DiscoveryCfg.KeepAliveTime, "V3 discovery: keepalive time for client connections.")
823 fs.DurationVar(&cfg.DiscoveryCfg.KeepAliveTimeout, "discovery-keepalive-timeout", cfg.DiscoveryCfg.KeepAliveTimeout, "V3 discovery: keepalive timeout for client connections.")
824 fs.BoolVar(&cfg.DiscoveryCfg.Secure.InsecureTransport, "discovery-insecure-transport", true, "V3 discovery: disable transport security for client connections.")
825 fs.BoolVar(&cfg.DiscoveryCfg.Secure.InsecureSkipVerify, "discovery-insecure-skip-tls-verify", false, "V3 discovery: skip server certificate verification (CAUTION: this option should be enabled only for testing purposes).")
826 fs.StringVar(&cfg.DiscoveryCfg.Secure.Cert, "discovery-cert", "", "V3 discovery: identify secure client using this TLS certificate file.")
827 fs.StringVar(&cfg.DiscoveryCfg.Secure.Key, "discovery-key", "", "V3 discovery: identify secure client using this TLS key file.")
828 fs.StringVar(&cfg.DiscoveryCfg.Secure.Cacert, "discovery-cacert", "", "V3 discovery: verify certificates of TLS-enabled secure servers using this CA bundle.")
829 fs.StringVar(&cfg.DiscoveryCfg.Auth.Username, "discovery-user", "", "V3 discovery: username[:password] for authentication (prompt if password is not supplied).")
830 fs.StringVar(&cfg.DiscoveryCfg.Auth.Password, "discovery-password", "", "V3 discovery: password for authentication (if this option is used, --user option shouldn't include password).")
831
832 fs.StringVar(&cfg.Dproxy, "discovery-proxy", cfg.Dproxy, "HTTP proxy to use for traffic to discovery service. Will be deprecated in v3.7, and be decommissioned in v3.8.")
833 fs.StringVar(&cfg.DNSCluster, "discovery-srv", cfg.DNSCluster, "DNS domain used to bootstrap initial cluster.")
834 fs.StringVar(&cfg.DNSClusterServiceName, "discovery-srv-name", cfg.DNSClusterServiceName, "Service name to query when using DNS discovery.")
835 fs.StringVar(&cfg.InitialCluster, "initial-cluster", cfg.InitialCluster, "Initial cluster configuration for bootstrapping.")
836 fs.StringVar(&cfg.InitialClusterToken, "initial-cluster-token", cfg.InitialClusterToken, "Initial cluster token for the etcd cluster during bootstrap.")
837 fs.BoolVar(&cfg.StrictReconfigCheck, "strict-reconfig-check", cfg.StrictReconfigCheck, "Reject reconfiguration requests that would cause quorum loss.")
838
839 fs.BoolVar(&cfg.PreVote, "pre-vote", cfg.PreVote, "Enable the raft Pre-Vote algorithm to prevent disruption when a node that has been partitioned away rejoins the cluster.")
840
841 // security
842 fs.StringVar(&cfg.ClientTLSInfo.CertFile, "cert-file", "", "Path to the client server TLS cert file.")
843 fs.StringVar(&cfg.ClientTLSInfo.KeyFile, "key-file", "", "Path to the client server TLS key file.")
844 fs.StringVar(&cfg.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.")
845 fs.StringVar(&cfg.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.")
846 fs.BoolVar(&cfg.ClientTLSInfo.ClientCertAuth, "client-cert-auth", false, "Enable client cert authentication.")
847 fs.StringVar(&cfg.ClientTLSInfo.CRLFile, "client-crl-file", "", "Path to the client certificate revocation list file.")
848 fs.Var(flags.NewStringsValue(""), "client-cert-allowed-hostname", "Comma-separated list of allowed SAN hostnames for client cert authentication.")
849 fs.StringVar(&cfg.ClientTLSInfo.TrustedCAFile, "trusted-ca-file", "", "Path to the client server TLS trusted CA cert file.")
850 fs.BoolVar(&cfg.ClientAutoTLS, "auto-tls", false, "Client TLS using generated certificates")
851 fs.StringVar(&cfg.PeerTLSInfo.CertFile, "peer-cert-file", "", "Path to the peer server TLS cert file.")
852 fs.StringVar(&cfg.PeerTLSInfo.KeyFile, "peer-key-file", "", "Path to the peer server TLS key file.")
853 fs.StringVar(&cfg.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.")
854 fs.StringVar(&cfg.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.")
855 fs.BoolVar(&cfg.PeerTLSInfo.ClientCertAuth, "peer-client-cert-auth", false, "Enable peer client cert authentication.")
856 fs.StringVar(&cfg.PeerTLSInfo.TrustedCAFile, "peer-trusted-ca-file", "", "Path to the peer server TLS trusted CA file.")
857 fs.BoolVar(&cfg.PeerAutoTLS, "peer-auto-tls", false, "Peer TLS using generated certificates")
858 fs.UintVar(&cfg.SelfSignedCertValidity, "self-signed-cert-validity", 1, "The validity period of the client and peer certificates, unit is year")
859 fs.StringVar(&cfg.PeerTLSInfo.CRLFile, "peer-crl-file", "", "Path to the peer certificate revocation list file.")
860 fs.Var(flags.NewStringsValue(""), "peer-cert-allowed-cn", "Comma-separated list of allowed CNs for inter-peer TLS authentication.")
861 fs.Var(flags.NewStringsValue(""), "peer-cert-allowed-hostname", "Comma-separated list of allowed SAN hostnames for inter-peer TLS authentication.")
862 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).")
863 fs.BoolVar(&cfg.ExperimentalPeerSkipClientSanVerification, "experimental-peer-skip-client-san-verification", false, "Skip verification of SAN field in client certificate for peer connections.Deprecated in v3.6 and will be decommissioned in v3.7. Use peer-skip-client-san-verification instead")
864 fs.BoolVar(&cfg.PeerTLSInfo.SkipClientSANVerify, "peer-skip-client-san-verification", false, "Skip verification of SAN field in client certificate for peer connections.")
865 fs.StringVar(&cfg.TlsMinVersion, "tls-min-version", string(tlsutil.TLSVersion12), "Minimum TLS version supported by etcd. Possible values: TLS1.2, TLS1.3.")
866 fs.StringVar(&cfg.TlsMaxVersion, "tls-max-version", string(tlsutil.TLSVersionDefault), "Maximum TLS version supported by etcd. Possible values: TLS1.2, TLS1.3 (empty defers to Go).")
867
868 fs.Var(
869 flags.NewUniqueURLsWithExceptions("*", "*"),
870 "cors",
871 "Comma-separated white list of origins for CORS, or cross-origin resource sharing, (empty or * means allow all)",
872 )
873 fs.Var(flags.NewUniqueStringsValue("*"), "host-whitelist", "Comma-separated acceptable hostnames from HTTP client requests, if server is not secure (empty means allow all).")
874
875 // logging
876 fs.StringVar(&cfg.Logger, "logger", "zap", "Currently only supports 'zap' for structured logging.")
877 fs.Var(flags.NewUniqueStringsValue(DefaultLogOutput), "log-outputs", "Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd, or list of comma separated output targets.")
878 fs.StringVar(&cfg.LogLevel, "log-level", logutil.DefaultLogLevel, "Configures log level. Only supports debug, info, warn, error, panic, or fatal. Default 'info'.")
879 fs.StringVar(&cfg.LogFormat, "log-format", logutil.DefaultLogFormat, "Configures log format. Only supports json, console. Default is 'json'.")
880 fs.BoolVar(&cfg.EnableLogRotation, "enable-log-rotation", false, "Enable log rotation of a single log-outputs file target.")
881 fs.StringVar(&cfg.LogRotationConfigJSON, "log-rotation-config-json", 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)")
882
883 fs.StringVar(&cfg.AutoCompactionRetention, "auto-compaction-retention", "0", "Auto compaction retention for mvcc key value store. 0 means disable auto compaction.")
884 fs.StringVar(&cfg.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.")
885
886 // pprof profiler via HTTP
887 fs.BoolVar(&cfg.EnablePprof, "enable-pprof", false, "Enable runtime profiling data via HTTP server. Address is at client URL + \"/debug/pprof/\"")
888
889 // additional metrics
890 fs.StringVar(&cfg.Metrics, "metrics", cfg.Metrics, "Set level of detail for exported metrics, specify 'extensive' to include server side grpc histogram metrics")
891
892 // experimental distributed tracing
893 fs.BoolVar(&cfg.ExperimentalEnableDistributedTracing, "experimental-enable-distributed-tracing", false, "Enable experimental distributed tracing using OpenTelemetry Tracing. Deprecated in v3.6 and will be decommissioned in v3.7. Use --enable-distributed-tracing instead.")
894 fs.BoolVar(&cfg.EnableDistributedTracing, "enable-distributed-tracing", false, "Enable distributed tracing using OpenTelemetry Tracing.")
895
896 fs.StringVar(&cfg.ExperimentalDistributedTracingAddress, "experimental-distributed-tracing-address", cfg.ExperimentalDistributedTracingAddress, "Address for distributed tracing used for OpenTelemetry Tracing (if enabled with experimental-enable-distributed-tracing flag). Deprecated in v3.6 and will be decommissioned in v3.7. Use --distributed-tracing-address instead.")
897 fs.StringVar(&cfg.DistributedTracingAddress, "distributed-tracing-address", cfg.DistributedTracingAddress, "Address for distributed tracing used for OpenTelemetry Tracing (if enabled with enable-distributed-tracing flag).")
898
899 fs.StringVar(&cfg.ExperimentalDistributedTracingServiceName, "experimental-distributed-tracing-service-name", cfg.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. Deprecated in v3.6 and will be decommissioned in v3.7. Use --distributed-tracing-service-name instead.")
900 fs.StringVar(&cfg.DistributedTracingServiceName, "distributed-tracing-service-name", cfg.DistributedTracingServiceName, "Configures service name for distributed tracing to be used to define service name for OpenTelemetry Tracing (if enabled with enable-distributed-tracing flag). 'etcd' is the default service name. Use the same service name for all instances of etcd.")
901
902 fs.StringVar(&cfg.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. Deprecated in v3.6 and will be decommissioned in v3.7. Use --distributed-tracing-instance-id instead.")
903 fs.StringVar(&cfg.DistributedTracingServiceInstanceID, "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 enable-distributed-tracing flag). There is no default value set. This ID must be unique per etcd instance.")
904
905 fs.IntVar(&cfg.ExperimentalDistributedTracingSamplingRatePerMillion, "experimental-distributed-tracing-sampling-rate", 0, "Number of samples to collect per million spans for OpenTelemetry Tracing (if enabled with experimental-enable-distributed-tracing flag). Deprecated in v3.6 and will be decommissioned in v3.7. Use --distributed-tracing-sampling-rate instead.")
906 fs.IntVar(&cfg.DistributedTracingSamplingRatePerMillion, "distributed-tracing-sampling-rate", 0, "Number of samples to collect per million spans for OpenTelemetry Tracing (if enabled with enable-distributed-tracing flag).")
907
908 // auth
909 fs.StringVar(&cfg.AuthToken, "auth-token", cfg.AuthToken, "Specify auth token specific options.")
910 fs.UintVar(&cfg.BcryptCost, "bcrypt-cost", cfg.BcryptCost, "Specify bcrypt algorithm cost factor for auth password hashing.")
911 fs.UintVar(&cfg.AuthTokenTTL, "auth-token-ttl", cfg.AuthTokenTTL, "The lifetime in seconds of the auth token.")
912
913 // gateway
914 fs.BoolVar(&cfg.EnableGRPCGateway, "enable-grpc-gateway", cfg.EnableGRPCGateway, "Enable GRPC gateway.")
915
916 // experimental
917 fs.BoolVar(&cfg.ExperimentalInitialCorruptCheck, "experimental-initial-corrupt-check", cfg.ExperimentalInitialCorruptCheck, "Enable to check data corruption before serving any client/peer traffic.")
918 // TODO: delete in v3.7
919 fs.DurationVar(&cfg.ExperimentalCorruptCheckTime, "experimental-corrupt-check-time", cfg.ExperimentalCorruptCheckTime, "Duration of time between cluster corruption check passes. Deprecated in v3.6 and will be decommissioned in v3.7. Use --corrupt-check-time instead")
920 fs.DurationVar(&cfg.CorruptCheckTime, "corrupt-check-time", cfg.CorruptCheckTime, "Duration of time between cluster corruption check passes.")
921 // TODO: delete in v3.7
922 fs.BoolVar(&cfg.ExperimentalCompactHashCheckEnabled, "experimental-compact-hash-check-enabled", cfg.ExperimentalCompactHashCheckEnabled, "Enable leader to periodically check followers compaction hashes. Deprecated in v3.6 and will be decommissioned in v3.7. Use '--feature-gates=CompactHashCheck=true' instead")
923 fs.DurationVar(&cfg.ExperimentalCompactHashCheckTime, "experimental-compact-hash-check-time", cfg.ExperimentalCompactHashCheckTime, "Duration of time between leader checks followers compaction hashes. Deprecated in v3.6 and will be decommissioned in v3.7. Use --compact-hash-check-time instead.")
924
925 fs.DurationVar(&cfg.CompactHashCheckTime, "compact-hash-check-time", cfg.CompactHashCheckTime, "Duration of time between leader checks followers compaction hashes.")
926
927 fs.BoolVar(&cfg.ExperimentalEnableLeaseCheckpoint, "experimental-enable-lease-checkpoint", false, "Enable leader to send regular checkpoints to other members to prevent reset of remaining TTL on leader change.")
928 // TODO: delete in v3.7
929 fs.BoolVar(&cfg.ExperimentalEnableLeaseCheckpointPersist, "experimental-enable-lease-checkpoint-persist", false, "Enable persisting remainingTTL to prevent indefinite auto-renewal of long lived leases. Always enabled in v3.6. Should be used to ensure smooth upgrade from v3.5 clusters with this feature enabled. Requires experimental-enable-lease-checkpoint to be enabled.")
930 // TODO: delete in v3.7
931 fs.IntVar(&cfg.ExperimentalCompactionBatchLimit, "experimental-compaction-batch-limit", cfg.ExperimentalCompactionBatchLimit, "Sets the maximum revisions deleted in each compaction batch. Deprecated in v3.6 and will be decommissioned in v3.7. Use --compaction-batch-limit instead.")
932 fs.IntVar(&cfg.CompactionBatchLimit, "compaction-batch-limit", cfg.CompactionBatchLimit, "Sets the maximum revisions deleted in each compaction batch.")
933 fs.DurationVar(&cfg.ExperimentalCompactionSleepInterval, "experimental-compaction-sleep-interval", cfg.ExperimentalCompactionSleepInterval, "Sets the sleep interval between each compaction batch. Deprecated in v3.6 and will be decommissioned in v3.7. Use --compaction-sleep-interval instead.")
934 fs.DurationVar(&cfg.CompactionSleepInterval, "compaction-sleep-interval", cfg.CompactionSleepInterval, "Sets the sleep interval between each compaction batch.")
935 // TODO: delete in v3.7
936 fs.DurationVar(&cfg.ExperimentalWatchProgressNotifyInterval, "experimental-watch-progress-notify-interval", cfg.ExperimentalWatchProgressNotifyInterval, "Duration of periodic watch progress notifications. Deprecated in v3.6 and will be decommissioned in v3.7. Use --watch-progress-notify-interval instead.")
937 fs.DurationVar(&cfg.WatchProgressNotifyInterval, "watch-progress-notify-interval", cfg.WatchProgressNotifyInterval, "Duration of periodic watch progress notifications.")
938 fs.DurationVar(&cfg.DowngradeCheckTime, "downgrade-check-time", cfg.DowngradeCheckTime, "Duration of time between two downgrade status checks.")
939 // TODO: delete in v3.7
940 fs.DurationVar(&cfg.ExperimentalDowngradeCheckTime, "experimental-downgrade-check-time", cfg.ExperimentalDowngradeCheckTime, "Duration of time between two downgrade status checks. Deprecated in v3.6 and will be decommissioned in v3.7. Use --downgrade-check-time instead.")
941 // TODO: delete in v3.7
942 fs.DurationVar(&cfg.ExperimentalWarningApplyDuration, "experimental-warning-apply-duration", cfg.ExperimentalWarningApplyDuration, "Time duration after which a warning is generated if request takes more time. Deprecated in v3.6 and will be decommissioned in v3.7. Use --warning-watch-progress-duration instead.")
943 fs.DurationVar(&cfg.WarningApplyDuration, "warning-apply-duration", cfg.WarningApplyDuration, "Time duration after which a warning is generated if watch progress takes more time.")
944 fs.DurationVar(&cfg.WarningUnaryRequestDuration, "warning-unary-request-duration", cfg.WarningUnaryRequestDuration, "Time duration after which a warning is generated if a unary request takes more time.")
945 fs.DurationVar(&cfg.ExperimentalWarningUnaryRequestDuration, "experimental-warning-unary-request-duration", cfg.ExperimentalWarningUnaryRequestDuration, "Time duration after which a warning is generated if a unary request takes more time. It's deprecated, and will be decommissioned in v3.7. Use --warning-unary-request-duration instead.")
946 // TODO: delete in v3.7
947 fs.BoolVar(&cfg.ExperimentalMemoryMlock, "experimental-memory-mlock", cfg.ExperimentalMemoryMlock, "Enable to enforce etcd pages (in particular bbolt) to stay in RAM.")
948 fs.BoolVar(&cfg.MemoryMlock, "memory-mlock", cfg.MemoryMlock, "Enable to enforce etcd pages (in particular bbolt) to stay in RAM.")
949 fs.BoolVar(&cfg.ExperimentalTxnModeWriteWithSharedBuffer, "experimental-txn-mode-write-with-shared-buffer", true, "Enable the write transaction to use a shared buffer in its readonly check operations.")
950 fs.BoolVar(&cfg.ExperimentalStopGRPCServiceOnDefrag, "experimental-stop-grpc-service-on-defrag", cfg.ExperimentalStopGRPCServiceOnDefrag, "Enable etcd gRPC service to stop serving client requests on defragmentation.")
951 // TODO: delete in v3.7
952 fs.UintVar(&cfg.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. It's deprecated, and will be decommissioned in v3.7. Use --bootstrap-defrag-threshold-megabytes instead.")
953 fs.UintVar(&cfg.BootstrapDefragThresholdMegabytes, "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.")
954 // TODO: delete in v3.7
955 fs.IntVar(&cfg.MaxLearners, "max-learners", membership.DefaultMaxLearners, "Sets the maximum number of learners that can be available in the cluster membership.")
956 fs.Uint64Var(&cfg.ExperimentalSnapshotCatchUpEntries, "experimental-snapshot-catchup-entries", cfg.ExperimentalSnapshotCatchUpEntries, "Number of entries for a slow follower to catch up after compacting the raft storage entries. Deprecated in v3.6 and will be decommissioned in v3.7. Use --snapshot-catchup-entries instead.")
957 fs.Uint64Var(&cfg.SnapshotCatchUpEntries, "snapshot-catchup-entries", cfg.SnapshotCatchUpEntries, "Number of entries for a slow follower to catch up after compacting the raft storage entries.")
958
959 // unsafe
960 fs.BoolVar(&cfg.UnsafeNoFsync, "unsafe-no-fsync", false, "Disables fsync, unsafe, will cause data loss.")
961 fs.BoolVar(&cfg.ForceNewCluster, "force-new-cluster", false, "Force to create a new one member cluster.")
962
963 // featuregate
964 cfg.ServerFeatureGate.(featuregate.MutableFeatureGate).AddFlag(fs, ServerFeatureGateFlagName)
965}
966
967func ConfigFromFile(path string) (*Config, error) {
968 cfg := &configYAML{Config: *NewConfig()}
969 if err := cfg.configFromFile(path); err != nil {
970 return nil, err
971 }
972 return &cfg.Config, nil
973}
974
975func (cfg *configYAML) configFromFile(path string) error {
976 b, err := os.ReadFile(path)
977 if err != nil {
978 return err
979 }
980
981 defaultInitialCluster := cfg.InitialCluster
982
983 err = yaml.Unmarshal(b, cfg)
984 if err != nil {
985 return err
986 }
987
988 if cfg.configJSON.ServerFeatureGatesJSON != "" {
989 err = cfg.Config.ServerFeatureGate.(featuregate.MutableFeatureGate).Set(cfg.configJSON.ServerFeatureGatesJSON)
990 if err != nil {
991 return err
992 }
993 }
994
995 // parses the yaml bytes to raw map first, then getBoolFlagVal can get the top level bool flag value.
996 var cfgMap map[string]any
997 err = yaml.Unmarshal(b, &cfgMap)
998 if err != nil {
999 return err
1000 }
1001
1002 for flg := range cfgMap {
1003 cfg.FlagsExplicitlySet[flg] = true
1004 }
1005
1006 if peerTransportSecurity, ok := cfgMap["peer-transport-security"]; ok {
1007 peerTransportSecurityMap, isMap := peerTransportSecurity.(map[string]any)
1008 if !isMap {
1009 return fmt.Errorf("invalid peer-transport-security")
1010 }
1011 for k := range peerTransportSecurityMap {
1012 cfg.FlagsExplicitlySet[fmt.Sprintf("peer-%s", k)] = true
1013 }
1014 }
1015
1016 // attempt to fix a bug introduced in https://github.com/etcd-io/etcd/pull/15033
1017 // both `experimental-snapshot-catch-up-entries` and `experimental-snapshot-catchup-entries` refer to the same field,
1018 // map the YAML field "experimental-snapshot-catch-up-entries" to the flag "experimental-snapshot-catchup-entries".
1019 if val, ok := cfgMap["experimental-snapshot-catch-up-entries"]; ok {
1020 cfgMap["experimental-snapshot-catchup-entries"] = val
1021 cfg.ExperimentalSnapshotCatchUpEntries = uint64(val.(float64))
1022 cfg.FlagsExplicitlySet["experimental-snapshot-catchup-entries"] = true
1023 }
1024
1025 getBoolFlagVal := func(flagName string) *bool {
1026 flagVal, ok := cfgMap[flagName]
1027 if !ok {
1028 return nil
1029 }
1030 boolVal := flagVal.(bool)
1031 return &boolVal
1032 }
1033 err = SetFeatureGatesFromExperimentalFlags(cfg.ServerFeatureGate, getBoolFlagVal, cfg.configJSON.ServerFeatureGatesJSON)
1034 if err != nil {
1035 return err
1036 }
1037
1038 if cfg.configJSON.ListenPeerURLs != "" {
1039 u, err := types.NewURLs(strings.Split(cfg.configJSON.ListenPeerURLs, ","))
1040 if err != nil {
1041 fmt.Fprintf(os.Stderr, "unexpected error setting up listen-peer-urls: %v\n", err)
1042 os.Exit(1)
1043 }
1044 cfg.Config.ListenPeerUrls = u
1045 }
1046
1047 if cfg.configJSON.ListenClientURLs != "" {
1048 u, err := types.NewURLs(strings.Split(cfg.configJSON.ListenClientURLs, ","))
1049 if err != nil {
1050 fmt.Fprintf(os.Stderr, "unexpected error setting up listen-client-urls: %v\n", err)
1051 os.Exit(1)
1052 }
1053 cfg.Config.ListenClientUrls = u
1054 }
1055
1056 if cfg.configJSON.ListenClientHTTPURLs != "" {
1057 u, err := types.NewURLs(strings.Split(cfg.configJSON.ListenClientHTTPURLs, ","))
1058 if err != nil {
1059 fmt.Fprintf(os.Stderr, "unexpected error setting up listen-client-http-urls: %v\n", err)
1060 os.Exit(1)
1061 }
1062 cfg.Config.ListenClientHttpUrls = u
1063 }
1064
1065 if cfg.configJSON.AdvertisePeerURLs != "" {
1066 u, err := types.NewURLs(strings.Split(cfg.configJSON.AdvertisePeerURLs, ","))
1067 if err != nil {
1068 fmt.Fprintf(os.Stderr, "unexpected error setting up initial-advertise-peer-urls: %v\n", err)
1069 os.Exit(1)
1070 }
1071 cfg.Config.AdvertisePeerUrls = u
1072 }
1073
1074 if cfg.configJSON.AdvertiseClientURLs != "" {
1075 u, err := types.NewURLs(strings.Split(cfg.configJSON.AdvertiseClientURLs, ","))
1076 if err != nil {
1077 fmt.Fprintf(os.Stderr, "unexpected error setting up advertise-peer-urls: %v\n", err)
1078 os.Exit(1)
1079 }
1080 cfg.Config.AdvertiseClientUrls = u
1081 }
1082
1083 if cfg.ListenMetricsUrlsJSON != "" {
1084 u, err := types.NewURLs(strings.Split(cfg.ListenMetricsUrlsJSON, ","))
1085 if err != nil {
1086 fmt.Fprintf(os.Stderr, "unexpected error setting up listen-metrics-urls: %v\n", err)
1087 os.Exit(1)
1088 }
1089 cfg.ListenMetricsUrls = u
1090 }
1091
1092 if cfg.CORSJSON != "" {
1093 uv := flags.NewUniqueURLsWithExceptions(cfg.CORSJSON, "*")
1094 cfg.CORS = uv.Values
1095 }
1096
1097 if cfg.HostWhitelistJSON != "" {
1098 uv := flags.NewUniqueStringsValue(cfg.HostWhitelistJSON)
1099 cfg.HostWhitelist = uv.Values
1100 }
1101
1102 // If a discovery or discovery-endpoints flag is set, clear default initial cluster set by InitialClusterFromName
1103 if (cfg.Durl != "" || cfg.DNSCluster != "" || len(cfg.DiscoveryCfg.Endpoints) > 0) && cfg.InitialCluster == defaultInitialCluster {
1104 cfg.InitialCluster = ""
1105 }
1106 if cfg.ClusterState == "" {
1107 cfg.ClusterState = ClusterStateFlagNew
1108 }
1109
1110 copySecurityDetails := func(tls *transport.TLSInfo, ysc *securityConfig) {
1111 tls.CertFile = ysc.CertFile
1112 tls.KeyFile = ysc.KeyFile
1113 tls.ClientCertFile = ysc.ClientCertFile
1114 tls.ClientKeyFile = ysc.ClientKeyFile
1115 tls.ClientCertAuth = ysc.CertAuth
1116 tls.TrustedCAFile = ysc.TrustedCAFile
1117 tls.AllowedCNs = ysc.AllowedCNs
1118 tls.AllowedHostnames = ysc.AllowedHostnames
1119 tls.SkipClientSANVerify = ysc.SkipClientSANVerify
1120 }
1121 copySecurityDetails(&cfg.ClientTLSInfo, &cfg.ClientSecurityJSON)
1122 copySecurityDetails(&cfg.PeerTLSInfo, &cfg.PeerSecurityJSON)
1123 cfg.ClientAutoTLS = cfg.ClientSecurityJSON.AutoTLS
1124 cfg.PeerAutoTLS = cfg.PeerSecurityJSON.AutoTLS
1125 if cfg.SelfSignedCertValidity == 0 {
1126 cfg.SelfSignedCertValidity = 1
1127 }
1128 return cfg.Validate()
1129}
1130
1131// SetFeatureGatesFromExperimentalFlags sets the feature gate values if the feature gate is not explicitly set
1132// while their corresponding experimental flags are explicitly set, for all the features in ExperimentalFlagToFeatureMap.
1133// TODO: remove after all experimental flags are deprecated.
1134func SetFeatureGatesFromExperimentalFlags(fg featuregate.FeatureGate, getExperimentalFlagVal func(string) *bool, featureGatesVal string) error {
1135 m := make(map[featuregate.Feature]bool)
1136 // verify that the feature gate and its experimental flag are not both set at the same time.
1137 for expFlagName, featureName := range features.ExperimentalFlagToFeatureMap {
1138 flagVal := getExperimentalFlagVal(expFlagName)
1139 if flagVal == nil {
1140 continue
1141 }
1142 if strings.Contains(featureGatesVal, string(featureName)) {
1143 return fmt.Errorf("cannot specify both flags: --%s=%v and --%s=%s=%v at the same time, please just use --%s=%s=%v",
1144 expFlagName, *flagVal, ServerFeatureGateFlagName, featureName, fg.Enabled(featureName), ServerFeatureGateFlagName, featureName, fg.Enabled(featureName))
1145 }
1146 m[featureName] = *flagVal
1147 }
1148
1149 // filter out unknown features for fg, because we could use SetFeatureGatesFromExperimentalFlags both for
1150 // server and cluster level feature gates.
1151 allFeatures := fg.(featuregate.MutableFeatureGate).GetAll()
1152 mFiltered := make(map[string]bool)
1153 for k, v := range m {
1154 if _, ok := allFeatures[k]; ok {
1155 mFiltered[string(k)] = v
1156 }
1157 }
1158 return fg.(featuregate.MutableFeatureGate).SetFromMap(mFiltered)
1159}
1160
1161func updateCipherSuites(tls *transport.TLSInfo, ss []string) error {
1162 if len(tls.CipherSuites) > 0 && len(ss) > 0 {
1163 return fmt.Errorf("TLSInfo.CipherSuites is already specified (given %v)", ss)
1164 }
1165 if len(ss) > 0 {
1166 cs, err := tlsutil.GetCipherSuites(ss)
1167 if err != nil {
1168 return err
1169 }
1170 tls.CipherSuites = cs
1171 }
1172 return nil
1173}
1174
1175func updateMinMaxVersions(info *transport.TLSInfo, min, max string) {
1176 // Validate() has been called to check the user input, so it should never fail.
1177 var err error
1178 if info.MinVersion, err = tlsutil.GetTLSVersion(min); err != nil {
1179 panic(err)
1180 }
1181 if info.MaxVersion, err = tlsutil.GetTLSVersion(max); err != nil {
1182 panic(err)
1183 }
1184}
1185
1186// Validate ensures that '*embed.Config' fields are properly configured.
1187func (cfg *Config) Validate() error {
1188 // make sure there is no conflict in the flag settings in the ExperimentalNonBoolFlagMigrationMap
1189 // TODO: delete in v3.7
1190 for oldFlag, newFlag := range experimentalFlagMigrationMap {
1191 if cfg.FlagsExplicitlySet[oldFlag] && cfg.FlagsExplicitlySet[newFlag] {
1192 return fmt.Errorf("cannot set --%s and --%s at the same time, please use --%s only", oldFlag, newFlag, newFlag)
1193 }
1194 }
1195
1196 if err := cfg.setupLogging(); err != nil {
1197 return err
1198 }
1199 if err := checkBindURLs(cfg.ListenPeerUrls); err != nil {
1200 return err
1201 }
1202 if err := checkBindURLs(cfg.ListenClientUrls); err != nil {
1203 return err
1204 }
1205 if err := checkBindURLs(cfg.ListenClientHttpUrls); err != nil {
1206 return err
1207 }
1208 if len(cfg.ListenClientHttpUrls) == 0 {
1209 cfg.logger.Warn("Running http and grpc server on single port. This is not recommended for production.")
1210 }
1211 if err := checkBindURLs(cfg.ListenMetricsUrls); err != nil {
1212 return err
1213 }
1214 if err := checkHostURLs(cfg.AdvertisePeerUrls); err != nil {
1215 addrs := cfg.getAdvertisePeerURLs()
1216 return fmt.Errorf(`--initial-advertise-peer-urls %q must be "host:port" (%w)`, strings.Join(addrs, ","), err)
1217 }
1218 if err := checkHostURLs(cfg.AdvertiseClientUrls); err != nil {
1219 addrs := cfg.getAdvertiseClientURLs()
1220 return fmt.Errorf(`--advertise-client-urls %q must be "host:port" (%w)`, strings.Join(addrs, ","), err)
1221 }
1222 // Check if conflicting flags are passed.
1223 nSet := 0
1224 for _, v := range []bool{cfg.Durl != "", cfg.InitialCluster != "", cfg.DNSCluster != "", len(cfg.DiscoveryCfg.Endpoints) > 0} {
1225 if v {
1226 nSet++
1227 }
1228 }
1229
1230 if cfg.ClusterState != ClusterStateFlagNew && cfg.ClusterState != ClusterStateFlagExisting {
1231 return fmt.Errorf("unexpected clusterState %q", cfg.ClusterState)
1232 }
1233
1234 if nSet > 1 {
1235 return ErrConflictBootstrapFlags
1236 }
1237
1238 // Check if both v2 discovery and v3 discovery flags are passed.
1239 v2discoveryFlagsExist := cfg.Dproxy != ""
1240 v3discoveryFlagsExist := len(cfg.DiscoveryCfg.Endpoints) > 0 ||
1241 cfg.DiscoveryCfg.Token != "" ||
1242 cfg.DiscoveryCfg.Secure.Cert != "" ||
1243 cfg.DiscoveryCfg.Secure.Key != "" ||
1244 cfg.DiscoveryCfg.Secure.Cacert != "" ||
1245 cfg.DiscoveryCfg.Auth.Username != "" ||
1246 cfg.DiscoveryCfg.Auth.Password != ""
1247
1248 if v2discoveryFlagsExist && v3discoveryFlagsExist {
1249 return errors.New("both v2 discovery settings (discovery, discovery-proxy) " +
1250 "and v3 discovery settings (discovery-token, discovery-endpoints, discovery-cert, " +
1251 "discovery-key, discovery-cacert, discovery-user, discovery-password) are set")
1252 }
1253
1254 // If one of `discovery-token` and `discovery-endpoints` is provided,
1255 // then the other one must be provided as well.
1256 if (cfg.DiscoveryCfg.Token != "") != (len(cfg.DiscoveryCfg.Endpoints) > 0) {
1257 return errors.New("both --discovery-token and --discovery-endpoints must be set")
1258 }
1259
1260 for _, ep := range cfg.DiscoveryCfg.Endpoints {
1261 if strings.TrimSpace(ep) == "" {
1262 return errors.New("--discovery-endpoints must not contain empty endpoints")
1263 }
1264 }
1265
1266 if cfg.TickMs == 0 {
1267 return fmt.Errorf("--heartbeat-interval must be >0 (set to %dms)", cfg.TickMs)
1268 }
1269 if cfg.ElectionMs == 0 {
1270 return fmt.Errorf("--election-timeout must be >0 (set to %dms)", cfg.ElectionMs)
1271 }
1272 if 5*cfg.TickMs > cfg.ElectionMs {
1273 return fmt.Errorf("--election-timeout[%vms] should be at least as 5 times as --heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
1274 }
1275 if cfg.ElectionMs > maxElectionMs {
1276 return fmt.Errorf("--election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs)
1277 }
1278
1279 // check this last since proxying in etcdmain may make this OK
1280 if cfg.ListenClientUrls != nil && cfg.AdvertiseClientUrls == nil {
1281 return ErrUnsetAdvertiseClientURLsFlag
1282 }
1283
1284 switch cfg.AutoCompactionMode {
1285 case CompactorModeRevision, CompactorModePeriodic:
1286 case "":
1287 return errors.New("undefined auto-compaction-mode")
1288 default:
1289 return fmt.Errorf("unknown auto-compaction-mode %q", cfg.AutoCompactionMode)
1290 }
1291
1292 // Validate distributed tracing configuration but only if enabled.
1293 if cfg.EnableDistributedTracing {
1294 if err := validateTracingConfig(cfg.DistributedTracingSamplingRatePerMillion); err != nil {
1295 return fmt.Errorf("distributed tracing configurition is not valid: (%w)", err)
1296 }
1297 }
1298
1299 if !cfg.ServerFeatureGate.Enabled(features.LeaseCheckpointPersist) && cfg.ServerFeatureGate.Enabled(features.LeaseCheckpoint) {
1300 cfg.logger.Warn("Detected that checkpointing is enabled without persistence. Consider enabling feature gate LeaseCheckpointPersist")
1301 }
1302
1303 if cfg.ServerFeatureGate.Enabled(features.LeaseCheckpointPersist) && !cfg.ServerFeatureGate.Enabled(features.LeaseCheckpoint) {
1304 return fmt.Errorf("enabling feature gate LeaseCheckpointPersist requires enabling feature gate LeaseCheckpoint")
1305 }
1306 // TODO: delete in v3.7
1307 if cfg.ExperimentalCompactHashCheckTime <= 0 {
1308 return fmt.Errorf("--experimental-compact-hash-check-time must be >0 (set to %v)", cfg.ExperimentalCompactHashCheckTime)
1309 }
1310 if cfg.CompactHashCheckTime <= 0 {
1311 return fmt.Errorf("--compact-hash-check-time must be >0 (set to %v)", cfg.CompactHashCheckTime)
1312 }
1313
1314 // If `--name` isn't configured, then multiple members may have the same "default" name.
1315 // When adding a new member with the "default" name as well, etcd may regards its peerURL
1316 // as one additional peerURL of the existing member which has the same "default" name,
1317 // because each member can have multiple client or peer URLs.
1318 // Please refer to https://github.com/etcd-io/etcd/issues/13757
1319 if cfg.Name == DefaultName {
1320 cfg.logger.Warn(
1321 "it isn't recommended to use default name, please set a value for --name. "+
1322 "Note that etcd might run into issue when multiple members have the same default name",
1323 zap.String("name", cfg.Name))
1324 }
1325
1326 minVersion, err := tlsutil.GetTLSVersion(cfg.TlsMinVersion)
1327 if err != nil {
1328 return err
1329 }
1330 maxVersion, err := tlsutil.GetTLSVersion(cfg.TlsMaxVersion)
1331 if err != nil {
1332 return err
1333 }
1334
1335 // maxVersion == 0 means that Go selects the highest available version.
1336 if maxVersion != 0 && minVersion > maxVersion {
1337 return fmt.Errorf("min version (%s) is greater than max version (%s)", cfg.TlsMinVersion, cfg.TlsMaxVersion)
1338 }
1339
1340 // Check if user attempted to configure ciphers for TLS1.3 only: Go does not support that currently.
1341 if minVersion == tls.VersionTLS13 && len(cfg.CipherSuites) > 0 {
1342 return fmt.Errorf("cipher suites cannot be configured when only TLS1.3 is enabled")
1343 }
1344
1345 return nil
1346}
1347
1348// PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.
1349func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error) {
1350 token = cfg.InitialClusterToken
1351 switch {
1352 case cfg.Durl != "":
1353 urlsmap = types.URLsMap{}
1354 // If using v2 discovery, generate a temporary cluster based on
1355 // self's advertised peer URLs
1356 urlsmap[cfg.Name] = cfg.AdvertisePeerUrls
1357 token = cfg.Durl
1358
1359 case len(cfg.DiscoveryCfg.Endpoints) > 0:
1360 urlsmap = types.URLsMap{}
1361 // If using v3 discovery, generate a temporary cluster based on
1362 // self's advertised peer URLs
1363 urlsmap[cfg.Name] = cfg.AdvertisePeerUrls
1364 token = cfg.DiscoveryCfg.Token
1365
1366 case cfg.DNSCluster != "":
1367 clusterStrs, cerr := cfg.GetDNSClusterNames()
1368 lg := cfg.logger
1369 if cerr != nil {
1370 lg.Warn("failed to resolve during SRV discovery", zap.Error(cerr))
1371 }
1372 if len(clusterStrs) == 0 {
1373 return nil, "", cerr
1374 }
1375 for _, s := range clusterStrs {
1376 lg.Info("got bootstrap from DNS for etcd-server", zap.String("node", s))
1377 }
1378 clusterStr := strings.Join(clusterStrs, ",")
1379 if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.TrustedCAFile == "" {
1380 cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
1381 }
1382 urlsmap, err = types.NewURLsMap(clusterStr)
1383 // only etcd member must belong to the discovered cluster.
1384 // proxy does not need to belong to the discovered cluster.
1385 if which == "etcd" {
1386 if _, ok := urlsmap[cfg.Name]; !ok {
1387 return nil, "", fmt.Errorf("cannot find local etcd member %q in SRV records", cfg.Name)
1388 }
1389 }
1390
1391 default:
1392 // We're statically configured, and cluster has appropriately been set.
1393 urlsmap, err = types.NewURLsMap(cfg.InitialCluster)
1394 }
1395 return urlsmap, token, err
1396}
1397
1398// GetDNSClusterNames uses DNS SRV records to get a list of initial nodes for cluster bootstrapping.
1399// This function will return a list of one or more nodes, as well as any errors encountered while
1400// performing service discovery.
1401// Note: Because this checks multiple sets of SRV records, discovery should only be considered to have
1402// failed if the returned node list is empty.
1403func (cfg *Config) GetDNSClusterNames() ([]string, error) {
1404 var (
1405 clusterStrs []string
1406 cerr error
1407 serviceNameSuffix string
1408 )
1409 if cfg.DNSClusterServiceName != "" {
1410 serviceNameSuffix = "-" + cfg.DNSClusterServiceName
1411 }
1412
1413 lg := cfg.GetLogger()
1414
1415 // Use both etcd-server-ssl and etcd-server for discovery.
1416 // Combine the results if both are available.
1417 clusterStrs, cerr = getCluster("https", "etcd-server-ssl"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.AdvertisePeerUrls)
1418 if cerr != nil {
1419 clusterStrs = make([]string, 0)
1420 }
1421 lg.Info(
1422 "get cluster for etcd-server-ssl SRV",
1423 zap.String("service-scheme", "https"),
1424 zap.String("service-name", "etcd-server-ssl"+serviceNameSuffix),
1425 zap.String("server-name", cfg.Name),
1426 zap.String("discovery-srv", cfg.DNSCluster),
1427 zap.Strings("advertise-peer-urls", cfg.getAdvertisePeerURLs()),
1428 zap.Strings("found-cluster", clusterStrs),
1429 zap.Error(cerr),
1430 )
1431
1432 defaultHTTPClusterStrs, httpCerr := getCluster("http", "etcd-server"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.AdvertisePeerUrls)
1433 if httpCerr == nil {
1434 clusterStrs = append(clusterStrs, defaultHTTPClusterStrs...)
1435 }
1436 lg.Info(
1437 "get cluster for etcd-server SRV",
1438 zap.String("service-scheme", "http"),
1439 zap.String("service-name", "etcd-server"+serviceNameSuffix),
1440 zap.String("server-name", cfg.Name),
1441 zap.String("discovery-srv", cfg.DNSCluster),
1442 zap.Strings("advertise-peer-urls", cfg.getAdvertisePeerURLs()),
1443 zap.Strings("found-cluster", clusterStrs),
1444 zap.Error(httpCerr),
1445 )
1446
1447 return clusterStrs, errors.Join(cerr, httpCerr)
1448}
1449
1450func (cfg *Config) InitialClusterFromName(name string) (ret string) {
1451 if len(cfg.AdvertisePeerUrls) == 0 {
1452 return ""
1453 }
1454 n := name
1455 if name == "" {
1456 n = DefaultName
1457 }
1458 for i := range cfg.AdvertisePeerUrls {
1459 ret = ret + "," + n + "=" + cfg.AdvertisePeerUrls[i].String()
1460 }
1461 return ret[1:]
1462}
1463
1464// InferLocalAddr tries to determine the LocalAddr used when communicating with
1465// an etcd peer. If SetMemberLocalAddr is true, then it will try to get the host
1466// from AdvertisePeerUrls by searching for the first URL with a specified
1467// non-loopback address. Otherwise, it defaults to empty string and the
1468// LocalAddr used will be the default for the Golang HTTP client.
1469func (cfg *Config) InferLocalAddr() string {
1470 if !cfg.ServerFeatureGate.Enabled(features.SetMemberLocalAddr) {
1471 return ""
1472 }
1473
1474 lg := cfg.GetLogger()
1475 lg.Info(
1476 "searching for a suitable member local address in AdvertisePeerURLs",
1477 zap.Strings("advertise-peer-urls", cfg.getAdvertisePeerURLs()),
1478 )
1479 for _, peerURL := range cfg.AdvertisePeerUrls {
1480 if addr, err := netip.ParseAddr(peerURL.Hostname()); err == nil {
1481 if addr.IsLoopback() || addr.IsUnspecified() {
1482 continue
1483 }
1484 lg.Info(
1485 "setting member local address",
1486 zap.String("LocalAddr", addr.String()),
1487 )
1488 return addr.String()
1489 }
1490 }
1491 lg.Warn(
1492 "unable to set a member local address due to lack of suitable local addresses",
1493 zap.Strings("advertise-peer-urls", cfg.getAdvertisePeerURLs()),
1494 )
1495 return ""
1496}
1497
1498func (cfg *Config) IsNewCluster() bool { return cfg.ClusterState == ClusterStateFlagNew }
1499func (cfg *Config) ElectionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) }
1500
1501func (cfg *Config) V2DeprecationEffective() config.V2DeprecationEnum {
1502 if cfg.V2Deprecation == "" {
1503 return config.V2DeprDefault
1504 }
1505 return cfg.V2Deprecation
1506}
1507
1508func (cfg *Config) defaultPeerHost() bool {
1509 return len(cfg.AdvertisePeerUrls) == 1 && cfg.AdvertisePeerUrls[0].String() == DefaultInitialAdvertisePeerURLs
1510}
1511
1512func (cfg *Config) defaultClientHost() bool {
1513 return len(cfg.AdvertiseClientUrls) == 1 && cfg.AdvertiseClientUrls[0].String() == DefaultAdvertiseClientURLs
1514}
1515
1516func (cfg *Config) ClientSelfCert() (err error) {
1517 if !cfg.ClientAutoTLS {
1518 return nil
1519 }
1520 if !cfg.ClientTLSInfo.Empty() {
1521 cfg.logger.Warn("ignoring client auto TLS since certs given")
1522 return nil
1523 }
1524 chosts := make([]string, 0, len(cfg.ListenClientUrls)+len(cfg.ListenClientHttpUrls))
1525 for _, u := range cfg.ListenClientUrls {
1526 chosts = append(chosts, u.Host)
1527 }
1528 for _, u := range cfg.ListenClientHttpUrls {
1529 chosts = append(chosts, u.Host)
1530 }
1531 cfg.ClientTLSInfo, err = transport.SelfCert(cfg.logger, filepath.Join(cfg.Dir, "fixtures", "client"), chosts, cfg.SelfSignedCertValidity)
1532 if err != nil {
1533 return err
1534 }
1535 return updateCipherSuites(&cfg.ClientTLSInfo, cfg.CipherSuites)
1536}
1537
1538func (cfg *Config) PeerSelfCert() (err error) {
1539 if !cfg.PeerAutoTLS {
1540 return nil
1541 }
1542 if !cfg.PeerTLSInfo.Empty() {
1543 cfg.logger.Warn("ignoring peer auto TLS since certs given")
1544 return nil
1545 }
1546 phosts := make([]string, len(cfg.ListenPeerUrls))
1547 for i, u := range cfg.ListenPeerUrls {
1548 phosts[i] = u.Host
1549 }
1550 cfg.PeerTLSInfo, err = transport.SelfCert(cfg.logger, filepath.Join(cfg.Dir, "fixtures", "peer"), phosts, cfg.SelfSignedCertValidity)
1551 if err != nil {
1552 return err
1553 }
1554 return updateCipherSuites(&cfg.PeerTLSInfo, cfg.CipherSuites)
1555}
1556
1557// UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host,
1558// if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
1559// e.g. advertise peer URL localhost:2380 or listen peer URL 0.0.0.0:2380
1560// then the advertise peer host would be updated with machine's default host,
1561// while keeping the listen URL's port.
1562// User can work around this by explicitly setting URL with 127.0.0.1.
1563// It returns the default hostname, if used, and the error, if any, from getting the machine's default host.
1564// TODO: check whether fields are set instead of whether fields have default value
1565func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error) {
1566 if defaultHostname == "" || defaultHostStatus != nil {
1567 // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
1568 if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
1569 cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
1570 }
1571 return "", defaultHostStatus
1572 }
1573
1574 used := false
1575 pip, pport := cfg.ListenPeerUrls[0].Hostname(), cfg.ListenPeerUrls[0].Port()
1576 if cfg.defaultPeerHost() && pip == "0.0.0.0" {
1577 cfg.AdvertisePeerUrls[0] = url.URL{Scheme: cfg.AdvertisePeerUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, pport)}
1578 used = true
1579 }
1580 // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
1581 if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
1582 cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
1583 }
1584
1585 cip, cport := cfg.ListenClientUrls[0].Hostname(), cfg.ListenClientUrls[0].Port()
1586 if cfg.defaultClientHost() && cip == "0.0.0.0" {
1587 cfg.AdvertiseClientUrls[0] = url.URL{Scheme: cfg.AdvertiseClientUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, cport)}
1588 used = true
1589 }
1590 dhost := defaultHostname
1591 if !used {
1592 dhost = ""
1593 }
1594 return dhost, defaultHostStatus
1595}
1596
1597// checkBindURLs returns an error if any URL uses a domain name.
1598func checkBindURLs(urls []url.URL) error {
1599 for _, url := range urls {
1600 if url.Scheme == "unix" || url.Scheme == "unixs" {
1601 continue
1602 }
1603 host, _, err := net.SplitHostPort(url.Host)
1604 if err != nil {
1605 return err
1606 }
1607 if host == "localhost" {
1608 // special case for local address
1609 // TODO: support /etc/hosts ?
1610 continue
1611 }
1612 if net.ParseIP(host) == nil {
1613 return fmt.Errorf("expected IP in URL for binding (%s)", url.String())
1614 }
1615 }
1616 return nil
1617}
1618
1619func checkHostURLs(urls []url.URL) error {
1620 for _, url := range urls {
1621 host, _, err := net.SplitHostPort(url.Host)
1622 if err != nil {
1623 return err
1624 }
1625 if host == "" {
1626 return fmt.Errorf("unexpected empty host (%s)", url.String())
1627 }
1628 }
1629 return nil
1630}
1631
1632func (cfg *Config) getAdvertisePeerURLs() (ss []string) {
1633 ss = make([]string, len(cfg.AdvertisePeerUrls))
1634 for i := range cfg.AdvertisePeerUrls {
1635 ss[i] = cfg.AdvertisePeerUrls[i].String()
1636 }
1637 return ss
1638}
1639
1640func (cfg *Config) getListenPeerURLs() (ss []string) {
1641 ss = make([]string, len(cfg.ListenPeerUrls))
1642 for i := range cfg.ListenPeerUrls {
1643 ss[i] = cfg.ListenPeerUrls[i].String()
1644 }
1645 return ss
1646}
1647
1648func (cfg *Config) getAdvertiseClientURLs() (ss []string) {
1649 ss = make([]string, len(cfg.AdvertiseClientUrls))
1650 for i := range cfg.AdvertiseClientUrls {
1651 ss[i] = cfg.AdvertiseClientUrls[i].String()
1652 }
1653 return ss
1654}
1655
1656func (cfg *Config) getListenClientURLs() (ss []string) {
1657 ss = make([]string, len(cfg.ListenClientUrls))
1658 for i := range cfg.ListenClientUrls {
1659 ss[i] = cfg.ListenClientUrls[i].String()
1660 }
1661 return ss
1662}
1663
1664func (cfg *Config) getMetricsURLs() (ss []string) {
1665 ss = make([]string, len(cfg.ListenMetricsUrls))
1666 for i := range cfg.ListenMetricsUrls {
1667 ss[i] = cfg.ListenMetricsUrls[i].String()
1668 }
1669 return ss
1670}
1671
1672func parseBackendFreelistType(freelistType string) bolt.FreelistType {
1673 if freelistType == freelistArrayType {
1674 return bolt.FreelistArrayType
1675 }
1676
1677 return bolt.FreelistMapType
1678}