@@ -32,6 +32,7 @@ import (
32
32
"github.com/googleapis/gax-go/v2/apierror"
33
33
"golang.org/x/oauth2"
34
34
computebeta "google.golang.org/api/compute/v0.beta"
35
+ "google.golang.org/api/compute/v1"
35
36
computev1 "google.golang.org/api/compute/v1"
36
37
"google.golang.org/api/googleapi"
37
38
"google.golang.org/api/iterator"
@@ -117,7 +118,7 @@ type GCECompute interface {
117
118
// Regional Disk Methods
118
119
GetReplicaZoneURI (project string , zone string ) string
119
120
// Instance Methods
120
- GetInstanceOrError (ctx context.Context , instanceZone , instanceName string ) (* computev1.Instance , error )
121
+ GetInstanceOrError (ctx context.Context , project , instanceZone , instanceName string ) (* computev1.Instance , error )
121
122
// Zone Methods
122
123
ListZones (ctx context.Context , region string ) ([]string , error )
123
124
ListSnapshots (ctx context.Context , filter string ) ([]* computev1.Snapshot , string , error )
@@ -160,40 +161,75 @@ func (cloud *CloudProvider) listDisksInternal(ctx context.Context, fields []goog
160
161
if err != nil {
161
162
return nil , "" , err
162
163
}
163
- items := []* computev1.Disk {}
164
+ disks := []* computev1.Disk {}
164
165
165
166
// listing out regional disks in the region
166
- rlCall := cloud .service .RegionDisks .List (cloud .project , region )
167
+ rDisks , err := listRegionalDisksForProject (cloud .service , cloud .project , region , fields , filter )
168
+ if err != nil {
169
+ return nil , "" , err
170
+ }
171
+ disks = append (disks , rDisks ... )
172
+ for tProject , tService := range cloud .tenantServiceMap {
173
+ rDisks , err := listRegionalDisksForProject (tService , tProject , region , fields , filter )
174
+ if err != nil {
175
+ return nil , "" , err
176
+ }
177
+ disks = append (disks , rDisks ... )
178
+ }
179
+
180
+ // listing out zonal disks in all zones of the region
181
+ zDisks , err := listZonalDisksForProject (cloud .service , cloud .project , zones , fields , filter )
182
+ if err != nil {
183
+ return nil , "" , err
184
+ }
185
+ disks = append (disks , zDisks ... )
186
+ for tProject , tService := range cloud .tenantServiceMap {
187
+ zDisks , err := listZonalDisksForProject (tService , tProject , zones , fields , filter )
188
+ if err != nil {
189
+ return nil , "" , err
190
+ }
191
+ disks = append (disks , zDisks ... )
192
+ }
193
+
194
+ return disks , "" , nil
195
+ }
196
+
197
+ func listRegionalDisksForProject (service * computev1.Service , project string , region string , fields []googleapi.Field , filter string ) ([]* computev1.Disk , error ) {
198
+ items := []* computev1.Disk {}
199
+ rlCall := service .RegionDisks .List (project , region )
167
200
rlCall .Fields (fields ... )
168
201
rlCall .Filter (filter )
169
202
nextPageToken := "pageToken"
170
203
for nextPageToken != "" {
171
204
rDiskList , err := rlCall .Do ()
172
205
if err != nil {
173
- return nil , "" , err
206
+ return nil , err
174
207
}
175
208
items = append (items , rDiskList .Items ... )
176
209
nextPageToken = rDiskList .NextPageToken
177
210
rlCall .PageToken (nextPageToken )
178
211
}
212
+ return items , nil
213
+ }
179
214
180
- // listing out zonal disks in all zones of the region
215
+ func listZonalDisksForProject (service * computev1.Service , project string , zones []string , fields []googleapi.Field , filter string ) ([]* computev1.Disk , error ) {
216
+ items := []* computev1.Disk {}
181
217
for _ , zone := range zones {
182
- lCall := cloud . service .Disks .List (cloud . project , zone )
218
+ lCall := service .Disks .List (project , zone )
183
219
lCall .Fields (fields ... )
184
220
lCall .Filter (filter )
185
221
nextPageToken := "pageToken"
186
222
for nextPageToken != "" {
187
223
diskList , err := lCall .Do ()
188
224
if err != nil {
189
- return nil , "" , err
225
+ return nil , err
190
226
}
191
227
items = append (items , diskList .Items ... )
192
228
nextPageToken = diskList .NextPageToken
193
229
lCall .PageToken (nextPageToken )
194
230
}
195
231
}
196
- return items , "" , nil
232
+ return items , nil
197
233
}
198
234
199
235
// ListInstances lists instances based on maxEntries and pageToken for the project and region
@@ -210,8 +246,27 @@ func (cloud *CloudProvider) ListInstances(ctx context.Context, fields []googleap
210
246
}
211
247
items := []* computev1.Instance {}
212
248
249
+ instances , err := cloud .listInstancesForProject (cloud .service , cloud .project , zones , fields )
250
+ if err != nil {
251
+ return nil , "" , err
252
+ }
253
+ items = append (items , instances ... )
254
+ for tProject , tService := range cloud .tenantServiceMap {
255
+ instances , err := cloud .listInstancesForProject (tService , tProject , zones , fields )
256
+ if err != nil {
257
+ return nil , "" , err
258
+ }
259
+ items = append (items , instances ... )
260
+ }
261
+
262
+ return items , "" , nil
263
+ }
264
+
265
+ func (cloud * CloudProvider ) listInstancesForProject (service * computev1.Service , project string , zones []string , fields []googleapi.Field ) ([]* computev1.Instance , error ) {
266
+ items := []* computev1.Instance {}
267
+
213
268
for _ , zone := range zones {
214
- lCall := cloud . service .Instances .List (cloud . project , zone )
269
+ lCall := service .Instances .List (project , zone )
215
270
for _ , filter := range cloud .listInstancesConfig .Filters {
216
271
lCall = lCall .Filter (filter )
217
272
}
@@ -220,15 +275,14 @@ func (cloud *CloudProvider) ListInstances(ctx context.Context, fields []googleap
220
275
for nextPageToken != "" {
221
276
instancesList , err := lCall .Do ()
222
277
if err != nil {
223
- return nil , "" , err
278
+ return nil , err
224
279
}
225
280
items = append (items , instancesList .Items ... )
226
281
nextPageToken = instancesList .NextPageToken
227
282
lCall .PageToken (nextPageToken )
228
283
}
229
284
}
230
-
231
- return items , "" , nil
285
+ return items , nil
232
286
}
233
287
234
288
// RepairUnderspecifiedVolumeKey will query the cloud provider and check each zone for the disk specified
@@ -857,7 +911,11 @@ func (cloud *CloudProvider) AttachDisk(ctx context.Context, project string, volK
857
911
ForceAttach : forceAttach ,
858
912
}
859
913
860
- op , err := cloud .service .Instances .AttachDisk (project , instanceZone , instanceName , attachedDiskV1 ).Context (ctx ).ForceAttach (forceAttach ).Do ()
914
+ service := cloud .service
915
+ if _ , ok := cloud .tenantServiceMap [project ]; ok {
916
+ service = cloud .tenantServiceMap [project ]
917
+ }
918
+ op , err := service .Instances .AttachDisk (project , instanceZone , instanceName , attachedDiskV1 ).Context (ctx ).ForceAttach (forceAttach ).Do ()
861
919
if err != nil {
862
920
return fmt .Errorf ("failed cloud service attach disk call: %w" , err )
863
921
}
@@ -872,7 +930,11 @@ func (cloud *CloudProvider) AttachDisk(ctx context.Context, project string, volK
872
930
873
931
func (cloud * CloudProvider ) DetachDisk (ctx context.Context , project , deviceName , instanceZone , instanceName string ) error {
874
932
klog .V (5 ).Infof ("Detaching disk %v from %v" , deviceName , instanceName )
875
- op , err := cloud .service .Instances .DetachDisk (project , instanceZone , instanceName , deviceName ).Context (ctx ).Do ()
933
+ service := cloud .service
934
+ if _ , ok := cloud .tenantServiceMap [project ]; ok {
935
+ service = cloud .tenantServiceMap [project ]
936
+ }
937
+ op , err := service .Instances .DetachDisk (project , instanceZone , instanceName , deviceName ).Context (ctx ).Do ()
876
938
if err != nil {
877
939
return err
878
940
}
@@ -1041,7 +1103,7 @@ func (cloud *CloudProvider) waitForAttachOnInstance(ctx context.Context, project
1041
1103
start := time .Now ()
1042
1104
return wait .ExponentialBackoff (AttachDiskBackoff , func () (bool , error ) {
1043
1105
klog .V (6 ).Infof ("Polling instances.get for attach of disk %v to instance %v to complete for %v" , volKey .Name , instanceName , time .Since (start ))
1044
- instance , err := cloud .GetInstanceOrError (ctx , instanceZone , instanceName )
1106
+ instance , err := cloud .GetInstanceOrError (ctx , project , instanceZone , instanceName )
1045
1107
if err != nil {
1046
1108
return false , fmt .Errorf ("GetInstance failed to get instance: %w" , err )
1047
1109
}
@@ -1145,10 +1207,13 @@ func opIsDone(op *computev1.Operation) (bool, error) {
1145
1207
return true , nil
1146
1208
}
1147
1209
1148
- func (cloud * CloudProvider ) GetInstanceOrError (ctx context.Context , instanceZone , instanceName string ) (* computev1.Instance , error ) {
1210
+ func (cloud * CloudProvider ) GetInstanceOrError (ctx context.Context , project , instanceZone , instanceName string ) (* computev1.Instance , error ) {
1149
1211
klog .V (5 ).Infof ("Getting instance %v from zone %v" , instanceName , instanceZone )
1150
- project := cloud .project
1151
- instance , err := cloud .service .Instances .Get (project , instanceZone , instanceName ).Do ()
1212
+ service := cloud .service
1213
+ if _ , ok := cloud .tenantServiceMap [project ]; ok {
1214
+ service = cloud .tenantServiceMap [project ]
1215
+ }
1216
+ instance , err := service .Instances .Get (project , instanceZone , instanceName ).Do ()
1152
1217
if err != nil {
1153
1218
return nil , err
1154
1219
}
@@ -1426,6 +1491,22 @@ func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, project
1426
1491
}
1427
1492
}
1428
1493
1494
+ func (cloud * CloudProvider ) getTenantService (ctx context.Context , project string ) (* compute.Service , error ) {
1495
+ s , exists := cloud .tenantServiceMap [project ]
1496
+ if ! exists {
1497
+ var err error
1498
+ s , err = cloud .createTenantService (project )
1499
+ if err != nil {
1500
+ return nil , fmt .Errorf ("error during tenant compute client creation: %w" , err )
1501
+ }
1502
+ }
1503
+ return s , nil
1504
+ }
1505
+
1506
+ func (cloud * CloudProvider ) createTenantService (project string ) (* compute.Service , error ) {
1507
+ return nil , nil
1508
+ }
1509
+
1429
1510
// getResourceManagerTags returns the map of tag keys and values. The tag keys are in the form `tagKeys/{tag_key_id}`
1430
1511
// and the tag values are in the format `tagValues/456`.
1431
1512
func getResourceManagerTags (ctx context.Context , tokenSource oauth2.TokenSource , tagsMap map [string ]string ) (map [string ]string , error ) {
0 commit comments