correcting some behaviors for $DOCKER_HOST, now uses container's private IP instead of localhost

This commit is contained in:
yusing
2024-10-11 09:13:38 +08:00
parent d91b66ae87
commit 7184c9cfe9
68 changed files with 925 additions and 570 deletions

View File

@@ -1,15 +1,15 @@
package provider
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/docker/docker/client"
"github.com/sirupsen/logrus"
"github.com/yusing/go-proxy/internal/common"
D "github.com/yusing/go-proxy/internal/docker"
E "github.com/yusing/go-proxy/internal/error"
R "github.com/yusing/go-proxy/internal/route"
"github.com/yusing/go-proxy/internal/types"
W "github.com/yusing/go-proxy/internal/watcher"
@@ -17,23 +17,24 @@ import (
)
type DockerProvider struct {
name, dockerHost, hostname string
ExplicitOnly bool
name, dockerHost string
ExplicitOnly bool
}
var AliasRefRegex = regexp.MustCompile(`#\d+`)
var AliasRefRegexOld = regexp.MustCompile(`\$\d+`)
var (
AliasRefRegex = regexp.MustCompile(`#\d+`)
AliasRefRegexOld = regexp.MustCompile(`\$\d+`)
)
func DockerProviderImpl(name, dockerHost string, explicitOnly bool) (ProviderImpl, E.NestedError) {
hostname, err := D.ParseDockerHostname(dockerHost)
if err.HasError() {
return nil, err
if dockerHost == common.DockerHostFromEnv {
dockerHost = common.GetEnv("DOCKER_HOST", client.DefaultDockerHost)
}
return &DockerProvider{name, dockerHost, hostname, explicitOnly}, nil
return &DockerProvider{name, dockerHost, explicitOnly}, nil
}
func (p *DockerProvider) String() string {
return fmt.Sprintf("docker: %s", p.name)
return "docker: " + p.name
}
func (p *DockerProvider) NewWatcher() W.Watcher {
@@ -49,7 +50,7 @@ func (p *DockerProvider) LoadRoutesImpl() (routes R.Routes, err E.NestedError) {
return routes, E.FailWith("connect to docker", err)
}
errors := E.NewBuilder("errors when parse docker labels")
errors := E.NewBuilder("errors in docker labels")
for _, c := range info.Containers {
container := D.FromDocker(&c, p.dockerHost)
@@ -80,7 +81,7 @@ func (p *DockerProvider) LoadRoutesImpl() (routes R.Routes, err E.NestedError) {
return routes, errors.Build()
}
func (p *DockerProvider) shouldIgnore(container D.Container) bool {
func (p *DockerProvider) shouldIgnore(container *D.Container) bool {
return container.IsExcluded ||
!container.IsExplicit && p.ExplicitOnly ||
!container.IsExplicit && container.IsDatabase ||
@@ -172,8 +173,8 @@ func (p *DockerProvider) OnEvent(event W.Event, routes R.Routes) (res EventResul
}
// Returns a list of proxy entries for a container.
// Always non-nil
func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entries types.RawEntries, _ E.NestedError) {
// Always non-nil.
func (p *DockerProvider) entriesFromContainerLabels(container *D.Container) (entries types.RawEntries, _ E.NestedError) {
entries = types.NewProxyEntries()
if p.shouldIgnore(container) {
@@ -183,9 +184,8 @@ func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entr
// init entries map for all aliases
for _, a := range container.Aliases {
entries.Store(a, &types.RawEntry{
Alias: a,
Host: p.hostname,
ProxyProperties: container.ProxyProperties,
Alias: a,
Container: container,
})
}
@@ -202,11 +202,11 @@ func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entr
return entries, errors.Build().Subject(container.ContainerName)
}
func (p *DockerProvider) applyLabel(container D.Container, entries types.RawEntries, key, val string) (res E.NestedError) {
func (p *DockerProvider) applyLabel(container *D.Container, entries types.RawEntries, key, val string) (res E.NestedError) {
b := E.NewBuilder("errors in label %s", key)
defer b.To(&res)
refErr := E.NewBuilder("errors parsing alias references")
refErr := E.NewBuilder("errors in alias references")
replaceIndexRef := func(ref string) string {
index, err := strconv.Atoi(ref[1:])
if err != nil {
@@ -231,7 +231,7 @@ func (p *DockerProvider) applyLabel(container D.Container, entries types.RawEntr
// apply label for all aliases
entries.RangeAll(func(a string, e *types.RawEntry) {
if err = D.ApplyLabel(e, lbl); err.HasError() {
b.Add(err.Subjectf("alias %s", lbl.Target))
b.Add(err)
}
})
} else {
@@ -250,7 +250,7 @@ func (p *DockerProvider) applyLabel(container D.Container, entries types.RawEntr
return
}
if err = D.ApplyLabel(config, lbl); err.HasError() {
b.Add(err.Subjectf("alias %s", lbl.Target))
b.Add(err)
}
}
return