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 4016fb4
Show file tree
Hide file tree
Showing 5 changed files with 123 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:
- datacenter
- vms
- infra
properties:
datacenter:
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.

54 changes: 52 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 @@ -73,6 +79,8 @@ func (c *Collector) run() {
return
}

// get vCenterID

opaServer := util.GetEnv("OPA_SERVER", "127.0.0.1:8181")
c.log.Infof("Create Provider")
provider := getProvider(creds)
Expand All @@ -88,6 +96,13 @@ func (c *Collector) run() {
}
defer resp.Body.Close()

// get id
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 +144,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 +217,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{
Datacenter: apiplanner.VCenter{
Id: vCenterID,
},
Vms: apiplanner.VMs{
Total: len(*vms),
PowerStates: map[string]int{},
Expand Down Expand Up @@ -561,6 +579,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 4016fb4

Please sign in to comment.