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/datastore"
|
2023-06-25 19:45:43 +02:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/pkg/client"
|
2023-02-02 10:55:24 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
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 {
|
2023-03-02 13:05:24 +01:00
|
|
|
cl := agent.Client(ctx)
|
|
|
|
|
2023-03-13 10:44:58 +01:00
|
|
|
agent, err := cl.GetAgent(
|
2023-03-02 13:05:24 +01:00
|
|
|
ctx,
|
2023-03-13 10:44:58 +01:00
|
|
|
state.AgentID(),
|
2023-03-02 13:05:24 +01:00
|
|
|
)
|
2023-02-02 10:55:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-03-13 10:44:58 +01:00
|
|
|
if err := c.reconcileAgent(ctx, cl, state, agent); err != nil {
|
2023-03-02 13:05:24 +01:00
|
|
|
return errors.WithStack(err)
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
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 {
|
2023-10-13 12:30:52 +02:00
|
|
|
logger.Warn(ctx, "unexpected agent status", logger.F("status", agent.Status))
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
specs, err := client.GetAgentSpecs(ctx, agent.ID)
|
2023-02-02 10:55:24 +01:00
|
|
|
if err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.Error(ctx, "could not retrieve agent specs", logger.CapturedE(err))
|
2023-02-02 10:55:24 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
state.ClearSpecs()
|
|
|
|
|
|
|
|
for _, spec := range specs {
|
|
|
|
state.SetSpec(spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
func NewController() *Controller {
|
|
|
|
return &Controller{}
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ agent.Controller = &Controller{}
|