Skip to content

Commit

Permalink
Merge pull request #38508 from vmware/automated-cherry-pick-of-#38423…
Browse files Browse the repository at this point in the history
…-kubernetes-release-1.4

Automated cherry pick of #38423
  • Loading branch information
jessfraz committed Dec 9, 2016
2 parents 4791b17 + 2c60d7b commit 47998b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 68 deletions.
67 changes: 18 additions & 49 deletions pkg/cloudprovider/providers/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ type VSphere struct {
cfg *VSphereConfig
// InstanceID of the server where this VSphere object is instantiated.
localInstanceID string
// Cluster that VirtualMachine belongs to
clusterName string
}

type VSphereConfig struct {
Expand Down Expand Up @@ -158,17 +156,16 @@ func init() {
})
}

// Returns the name of the VM and its Cluster on which this code is running.
// This is done by searching for the name of virtual machine by current IP.
// Returns the name of the VM on which this code is running.
// Prerequisite: this code assumes VMWare vmtools or open-vm-tools to be installed in the VM.
func readInstance(cfg *VSphereConfig) (string, string, error) {
func getVMName(cfg *VSphereConfig) (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", "", err
return "", err
}

if len(addrs) == 0 {
return "", "", fmt.Errorf("unable to retrieve Instance ID")
return "", fmt.Errorf("unable to retrieve Instance ID")
}

// Create context
Expand All @@ -178,7 +175,7 @@ func readInstance(cfg *VSphereConfig) (string, string, error) {
// Create vSphere client
c, err := vsphereLogin(cfg, ctx)
if err != nil {
return "", "", err
return "", err
}
defer c.Logout(ctx)

Expand All @@ -188,7 +185,7 @@ func readInstance(cfg *VSphereConfig) (string, string, error) {
// Fetch and set data center
dc, err := f.Datacenter(ctx, cfg.Global.Datacenter)
if err != nil {
return "", "", err
return "", err
}
f.SetDatacenter(dc)

Expand All @@ -198,7 +195,7 @@ func readInstance(cfg *VSphereConfig) (string, string, error) {
for _, v := range addrs {
ip, _, err := net.ParseCIDR(v.String())
if err != nil {
return "", "", fmt.Errorf("unable to parse cidr from ip")
return "", fmt.Errorf("unable to parse cidr from ip")
}

// Finds a virtual machine or host by IP address.
Expand All @@ -208,40 +205,18 @@ func readInstance(cfg *VSphereConfig) (string, string, error) {
}
}
if svm == nil {
return "", "", fmt.Errorf("unable to retrieve vm reference from vSphere")
return "", fmt.Errorf("unable to retrieve vm reference from vSphere")
}

var vm mo.VirtualMachine
err = s.Properties(ctx, svm.Reference(), []string{"name", "resourcePool"}, &vm)
if err != nil {
return "", "", err
}

var cluster string
if vm.ResourcePool != nil {
// Extract the Cluster Name if VM belongs to a ResourcePool
var rp mo.ResourcePool
err = s.Properties(ctx, *vm.ResourcePool, []string{"parent"}, &rp)
if err == nil {
var ccr mo.ComputeResource
err = s.Properties(ctx, *rp.Parent, []string{"name"}, &ccr)
if err == nil {
cluster = ccr.Name
} else {
glog.Warningf("VM %s, does not belong to a vSphere Cluster, will not have FailureDomain label", vm.Name)
}
} else {
glog.Warningf("VM %s, does not belong to a vSphere Cluster, will not have FailureDomain label", vm.Name)
}
return "", err
}
return vm.Name, cluster, nil
return vm.Name, nil
}

func newVSphere(cfg VSphereConfig) (*VSphere, error) {
id, cluster, err := readInstance(&cfg)
if err != nil {
return nil, err
}

if cfg.Disk.SCSIControllerType == "" {
cfg.Disk.SCSIControllerType = PVSCSIControllerType
Expand All @@ -252,10 +227,15 @@ func newVSphere(cfg VSphereConfig) (*VSphere, error) {
if cfg.Global.WorkingDir != "" {
cfg.Global.WorkingDir = path.Clean(cfg.Global.WorkingDir) + "/"
}

id, err := getVMName(&cfg)
if err != nil {
return nil, err
}

vs := VSphere{
cfg: &cfg,
localInstanceID: id,
clusterName: cluster,
}
return &vs, nil
}
Expand Down Expand Up @@ -543,20 +523,9 @@ func (vs *VSphere) LoadBalancer() (cloudprovider.LoadBalancer, bool) {

// Zones returns an implementation of Zones for Google vSphere.
func (vs *VSphere) Zones() (cloudprovider.Zones, bool) {
glog.V(4).Info("Claiming to support Zones")

return vs, true
}

func (vs *VSphere) GetZone() (cloudprovider.Zone, error) {
glog.V(4).Infof("Current datacenter is %v, cluster is %v", vs.cfg.Global.Datacenter, vs.clusterName)
glog.V(1).Info("The vSphere cloud provider does not support zones")

// The clusterName is determined from the VirtualMachine ManagedObjectReference during init
// If the VM is not created within a Cluster, this will return empty-string
return cloudprovider.Zone{
Region: vs.cfg.Global.Datacenter,
FailureDomain: vs.clusterName,
}, nil
return nil, false
}

// Routes returns a false since the interface is not supported for vSphere.
Expand Down
23 changes: 4 additions & 19 deletions pkg/cloudprovider/providers/vsphere/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,15 @@ func TestVSphereLogin(t *testing.T) {
func TestZones(t *testing.T) {
cfg := VSphereConfig{}
cfg.Global.Datacenter = "myDatacenter"
failureZone := "myCluster"

// Create vSphere configuration object
vs := VSphere{
cfg: &cfg,
clusterName: failureZone,
cfg: &cfg,
}

z, ok := vs.Zones()
if !ok {
t.Fatalf("Zones() returned false")
}

zone, err := z.GetZone()
if err != nil {
t.Fatalf("GetZone() returned error: %s", err)
}

if zone.Region != vs.cfg.Global.Datacenter {
t.Fatalf("GetZone() returned wrong region (%s)", zone.Region)
}

if zone.FailureDomain != failureZone {
t.Fatalf("GetZone() returned wrong Failure Zone (%s)", zone.FailureDomain)
_, ok := vs.Zones()
if ok {
t.Fatalf("Zones() returned true")
}
}

Expand Down

0 comments on commit 47998b4

Please sign in to comment.