Skip to content

Commit f516427

Browse files
committed
Add FetchAvailableDiskTypesByFilter.
1 parent 214215f commit f516427

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed

cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ type AutoscalingGceClient interface {
131131
FetchZones(region string) ([]string, error)
132132
FetchAvailableCpuPlatforms() (map[string][]string, error)
133133
FetchAvailableDiskTypes() (map[string][]string, error)
134+
FetchAvailableDiskTypesByFilter(filter string) (map[string][]string, error)
134135
FetchReservations() ([]*gce.Reservation, error)
135136
FetchReservationsInProject(projectId string) ([]*gce.Reservation, error)
136137
FetchListManagedInstancesResults(migRef GceRef) (string, error)
@@ -777,6 +778,29 @@ func (client *autoscalingGceClientV1) FetchAvailableDiskTypes() (map[string][]st
777778
return availableDiskTypes, nil
778779
}
779780

781+
func (client *autoscalingGceClientV1) FetchAvailableDiskTypesByFilter(filter string) (map[string][]string, error) {
782+
availableDiskTypes := make(map[string][]string)
783+
784+
req := client.gceService.DiskTypes.AggregatedList(client.projectId).Filter(filter).ReturnPartialSuccess(true)
785+
if err := req.Pages(context.TODO(), func(page *gce.DiskTypeAggregatedList) error {
786+
for _, diskTypesScopedList := range page.Items {
787+
for _, diskType := range diskTypesScopedList.DiskTypes {
788+
if diskType.Zone == "" {
789+
continue
790+
}
791+
// convert URL of the zone, into the short name, e.g. us-central1-a
792+
zone := path.Base(diskType.Zone)
793+
availableDiskTypes[zone] = append(availableDiskTypes[zone], diskType.Name)
794+
}
795+
}
796+
return nil
797+
}); err != nil {
798+
return nil, err
799+
}
800+
801+
return availableDiskTypes, nil
802+
}
803+
780804
func (client *autoscalingGceClientV1) FetchMigTemplateName(migRef GceRef) (InstanceTemplateName, error) {
781805
registerRequest("instance_group_managers", "get")
782806
ctx, cancel := context.WithTimeout(context.Background(), client.operationPerCallTimeout)

cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,33 @@ func TestFetchAvailableDiskTypes(t *testing.T) {
673673
})
674674
}
675675

