Skip to content

Commit

Permalink
collector: Retrieve vCenterID
Browse files Browse the repository at this point in the history
This commit adds vCenterID to the inventory model.
The id is retrieve by making another request to vCenter. The id is read
from `client.ServiceContent.About.InstanceUuid`

Signed-off-by: Cosmin Tupangiu <[email protected]>
  • Loading branch information
tupyy committed Nov 25, 2024
1 parent ebba4c7 commit 5135131
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 55 deletions.
42 changes: 21 additions & 21 deletions api/v1alpha1/agent/spec.gen.go

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

12 changes: 12 additions & 0 deletions api/v1alpha1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,28 @@ components:
Inventory:
type: object
required:
- vcenter
- vms
- infra
properties:
vcenter:
type: object
$ref: '#/components/schemas/VCenter'
vms:
type: object
$ref: '#/components/schemas/VMs'
infra:
type: object
$ref: '#/components/schemas/Infra'

VCenter:
type: object
required:
- id
- name
properties:
id:
type: string
VMs:
type: object
required:
Expand Down
60 changes: 30 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.

10 changes: 8 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.

51 changes: 49 additions & 2 deletions internal/agent/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"

Expand All @@ -19,10 +20,15 @@ import (
"github.com/konveyor/forklift-controller/pkg/controller/provider/model"
vspheremodel "github.com/konveyor/forklift-controller/pkg/controller/provider/model/vsphere"
web "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere"
liberr "github.com/konveyor/forklift-controller/pkg/lib/error"
libmodel "github.com/konveyor/forklift-controller/pkg/lib/inventory/model"
apiplanner "github.com/kubev2v/migration-planner/api/v1alpha1"
"github.com/kubev2v/migration-planner/internal/util"
"github.com/kubev2v/migration-planner/pkg/log"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/session"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/soap"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -88,6 +94,12 @@ func (c *Collector) run() {
}
defer resp.Body.Close()

vCenterID, err := getCenterID(context.TODO(), &creds)
if err != nil {
c.log.Errorf("failed to get vCenterID: %s", err)
return
}

c.log.Infof("Create DB")
db, err := createDB(provider)
if err != nil {
Expand Down Expand Up @@ -129,7 +141,7 @@ func (c *Collector) run() {
}

c.log.Infof("Create inventory")
inv := createBasicInventoryObj(vms, collector, hosts, clusters)
inv := createBasicInventoryObj(vCenterID, vms, collector, hosts, clusters)

c.log.Infof("Run the validation of VMs")
vms, err = validation(vms, opaServer)
Expand Down Expand Up @@ -202,8 +214,11 @@ func fillInventoryObjectWithMoreData(vms *[]vspheremodel.VM, inv *apiplanner.Inv
inv.Vms.DiskGB.Histogram = histogram(diskGBSet)
}

func createBasicInventoryObj(vms *[]vspheremodel.VM, collector *vsphere.Collector, hosts *[]vspheremodel.Host, clusters *[]vspheremodel.Cluster) *apiplanner.Inventory {
func createBasicInventoryObj(vCenterID string, vms *[]vspheremodel.VM, collector *vsphere.Collector, hosts *[]vspheremodel.Host, clusters *[]vspheremodel.Cluster) *apiplanner.Inventory {
return &apiplanner.Inventory{
Vcenter: apiplanner.VCenter{
Id: vCenterID,
},
Vms: apiplanner.VMs{
Total: len(*vms),
PowerStates: map[string]int{},
Expand Down Expand Up @@ -561,6 +576,38 @@ func waitForFile(filename string) {
}
}

func getCenterID(ctx context.Context, credentials *Credentials) (string, error) {
u, err := parseUrl(credentials)
if err != nil {
return "", liberr.Wrap(err)
}

ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
vimClient, err := vim25.NewClient(ctx, soap.NewClient(u, true))
if err != nil {
return "", liberr.Wrap(err)
}
client := &govmomi.Client{
SessionManager: session.NewManager(vimClient),
Client: vimClient,
}
err = client.Login(ctx, u.User)
if err != nil {
err = liberr.Wrap(err)
if strings.Contains(err.Error(), "incorrect") && strings.Contains(err.Error(), "password") {
return "", err
}
return "", err
}

id := client.ServiceContent.About.InstanceUuid
_ = client.Logout(ctx)
client.CloseIdleConnections()

return id, nil
}

type NotMigratableReasons []NotMigratableReason

type NotMigratableReason struct {
Expand Down

0 comments on commit 5135131

Please sign in to comment.