Adding a check to make sure UseInstanceMetadata flag is true to get data from metadata.

pull/564/head
Rohit Jaini 2019-03-04 19:37:49 -08:00
parent 7955a2865c
commit e0f51bcc2e
2 changed files with 33 additions and 23 deletions

View File

@ -1676,7 +1676,8 @@ func validateEmptyConfig(t *testing.T, config string) {
func TestGetZone(t *testing.T) { func TestGetZone(t *testing.T) {
cloud := &Cloud{ cloud := &Cloud{
Config: Config{ Config: Config{
Location: "eastus", Location: "eastus",
UseInstanceMetadata: true,
}, },
} }
testcases := []struct { testcases := []struct {

View File

@ -19,6 +19,7 @@ package azure
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings" "strings"
@ -49,31 +50,39 @@ func (az *Cloud) GetZoneID(zoneLabel string) string {
// GetZone returns the Zone containing the current availability zone and locality region that the program is running in. // GetZone returns the Zone containing the current availability zone and locality region that the program is running in.
// If the node is not running with availability zones, then it will fall back to fault domain. // If the node is not running with availability zones, then it will fall back to fault domain.
func (az *Cloud) GetZone(ctx context.Context) (cloudprovider.Zone, error) { func (az *Cloud) GetZone(ctx context.Context) (cloudprovider.Zone, error) {
metadata, err := az.metadata.GetMetadata() if az.UseInstanceMetadata {
if err != nil { metadata, err := az.metadata.GetMetadata()
return cloudprovider.Zone{}, err
}
if metadata.Compute == nil {
return cloudprovider.Zone{}, fmt.Errorf("failure of getting compute information from instance metadata")
}
zone := ""
if metadata.Compute.Zone != "" {
zoneID, err := strconv.Atoi(metadata.Compute.Zone)
if err != nil { if err != nil {
return cloudprovider.Zone{}, fmt.Errorf("failed to parse zone ID %q: %v", metadata.Compute.Zone, err) return cloudprovider.Zone{}, err
} }
zone = az.makeZone(zoneID)
} else {
klog.V(3).Infof("Availability zone is not enabled for the node, falling back to fault domain")
zone = metadata.Compute.FaultDomain
}
return cloudprovider.Zone{ if metadata.Compute == nil {
FailureDomain: zone, return cloudprovider.Zone{}, fmt.Errorf("failure of getting compute information from instance metadata")
Region: az.Location, }
}, nil
zone := ""
if metadata.Compute.Zone != "" {
zoneID, err := strconv.Atoi(metadata.Compute.Zone)
if err != nil {
return cloudprovider.Zone{}, fmt.Errorf("failed to parse zone ID %q: %v", metadata.Compute.Zone, err)
}
zone = az.makeZone(zoneID)
} else {
klog.V(3).Infof("Availability zone is not enabled for the node, falling back to fault domain")
zone = metadata.Compute.FaultDomain
}
return cloudprovider.Zone{
FailureDomain: zone,
Region: az.Location,
}, nil
}
// if UseInstanceMetadata is false, get Zone name by calling ARM
hostname, err := os.Hostname()
if err != nil {
return cloudprovider.Zone{}, fmt.Errorf("failure getting hostname from kernel")
}
return az.vmSet.GetZoneByNodeName(strings.ToLower(hostname))
} }
// GetZoneByProviderID implements Zones.GetZoneByProviderID // GetZoneByProviderID implements Zones.GetZoneByProviderID