go.etcd.io/etcd@v3.3.27+incompatible/etcdmain/config.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Every change should be reflected on help.go as well.
    16  
    17  package etcdmain
    18  
    19  import (
    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  	"sigs.k8s.io/yaml"
    34  )
    35  
    36  var (
    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  
    64  type 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
    77  type 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
    87  type configFlags struct {
    88  	flagSet      *flag.FlagSet
    89  	clusterState *flags.StringsFlag
    90  	fallback     *flags.StringsFlag
    91  	proxy        *flags.StringsFlag
    92  }
    93  
    94  func 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  	fs.BoolVar(&cfg.ec.PeerTLSInfo.SkipClientSANVerify, "experimental-peer-skip-client-san-verification", false, "Skip verification of SAN field in client certificate for peer connections.")
   193  
   194  	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).")
   195  
   196  	// logging
   197  	fs.BoolVar(&cfg.ec.Debug, "debug", false, "Enable debug-level logging for etcd.")
   198  	fs.StringVar(&cfg.ec.LogPkgLevels, "log-package-levels", "", "Specify a particular log level for each etcd package (eg: 'etcdmain=CRITICAL,etcdserver=DEBUG').")
   199  	fs.StringVar(&cfg.ec.LogOutput, "log-output", embed.DefaultLogOutput, "Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd.")
   200  
   201  	// unsafe
   202  	fs.BoolVar(&cfg.ec.ForceNewCluster, "force-new-cluster", false, "Force to create a new one member cluster.")
   203  
   204  	// version
   205  	fs.BoolVar(&cfg.printVersion, "version", false, "Print the version and exit.")
   206  
   207  	fs.StringVar(&cfg.ec.AutoCompactionRetention, "auto-compaction-retention", "0", "Auto compaction retention for mvcc key value store. 0 means disable auto compaction.")
   208  	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.")
   209  
   210  	// pprof profiler via HTTP
   211  	fs.BoolVar(&cfg.ec.EnablePprof, "enable-pprof", false, "Enable runtime profiling data via HTTP server. Address is at client URL + \"/debug/pprof/\"")
   212  
   213  	// additional metrics
   214  	fs.StringVar(&cfg.ec.Metrics, "metrics", cfg.ec.Metrics, "Set level of detail for exported metrics, specify 'extensive' to include histogram metrics")
   215  
   216  	// auth
   217  	fs.StringVar(&cfg.ec.AuthToken, "auth-token", cfg.ec.AuthToken, "Specify auth token specific options.")
   218  	fs.UintVar(&cfg.ec.AuthTokenTTL, "auth-token-ttl", cfg.ec.AuthTokenTTL, "The lifetime in seconds of the auth token.")
   219  
   220  	// experimental
   221  	fs.BoolVar(&cfg.ec.ExperimentalInitialCorruptCheck, "experimental-initial-corrupt-check", cfg.ec.ExperimentalInitialCorruptCheck, "Enable to check data corruption before serving any client/peer traffic.")
   222  	fs.DurationVar(&cfg.ec.ExperimentalCorruptCheckTime, "experimental-corrupt-check-time", cfg.ec.ExperimentalCorruptCheckTime, "Duration of time between cluster corruption check passes.")
   223  
   224  	// ignored
   225  	for _, f := range cfg.ignored {
   226  		fs.Var(&flags.IgnoredFlag{Name: f}, f, "")
   227  	}
   228  	return cfg
   229  }
   230  
   231  func (cfg *config) parse(arguments []string) error {
   232  	perr := cfg.cf.flagSet.Parse(arguments)
   233  	switch perr {
   234  	case nil:
   235  	case flag.ErrHelp:
   236  		fmt.Println(flagsline)
   237  		os.Exit(0)
   238  	default:
   239  		os.Exit(2)
   240  	}
   241  	if len(cfg.cf.flagSet.Args()) != 0 {
   242  		return fmt.Errorf("'%s' is not a valid flag", cfg.cf.flagSet.Arg(0))
   243  	}
   244  
   245  	if cfg.printVersion {
   246  		fmt.Printf("etcd Version: %s\n", version.Version)
   247  		fmt.Printf("Git SHA: %s\n", version.GitSHA)
   248  		fmt.Printf("Go Version: %s\n", runtime.Version())
   249  		fmt.Printf("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
   250  		os.Exit(0)
   251  	}
   252  
   253  	var err error
   254  	if cfg.configFile != "" {
   255  		plog.Infof("Loading server configuration from %q", cfg.configFile)
   256  		err = cfg.configFromFile(cfg.configFile)
   257  	} else {
   258  		err = cfg.configFromCmdLine()
   259  	}
   260  	return err
   261  }
   262  
   263  func (cfg *config) configFromCmdLine() error {
   264  	err := flags.SetFlagsFromEnv("ETCD", cfg.cf.flagSet)
   265  	if err != nil {
   266  		plog.Fatalf("%v", err)
   267  	}
   268  
   269  	cfg.ec.LPUrls = flags.URLsFromFlag(cfg.cf.flagSet, "listen-peer-urls")
   270  	cfg.ec.APUrls = flags.URLsFromFlag(cfg.cf.flagSet, "initial-advertise-peer-urls")
   271  	cfg.ec.LCUrls = flags.URLsFromFlag(cfg.cf.flagSet, "listen-client-urls")
   272  	cfg.ec.ACUrls = flags.URLsFromFlag(cfg.cf.flagSet, "advertise-client-urls")
   273  
   274  	if len(cfg.ec.ListenMetricsUrlsJSON) > 0 {
   275  		u, err := types.NewURLs(strings.Split(cfg.ec.ListenMetricsUrlsJSON, ","))
   276  		if err != nil {
   277  			plog.Fatalf("unexpected error setting up listen-metrics-urls: %v", err)
   278  		}
   279  		cfg.ec.ListenMetricsUrls = []url.URL(u)
   280  	}
   281  
   282  	cfg.ec.CipherSuites = flags.StringsFromFlagV2(cfg.cf.flagSet, "cipher-suites")
   283  
   284  	cfg.ec.ClusterState = cfg.cf.clusterState.String()
   285  	cfg.cp.Fallback = cfg.cf.fallback.String()
   286  	cfg.cp.Proxy = cfg.cf.proxy.String()
   287  
   288  	// disable default advertise-client-urls if lcurls is set
   289  	missingAC := flags.IsSet(cfg.cf.flagSet, "listen-client-urls") && !flags.IsSet(cfg.cf.flagSet, "advertise-client-urls")
   290  	if !cfg.mayBeProxy() && missingAC {
   291  		cfg.ec.ACUrls = nil
   292  	}
   293  
   294  	// disable default initial-cluster if discovery is set
   295  	if (cfg.ec.Durl != "" || cfg.ec.DNSCluster != "") && !flags.IsSet(cfg.cf.flagSet, "initial-cluster") {
   296  		cfg.ec.InitialCluster = ""
   297  	}
   298  
   299  	return cfg.validate()
   300  }
   301  
   302  func (cfg *config) configFromFile(path string) error {
   303  	eCfg, err := embed.ConfigFromFile(path)
   304  	if err != nil {
   305  		return err
   306  	}
   307  	cfg.ec = *eCfg
   308  
   309  	// load extra config information
   310  	b, rerr := ioutil.ReadFile(path)
   311  	if rerr != nil {
   312  		return rerr
   313  	}
   314  	if yerr := yaml.Unmarshal(b, &cfg.cp); yerr != nil {
   315  		return yerr
   316  	}
   317  	if cfg.cp.FallbackJSON != "" {
   318  		if err := cfg.cf.fallback.Set(cfg.cp.FallbackJSON); err != nil {
   319  			plog.Panicf("unexpected error setting up discovery-fallback flag: %v", err)
   320  		}
   321  		cfg.cp.Fallback = cfg.cf.fallback.String()
   322  	}
   323  	if cfg.cp.ProxyJSON != "" {
   324  		if err := cfg.cf.proxy.Set(cfg.cp.ProxyJSON); err != nil {
   325  			plog.Panicf("unexpected error setting up proxyFlag: %v", err)
   326  		}
   327  		cfg.cp.Proxy = cfg.cf.proxy.String()
   328  	}
   329  	return nil
   330  }
   331  
   332  func (cfg *config) mayBeProxy() bool {
   333  	mayFallbackToProxy := cfg.ec.Durl != "" && cfg.cp.Fallback == fallbackFlagProxy
   334  	return cfg.cp.Proxy != proxyFlagOff || mayFallbackToProxy
   335  }
   336  
   337  func (cfg *config) validate() error {
   338  	err := cfg.ec.Validate()
   339  	// TODO(yichengq): check this for joining through discovery service case
   340  	if err == embed.ErrUnsetAdvertiseClientURLsFlag && cfg.mayBeProxy() {
   341  		return nil
   342  	}
   343  	return err
   344  }
   345  
   346  func (cfg config) isProxy() bool               { return cfg.cf.proxy.String() != proxyFlagOff }
   347  func (cfg config) isReadonlyProxy() bool       { return cfg.cf.proxy.String() == proxyFlagReadonly }
   348  func (cfg config) shouldFallbackToProxy() bool { return cfg.cf.fallback.String() == fallbackFlagProxy }