mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-15 06:26:47 +02:00
refactor(agent): move agent pool to agent package, rename route.Agent() to route.GetAgent()
This commit is contained in:
parent
cabb840a91
commit
7d17a01de1
13 changed files with 23 additions and 55 deletions
|
@ -5,18 +5,18 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListAgents(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
func ListAgents(w http.ResponseWriter, r *http.Request) {
|
||||||
if httpheaders.IsWebsocket(r.Header) {
|
if httpheaders.IsWebsocket(r.Header) {
|
||||||
gpwebsocket.Periodic(w, r, 10*time.Second, func(conn *websocket.Conn) error {
|
gpwebsocket.Periodic(w, r, 10*time.Second, func(conn *websocket.Conn) error {
|
||||||
return conn.WriteJSON(cfg.ListAgents())
|
return conn.WriteJSON(agent.ListAgents())
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
gphttp.RespondJSON(w, r, cfg.ListAgents())
|
gphttp.RespondJSON(w, r, agent.ListAgents())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
|
@ -43,7 +44,7 @@ func getDockerClients() (DockerClients, gperr.Error) {
|
||||||
dockerClients[name] = dockerClient
|
dockerClients[name] = dockerClient
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, agent := range cfg.ListAgents() {
|
for _, agent := range agent.ListAgents() {
|
||||||
dockerClient, err := docker.NewClient(agent.FakeDockerHost())
|
dockerClient, err := docker.NewClient(agent.FakeDockerHost())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
connErrs.Add(err)
|
connErrs.Add(err)
|
||||||
|
@ -65,7 +66,7 @@ func getDockerClient(server string) (*docker.SharedClient, bool, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if host == "" {
|
if host == "" {
|
||||||
for _, agent := range cfg.ListAgents() {
|
for _, agent := range agent.ListAgents() {
|
||||||
if agent.Name() == server {
|
if agent.Name() == server {
|
||||||
host = agent.FakeDockerHost()
|
host = agent.FakeDockerHost()
|
||||||
break
|
break
|
||||||
|
|
|
@ -39,7 +39,7 @@ func NewAgent(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hostport := fmt.Sprintf("%s:%d", host, port)
|
hostport := fmt.Sprintf("%s:%d", host, port)
|
||||||
if _, ok := config.GetInstance().GetAgent(hostport); ok {
|
if _, ok := agent.GetAgent(hostport); ok {
|
||||||
gphttp.KeyAlreadyExists(w, "agent", hostport)
|
gphttp.KeyAlreadyExists(w, "agent", hostport)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
|
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/metrics/systeminfo"
|
"github.com/yusing/go-proxy/internal/metrics/systeminfo"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
|
@ -13,7 +12,7 @@ import (
|
||||||
nettypes "github.com/yusing/go-proxy/internal/net/types"
|
nettypes "github.com/yusing/go-proxy/internal/net/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
func SystemInfo(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
agentAddr := query.Get("agent_addr")
|
agentAddr := query.Get("agent_addr")
|
||||||
query.Del("agent_addr")
|
query.Del("agent_addr")
|
||||||
|
@ -22,7 +21,7 @@ func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Reques
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
agent, ok := cfg.GetAgent(agentAddr)
|
agent, ok := agentPkg.GetAgent(agentAddr)
|
||||||
if !ok {
|
if !ok {
|
||||||
gphttp.NotFound(w, "agent_addr")
|
gphttp.NotFound(w, "agent_addr")
|
||||||
return
|
return
|
||||||
|
|
|
@ -6,31 +6,8 @@ import (
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/route/provider"
|
"github.com/yusing/go-proxy/internal/route/provider"
|
||||||
"github.com/yusing/go-proxy/internal/utils/functional"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var agentPool = functional.NewMapOf[string, *agent.AgentConfig]()
|
|
||||||
|
|
||||||
func addAgent(agent *agent.AgentConfig) {
|
|
||||||
agentPool.Store(agent.Addr, agent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeAllAgents() {
|
|
||||||
agentPool.Clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAgent(addr string) (agent *agent.AgentConfig, ok bool) {
|
|
||||||
agent, ok = agentPool.Load(addr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *Config) GetAgent(agentAddrOrDockerHost string) (*agent.AgentConfig, bool) {
|
|
||||||
if !agent.IsDockerHostAgent(agentAddrOrDockerHost) {
|
|
||||||
return GetAgent(agentAddrOrDockerHost)
|
|
||||||
}
|
|
||||||
return GetAgent(agent.GetAgentAddrFromDockerHost(agentAddrOrDockerHost))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *Config) VerifyNewAgent(host string, ca agent.PEMPair, client agent.PEMPair) (int, gperr.Error) {
|
func (cfg *Config) VerifyNewAgent(host string, ca agent.PEMPair, client agent.PEMPair) (int, gperr.Error) {
|
||||||
if slices.ContainsFunc(cfg.value.Providers.Agents, func(a *agent.AgentConfig) bool {
|
if slices.ContainsFunc(cfg.value.Providers.Agents, func(a *agent.AgentConfig) bool {
|
||||||
return a.Addr == host
|
return a.Addr == host
|
||||||
|
@ -44,23 +21,17 @@ func (cfg *Config) VerifyNewAgent(host string, ca agent.PEMPair, client agent.PE
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, gperr.Wrap(err, "failed to start agent")
|
return 0, gperr.Wrap(err, "failed to start agent")
|
||||||
}
|
}
|
||||||
addAgent(&agentCfg)
|
agent.AddAgent(&agentCfg)
|
||||||
|
|
||||||
provider := provider.NewAgentProvider(&agentCfg)
|
provider := provider.NewAgentProvider(&agentCfg)
|
||||||
if err := cfg.errIfExists(provider); err != nil {
|
if err := cfg.errIfExists(provider); err != nil {
|
||||||
|
agent.RemoveAgent(&agentCfg)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
err = provider.LoadRoutes()
|
err = provider.LoadRoutes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
agent.RemoveAgent(&agentCfg)
|
||||||
return 0, gperr.Wrap(err, "failed to load routes")
|
return 0, gperr.Wrap(err, "failed to load routes")
|
||||||
}
|
}
|
||||||
return provider.NumRoutes(), nil
|
return provider.NumRoutes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) ListAgents() []*agent.AgentConfig {
|
|
||||||
agents := make([]*agent.AgentConfig, 0, agentPool.Size())
|
|
||||||
agentPool.RangeAll(func(key string, value *agent.AgentConfig) {
|
|
||||||
agents = append(agents, value)
|
|
||||||
})
|
|
||||||
return agents
|
|
||||||
}
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/internal/api"
|
"github.com/yusing/go-proxy/internal/api"
|
||||||
autocert "github.com/yusing/go-proxy/internal/autocert"
|
autocert "github.com/yusing/go-proxy/internal/autocert"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
|
@ -323,14 +324,14 @@ func (cfg *Config) loadRouteProviders(providers *config.Providers) gperr.Error {
|
||||||
errs := gperr.NewBuilder("route provider errors")
|
errs := gperr.NewBuilder("route provider errors")
|
||||||
results := gperr.NewBuilder("loaded route providers")
|
results := gperr.NewBuilder("loaded route providers")
|
||||||
|
|
||||||
removeAllAgents()
|
agentPkg.RemoveAllAgents()
|
||||||
|
|
||||||
for _, agent := range providers.Agents {
|
for _, agent := range providers.Agents {
|
||||||
if err := agent.Start(cfg.task.Context()); err != nil {
|
if err := agent.Start(cfg.task.Context()); err != nil {
|
||||||
errs.Add(gperr.PrependSubject(agent.String(), err))
|
errs.Add(gperr.PrependSubject(agent.String(), err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
addAgent(agent)
|
agentPkg.AddAgent(agent)
|
||||||
p := proxy.NewAgentProvider(agent)
|
p := proxy.NewAgentProvider(agent)
|
||||||
if err := cfg.errIfExists(p); err != nil {
|
if err := cfg.errIfExists(p); err != nil {
|
||||||
errs.Add(err.Subject(p.String()))
|
errs.Add(err.Subject(p.String()))
|
||||||
|
|
|
@ -52,9 +52,7 @@ type (
|
||||||
Statistics() map[string]any
|
Statistics() map[string]any
|
||||||
RouteProviderList() []RouteProviderListResponse
|
RouteProviderList() []RouteProviderListResponse
|
||||||
Context() context.Context
|
Context() context.Context
|
||||||
GetAgent(agentAddrOrDockerHost string) (*agent.AgentConfig, bool)
|
|
||||||
VerifyNewAgent(host string, ca agent.PEMPair, client agent.PEMPair) (int, gperr.Error)
|
VerifyNewAgent(host string, ca agent.PEMPair, client agent.PEMPair) (int, gperr.Error)
|
||||||
ListAgents() []*agent.AgentConfig
|
|
||||||
AutoCertProvider() *autocert.Provider
|
AutoCertProvider() *autocert.Provider
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -16,7 +16,6 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -122,7 +121,7 @@ func NewClient(host string) (*SharedClient, error) {
|
||||||
var dial func(ctx context.Context) (net.Conn, error)
|
var dial func(ctx context.Context) (net.Conn, error)
|
||||||
|
|
||||||
if agent.IsDockerHostAgent(host) {
|
if agent.IsDockerHostAgent(host) {
|
||||||
cfg, ok := config.GetInstance().GetAgent(host)
|
cfg, ok := agent.GetAgent(host)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Errorf("agent %q not found", host))
|
panic(fmt.Errorf("agent %q not found", host))
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
|
@ -102,7 +101,7 @@ func FromDocker(c *container.SummaryTrimmed, dockerHost string) (res *Container)
|
||||||
|
|
||||||
if agent.IsDockerHostAgent(dockerHost) {
|
if agent.IsDockerHostAgent(dockerHost) {
|
||||||
var ok bool
|
var ok bool
|
||||||
res.Agent, ok = config.GetInstance().GetAgent(dockerHost)
|
res.Agent, ok = agent.GetAgent(dockerHost)
|
||||||
if !ok {
|
if !ok {
|
||||||
res.addError(fmt.Errorf("agent %q not found", dockerHost))
|
res.addError(fmt.Errorf("agent %q not found", dockerHost))
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, gperr.Error) {
|
||||||
proxyURL := base.ProxyURL
|
proxyURL := base.ProxyURL
|
||||||
|
|
||||||
var trans *http.Transport
|
var trans *http.Transport
|
||||||
a := base.Agent()
|
a := base.GetAgent()
|
||||||
if a != nil {
|
if a != nil {
|
||||||
trans = a.Transport()
|
trans = a.Transport()
|
||||||
proxyURL = nettypes.NewURL(agent.HTTPProxyURL)
|
proxyURL = nettypes.NewURL(agent.HTTPProxyURL)
|
||||||
|
|
|
@ -351,7 +351,7 @@ func (r *Route) Type() route.RouteType {
|
||||||
panic(fmt.Errorf("unexpected scheme %s for alias %s", r.Scheme, r.Alias))
|
panic(fmt.Errorf("unexpected scheme %s for alias %s", r.Scheme, r.Alias))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) Agent() *agent.AgentConfig {
|
func (r *Route) GetAgent() *agent.AgentConfig {
|
||||||
if r.Container == nil {
|
if r.Container == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -359,7 +359,7 @@ func (r *Route) Agent() *agent.AgentConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) IsAgent() bool {
|
func (r *Route) IsAgent() bool {
|
||||||
return r.Container != nil && r.Container.Agent != nil
|
return r.GetAgent() != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) HealthMonitor() health.HealthMonitor {
|
func (r *Route) HealthMonitor() health.HealthMonitor {
|
||||||
|
|
|
@ -38,7 +38,7 @@ type (
|
||||||
HomepageItem() *homepage.Item
|
HomepageItem() *homepage.Item
|
||||||
ContainerInfo() *docker.Container
|
ContainerInfo() *docker.Container
|
||||||
|
|
||||||
Agent() *agent.AgentConfig
|
GetAgent() *agent.AgentConfig
|
||||||
|
|
||||||
IsDocker() bool
|
IsDocker() bool
|
||||||
IsAgent() bool
|
IsAgent() bool
|
||||||
|
|
|
@ -42,7 +42,7 @@ var ErrNegativeInterval = gperr.New("negative interval")
|
||||||
func NewMonitor(r routes.Route) health.HealthMonCheck {
|
func NewMonitor(r routes.Route) health.HealthMonCheck {
|
||||||
var mon health.HealthMonCheck
|
var mon health.HealthMonCheck
|
||||||
if r.IsAgent() {
|
if r.IsAgent() {
|
||||||
mon = NewAgentProxiedMonitor(r.Agent(), r.HealthCheckConfig(), AgentTargetFromURL(&r.TargetURL().URL))
|
mon = NewAgentProxiedMonitor(r.GetAgent(), r.HealthCheckConfig(), AgentTargetFromURL(&r.TargetURL().URL))
|
||||||
} else {
|
} else {
|
||||||
switch r := r.(type) {
|
switch r := r.(type) {
|
||||||
case routes.HTTPRoute:
|
case routes.HTTPRoute:
|
||||||
|
|
Loading…
Add table
Reference in a new issue