From 4219baa61f6d61b4407d33cc578d85df675b91a4 Mon Sep 17 00:00:00 2001 From: Crash129 Date: Tue, 19 May 2020 11:55:53 +0200 Subject: [PATCH 1/2] PMM-5967 Bind-IP for agent listeners --- agents/supervisor/supervisor.go | 7 +++++-- agents/supervisor/supervisor_test.go | 9 +++++---- commands/run.go | 2 +- config/config.go | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/agents/supervisor/supervisor.go b/agents/supervisor/supervisor.go index bc5ae9c1f..f5f3f244e 100644 --- a/agents/supervisor/supervisor.go +++ b/agents/supervisor/supervisor.go @@ -50,6 +50,7 @@ import ( type Supervisor struct { ctx context.Context paths *config.Paths + bindIP *string portsRegistry *portsRegistry changes chan agentpb.StateChangedRequest qanRequests chan agentpb.QANCollectRequest @@ -83,10 +84,11 @@ type builtinAgentInfo struct { // Supervisor is gracefully stopped when context passed to NewSupervisor is canceled. // Changes of Agent statuses are reported via Changes() channel which must be read until it is closed. // QAN data is sent to QANRequests() channel which must be read until it is closed. -func NewSupervisor(ctx context.Context, paths *config.Paths, ports *config.Ports) *Supervisor { +func NewSupervisor(ctx context.Context, paths *config.Paths, ports *config.Ports, bindIP *string) *Supervisor { supervisor := &Supervisor{ ctx: ctx, paths: paths, + bindIP: bindIP, portsRegistry: newPortsRegistry(ports.Min, ports.Max, nil), changes: make(chan agentpb.StateChangedRequest, 10), qanRequests: make(chan agentpb.QANCollectRequest, 10), @@ -500,7 +502,8 @@ func (s *Supervisor) processParams(agentID string, agentProcess *agentpb.SetStat } templateParams := map[string]interface{}{ - "listen_port": port, + "listen_address": *s.bindIP, + "listen_port": port, } // render files only if they are present to avoid creating temporary directory for every agent diff --git a/agents/supervisor/supervisor_test.go b/agents/supervisor/supervisor_test.go index c37f5c944..1d04ecbf6 100644 --- a/agents/supervisor/supervisor_test.go +++ b/agents/supervisor/supervisor_test.go @@ -49,7 +49,7 @@ func assertChanges(t *testing.T, s *Supervisor, expected ...agentpb.StateChanged func TestSupervisor(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) - s := NewSupervisor(ctx, nil, &config.Ports{Min: 65000, Max: 65099}) + s := NewSupervisor(ctx, nil, &config.Ports{Min: 65000, Max: 65099}, new(string)) t.Run("Start13", func(t *testing.T) { expectedList := []*agentlocalpb.AgentInfo{} @@ -299,7 +299,8 @@ func TestSupervisorProcessParams(t *testing.T) { MySQLdExporter: "/path/to/mysql_exporter", TempDir: temp, } - s := NewSupervisor(ctx, paths, new(config.Ports)) + bindIP := "127.0.0.1" + s := NewSupervisor(ctx, paths, new(config.Ports), &bindIP) teardown := func() { cancel() @@ -320,7 +321,7 @@ func TestSupervisorProcessParams(t *testing.T) { p := &agentpb.SetStateRequest_AgentProcess{ Type: inventorypb.AgentType_MYSQLD_EXPORTER, Args: []string{ - "-web.listen-address=:{{ .listen_port }}", + "-web.listen-address={{ .listen_address }}:{{ .listen_port }}", "-web.ssl-cert-file={{ .TextFiles.Cert }}", }, Env: []string{ @@ -338,7 +339,7 @@ func TestSupervisorProcessParams(t *testing.T) { expected := process.Params{ Path: "/path/to/mysql_exporter", Args: []string{ - "-web.listen-address=:12345", + "-web.listen-address=127.0.0.1:12345", "-web.ssl-cert-file=" + filepath.Join(s.paths.TempDir, "mysqld_exporter", "ID", "Cert"), }, Env: []string{ diff --git a/commands/run.go b/commands/run.go index 1d565c323..14fdf8afe 100644 --- a/commands/run.go +++ b/commands/run.go @@ -56,7 +56,7 @@ func Run() { for appCtx.Err() == nil { ctx, cancel := context.WithCancel(appCtx) - supervisor := supervisor.NewSupervisor(ctx, &cfg.Paths, &cfg.Ports) + supervisor := supervisor.NewSupervisor(ctx, &cfg.Paths, &cfg.Ports, &cfg.BindIP) connectionChecker := connectionchecker.New(ctx) client := client.New(cfg, supervisor, connectionChecker) localServer := agentlocal.NewServer(cfg, supervisor, client, configFilepath) diff --git a/config/config.go b/config/config.go index 47bcd537f..8c55ccd1e 100644 --- a/config/config.go +++ b/config/config.go @@ -128,6 +128,7 @@ type Config struct { Server Server `yaml:"server"` Paths Paths `yaml:"paths"` + BindIP string `yaml:"bind-ip"` Ports Ports `yaml:"ports"` Debug bool `yaml:"debug"` From b7581a913603f73892d3f45667a9d5e10e10ceb3 Mon Sep 17 00:00:00 2001 From: Crash129 Date: Tue, 25 Aug 2020 10:19:30 +0200 Subject: [PATCH 2/2] PMM-5967 Bind-IP for agent listeners --- agents/supervisor/supervisor.go | 6 +++--- agents/supervisor/supervisor_test.go | 5 ++--- commands/run.go | 2 +- config/config.go | 2 ++ config/config_test.go | 4 ++++ 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/agents/supervisor/supervisor.go b/agents/supervisor/supervisor.go index f5f3f244e..6e261f120 100644 --- a/agents/supervisor/supervisor.go +++ b/agents/supervisor/supervisor.go @@ -50,7 +50,7 @@ import ( type Supervisor struct { ctx context.Context paths *config.Paths - bindIP *string + bindIP string portsRegistry *portsRegistry changes chan agentpb.StateChangedRequest qanRequests chan agentpb.QANCollectRequest @@ -84,7 +84,7 @@ type builtinAgentInfo struct { // Supervisor is gracefully stopped when context passed to NewSupervisor is canceled. // Changes of Agent statuses are reported via Changes() channel which must be read until it is closed. // QAN data is sent to QANRequests() channel which must be read until it is closed. -func NewSupervisor(ctx context.Context, paths *config.Paths, ports *config.Ports, bindIP *string) *Supervisor { +func NewSupervisor(ctx context.Context, paths *config.Paths, ports *config.Ports, bindIP string) *Supervisor { supervisor := &Supervisor{ ctx: ctx, paths: paths, @@ -502,7 +502,7 @@ func (s *Supervisor) processParams(agentID string, agentProcess *agentpb.SetStat } templateParams := map[string]interface{}{ - "listen_address": *s.bindIP, + "listen_address": s.bindIP, "listen_port": port, } diff --git a/agents/supervisor/supervisor_test.go b/agents/supervisor/supervisor_test.go index 1d04ecbf6..194a18956 100644 --- a/agents/supervisor/supervisor_test.go +++ b/agents/supervisor/supervisor_test.go @@ -49,7 +49,7 @@ func assertChanges(t *testing.T, s *Supervisor, expected ...agentpb.StateChanged func TestSupervisor(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) - s := NewSupervisor(ctx, nil, &config.Ports{Min: 65000, Max: 65099}, new(string)) + s := NewSupervisor(ctx, nil, &config.Ports{Min: 65000, Max: 65099}, "") t.Run("Start13", func(t *testing.T) { expectedList := []*agentlocalpb.AgentInfo{} @@ -299,8 +299,7 @@ func TestSupervisorProcessParams(t *testing.T) { MySQLdExporter: "/path/to/mysql_exporter", TempDir: temp, } - bindIP := "127.0.0.1" - s := NewSupervisor(ctx, paths, new(config.Ports), &bindIP) + s := NewSupervisor(ctx, paths, new(config.Ports), "127.0.0.1") teardown := func() { cancel() diff --git a/commands/run.go b/commands/run.go index 14fdf8afe..ea9c178b4 100644 --- a/commands/run.go +++ b/commands/run.go @@ -56,7 +56,7 @@ func Run() { for appCtx.Err() == nil { ctx, cancel := context.WithCancel(appCtx) - supervisor := supervisor.NewSupervisor(ctx, &cfg.Paths, &cfg.Ports, &cfg.BindIP) + supervisor := supervisor.NewSupervisor(ctx, &cfg.Paths, &cfg.Ports, cfg.BindIP) connectionChecker := connectionchecker.New(ctx) client := client.New(cfg, supervisor, connectionChecker) localServer := agentlocal.NewServer(cfg, supervisor, client, configFilepath) diff --git a/config/config.go b/config/config.go index 8c55ccd1e..43f1b40bd 100644 --- a/config/config.go +++ b/config/config.go @@ -302,6 +302,8 @@ func Application(cfg *Config) (*kingpin.Application, *string) { Envar("PMM_AGENT_PATHS_TEMPDIR").StringVar(&cfg.Paths.TempDir) // no flag for SlowLogFilePrefix - it is only for development and testing + app.Flag("bind-ip", "Bind listening sockets to ip address [PMM_AGENT_BIND_ADDR]"). + Envar("PMM_AGENT_BIND_ADDR").StringVar(&cfg.BindIP) app.Flag("ports-min", "Minimal allowed port number for listening sockets [PMM_AGENT_PORTS_MIN]"). Envar("PMM_AGENT_PORTS_MIN").Uint16Var(&cfg.Ports.Min) app.Flag("ports-max", "Maximal allowed port number for listening sockets [PMM_AGENT_PORTS_MAX]"). diff --git a/config/config_test.go b/config/config_test.go index 8de317d4f..0d20f9046 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -84,6 +84,7 @@ func TestGet(t *testing.T) { actual, configFilepath, err := get([]string{ "--id=agent-id", "--server-address=127.0.0.1", + "--bind-ip=127.0.0.1", }, logrus.WithField("test", t.Name())) require.NoError(t, err) @@ -103,6 +104,7 @@ func TestGet(t *testing.T) { RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter", TempDir: os.TempDir(), }, + BindIP: "127.0.0.1", Ports: Ports{ Min: 42000, Max: 51999, @@ -118,6 +120,7 @@ func TestGet(t *testing.T) { Server: Server{ Address: "127.0.0.1", }, + BindIP: "127.0.0.1", }) defer removeConfig(t, name) @@ -142,6 +145,7 @@ func TestGet(t *testing.T) { RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter", TempDir: os.TempDir(), }, + BindIP: "127.0.0.1", Ports: Ports{ Min: 42000, Max: 51999,