@@ -18,6 +18,7 @@ package govmomi
1818
1919import (
2020 gonet "net"
21+ "path"
2122
2223 "github.com/pkg/errors"
2324 "github.com/vmware/govmomi/object"
@@ -49,15 +50,19 @@ func sanitizeIPAddrs(ctx *context.VMContext, ipAddrs []string) []string {
4950// 1. If the BIOS UUID is available, then it is used to find the VM.
5051// 2. Lacking the BIOS UUID, the VM is queried by its instance UUID,
5152// which was assigned the value of the VSphereVM resource's UID string.
53+ // 3. If it is not found by instance UUID, fallback to an inventory path search
54+ // using the vm folder path and the VSphereVM name
5255func findVM (ctx * context.VMContext ) (types.ManagedObjectReference , error ) {
5356 if biosUUID := ctx .VSphereVM .Spec .BiosUUID ; biosUUID != "" {
5457 objRef , err := ctx .Session .FindByBIOSUUID (ctx , biosUUID )
5558 if err != nil {
5659 return types.ManagedObjectReference {}, err
5760 }
5861 if objRef == nil {
62+ ctx .Logger .Info ("vm not found by bios uuid" , "biosuuid" , biosUUID )
5963 return types.ManagedObjectReference {}, errNotFound {uuid : biosUUID }
6064 }
65+ ctx .Logger .Info ("vm found by bios uuid" , "vmref" , objRef .Reference ())
6166 return objRef .Reference (), nil
6267 }
6368
@@ -67,8 +72,24 @@ func findVM(ctx *context.VMContext) (types.ManagedObjectReference, error) {
6772 return types.ManagedObjectReference {}, err
6873 }
6974 if objRef == nil {
70- return types.ManagedObjectReference {}, errNotFound {instanceUUID : true , uuid : instanceUUID }
75+ // fallback to use inventory paths
76+ folder , err := ctx .Session .Finder .FolderOrDefault (ctx , ctx .VSphereVM .Spec .Folder )
77+ if err != nil {
78+ return types.ManagedObjectReference {}, errors .Wrapf (err , "unable to get folder for %s/%s" , ctx .VSphereVM .Namespace , ctx .VSphereVM .Name )
79+ }
80+ inventoryPath := path .Join (folder .InventoryPath , ctx .VSphereVM .Name )
81+ ctx .Logger .Info ("using inventory path to find vm" , "path" , inventoryPath )
82+ vm , err := ctx .Session .Finder .VirtualMachine (ctx , inventoryPath )
83+ if err != nil {
84+ if isVirtualMachineNotFound (err ) {
85+ return types.ManagedObjectReference {}, errNotFound {byInventoryPath : inventoryPath }
86+ }
87+ return types.ManagedObjectReference {}, err
88+ }
89+ ctx .Logger .Info ("vm found by name" , "vmref" , vm .Reference ())
90+ return vm .Reference (), nil
7191 }
92+ ctx .Logger .Info ("vm found by instance uuid" , "vmref" , objRef .Reference ())
7293 return objRef .Reference (), nil
7394}
7495
0 commit comments