Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

PMM-5967 Bind-IP for agent listeners #160

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
7 changes: 5 additions & 2 deletions agents/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions agents/supervisor/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}, "")

t.Run("Start13", func(t *testing.T) {
expectedList := []*agentlocalpb.AgentInfo{}
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestSupervisorProcessParams(t *testing.T) {
MySQLdExporter: "/path/to/mysql_exporter",
TempDir: temp,
}
s := NewSupervisor(ctx, paths, new(config.Ports))
s := NewSupervisor(ctx, paths, new(config.Ports), "127.0.0.1")

teardown := func() {
cancel()
Expand All @@ -320,7 +320,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{
Expand All @@ -338,7 +338,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{
Expand Down
2 changes: 1 addition & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -301,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]").
Expand Down
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -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)

Expand All @@ -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,
Expand Down