mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 08:48:32 +02:00
feat(docker): implement container ID to Docker host mapping
This commit is contained in:
21
internal/docker/id_lookup.go
Normal file
21
internal/docker/id_lookup.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package docker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
var idDockerHostMap = xsync.NewMap[string, string](xsync.WithPresize(100))
|
||||||
|
|
||||||
|
func GetDockerHostByContainerID(id string) (string, bool) {
|
||||||
|
return idDockerHostMap.Load(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetDockerHostByContainerID(id, host string) {
|
||||||
|
log.Debug().Str("id", id).Str("host", host).Int("size", idDockerHostMap.Size()).Msg("setting docker host by container id")
|
||||||
|
idDockerHostMap.Store(id, host)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteDockerHostByContainerID(id string) {
|
||||||
|
idDockerHostMap.Delete(id)
|
||||||
|
}
|
||||||
@@ -24,7 +24,6 @@ import (
|
|||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/route/routes"
|
|
||||||
"github.com/yusing/go-proxy/internal/route/rules"
|
"github.com/yusing/go-proxy/internal/route/rules"
|
||||||
route "github.com/yusing/go-proxy/internal/route/types"
|
route "github.com/yusing/go-proxy/internal/route/types"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
@@ -85,6 +84,7 @@ type (
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
}
|
}
|
||||||
Routes map[string]*Route
|
Routes map[string]*Route
|
||||||
|
Port = route.Port
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultHost = "localhost"
|
const DefaultHost = "localhost"
|
||||||
@@ -298,25 +298,20 @@ func (r *Route) start(parent task.Parent) gperr.Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.impl.Start(parent); err != nil {
|
if cont := r.ContainerInfo(); cont != nil {
|
||||||
return err
|
docker.SetDockerHostByContainerID(cont.ContainerID, cont.DockerHost)
|
||||||
}
|
}
|
||||||
|
|
||||||
if conflict, added := routes.All.AddIfNotExists(r.impl); !added {
|
if err := r.impl.Start(parent); err != nil {
|
||||||
err := gperr.Errorf("route %s already exists: from %s and %s", r.Alias, r.ProviderName(), conflict.ProviderName())
|
|
||||||
r.task.FinishAndWait(err)
|
|
||||||
return err
|
return err
|
||||||
} else {
|
|
||||||
// reference here because r.impl will be nil after Finish() is called.
|
|
||||||
impl := r.impl
|
|
||||||
r.task.OnCancel("remove_routes_from_all", func() {
|
|
||||||
routes.All.Del(impl)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) Finish(reason any) {
|
func (r *Route) Finish(reason any) {
|
||||||
|
if cont := r.ContainerInfo(); cont != nil {
|
||||||
|
docker.DeleteDockerHostByContainerID(cont.ContainerID)
|
||||||
|
}
|
||||||
r.FinishAndWait(reason)
|
r.FinishAndWait(reason)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -421,16 +416,21 @@ func (r *Route) LoadBalanceConfig() *types.LoadBalancerConfig {
|
|||||||
return r.LoadBalance
|
return r.LoadBalance
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) HomepageConfig() *homepage.ItemConfig {
|
func (r *Route) HomepageItem() homepage.Item {
|
||||||
return r.Homepage.GetOverride(r.Alias)
|
containerID := ""
|
||||||
|
if r.Container != nil {
|
||||||
|
containerID = r.Container.ContainerID
|
||||||
|
}
|
||||||
|
return homepage.Item{
|
||||||
|
Alias: r.Alias,
|
||||||
|
Provider: r.Provider,
|
||||||
|
ItemConfig: *r.Homepage,
|
||||||
|
ContainerID: containerID,
|
||||||
|
}.GetOverride()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) HomepageItem() *homepage.Item {
|
func (r *Route) DisplayName() string {
|
||||||
return &homepage.Item{
|
return r.Homepage.Name
|
||||||
Alias: r.Alias,
|
|
||||||
Provider: r.Provider,
|
|
||||||
ItemConfig: r.HomepageConfig(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) ContainerInfo() *types.Container {
|
func (r *Route) ContainerInfo() *types.Container {
|
||||||
@@ -639,9 +639,11 @@ func (r *Route) FinalizeHomepageConfig() {
|
|||||||
isDocker := r.Container != nil
|
isDocker := r.Container != nil
|
||||||
|
|
||||||
if r.Homepage == nil {
|
if r.Homepage == nil {
|
||||||
r.Homepage = &homepage.ItemConfig{Show: true}
|
r.Homepage = &homepage.ItemConfig{
|
||||||
|
Show: true,
|
||||||
|
Name: r.Alias,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
r.Homepage = r.Homepage.GetOverride(r.Alias)
|
|
||||||
|
|
||||||
if r.ShouldExclude() && isDocker {
|
if r.ShouldExclude() && isDocker {
|
||||||
r.Homepage.Show = false
|
r.Homepage.Show = false
|
||||||
|
|||||||
Reference in New Issue
Block a user