From 556a34f1fbecb70b968c35a40f11efa557f9a375 Mon Sep 17 00:00:00 2001 From: Umputun Date: Wed, 26 May 2021 01:45:42 -0500 Subject: [PATCH] allow reproxy.server to be default for numeric rules --- app/discovery/provider/docker.go | 14 +++++---- app/discovery/provider/docker_test.go | 43 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/app/discovery/provider/docker.go b/app/discovery/provider/docker.go index ff03845..f86cc93 100644 --- a/app/discovery/provider/docker.go +++ b/app/discovery/provider/docker.go @@ -116,6 +116,7 @@ func (d *Docker) parseContainerInfo(c containerInfo) (res []discovery.URLMapper) enabled, explicit = true, true srcURL = v } + if v, ok := d.labelN(c.Labels, n, "dest"); ok { enabled, explicit = true, true if strings.HasPrefix(v, "http://") || strings.HasPrefix(v, "https://") { @@ -124,14 +125,18 @@ func (d *Docker) parseContainerInfo(c containerInfo) (res []discovery.URLMapper) destURL = fmt.Sprintf("http://%s:%d%s", c.IP, port, v) } } + if v, ok := d.labelN(c.Labels, n, "server"); ok { enabled = true server = v + } else if v, ok = c.Labels["reproxy.server"]; ok { // fallback if no reproxy.N.server + server = v } + if v, ok := d.labelN(c.Labels, n, "ping"); ok { enabled = true if strings.HasPrefix(v, "http://") || strings.HasPrefix(v, "https://") { - pingURL = v // if ping is fulle url with http:// or https:// use it as-is + pingURL = v // if ping is full url with http:// or https:// use it as-is } else { pingURL = fmt.Sprintf("http://%s:%d%s", c.IP, port, v) } @@ -140,8 +145,7 @@ func (d *Docker) parseContainerInfo(c containerInfo) (res []discovery.URLMapper) if v, ok := d.labelN(c.Labels, n, "assets"); ok { if ae := strings.Split(v, ":"); len(ae) == 2 { enabled = true - assetsWebRoot = ae[0] - assetsLocation = ae[1] + assetsWebRoot, assetsLocation = ae[0], ae[1] } } @@ -209,9 +213,9 @@ func (d *Docker) matchedPort(c containerInfo, n int) (port int, err error) { func (d *Docker) labelN(labels map[string]string, n int, suffix string) (result string, ok bool) { switch n { case 0: - result, ok = labels["reproxy."+suffix] + result, ok = labels["reproxy.0."+suffix] if !ok { - result, ok = labels["reproxy.0."+suffix] + result, ok = labels["reproxy."+suffix] } default: result, ok = labels[fmt.Sprintf("reproxy.%d.%s", n, suffix)] diff --git a/app/discovery/provider/docker_test.go b/app/discovery/provider/docker_test.go index bc4ec8f..99bd47b 100644 --- a/app/discovery/provider/docker_test.go +++ b/app/discovery/provider/docker_test.go @@ -173,6 +173,49 @@ func TestDocker_ListMulti(t *testing.T) { assert.Equal(t, "example.com", res[5].Server) } +func TestDocker_ListMultiFallBack(t *testing.T) { + dclient := &DockerClientMock{ + ListContainersFunc: func() ([]containerInfo, error) { + return []containerInfo{ + { + Name: "c0", State: "running", IP: "127.0.0.2", Ports: []int{12348}, + Labels: map[string]string{ + "reproxy.server": "example.com", "reproxy.route": "^/a/(.*)", "reproxy.dest": "/a/$1", + "reproxy.ping": "/ping", "reproxy.assets": "/web:/var/www", + + "reproxy.1.route": "^/a/1/(.*)", "reproxy.1.dest": "/a/1/$1", "reproxy.1.ping": "/ping", + + "reproxy.2.server": "m2.example.com", "reproxy.2.route": "^/a/2/(.*)", + "reproxy.2.dest": "/a/2/$1", "reproxy.2.assets": "/web2:/var/www2", + }, + }, + }, nil + }, + } + + d := Docker{DockerClient: dclient} + res, err := d.List() + require.NoError(t, err) + require.Equal(t, 5, len(res), "3 proxy, 2 assets") + + assert.Equal(t, "^/a/1/(.*)", res[0].SrcMatch.String()) + assert.Equal(t, "http://127.0.0.2:12348/a/1/$1", res[0].Dst) + assert.Equal(t, "example.com", res[0].Server) + assert.Equal(t, "http://127.0.0.2:12348/ping", res[0].PingURL) + + assert.Equal(t, "^/a/2/(.*)", res[1].SrcMatch.String()) + assert.Equal(t, "http://127.0.0.2:12348/a/2/$1", res[1].Dst) + assert.Equal(t, "http://127.0.0.2:12348/ping", res[1].PingURL) + assert.Equal(t, "m2.example.com", res[1].Server) + + assert.Equal(t, "^/a/2/(.*)", res[2].SrcMatch.String()) + assert.Equal(t, "http://127.0.0.2:12348/a/2/$1", res[2].Dst) + assert.Equal(t, "http://127.0.0.2:12348/ping", res[2].PingURL) + assert.Equal(t, "m2.example.com", res[2].Server) + assert.Equal(t, "/web2", res[2].AssetsWebRoot) + assert.Equal(t, "/var/www2", res[2].AssetsLocation) +} + func TestDocker_ListWithAutoAPI(t *testing.T) { dclient := &DockerClientMock{ ListContainersFunc: func() ([]containerInfo, error) {