Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent/multi-source: Implementation of multi-source model in the agent #80

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/v1alpha1/agent/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ components:
required:
- inventory
- agentId

AgentStatusUpdate:
type: object
properties:
Expand Down
44 changes: 22 additions & 22 deletions api/v1alpha1/agent/spec.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions api/v1alpha1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,6 @@ components:
type: string
inventory:
$ref: '#/components/schemas/Inventory'
credentialUrl:
type: string
createdAt:
type: string
format: date-time
Expand Down Expand Up @@ -328,7 +326,7 @@ components:
type: array
items:
$ref: '#/components/schemas/Source'

SourceAgentItem:
type: object
properties:
Expand Down
42 changes: 21 additions & 21 deletions api/v1alpha1/spec.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions api/v1alpha1/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion cmd/planner-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (
"fmt"
"os"

"github.com/google/uuid"
"github.com/kubev2v/migration-planner/internal/agent"
"github.com/kubev2v/migration-planner/pkg/log"
)

var (
agentID string
)

func main() {
command := NewAgentCommand()
if err := command.Execute(); err != nil {
Expand All @@ -30,6 +35,7 @@ func NewAgentCommand() *agentCmd {
}

flag.StringVar(&a.configFile, "config", agent.DefaultConfigFile, "Path to the agent's configuration file.")
flag.StringVar(&agentID, "id", os.Getenv("AGENT_ID"), "ID of the agent")

flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
Expand All @@ -52,7 +58,7 @@ func NewAgentCommand() *agentCmd {
}

func (a *agentCmd) Execute() error {
agentInstance := agent.New(a.log, a.config)
agentInstance := agent.New(uuid.MustParse(agentID), a.log, a.config)
if err := agentInstance.Run(context.Background()); err != nil {
a.log.Fatalf("running device agent: %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/planner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func NewPlannerCtlCommand() *cobra.Command {
},
}
cmd.AddCommand(cli.NewCmdGet())
cmd.AddCommand(cli.NewCmdCreate())
cmd.AddCommand(cli.NewCmdDelete())
cmd.AddCommand(cli.NewCmdVersion())

Expand Down
45 changes: 38 additions & 7 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"errors"
"fmt"
"net"
"net/url"
Expand All @@ -10,6 +11,8 @@ import (
"syscall"
"time"

"github.com/google/uuid"
api "github.com/kubev2v/migration-planner/api/v1alpha1"
"github.com/kubev2v/migration-planner/internal/agent/client"
"github.com/kubev2v/migration-planner/pkg/log"
"github.com/lthibault/jitterbug"
Expand All @@ -28,11 +31,12 @@ const (
var version string

// New creates a new agent.
func New(log *log.PrefixLogger, config *Config) *Agent {
func New(id uuid.UUID, log *log.PrefixLogger, config *Config) *Agent {
return &Agent{
config: config,
log: log,
healtCheckStopCh: make(chan chan any),
id: id,
}
}

Expand All @@ -42,6 +46,7 @@ type Agent struct {
server *Server
healtCheckStopCh chan chan any
credUrl string
id uuid.UUID
}

func (a *Agent) GetLogPrefix() string {
Expand All @@ -67,7 +72,10 @@ func (a *Agent) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
a.start(ctx, client)

<-sig
select {
case <-sig:
case <-ctx.Done():
}

a.log.Info("stopping agent...")

Expand All @@ -91,9 +99,12 @@ func (a *Agent) Stop() {
}

func (a *Agent) start(ctx context.Context, plannerClient client.Planner) {
inventoryUpdater := NewInventoryUpdater(a.log, a.id, plannerClient)
statusUpdater := NewStatusUpdater(a.log, a.id, version, a.credUrl, a.config, plannerClient)

// start server
a.server = NewServer(defaultAgentPort, a.config.DataDir, a.config.WwwDir)
go a.server.Start(a.log)
go a.server.Start(a.log, statusUpdater)

// get the credentials url
a.initializeCredentialUrl()
Expand All @@ -115,9 +126,15 @@ func (a *Agent) start(ctx context.Context, plannerClient client.Planner) {
collector := NewCollector(a.log, a.config.DataDir)
collector.collect(ctx)

inventoryUpdater := NewInventoryUpdater(a.log, a.config, a.credUrl, plannerClient)
updateTicker := jitterbug.New(time.Duration(a.config.UpdateInterval.Duration), &jitterbug.Norm{Stdev: 30 * time.Millisecond, Mean: 0})

/*
Main loop
The status of agent is always computed even if we don't have connectivity with the backend.
If we're connected to the backend, the agent sends its status and if the status is UpToDate,
it sends the inventory.
In case of "source gone", it stops everything and break from the loop.
*/
go func() {
for {
select {
Expand All @@ -126,16 +143,30 @@ func (a *Agent) start(ctx context.Context, plannerClient client.Planner) {
case <-updateTicker.C:
}

// calculate status regardless if we have connectivity withe the backend.
status, statusInfo, inventory := statusUpdater.CalculateStatus()

// check for health. Send requests only if we have connectivity
if healthChecker.State() == HealthCheckStateConsoleUnreachable {
continue
}

// set the status
inventoryUpdater.UpdateServiceWithInventory(ctx)
if err := statusUpdater.UpdateStatus(ctx, status, statusInfo); err != nil {
if errors.Is(err, client.ErrSourceGone) {
a.log.Info("Source is gone..Stop sending requests")
// stop the server and the healthchecker
a.Stop()
break
}
a.log.Errorf("unable to update agent status: %s", err)
continue // skip inventory update if we cannot update agent's state.
}

if status == api.AgentStatusUpToDate {
inventoryUpdater.UpdateServiceWithInventory(ctx, api.SourceStatusUpToDate, "Inventory collected with success", inventory)
}
}
}()

}

func (a *Agent) initializeCredentialUrl() {
Expand Down
Loading
Loading