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

82 lines
1.8 KiB
Go

package spec
import (
"context"
"forge.cadoles.com/Cadoles/emissary/internal/agent"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"forge.cadoles.com/Cadoles/emissary/pkg/client"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
type Controller struct{}
// 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)
agent, err := cl.GetAgent(
ctx,
state.AgentID(),
)
if err != nil {
return errors.WithStack(err)
}
if err := c.reconcileAgent(ctx, cl, state, agent); err != nil {
return errors.WithStack(err)
}
return nil
}
func (c *Controller) reconcileAgent(ctx context.Context, client *client.Client, state *agent.State, agent *datastore.Agent) error {
ctx = logger.With(ctx, logger.F("agentID", agent.ID))
if agent.Status != datastore.AgentStatusAccepted {
logger.Warn(ctx, "unexpected agent status", logger.F("status", agent.Status))
return nil
}
specHeaders, err := client.QueryAgentSpecs(ctx, agent.ID)
if err != nil {
err = errors.WithStack(err)
logger.Error(ctx, "could not query agent specs", logger.CapturedE(err))
return nil
}
state.ClearSpecs()
for _, sh := range specHeaders {
spec, err := client.GetAgentSpec(ctx, agent.ID, sh.DefinitionName, sh.DefinitionVersion)
if err != nil {
logger.Error(
ctx, "could not retrieve agent spec",
logger.F("specName", sh.DefinitionName),
logger.F("specVersion", sh.DefinitionVersion),
logger.CapturedE(errors.WithStack(err)),
)
continue
}
state.SetSpec(spec)
}
return nil
}
func NewController() *Controller {
return &Controller{}
}
var _ agent.Controller = &Controller{}