v0.5-rc3: update docker port detect mechanism, docker compose file and doc update

This commit is contained in:
yusing
2024-09-17 03:11:04 +08:00
parent 1120991019
commit 16b507bc7c
10 changed files with 77 additions and 34 deletions

View File

@@ -25,7 +25,7 @@ func GetClientInfo(clientHost string) (*ClientInfo, E.NestedError) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
containers, err := E.Check(dockerClient.ContainerList(ctx, container.ListOptions{All: true}))
containers, err := E.Check(dockerClient.ContainerList(ctx, container.ListOptions{}))
if err.IsNotNil() {
return nil, E.Failure("list containers").With(err)
}

View File

@@ -16,6 +16,10 @@ func Failure(what string) NestedError {
return errorf("%s %w", what, ErrFailure)
}
func FailureWhy(what string, why string) NestedError {
return errorf("%s %w because %s", what, ErrFailure, why)
}
func Invalid(subject, what any) NestedError {
return errorf("%w %v - %v", ErrInvalid, subject, what)
}

View File

@@ -16,7 +16,7 @@ func NewPort(v string) (Port, E.NestedError) {
return NewPortInt(p)
}
func NewPortInt(v int) (Port, E.NestedError) {
func NewPortInt[Int int | uint16](v Int) (Port, E.NestedError) {
pp := Port(v)
if err := pp.boundCheck(); err.IsNotNil() {
return ErrPort, err

View File

@@ -5,6 +5,7 @@ import (
"strings"
"github.com/docker/docker/api/types"
"github.com/sirupsen/logrus"
D "github.com/yusing/go-proxy/docker"
E "github.com/yusing/go-proxy/error"
M "github.com/yusing/go-proxy/models"
@@ -39,10 +40,10 @@ func (p DockerProvider) GetProxyEntries() (M.ProxyEntries, E.NestedError) {
info, err := D.GetClientInfo(p.dockerHost)
if err.IsNotNil() {
return entries, E.From(err)
return entries, err
}
errors := E.NewBuilder("errors when parse docker labels for %q", p.dockerHost)
errors := E.NewBuilder("errors when parse docker labels")
for _, container := range info.Containers {
en, err := p.getEntriesFromLabels(&container, info.Host)
@@ -93,9 +94,9 @@ func (p *DockerProvider) getEntriesFromLabels(container *types.Container, client
entries := M.NewProxyEntries()
// find first port, return if no port exposed
defaultPort := findFirstPort(container)
if defaultPort == PT.NoPort {
return entries, E.Nil()
defaultPort, err := findFirstPort(container)
if err.IsNotNil() {
logrus.Debug(mainAlias, " ", err.Error())
}
// init entries map for all aliases
@@ -103,7 +104,7 @@ func (p *DockerProvider) getEntriesFromLabels(container *types.Container, client
entries.Set(string(a), &M.ProxyEntry{
Alias: string(a),
Host: clientHost,
Port: fmt.Sprint(defaultPort),
Port: defaultPort,
})
})
@@ -136,15 +137,23 @@ func (p *DockerProvider) getEntriesFromLabels(container *types.Container, client
}
}
entries.EachKV(func(a string, e *M.ProxyEntry) {
if e.Port == "" {
entries.UnsafeDelete(a)
}
})
return entries, errors.Build()
}
func findFirstPort(c *types.Container) (pp PT.Port) {
func findFirstPort(c *types.Container) (string, E.NestedError) {
if len(c.Ports) == 0 {
return "", E.FailureWhy("findFirstPort", "no port exposed")
}
for _, p := range c.Ports {
if p.PublicPort != 0 || c.HostConfig.NetworkMode == "host" {
pp, _ = PT.NewPortInt(int(p.PublicPort))
return
if p.PublicPort != 0 {
return fmt.Sprint(p.PublicPort), E.Nil()
}
}
return PT.NoPort
return "", E.Failure("findFirstPort")
}

View File

@@ -175,12 +175,13 @@ func (p *Provider) processReloadRequests() {
select {
case p.cooldownCh <- struct{}{}:
p.l.Info("Starting to reload routes")
nRoutes := p.routes.Size()
p.StopAllRoutes()
p.loadRoutes()
p.StartAllRoutes()
p.l.Info("Routes reloaded")
p.l.Infof("Routes reloaded (%d -> %d)", nRoutes, p.routes.Size())
go func() {
time.Sleep(reloadCooldown)
@@ -212,4 +213,4 @@ func (p *Provider) loadRoutes() E.NestedError {
return errors.Build()
}
const reloadCooldown = 300 * time.Millisecond
const reloadCooldown = 50 * time.Millisecond

View File

@@ -135,6 +135,10 @@ func (m *Map[KT, VT]) Delete(key KT) {
m.Unlock()
}
func (m *Map[KT, VT]) UnsafeDelete(key KT) {
delete(m.m, key)
}
// MergeWith merges the contents of another Map[KT, VT]
// into the current Map[KT, VT] and
// returns a map that were duplicated.