Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
return pkgerr.ResultFromError(r.ReconcileDelete(vmCtx))
}

err = r.VMProvider.ListNsAndNameoftheVM(ctx, vm.Namespace)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to list all the VM and validate namespaces")
}

err = r.ReconcileNormal(vmCtx)
if err != nil && !ignoredCreateErr(err) {
return pkgerr.ResultFromError(err)
Expand Down
24 changes: 24 additions & 0 deletions pkg/providers/fake/fake_vm_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type funcs struct {
DeleteSnapshotFn func(ctx context.Context, vmSnapshot *vmopv1.VirtualMachineSnapshot, vm *vmopv1.VirtualMachine, removeChildren bool, consolidate *bool) (bool, error)
GetSnapshotSizeFn func(ctx context.Context, vmSnapshotName string, vm *vmopv1.VirtualMachine) (int64, error)
SyncVMSnapshotTreeStatusFn func(ctx context.Context, vm *vmopv1.VirtualMachine) error
ListVMsFn func(ctx context.Context) ([]*object.VirtualMachine, error)
ListNsAndNameoftheVMFn func(ctx context.Context, expectedNs string) error
}

type VMProvider struct {
Expand Down Expand Up @@ -102,6 +104,28 @@ func (s *VMProvider) CreateOrUpdateVirtualMachine(ctx context.Context, vm *vmopv
return nil
}

func (s *VMProvider) ListVMs(ctx context.Context) ([]*object.VirtualMachine, error) {
_ = pkgcfg.FromContext(ctx)

s.Lock()
defer s.Unlock()
if s.ListVMsFn != nil {
return s.ListVMsFn(ctx)
}
return nil, nil
}

func (s *VMProvider) ListNsAndNameoftheVM(ctx context.Context, expectedNs string) error {
_ = pkgcfg.FromContext(ctx)

s.Lock()
defer s.Unlock()
if s.ListNsAndNameoftheVMFn != nil {
return s.ListNsAndNameoftheVMFn(ctx, expectedNs)
}
return nil
}

func (s *VMProvider) CreateOrUpdateVirtualMachineAsync(ctx context.Context, vm *vmopv1.VirtualMachine) (<-chan error, error) {
_ = pkgcfg.FromContext(ctx)

Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/vm_provider_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ type VirtualMachineProviderInterface interface {
GetSnapshotSize(ctx context.Context, vmSnapshotName string, vm *vmopv1.VirtualMachine) (int64, error)
// SyncVMSnapshotTreeStatus syncs the VM's current and root snapshots status.
SyncVMSnapshotTreeStatus(ctx context.Context, vm *vmopv1.VirtualMachine) error
ListVMs(ctx context.Context) ([]*object.VirtualMachine, error)
ListNsAndNameoftheVM(ctx context.Context, expectedNs string) error
}
69 changes: 69 additions & 0 deletions pkg/providers/vsphere/vmprovider_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
pbmtypes "github.com/vmware/govmomi/pbm/types"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vapi/tags"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25/mo"
vimtypes "github.com/vmware/govmomi/vim25/types"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -149,6 +150,74 @@
return vs.createOrUpdateVirtualMachine(ctx, vm, true)
}

func (vs *vSphereVMProvider) ListNsAndNameoftheVM(ctx context.Context, expectedNs string) error {
vms, err := vs.ListVMs(ctx)
if err != nil {
return err
}

for _, vm := range vms {
var mvm mo.VirtualMachine

// CRITICAL CHANGE: Fetch "resourcePool" instead of just "parent"
err := vm.Properties(ctx, vm.Reference(), []string{"name", "parent", "resourcePool"}, &mvm)
if err != nil {
continue
}

var nsName string
if mvm.ResourcePool != nil {
// A Namespace in vSphere is technically a ResourcePool object
rp := object.NewResourcePool(vm.Client(), *mvm.ResourcePool)
name, err := rp.ObjectName(ctx)
if err != nil {
nsName = "err-fetching-rp"
} else {
nsName = name
}
} else {
nsName = "no-resource-pool"
}

fmt.Printf("Current Namespace (RP): %-15s | VM Name: %-20s\n", nsName, mvm.Name)
}
return nil
}

func (vs *vSphereVMProvider) ListVMs(ctx context.Context) ([]*object.VirtualMachine, error) {
client, err := vs.VSphereClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get vSphere client: %w", err)
}
//manager := view.NewManager(client.VimClient())

Check failure on line 192 in pkg/providers/vsphere/vmprovider_vm.go

View workflow job for this annotation

GitHub Actions / lint-go

commentFormatting: put a space between `//` and comment text (gocritic)

// Create a container view of all Virtual Machines in the Datacenter
vimClient := client.VimClient()
rootFolder := vimClient.ServiceContent.RootFolder

// 3. Create the Manager using the VIM client
m := view.NewManager(vimClient)
v, err := m.CreateContainerView(ctx, rootFolder, []string{"VirtualMachine"}, true)
if err != nil {
return nil, fmt.Errorf("failed to create container view: %w", err)
}
defer v.Destroy(ctx)

Check failure on line 204 in pkg/providers/vsphere/vmprovider_vm.go

View workflow job for this annotation

GitHub Actions / lint-go

Error return value of `v.Destroy` is not checked (errcheck)

// Retrieve all VM references
var vms []mo.VirtualMachine
err = v.Retrieve(ctx, []string{"VirtualMachine"}, []string{"name", "resourcePool"}, &vms)
if err != nil {
return nil, err
}

var objects []*object.VirtualMachine

Check failure on line 213 in pkg/providers/vsphere/vmprovider_vm.go

View workflow job for this annotation

GitHub Actions / lint-go

Consider pre-allocating `objects` (prealloc)
for _, mvm := range vms {
objects = append(objects, object.NewVirtualMachine(client.VimClient(), mvm.Reference()))
}
return objects, nil

}

func (vs *vSphereVMProvider) createOrUpdateVirtualMachine(
ctx context.Context,
vm *vmopv1.VirtualMachine,
Expand Down
Loading