support multiple servers in reproxy.server label #20

This commit is contained in:
Umputun 2021-04-12 03:13:25 -05:00
parent a20abd0140
commit 02c87ffc7a
2 changed files with 17 additions and 10 deletions

View File

@ -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
}

View File

@ -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) {