diff --git a/Gopkg.lock b/Gopkg.lock index 3e92ffda281e..294c5582d913 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -958,7 +958,6 @@ revision = "e3762e86a74c878ffed47484592986685639c2cd" [[projects]] - branch = "master" digest = "1:797d85ec2636b83dafa570b602919912108b913b34e213c9205214361bab722a" name = "sigs.k8s.io/cluster-api" packages = [ @@ -1002,6 +1001,7 @@ input-imports = [ "github.com/ghodss/yaml", "github.com/golang/glog", + "github.com/google/uuid", "github.com/kubernetes-incubator/apiserver-builder/pkg/controller", "github.com/spf13/pflag", "github.com/vmware/govmomi", diff --git a/pkg/cloud/vsphere/provisioner/govmomi/create.go b/pkg/cloud/vsphere/provisioner/govmomi/create.go index 15967aada4aa..0e47c2ff8402 100644 --- a/pkg/cloud/vsphere/provisioner/govmomi/create.go +++ b/pkg/cloud/vsphere/provisioner/govmomi/create.go @@ -160,10 +160,30 @@ func (pv *Provisioner) cloneVirtualMachine(s *SessionContext, cluster *clusterv1 } spec.Config.Annotation = fmt.Sprintf("Virtual Machine is part of the cluster %s managed by cluster-api", cluster.Name) spec.Location.DiskMoveType = string(types.VirtualMachineRelocateDiskMoveOptionsMoveAllDiskBackingsAndAllowSharing) - src, err := s.finder.VirtualMachine(ctx, machineConfig.MachineSpec.VMTemplate) - if err != nil { - return err + var src *object.VirtualMachine + if vsphereutils.IsValidUUID(machineConfig.MachineSpec.VMTemplate) { + // If the passed VMTemplate is a valid UUID, then first try to find it treating that as InstanceUUID + // In case if are not able to locate a matching VM then fall back to searching using the VMTemplate + // as a name + glog.V(4).Infof("Trying to resolve the VMTemplate as InstanceUUID %s", machineConfig.MachineSpec.VMTemplate) + si := object.NewSearchIndex(s.session.Client) + instanceUUID := true + templateref, err := si.FindByUuid(ctx, dc, machineConfig.MachineSpec.VMTemplate, true, &instanceUUID) + if err != nil { + return fmt.Errorf("error quering virtual machine or template using FindByUuid: %s", err) + } + if templateref != nil { + src = object.NewVirtualMachine(s.session.Client, templateref.Reference()) + } } + if src == nil { + glog.V(4).Infof("Trying to resolve the VMTemplate as Name %s", machineConfig.MachineSpec.VMTemplate) + src, err = s.finder.VirtualMachine(ctx, machineConfig.MachineSpec.VMTemplate) + if err != nil { + return err + } + } + vmProps, err := Properties(src) if err != nil { return fmt.Errorf("error fetching virtual machine or template properties: %s", err) diff --git a/pkg/cloud/vsphere/utils/utils.go b/pkg/cloud/vsphere/utils/utils.go index 2561bb9f0237..97b44252e2ce 100644 --- a/pkg/cloud/vsphere/utils/utils.go +++ b/pkg/cloud/vsphere/utils/utils.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/golang/glog" + "github.com/google/uuid" "k8s.io/apimachinery/pkg/labels" vsphereconfig "sigs.k8s.io/cluster-api-provider-vsphere/pkg/apis/vsphereproviderconfig" vsphereconfigv1 "sigs.k8s.io/cluster-api-provider-vsphere/pkg/apis/vsphereproviderconfig/v1alpha1" @@ -197,3 +198,8 @@ func ByteToGiB(n int64) int64 { func GiBToByte(n int64) int64 { return int64(n * int64(math.Pow(1024, 3))) } + +func IsValidUUID(str string) bool { + _, err := uuid.Parse(str) + return err == nil +}