refactor(config): streamline agent configuration and error handling

This commit is contained in:
yusing
2025-09-05 11:37:55 +08:00
parent 3e43f7d27f
commit 2e547d15c5

View File

@@ -15,23 +15,24 @@ func (cfg *Config) VerifyNewAgent(host string, ca agent.PEMPair, client agent.PE
return 0, gperr.New("agent already exists")
}
var agentCfg agent.AgentConfig
agentCfg.Addr = host
agentCfg := agent.AgentConfig{
Addr: host,
}
err := agentCfg.StartWithCerts(cfg.Task().Context(), ca.Cert, client.Cert, client.Key)
if err != nil {
return 0, gperr.Wrap(err, "failed to start agent")
}
agent.AddAgent(&agentCfg)
provider := provider.NewAgentProvider(&agentCfg)
if err := cfg.errIfExists(provider); err != nil {
agent.RemoveAgent(&agentCfg)
return 0, err
if _, loaded := cfg.providers.LoadOrStore(provider.String(), provider); loaded {
return 0, gperr.Errorf("provider %s already exists", provider.String())
}
err = provider.LoadRoutes()
if err != nil {
agent.RemoveAgent(&agentCfg)
return 0, gperr.Wrap(err, "failed to load routes")
}
agent.AddAgent(&agentCfg)
return provider.NumRoutes(), nil
}