Skip to content

Commit

Permalink
Merge pull request #2656 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…2655-to-release-1.31

[release-1.31] feat: pick region when zonal node pool is in different region
  • Loading branch information
k8s-ci-robot authored Nov 21, 2024
2 parents dc199d5 + 2eb65f9 commit 93bdd81
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/azuredisk/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/azureutils/azure_disk_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <region>-<zone-id>
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)
Expand Down
19 changes: 19 additions & 0 deletions pkg/azureutils/azure_disk_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 93bdd81

Please sign in to comment.