Skip to content

Commit e67b896

Browse files
committed
Add location filter to FetchAvailableDiskTypes.
1 parent c107f2b commit e67b896

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type AutoscalingGceClient interface {
130130
FetchMigsWithName(zone string, filter *regexp.Regexp) ([]string, error)
131131
FetchZones(region string) ([]string, error)
132132
FetchAvailableCpuPlatforms() (map[string][]string, error)
133-
FetchAvailableDiskTypes() (map[string][]string, error)
133+
FetchAvailableDiskTypes(region string) (map[string][]string, error)
134134
FetchReservations() ([]*gce.Reservation, error)
135135
FetchReservationsInProject(projectId string) ([]*gce.Reservation, error)
136136
FetchListManagedInstancesResults(migRef GceRef) (string, error)
@@ -753,14 +753,19 @@ func (client *autoscalingGceClientV1) FetchAvailableCpuPlatforms() (map[string][
753753
return availableCpuPlatforms, nil
754754
}
755755

756-
func (client *autoscalingGceClientV1) FetchAvailableDiskTypes() (map[string][]string, error) {
756+
// FetchAvailableDiskTypes returns a map of zone to available disk types filtered by region.
757+
func (client *autoscalingGceClientV1) FetchAvailableDiskTypes(region string) (map[string][]string, error) {
757758
availableDiskTypes := make(map[string][]string)
758759

759-
req := client.gceService.DiskTypes.AggregatedList(client.projectId)
760+
// We use a filter to retrieve disk types available within a specific region.
761+
// This filter leverages the GCE naming convention where zone names are prefixed with their region (e.g., "us-central1-a" is in the "us-central1" region).
762+
// By using the provided region, the filter effectively selects all matching zones within that region.
763+
// For example, if region is "us-central1", the filter will match all zones in that region (e.g., "us-central1-a", "us-central1-b").
764+
filter := fmt.Sprintf("zone eq .+%s.*", region)
765+
req := client.gceService.DiskTypes.AggregatedList(client.projectId).Filter(filter).ReturnPartialSuccess(true)
760766
if err := req.Pages(context.TODO(), func(page *gce.DiskTypeAggregatedList) error {
761767
for _, diskTypesScopedList := range page.Items {
762768
for _, diskType := range diskTypesScopedList.DiskTypes {
763-
// skip data for regions
764769
if diskType.Zone == "" {
765770
continue
766771
}

cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,6 @@ func TestFetchMigInstancesInstanceUrlHandling(t *testing.T) {
647647
})
648648
}
649649
}
650-
651650
func TestFetchAvailableDiskTypes(t *testing.T) {
652651
server := test_util.NewHttpServerMock()
653652
defer server.Close()
@@ -657,14 +656,14 @@ func TestFetchAvailableDiskTypes(t *testing.T) {
657656
getDiskTypesAggregatedListOKResponse, _ := os.ReadFile("fixtures/diskTypes_aggregatedList.json")
658657
server.On("handle", "/projects/project-id/aggregated/diskTypes").Return(string(getDiskTypesAggregatedListOKResponse)).Times(1)
659658

660-
t.Run("correctly parse a response", func(t *testing.T) {
659+
t.Run("correctly parse a filtered response", func(t *testing.T) {
661660
want := map[string][]string{
662661
// "us-central1" region should be skipped
663662
"us-central1-a": {"local-ssd", "pd-balanced", "pd-ssd", "pd-standard"},
664663
"us-central1-b": {"hyperdisk-balanced", "hyperdisk-extreme", "hyperdisk-throughput", "local-ssd", "pd-balanced", "pd-extreme", "pd-ssd", "pd-standard"},
665664
}
666665

667-
got, err := g.FetchAvailableDiskTypes()
666+
got, err := g.FetchAvailableDiskTypes("us-central1")
668667

669668
assert.NoError(t, err)
670669
if diff := cmp.Diff(want, got, cmpopts.EquateErrors()); diff != "" {
@@ -860,7 +859,7 @@ func TestAutoscalingClientTimeouts(t *testing.T) {
860859
},
861860
"FetchAvailableDiskTypes_HttpClientTimeout": {
862861
clientFunc: func(client *autoscalingGceClientV1) error {
863-
_, err := client.FetchAvailableDiskTypes()
862+
_, err := client.FetchAvailableDiskTypes("")
864863
return err
865864
},
866865
httpTimeout: instantTimeout,

cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (client *mockAutoscalingGceClient) FetchAvailableCpuPlatforms() (map[string
172172
return nil, nil
173173
}
174174

175-
func (client *mockAutoscalingGceClient) FetchAvailableDiskTypes() (map[string][]string, error) {
175+
func (client *mockAutoscalingGceClient) FetchAvailableDiskTypes(_ string) (map[string][]string, error) {
176176
return nil, nil
177177
}
178178

0 commit comments

Comments
 (0)