emissary/internal/agent/controller/spec/controller.go

76 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-02-02 10:55:24 +01:00
package spec
import (
"context"
"forge.cadoles.com/Cadoles/emissary/internal/agent"
"forge.cadoles.com/Cadoles/emissary/internal/client"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
type Controller struct{}
2023-02-02 10:55:24 +01:00
// Name implements node.Controller.
func (c *Controller) Name() string {
return "spec-controller"
}
// Reconcile implements node.Controller.
func (c *Controller) Reconcile(ctx context.Context, state *agent.State) error {
cl := agent.Client(ctx)
agents, _, err := cl.QueryAgents(
ctx,
client.WithQueryAgentsLimit(1),
client.WithQueryAgentsID(state.AgentID()),
)
2023-02-02 10:55:24 +01:00
if err != nil {
return errors.WithStack(err)
}
if len(agents) == 0 {
logger.Error(ctx, "could not find remote matching agent")
2023-02-02 10:55:24 +01:00
return nil
}
2023-02-02 10:55:24 +01:00
if err := c.reconcileAgent(ctx, cl, state, agents[0]); err != nil {
return errors.WithStack(err)
2023-02-02 10:55:24 +01:00
}
return nil
}
func (c *Controller) reconcileAgent(ctx context.Context, client *client.Client, state *agent.State, agent *datastore.Agent) error {
2023-02-02 10:55:24 +01:00
ctx = logger.With(ctx, logger.F("agentID", agent.ID))
if agent.Status != datastore.AgentStatusAccepted {
logger.Error(ctx, "unexpected agent status", logger.F("status", agent.Status))
return nil
}
specs, err := client.GetAgentSpecs(ctx, agent.ID)
2023-02-02 10:55:24 +01:00
if err != nil {
logger.Error(ctx, "could not retrieve agent specs", logger.E(errors.WithStack(err)))
return nil
}
state.ClearSpecs()
for _, spec := range specs {
state.SetSpec(spec)
}
return nil
}
func NewController() *Controller {
return &Controller{}
2023-02-02 10:55:24 +01:00
}
var _ agent.Controller = &Controller{}