2023-02-02 10:55:24 +01:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata"
|
2023-03-07 23:10:42 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/auth/agent"
|
2023-03-02 13:05:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/client"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
|
2023-02-02 10:55:24 +01:00
|
|
|
"github.com/pkg/errors"
|
2023-03-02 13:05:24 +01:00
|
|
|
"gitlab.com/wpetit/goweb/api"
|
2023-02-02 10:55:24 +01:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Agent struct {
|
2023-03-02 13:05:24 +01:00
|
|
|
thumbprint string
|
|
|
|
privateKey jwk.Key
|
2023-03-07 23:10:42 +01:00
|
|
|
serverURL string
|
2023-02-02 10:55:24 +01:00
|
|
|
controllers []Controller
|
|
|
|
interval time.Duration
|
2023-03-02 13:05:24 +01:00
|
|
|
collectors []metadata.Collector
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) Run(ctx context.Context) error {
|
|
|
|
state := NewState()
|
|
|
|
|
|
|
|
logger.Info(ctx, "starting reconciliation ticker", logger.F("interval", a.interval))
|
|
|
|
|
|
|
|
ticker := time.NewTicker(a.interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
logger.Info(ctx, "generating token")
|
|
|
|
token, err := agent.GenerateToken(a.privateKey, a.thumbprint)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := client.New(a.serverURL, client.WithToken(token))
|
|
|
|
|
|
|
|
ctx = withClient(ctx, client)
|
2023-03-02 13:05:24 +01:00
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
logger.Debug(ctx, "registering agent")
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
if err := a.registerAgent(ctx, client, state); err != nil {
|
2023-03-02 13:05:24 +01:00
|
|
|
logger.Error(ctx, "could not register agent", logger.E(errors.WithStack(err)))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
logger.Debug(ctx, "state before reconciliation", logger.F("state", state))
|
|
|
|
|
|
|
|
if err := a.Reconcile(ctx, state); err != nil {
|
|
|
|
logger.Error(ctx, "could not reconcile node with state", logger.E(errors.WithStack(err)))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug(ctx, "state after reconciliation", logger.F("state", state))
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.WithStack(ctx.Err())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) Reconcile(ctx context.Context, state *State) error {
|
|
|
|
for _, ctrl := range a.controllers {
|
|
|
|
ctrlCtx := logger.With(ctx, logger.F("controller", ctrl.Name()))
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctrlCtx, "executing controller",
|
|
|
|
logger.F("state", state),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := ctrl.Reconcile(ctrlCtx, state); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
func (a *Agent) registerAgent(ctx context.Context, client *client.Client, state *State) error {
|
2023-03-02 13:05:24 +01:00
|
|
|
meta, err := a.collectMetadata(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sorted := metadata.Sort(meta)
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
agent, err := client.RegisterAgent(ctx, a.privateKey, a.thumbprint, sorted)
|
2023-03-02 13:05:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.agentID = agent.ID
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) collectMetadata(ctx context.Context) (map[string]any, error) {
|
|
|
|
metadata := make(map[string]any)
|
|
|
|
|
|
|
|
for _, collector := range a.collectors {
|
|
|
|
name, value, err := collector.Collect(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not collect metadata",
|
|
|
|
logger.E(errors.WithStack(err)), logger.F("name", name),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata[name] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAPIError(err error, code api.ErrorCode) (bool, any) {
|
|
|
|
apiError := &api.Error{}
|
|
|
|
if errors.As(err, &apiError) && apiError.Code == code {
|
|
|
|
return true, apiError.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(serverURL string, privateKey jwk.Key, thumbprint string, funcs ...OptionFunc) *Agent {
|
2023-02-02 10:55:24 +01:00
|
|
|
opt := defaultOption()
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Agent{
|
2023-03-07 23:10:42 +01:00
|
|
|
serverURL: serverURL,
|
2023-03-02 13:05:24 +01:00
|
|
|
privateKey: privateKey,
|
|
|
|
thumbprint: thumbprint,
|
2023-02-02 10:55:24 +01:00
|
|
|
controllers: opt.Controllers,
|
|
|
|
interval: opt.Interval,
|
2023-03-02 13:05:24 +01:00
|
|
|
collectors: opt.Collectors,
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
}
|