Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#110 from sidharthsurana/template-r…
Browse files Browse the repository at this point in the history
…esolution

Overload the VMTemplate parameter for search purposes
  • Loading branch information
frapposelli authored Nov 8, 2018
2 parents cce2b5f + 7636d15 commit 15b43e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

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

26 changes: 23 additions & 3 deletions pkg/cloud/vsphere/provisioner/govmomi/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cloud/vsphere/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 15b43e2

Please sign in to comment.