From 06fda7accf73f99dc18bfc9de29a5883420183d3 Mon Sep 17 00:00:00 2001 From: Yuriy <70581130+yuriydzobak@users.noreply.github.com> Date: Sat, 23 Jan 2021 03:40:48 +0200 Subject: [PATCH] Add functionality to bind custom IP address for Etcd metrics endpoint (#2750) * Add functionality to bind custom IP address for Etcd metrics endpoint Signed-off-by: yuriydzobak --- pkg/cli/cmds/server.go | 6 ++++++ pkg/cli/server/server.go | 1 + pkg/daemons/config/types.go | 1 + pkg/etcd/etcd.go | 10 +++++++++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index d4429c4f1f..a2df2724f3 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -60,6 +60,7 @@ type Server struct { StartupHooks []func(context.Context, <-chan struct{}, string) error EtcdSnapshotName string EtcdDisableSnapshots bool + EtcdExposeMetrics bool EtcdSnapshotDir string EtcdSnapshotCron string EtcdSnapshotRetention int @@ -210,6 +211,11 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command { Destination: &ServerConfig.DatastoreKeyFile, EnvVar: version.ProgramUpper + "_DATASTORE_KEYFILE", }, + &cli.BoolFlag{ + Name: "etcd-expose-metrics", + Usage: "(db) Expose etcd metrics to client interface. (Default false)", + Destination: &ServerConfig.EtcdExposeMetrics, + }, &cli.BoolFlag{ Name: "etcd-disable-snapshots", Usage: "(db) Disable automatic etcd snapshots", diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index 15c9abea5d..cd0cdbfe26 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -116,6 +116,7 @@ func run(app *cli.Context, cfg *cmds.Server) error { serverConfig.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir serverConfig.ControlConfig.EtcdSnapshotRetention = cfg.EtcdSnapshotRetention serverConfig.ControlConfig.EtcdDisableSnapshots = cfg.EtcdDisableSnapshots + serverConfig.ControlConfig.EtcdExposeMetrics = cfg.EtcdExposeMetrics if cfg.ClusterResetRestorePath != "" && !cfg.ClusterReset { return errors.New("Invalid flag use. --cluster-reset required with --cluster-reset-restore-path") diff --git a/pkg/daemons/config/types.go b/pkg/daemons/config/types.go index 23db3274bc..2f05f3129d 100644 --- a/pkg/daemons/config/types.go +++ b/pkg/daemons/config/types.go @@ -132,6 +132,7 @@ type Control struct { TLSCipherSuites []uint16 EtcdSnapshotName string EtcdDisableSnapshots bool + EtcdExposeMetrics bool EtcdSnapshotDir string EtcdSnapshotCron string EtcdSnapshotRetention int diff --git a/pkg/etcd/etcd.go b/pkg/etcd/etcd.go index 220986d400..7d5364cde1 100644 --- a/pkg/etcd/etcd.go +++ b/pkg/etcd/etcd.go @@ -482,6 +482,14 @@ func (e *ETCD) clientURL() string { return fmt.Sprintf("https://%s:2379", e.address) } +// metricsURL returns the metrics access address +func (e *ETCD) metricsURL(expose bool) string { + if expose { + return fmt.Sprintf("http://%s:2381", e.address) + } + return "http://127.0.0.1:2381" +} + // cluster returns ETCDConfig for a cluster func (e *ETCD) cluster(ctx context.Context, forceNew bool, options executor.InitialOptions) error { return executor.ETCD(executor.ETCDConfig{ @@ -489,7 +497,7 @@ func (e *ETCD) cluster(ctx context.Context, forceNew bool, options executor.Init InitialOptions: options, ForceNewCluster: forceNew, ListenClientURLs: fmt.Sprintf(e.clientURL() + ",https://127.0.0.1:2379"), - ListenMetricsURLs: "http://127.0.0.1:2381", + ListenMetricsURLs: e.metricsURL(e.config.EtcdExposeMetrics), ListenPeerURLs: e.peerURL(), AdvertiseClientURLs: e.clientURL(), DataDir: etcdDBDir(e.config),