Skip to content

Commit

Permalink
Adapt e2e to multi-source model
Browse files Browse the repository at this point in the history
Signed-off-by: Ondra Machacek <[email protected]>
  • Loading branch information
machacekondra committed Nov 28, 2024
1 parent 4737751 commit f051548
Show file tree
Hide file tree
Showing 17 changed files with 528 additions and 505 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ jobs:
- name: Prepare
run: |
# FIXME: Move to Makefile!
go install github.com/matryer/moq@latest
export PATH=$PATH:$(go env GOPATH)/bin
make generate
DOWNLOAD_RHCOS=false make build
Expand Down
32 changes: 13 additions & 19 deletions api/v1alpha1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,19 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/api/v1/sources/{id}/image:
/api/v1/image:
head:
tags:
- source
- image
description: head the OVA file for the source
operationId: headSourceImage
description: head the OVA image
operationId: headImage
parameters:
- name: id
in: path
description: ID of the source
required: true
- name: sshKey
in: query
description: public SSH key
required: false
schema:
type: string
format: uuid
responses:
"200":
description: An OVA image
Expand All @@ -150,18 +148,16 @@ paths:
description: Internal Server Error
get:
tags:
- source
- image
description: get the OVA file for the source
operationId: getSourceImage
description: get the OVA image
operationId: getImage
parameters:
- name: id
in: path
description: ID of the source
required: true
- name: sshKey
in: query
description: public SSH key
required: false
schema:
type: string
format: uuid
responses:
"200":
description: An OVA image
Expand Down Expand Up @@ -317,8 +313,6 @@ components:
properties:
name:
type: string
sshKey:
type: string
required:
- name

Expand Down
61 changes: 31 additions & 30 deletions api/v1alpha1/spec.gen.go

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

15 changes: 13 additions & 2 deletions api/v1alpha1/types.gen.go

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

4 changes: 4 additions & 0 deletions cmd/planner-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func NewAgentCommand() *agentCmd {
}

func (a *agentCmd) Execute() error {
// FIXME: !!!
if agentID == "" {
agentID = uuid.New().String()
}
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 data/ignition.template
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ storage:
data-dir: /agent/data
www-dir: /app/www
log-level: debug
source-id: {{.SourceId}}
update-interval: 5s
planner-service:
service:
Expand Down
17 changes: 8 additions & 9 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (a *Agent) start(ctx context.Context, plannerClient client.Planner) {
go a.server.Start(a.log, statusUpdater)

// get the credentials url
a.initializeCredentialUrl()
credUrl := a.initializeCredentialUrl()

// start the health check
healthChecker, err := NewHealthChecker(
Expand Down Expand Up @@ -151,7 +151,7 @@ func (a *Agent) start(ctx context.Context, plannerClient client.Planner) {
continue
}

if err := statusUpdater.UpdateStatus(ctx, status, statusInfo); err != nil {
if err := statusUpdater.UpdateStatus(ctx, status, statusInfo, credUrl); err != nil {
if errors.Is(err, client.ErrSourceGone) {
a.log.Info("Source is gone..Stop sending requests")
// stop the server and the healthchecker
Expand All @@ -169,13 +169,12 @@ func (a *Agent) start(ctx context.Context, plannerClient client.Planner) {
}()
}

func (a *Agent) initializeCredentialUrl() {
func (a *Agent) initializeCredentialUrl() string {
// Parse the service URL
parsedURL, err := url.Parse(a.config.PlannerService.Service.Server)
if err != nil {
a.log.Errorf("error parsing service URL: %v", err)
a.credUrl = "N/A"
return
return "N/A"
}

// Use either port if specified, or scheme
Expand All @@ -188,14 +187,14 @@ func (a *Agent) initializeCredentialUrl() {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", parsedURL.Hostname(), port))
if err != nil {
a.log.Errorf("failed connecting to migration planner: %v", err)
a.credUrl = "N/A"
return
return "N/A"
}
defer conn.Close()

localAddr := conn.LocalAddr().(*net.TCPAddr)
a.credUrl = fmt.Sprintf("http://%s:%d", localAddr.IP.String(), defaultAgentPort)
a.log.Infof("Discovered Agent IP address: %s", a.credUrl)
credUrl := fmt.Sprintf("http://%s:%d", localAddr.IP.String(), defaultAgentPort)
a.log.Infof("Discovered Agent IP address: %s", credUrl)
return credUrl
}

func newPlannerClient(cfg *Config) (client.Planner, error) {
Expand Down
6 changes: 2 additions & 4 deletions internal/agent/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ func NewInventoryUpdater(log *log.PrefixLogger, agentID uuid.UUID, client client

func (u *InventoryUpdater) UpdateServiceWithInventory(ctx context.Context, status api.SourceStatus, statusInfo string, inventory *api.Inventory) {
update := agentapi.SourceStatusUpdate{
Status: string(status),
StatusInfo: statusInfo,
Inventory: *inventory,
AgentId: u.agentID,
Inventory: *inventory,
AgentId: u.agentID,
}

newContents, err := json.Marshal(update)
Expand Down
5 changes: 2 additions & 3 deletions internal/agent/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ var _ = Describe("Inventory", func() {
UpdateSourceStatusFunc: func(ctx context.Context, id uuid.UUID, params v1alpha1.SourceStatusUpdate) error {
Expect(id).To(Equal(sourceID))
Expect(params.AgentId).To(Equal(agentID))
Expect(params.Status).To(Equal("up-to-date"))
Expect(params.StatusInfo).To(Equal("status_info"))
Expect(params.Inventory).ToNot(BeNil())
return nil

},
}

inventory := &api.Inventory{
Vms: api.VMs{Total: 2},
Vms: api.VMs{Total: 2},
Vcenter: api.VCenter{Id: sourceID.String()},
}
inventoryUpdater := agent.NewInventoryUpdater(log.NewPrefixLogger(""), agentID, &client)
inventoryUpdater.UpdateServiceWithInventory(context.TODO(), api.SourceStatusUpToDate, "status_info", inventory)
Expand Down
4 changes: 2 additions & 2 deletions internal/agent/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func NewStatusUpdater(log *log.PrefixLogger, agentID uuid.UUID, version, credUrl
}
}

func (s *StatusUpdater) UpdateStatus(ctx context.Context, status api.AgentStatus, statusInfo string) error {
func (s *StatusUpdater) UpdateStatus(ctx context.Context, status api.AgentStatus, statusInfo string, credUrl string) error {
ctx, cancel := context.WithTimeout(ctx, defaultUpdateStatusTimeout*time.Second)
defer cancel()

bodyParameters := agentapi.AgentStatusUpdate{
Id: s.agentID.String(),
Status: string(status),
StatusInfo: statusInfo,
CredentialUrl: s.credUrl,
CredentialUrl: credUrl,
Version: s.version,
}

Expand Down
2 changes: 1 addition & 1 deletion internal/agent/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ = Describe("Status", func() {
}

statusUpdater := agent.NewStatusUpdater(log.NewPrefixLogger(""), agentID, "best_version", "www-cred-url", &agent.Config{}, &client)
Expect(statusUpdater.UpdateStatus(context.TODO(), api.AgentStatusUpToDate, "status_info"))
Expect(statusUpdater.UpdateStatus(context.TODO(), api.AgentStatusUpToDate, "status_info", "www-cred-url"))
})
})

Expand Down
Loading

0 comments on commit f051548

Please sign in to comment.