From 06a6d595b384e935f2b702bc882ea7666646e364 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 12 Apr 2021 21:26:31 -0500 Subject: [PATCH] change docker provider defaults skip without --docker.auto all containers without reproxy.* label --- README.md | 15 ++++++++++----- app/discovery/provider/docker.go | 23 ++++++++++++++++++++++- app/discovery/provider/docker_test.go | 1 + 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 32c75b5..c455e16 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ This is a dynamic provider and file change will be applied automatically. ### Docker -Docker provider works with no extra configuration and by default redirects all requests like `https://server//(.*)` to the internal IP of the given container and the exposed port. Only active (running) containers will be detected. +Docker provider supports a fully automatic discovery (with `--docker.auto`) with no extra configuration and by default redirects all requests like `https://server//(.*)` to the internal IP of the given container and the exposed port. Only active (running) containers will be detected. This default can be changed with labels: @@ -67,12 +67,15 @@ This default can be changed with labels: - `reproxy.route` - source route (location) - `reproxy.dest` - destination path. Note: this is not full url, but just the path which will be appended to container's ip:port - `reproxy.ping` - ping path for the destination container. +- `reproxy.enabled` - enable (`yes`, `true`, `1`) or disable (`no`, `false`, `0`) container from reproxy destinations. -By default all containers with exposed port will be considered as routing destinations. There are 3 ways to restrict it: +Pls note: without `--docker.auto` the destination container has to have at least one of `reproxy.*` labels to be considered as a potential destination. + +With `--docker.auto`, all containers with exposed port will be considered as routing destinations. There are 3 ways to restrict it: - Exclude some containers explicitly with `--docker.exclude`, i.e. `--docker.exclude=c1 --docker.exclude=c2 ...` - Allow only a particular docker network with `--docker.network` -- Set the label `reproxy.enabled=false` or `reproxy.enabled=no` +- Set the label `reproxy.enabled=false` or `reproxy.enabled=no` or `reproxy.enabled=0` This is a dynamic provider and any change in container's status will be applied automatically. @@ -136,8 +139,9 @@ logger: docker: --docker.enabled enable docker provider [$DOCKER_ENABLED] --docker.host= docker host (default: unix:///var/run/docker.sock) [$DOCKER_HOST] - --docker.network= docker network (default: bridge) [$DOCKER_NETWORK] + --docker.network= docker network [$DOCKER_NETWORK] --docker.exclude= excluded containers [$DOCKER_EXCLUDE] + --docker.auto enable automatic routing (without labels) [$DOCKER_AUTO] file: --file.enabled enable file provider [$FILE_ENABLED] @@ -151,7 +155,8 @@ static: Help Options: -h, --help Show this help message - + + ``` ## Status diff --git a/app/discovery/provider/docker.go b/app/discovery/provider/docker.go index 454d0ff..37ced26 100644 --- a/app/discovery/provider/docker.go +++ b/app/discovery/provider/docker.go @@ -64,6 +64,7 @@ func (d *Docker) Events(ctx context.Context) (res <-chan discovery.ProviderID) { } // List all containers and make url mappers +// If AutoAPI enabled all each container and set all params, if not - allow only container with reproxy.* labels func (d *Docker) List() ([]discovery.URLMapper, error) { containers, err := d.listContainers() if err != nil { @@ -72,27 +73,47 @@ func (d *Docker) List() ([]discovery.URLMapper, error) { res := make([]discovery.URLMapper, 0, len(containers)) for _, c := range containers { + enabled := false srcURL := "^/(.*)" if d.AutoAPI { + enabled = true srcURL = fmt.Sprintf("^/api/%s/(.*)", c.Name) + } destURL := fmt.Sprintf("http://%s:%d/$1", c.IP, c.Port) pingURL := fmt.Sprintf("http://%s:%d/ping", c.IP, c.Port) server := "*" + // we don't care about value because disabled will be filtered before + if _, ok := c.Labels["reproxy.enabled"]; ok { + enabled = true + } + if v, ok := c.Labels["reproxy.route"]; ok { + enabled = true srcURL = v } + if v, ok := c.Labels["reproxy.dest"]; ok { + enabled = true destURL = fmt.Sprintf("http://%s:%d%s", c.IP, c.Port, v) } + if v, ok := c.Labels["reproxy.server"]; ok { + enabled = true server = v } + if v, ok := c.Labels["reproxy.ping"]; ok { + enabled = true pingURL = fmt.Sprintf("http://%s:%d%s", c.IP, c.Port, v) } + if !enabled { + log.Printf("[DEBUG] container %s disabled", c.Name) + continue + } + srcRegex, err := regexp.Compile(srcURL) if err != nil { return nil, errors.Wrapf(err, "invalid src regex %s", srcURL) @@ -167,7 +188,7 @@ func (d *Docker) listContainers() (res []containerInfo, err error) { } if v, ok := c.Labels["reproxy.enabled"]; ok { - if strings.EqualFold(v, "false") || strings.EqualFold(v, "no") { + if strings.EqualFold(v, "false") || strings.EqualFold(v, "no") || v == "0" { log.Printf("[DEBUG] skip container %s due to reproxy.enabled=%s", containerName, v) continue } diff --git a/app/discovery/provider/docker_test.go b/app/discovery/provider/docker_test.go index b675308..c70eb3d 100644 --- a/app/discovery/provider/docker_test.go +++ b/app/discovery/provider/docker_test.go @@ -31,6 +31,7 @@ func TestDocker_List(t *testing.T) { Ports: []dc.APIPort{ {PrivatePort: 12346}, }, + Labels: map[string]string{"reproxy.enabled": "y"}, }, {Names: []string{"c3"}, State: "stopped"}, {Names: []string{"c4"}, State: "running",