mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 08:48:32 +02:00
refactor(docker): simplify docker host parsing
This commit is contained in:
@@ -2,7 +2,6 @@ package docker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
"maps"
|
||||||
"net"
|
"net"
|
||||||
@@ -17,7 +16,6 @@ import (
|
|||||||
"github.com/moby/moby/client"
|
"github.com/moby/moby/client"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/godoxy/agent/pkg/agent"
|
"github.com/yusing/godoxy/agent/pkg/agent"
|
||||||
"github.com/yusing/godoxy/internal/common"
|
|
||||||
"github.com/yusing/godoxy/internal/types"
|
"github.com/yusing/godoxy/internal/types"
|
||||||
httputils "github.com/yusing/goutils/http"
|
httputils "github.com/yusing/goutils/http"
|
||||||
"github.com/yusing/goutils/task"
|
"github.com/yusing/goutils/task"
|
||||||
@@ -118,7 +116,7 @@ func Clients() map[string]*SharedClient {
|
|||||||
// Returns existing client if available.
|
// Returns existing client if available.
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - host: the host to connect to (either a URL or common.DockerHostFromEnv).
|
// - host: the host to connect to (either a URL or client.DefaultDockerHost).
|
||||||
//
|
//
|
||||||
// Returns:
|
// Returns:
|
||||||
// - Client: the Docker client connection.
|
// - Client: the Docker client connection.
|
||||||
@@ -161,27 +159,18 @@ func NewClient(cfg types.DockerProviderConfig, unique ...bool) (*SharedClient, e
|
|||||||
addr = "tcp://" + cfg.Addr
|
addr = "tcp://" + cfg.Addr
|
||||||
dial = cfg.DialContext
|
dial = cfg.DialContext
|
||||||
} else {
|
} else {
|
||||||
switch host {
|
helper, err := connhelper.GetConnectionHelper(host)
|
||||||
case "":
|
if err != nil {
|
||||||
return nil, errors.New("empty docker host")
|
log.Panic().Err(err).Msg("failed to get connection helper")
|
||||||
case common.DockerHostFromEnv:
|
}
|
||||||
|
if helper != nil {
|
||||||
opt = []client.Opt{
|
opt = []client.Opt{
|
||||||
client.WithHostFromEnv(),
|
client.WithHost(helper.Host),
|
||||||
|
client.WithDialContext(helper.Dialer),
|
||||||
}
|
}
|
||||||
default:
|
} else {
|
||||||
helper, err := connhelper.GetConnectionHelper(host)
|
opt = []client.Opt{
|
||||||
if err != nil {
|
client.WithHost(host),
|
||||||
log.Panic().Err(err).Msg("failed to get connection helper")
|
|
||||||
}
|
|
||||||
if helper != nil {
|
|
||||||
opt = []client.Opt{
|
|
||||||
client.WithHost(helper.Host),
|
|
||||||
client.WithDialContext(helper.Dialer),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
opt = []client.Opt{
|
|
||||||
client.WithHost(host),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,17 +8,14 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/moby/moby/client"
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/yusing/godoxy/agent/pkg/agent"
|
"github.com/yusing/godoxy/agent/pkg/agent"
|
||||||
"github.com/yusing/godoxy/internal/common"
|
|
||||||
"github.com/yusing/godoxy/internal/docker"
|
"github.com/yusing/godoxy/internal/docker"
|
||||||
"github.com/yusing/godoxy/internal/route"
|
"github.com/yusing/godoxy/internal/route"
|
||||||
provider "github.com/yusing/godoxy/internal/route/provider/types"
|
provider "github.com/yusing/godoxy/internal/route/provider/types"
|
||||||
"github.com/yusing/godoxy/internal/types"
|
"github.com/yusing/godoxy/internal/types"
|
||||||
W "github.com/yusing/godoxy/internal/watcher"
|
W "github.com/yusing/godoxy/internal/watcher"
|
||||||
"github.com/yusing/godoxy/internal/watcher/events"
|
"github.com/yusing/godoxy/internal/watcher/events"
|
||||||
"github.com/yusing/goutils/env"
|
|
||||||
gperr "github.com/yusing/goutils/errs"
|
gperr "github.com/yusing/goutils/errs"
|
||||||
"github.com/yusing/goutils/task"
|
"github.com/yusing/goutils/task"
|
||||||
)
|
)
|
||||||
@@ -70,10 +67,6 @@ func NewFileProvider(filename string) (p *Provider, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerProvider(name string, dockerCfg types.DockerProviderConfig) *Provider {
|
func NewDockerProvider(name string, dockerCfg types.DockerProviderConfig) *Provider {
|
||||||
if dockerCfg.URL == common.DockerHostFromEnv {
|
|
||||||
dockerCfg.URL = env.GetEnvString("DOCKER_HOST", client.DefaultDockerHost)
|
|
||||||
}
|
|
||||||
|
|
||||||
p := newProvider(provider.ProviderTypeDocker)
|
p := newProvider(provider.ProviderTypeDocker)
|
||||||
p.ProviderImpl = DockerProviderImpl(name, dockerCfg)
|
p.ProviderImpl = DockerProviderImpl(name, dockerCfg)
|
||||||
p.watcher = p.NewWatcher()
|
p.watcher = p.NewWatcher()
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/yusing/godoxy/internal/common"
|
"github.com/yusing/godoxy/internal/common"
|
||||||
"github.com/yusing/godoxy/internal/serialization"
|
"github.com/yusing/godoxy/internal/serialization"
|
||||||
|
"github.com/yusing/goutils/env"
|
||||||
gperr "github.com/yusing/goutils/errs"
|
gperr "github.com/yusing/goutils/errs"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,6 +37,11 @@ func (cfg *DockerProviderConfig) MarshalJSON() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *DockerProviderConfig) Parse(value string) error {
|
func (cfg *DockerProviderConfig) Parse(value string) error {
|
||||||
|
if value == common.DockerHostFromEnv {
|
||||||
|
cfg.URL = env.GetEnvString("DOCKER_HOST", "unix:///var/run/docker.sock")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
u, err := url.Parse(value)
|
u, err := url.Parse(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user