diff --git a/pkg/azuredisk/controllerserver.go b/pkg/azuredisk/controllerserver.go index 1f4cd48217..7dc962716a 100644 --- a/pkg/azuredisk/controllerserver.go +++ b/pkg/azuredisk/controllerserver.go @@ -118,10 +118,6 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) return nil, status.Error(codes.InvalidArgument, "After round-up, volume size exceeds the limit specified") } - if diskParams.Location == "" { - diskParams.Location = d.cloud.Location - } - localCloud := d.cloud localDiskController := d.diskController @@ -188,6 +184,14 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) } diskZone := azureutils.PickAvailabilityZone(req.GetAccessibilityRequirements(), diskParams.Location, topologyKey) + if diskParams.Location == "" { + diskParams.Location = d.cloud.Location + region := azureutils.GetRegionFromAvailabilityZone(diskZone) + if region != "" && region != d.cloud.Location { + klog.V(2).Infof("got a different region from zone %s for disk %s", diskZone, diskParams.DiskName) + diskParams.Location = region + } + } accessibleTopology := []*csi.Topology{} if d.enableDiskCapacityCheck { diff --git a/pkg/azureutils/azure_disk_utils.go b/pkg/azureutils/azure_disk_utils.go index c77426466f..c1d7b65979 100644 --- a/pkg/azureutils/azure_disk_utils.go +++ b/pkg/azureutils/azure_disk_utils.go @@ -432,6 +432,15 @@ func IsValidAvailabilityZone(zone, region string) bool { return strings.HasPrefix(zone, fmt.Sprintf("%s-", region)) } +// GetRegionFromAvailabilityZone returns region from availability zone if it's in format of - +func GetRegionFromAvailabilityZone(zone string) string { + parts := strings.Split(zone, "-") + if len(parts) == 2 { + return parts[0] + } + return "" +} + func IsValidDiskURI(diskURI string) error { if strings.Index(strings.ToLower(diskURI), "/subscriptions/") != 0 { return fmt.Errorf("invalid DiskURI: %v, correct format: %v", diskURI, diskURISupportedManaged) diff --git a/pkg/azureutils/azure_disk_utils_test.go b/pkg/azureutils/azure_disk_utils_test.go index 6f677a380e..5ccfdcb092 100644 --- a/pkg/azureutils/azure_disk_utils_test.go +++ b/pkg/azureutils/azure_disk_utils_test.go @@ -864,6 +864,25 @@ func TestIsAvailabilityZone(t *testing.T) { } } +func TestGetRegionFromAvailabilityZone(t *testing.T) { + tests := []struct { + desc string + zone string + expected string + }{ + {"empty string", "", ""}, + {"invallid zone", "1", ""}, + {"valid zone", "eastus-2", "eastus"}, + } + + for _, test := range tests { + result := GetRegionFromAvailabilityZone(test.zone) + if result != test.expected { + t.Errorf("test [%q] got unexpected result: %v != %v", test.desc, result, test.expected) + } + } +} + func TestIsAzureStackCloud(t *testing.T) { tests := []struct { cloud string