676+
func TestFetchAvailableDiskTypesByFilter(t *testing.T) {
677+
server := test_util.NewHttpServerMock()
678+
defer server.Close()
679+
g := newTestAutoscalingGceClient(t, "project-id", server.URL, "")
680+
681+
// ref: https://cloud.google.com/compute/docs/reference/rest/v1/diskTypes/aggregatedList
682+
getDiskTypesAggregatedListOKResponse, _ := os.ReadFile("fixtures/diskTypes_aggregatedList_withFiltering.json")
683+
server.On("handle", "/projects/project-id/aggregated/diskTypes").Return(string(getDiskTypesAggregatedListOKResponse)).Times(1)
684+
685+
t.Run("correctly parse a filtered response", func(t *testing.T) {
686+
want := map[string][]string{
687+
// "us-central1" region should be skipped
688+
"us-central1-a": {"local-ssd", "pd-balanced", "pd-ssd", "pd-standard"},
689+
"us-central1-b": {"hyperdisk-balanced", "hyperdisk-extreme", "hyperdisk-throughput", "local-ssd", "pd-balanced", "pd-extreme", "pd-ssd", "pd-standard"},
690+
}
691+
692+
// filtering by zones in us-central1
693+
filter := fmt.Sprintf("zone eq .+us-central1.*")
694+
got, err := g.FetchAvailableDiskTypesByFilter(filter)
695+
696+
assert.NoError(t, err)
697+
if diff := cmp.Diff(want, got, cmpopts.EquateErrors()); diff != "" {
698+
t.Errorf("FetchAvailableDiskTypes(): err diff (-want +got):\n%s", diff)
699+
}
700+
})
701+
}
702+
676703
func TestUserAgent(t *testing.T) {
677704
server := test_util.NewHttpServerMock(test_util.MockFieldUserAgent, test_util.MockFieldResponse)
678705
defer server.Close()
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
{
2+
"kind": "compute#diskTypeAggregatedList",
3+
"id": "projects/project-id/aggregated/diskTypes",
4+
"items": {
5+
"regions/us-central1": {
6+
"warning": {
7+
"code": "NO_RESULTS_ON_PAGE",
8+
"message": "No results for the scope 'regions/us-central1'.",
9+
"data": [
10+
{
11+
"key": "scope",
12+
"value": "regions/us-central1"
13+
}
14+
]
15+
}
16+
},
17+
"regions/us-central2": {
18+
"warning": {
19+
"code": "NO_RESULTS_ON_PAGE",
20+
"message": "No results for the scope 'regions/us-central2'.",
21+
"data": [
22+
{
23+
"key": "scope",
24+
"value": "regions/us-central2"
25+
}
26+
]
27+
}
28+
},
29+
"zones/us-central1-a": {
30+
"diskTypes": [
31+
{
32+
"kind": "compute#diskType",
33+
"id": "30003",
34+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
35+
"name": "local-ssd",
36+
"description": "Local SSD",
37+
"validDiskSize": "375GB-375GB",
38+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a",
39+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a/diskTypes/local-ssd",
40+
"defaultDiskSizeGb": "375"
41+
},
42+
{
43+
"kind": "compute#diskType",
44+
"id": "30007",
45+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
46+
"name": "pd-balanced",
47+
"description": "Balanced Persistent Disk",
48+
"validDiskSize": "10GB-65536GB",
49+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a",
50+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a/diskTypes/pd-balanced",
51+
"defaultDiskSizeGb": "100"
52+
},
53+
{
54+
"kind": "compute#diskType",
55+
"id": "30002",
56+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
57+
"name": "pd-ssd",
58+
"description": "SSD Persistent Disk",
59+
"validDiskSize": "10GB-65536GB",
60+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a",
61+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a/diskTypes/pd-ssd",
62+
"defaultDiskSizeGb": "100"
63+
},
64+
{
65+
"kind": "compute#diskType",
66+
"id": "30001",
67+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
68+
"name": "pd-standard",
69+
"description": "Standard Persistent Disk",
70+
"validDiskSize": "10GB-65536GB",
71+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a",
72+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-a/diskTypes/pd-standard",
73+
"defaultDiskSizeGb": "500"
74+
}
75+
]
76+
},
77+
"zones/us-central1-b": {
78+
"diskTypes": [
79+
{
80+
"kind": "compute#diskType",
81+
"id": "30014",
82+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
83+
"name": "hyperdisk-balanced",
84+
"description": "Hyperdisk Balanced Persistent Disk",
85+
"validDiskSize": "4GB-65536GB",
86+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
87+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/hyperdisk-balanced",
88+
"defaultDiskSizeGb": "100"
89+
},
90+
{
91+
"kind": "compute#diskType",
92+
"id": "30012",
93+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
94+
"name": "hyperdisk-extreme",
95+
"description": "Hyperdisk Extreme Persistent Disk",
96+
"validDiskSize": "64GB-65536GB",
97+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
98+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/hyperdisk-extreme",
99+
"defaultDiskSizeGb": "1000"
100+
},
101+
{
102+
"kind": "compute#diskType",
103+
"id": "30013",
104+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
105+
"name": "hyperdisk-throughput",
106+
"description": "Hyperdisk Throughput Persistent Disk",
107+
"validDiskSize": "2048GB-32768GB",
108+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
109+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/hyperdisk-throughput",
110+
"defaultDiskSizeGb": "2048"
111+
},
112+
{
113+
"kind": "compute#diskType",
114+
"id": "30003",
115+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
116+
"name": "local-ssd",
117+
"description": "Local SSD",
118+
"validDiskSize": "375GB-375GB",
119+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
120+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/local-ssd",
121+
"defaultDiskSizeGb": "375"
122+
},
123+
{
124+
"kind": "compute#diskType",
125+
"id": "30007",
126+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
127+
"name": "pd-balanced",
128+
"description": "Balanced Persistent Disk",
129+
"validDiskSize": "10GB-65536GB",
130+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
131+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/pd-balanced",
132+
"defaultDiskSizeGb": "100"
133+
},
134+
{
135+
"kind": "compute#diskType",
136+
"id": "30008",
137+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
138+
"name": "pd-extreme",
139+
"description": "Extreme Persistent Disk",
140+
"validDiskSize": "500GB-65536GB",
141+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
142+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/pd-extreme",
143+
"defaultDiskSizeGb": "1000"
144+
},
145+
{
146+
"kind": "compute#diskType",
147+
"id": "30002",
148+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
149+
"name": "pd-ssd",
150+
"description": "SSD Persistent Disk",
151+
"validDiskSize": "10GB-65536GB",
152+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
153+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/pd-ssd",
154+
"defaultDiskSizeGb": "100"
155+
},
156+
{
157+
"kind": "compute#diskType",
158+
"id": "30001",
159+
"creationTimestamp": "1969-12-31T16:00:00.000-08:00",
160+
"name": "pd-standard",
161+
"description": "Standard Persistent Disk",
162+
"validDiskSize": "10GB-65536GB",
163+
"zone": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b",
164+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/zones/us-central1-b/diskTypes/pd-standard",
165+
"defaultDiskSizeGb": "500"
166+
}
167+
]
168+
}
169+
},
170+
"zones/us-central2-a": {
171+
"warning": {
172+
"code": "NO_RESULTS_ON_PAGE",
173+
"message": "No results for the scope 'zones/us-central2-a'.",
174+
"data": [
175+
{
176+
"key": "scope",
177+
"value": "zones/us-central2-a"
178+
}
179+
]
180+
}
181+
},
182+
"zones/us-central2-b": {
183+
"warning": {
184+
"code": "NO_RESULTS_ON_PAGE",
185+
"message": "No results for the scope 'zones/us-central2-b'.",
186+
"data": [
187+
{
188+
"key": "scope",
189+
"value": "zones/us-central2-b"
190+
}
191+
]
192+
}
193+
},
194+
"selfLink": "https://www.googleapis.com/compute/v1/projects/project-id/aggregated/diskTypes"
195+
}

cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (client *mockAutoscalingGceClient) FetchAvailableDiskTypes() (map[string][]
176176
return nil, nil
177177
}
178178

179+
func (client *mockAutoscalingGceClient) FetchAvailableDiskTypesByFilter(_ string) (map[string][]string, error) {
180+
return nil, nil
181+
}
182+
179183
func (client *mockAutoscalingGceClient) FetchReservations() ([]*gce.Reservation, error) {
180184
return nil, nil
181185
}

0 commit comments

Comments
 (0)