From 02c87ffc7a7e97e37008418e0a4b9e78c53fde74 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 12 Apr 2021 03:13:25 -0500 Subject: [PATCH] support multiple servers in reproxy.server label #20 --- app/discovery/provider/docker.go | 10 ++++++---- app/discovery/provider/docker_test.go | 17 +++++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/discovery/provider/docker.go b/app/discovery/provider/docker.go index 959a230..454d0ff 100644 --- a/app/discovery/provider/docker.go +++ b/app/discovery/provider/docker.go @@ -89,18 +89,20 @@ func (d *Docker) List() ([]discovery.URLMapper, error) { if v, ok := c.Labels["reproxy.server"]; ok { server = v } - srcRegex, err := regexp.Compile(srcURL) - if v, ok := c.Labels["reproxy.ping"]; ok { pingURL = fmt.Sprintf("http://%s:%d%s", c.IP, c.Port, v) } + srcRegex, err := regexp.Compile(srcURL) if err != nil { return nil, errors.Wrapf(err, "invalid src regex %s", srcURL) } - res = append(res, discovery.URLMapper{Server: server, SrcMatch: *srcRegex, Dst: destURL, - PingURL: pingURL, ProviderID: discovery.PIDocker}) + // docker server label may have multiple, comma separated servers + for _, srv := range strings.Split(server, ",") { + res = append(res, discovery.URLMapper{Server: strings.TrimSpace(srv), SrcMatch: *srcRegex, Dst: destURL, + PingURL: pingURL, ProviderID: discovery.PIDocker}) + } } return res, nil } diff --git a/app/discovery/provider/docker_test.go b/app/discovery/provider/docker_test.go index ebb4828..b675308 100644 --- a/app/discovery/provider/docker_test.go +++ b/app/discovery/provider/docker_test.go @@ -82,7 +82,7 @@ func TestDocker_ListWithAutoAPI(t *testing.T) { {PrivatePort: 12345}, }, Labels: map[string]string{"reproxy.route": "^/api/123/(.*)", "reproxy.dest": "/blah/$1", - "reproxy.server": "example.com", "reproxy.ping": "/ping"}, + "reproxy.server": "example.com, example2.com", "reproxy.ping": "/ping"}, }, {Names: []string{"c2"}, State: "running", Networks: dc.NetworkList{ @@ -117,17 +117,22 @@ func TestDocker_ListWithAutoAPI(t *testing.T) { d := Docker{DockerClient: dclient, Network: "bridge", AutoAPI: true} res, err := d.List() require.NoError(t, err) - require.Equal(t, 2, len(res)) + require.Equal(t, 3, len(res)) assert.Equal(t, "^/api/123/(.*)", res[0].SrcMatch.String()) assert.Equal(t, "http://127.0.0.2:12345/blah/$1", res[0].Dst) assert.Equal(t, "example.com", res[0].Server) assert.Equal(t, "http://127.0.0.2:12345/ping", res[0].PingURL) - assert.Equal(t, "^/api/c2/(.*)", res[1].SrcMatch.String()) - assert.Equal(t, "http://127.0.0.3:12346/$1", res[1].Dst) - assert.Equal(t, "http://127.0.0.3:12346/ping", res[1].PingURL) - assert.Equal(t, "*", res[1].Server) + assert.Equal(t, "^/api/123/(.*)", res[1].SrcMatch.String()) + assert.Equal(t, "http://127.0.0.2:12345/blah/$1", res[1].Dst) + assert.Equal(t, "example2.com", res[1].Server) + assert.Equal(t, "http://127.0.0.2:12345/ping", res[1].PingURL) + + assert.Equal(t, "^/api/c2/(.*)", res[2].SrcMatch.String()) + assert.Equal(t, "http://127.0.0.3:12346/$1", res[2].Dst) + assert.Equal(t, "http://127.0.0.3:12346/ping", res[2].PingURL) + assert.Equal(t, "*", res[2].Server) } func TestDocker_Events(t *testing.T) {