mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-28 20:01:50 +01:00
refactor(agent): update AgentConfig struct to use exported fields and improve JSON serialization
This commit is contained in:
@@ -24,7 +24,7 @@ func GetAgent(agentAddrOrDockerHost string) (*AgentConfig, bool) {
|
|||||||
|
|
||||||
func GetAgentByName(name string) (*AgentConfig, bool) {
|
func GetAgentByName(name string) (*AgentConfig, bool) {
|
||||||
for _, agent := range agentPool.Range {
|
for _, agent := range agentPool.Range {
|
||||||
if agent.Name() == name {
|
if agent.Name == name {
|
||||||
return agent, true
|
return agent, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@@ -21,14 +20,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AgentConfig struct {
|
type AgentConfig struct {
|
||||||
Addr string
|
Addr string `json:"addr"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
tlsConfig *tls.Config
|
tlsConfig *tls.Config
|
||||||
name string
|
|
||||||
version string
|
|
||||||
l zerolog.Logger
|
l zerolog.Logger
|
||||||
}
|
} // @name Agent
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EndpointVersion = "/version"
|
EndpointVersion = "/version"
|
||||||
@@ -113,9 +112,9 @@ func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.name = string(name)
|
cfg.Name = string(name)
|
||||||
|
|
||||||
cfg.l = log.With().Str("agent", cfg.name).Logger()
|
cfg.l = log.With().Str("agent", cfg.Name).Logger()
|
||||||
|
|
||||||
// check agent version
|
// check agent version
|
||||||
agentVersionBytes, _, err := cfg.Fetch(ctx, EndpointVersion)
|
agentVersionBytes, _, err := cfg.Fetch(ctx, EndpointVersion)
|
||||||
@@ -123,14 +122,14 @@ func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.version = string(agentVersionBytes)
|
cfg.Version = string(agentVersionBytes)
|
||||||
agentVersion := pkg.ParseVersion(cfg.version)
|
agentVersion := pkg.ParseVersion(cfg.Version)
|
||||||
|
|
||||||
if serverVersion.IsNewerMajorThan(agentVersion) {
|
if serverVersion.IsNewerMajorThan(agentVersion) {
|
||||||
log.Warn().Msgf("agent %s major version mismatch: server: %s, agent: %s", cfg.name, serverVersion, agentVersion)
|
log.Warn().Msgf("agent %s major version mismatch: server: %s, agent: %s", cfg.Name, serverVersion, agentVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Msgf("agent %q initialized", cfg.name)
|
log.Info().Msgf("agent %q initialized", cfg.Name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,18 +179,6 @@ func (cfg *AgentConfig) DialContext(ctx context.Context) (net.Conn, error) {
|
|||||||
return dialer.DialContext(ctx, "tcp", cfg.Addr)
|
return dialer.DialContext(ctx, "tcp", cfg.Addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *AgentConfig) Name() string {
|
|
||||||
return cfg.name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *AgentConfig) String() string {
|
func (cfg *AgentConfig) String() string {
|
||||||
return cfg.name + "@" + cfg.Addr
|
return cfg.Name + "@" + cfg.Addr
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *AgentConfig) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(map[string]string{
|
|
||||||
"name": cfg.Name(),
|
|
||||||
"addr": cfg.Addr,
|
|
||||||
"version": cfg.version,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user