@@ -176,25 +176,8 @@ func (a *sqlInstanceAdapter) cloneInstance(ctx context.Context, u *unstructured.
176
176
if err != nil {
177
177
return fmt .Errorf ("cloning SQLInstance %s failed: %w" , a .desired .Name , err )
178
178
}
179
-
180
- pollingBackoff := gax.Backoff {
181
- Initial : time .Second ,
182
- Max : time .Minute ,
183
- Multiplier : 2 ,
184
- }
185
- for {
186
- log .V (2 ).Info ("polling" , "op" , op )
187
-
188
- if op .Status == "DONE" {
189
- break
190
- }
191
- if err := gax .Sleep (ctx , pollingBackoff .Pause ()); err != nil {
192
- return fmt .Errorf ("waiting for SQLInstance %s clone failed: %w" , a .desired .Name , err )
193
- }
194
- op , err = a .sqlOperationsClient .Get (a .projectID , op .Name ).Do ()
195
- if err != nil {
196
- return fmt .Errorf ("getting SQLInstance %s clone operation %s failed: %w" , a .desired .Name , op .Name , err )
197
- }
179
+ if err := a .pollForLROCompletion (ctx , op , "clone" ); err != nil {
180
+ return err
198
181
}
199
182
200
183
created , err := a .sqlInstancesClient .Get (a .projectID , a .resourceID ).Context (ctx ).Do ()
@@ -221,25 +204,8 @@ func (a *sqlInstanceAdapter) insertInstance(ctx context.Context, u *unstructured
221
204
if err != nil {
222
205
return fmt .Errorf ("creating SQLInstance %s failed: %w" , a .desired .Name , err )
223
206
}
224
-
225
- pollingBackoff := gax.Backoff {
226
- Initial : time .Second ,
227
- Max : time .Minute ,
228
- Multiplier : 2 ,
229
- }
230
- for {
231
- log .V (2 ).Info ("polling" , "op" , op )
232
-
233
- if op .Status == "DONE" {
234
- break
235
- }
236
- if err := gax .Sleep (ctx , pollingBackoff .Pause ()); err != nil {
237
- return fmt .Errorf ("waiting for SQLInstance %s creation failed: %w" , a .desired .Name , err )
238
- }
239
- op , err = a .sqlOperationsClient .Get (a .projectID , op .Name ).Do ()
240
- if err != nil {
241
- return fmt .Errorf ("getting SQLInstance %s create operation %s failed: %w" , a .desired .Name , op .Name , err )
242
- }
207
+ if err := a .pollForLROCompletion (ctx , op , "create" ); err != nil {
208
+ return err
243
209
}
244
210
245
211
created , err := a .sqlInstancesClient .Get (a .projectID , a .resourceID ).Context (ctx ).Do ()
@@ -261,19 +227,8 @@ func (a *sqlInstanceAdapter) insertInstance(ctx context.Context, u *unstructured
261
227
if err != nil {
262
228
return fmt .Errorf ("deleting SQLInstance %s root user failed: %w" , a .desired .Name , err )
263
229
}
264
- for {
265
- log .V (2 ).Info ("polling" , "op" , op )
266
-
267
- if op .Status == "DONE" {
268
- break
269
- }
270
- if err := gax .Sleep (ctx , pollingBackoff .Pause ()); err != nil {
271
- return fmt .Errorf ("waiting for SQLInstance %s delete user failed: %w" , a .desired .Name , err )
272
- }
273
- op , err = a .sqlOperationsClient .Get (a .projectID , op .Name ).Do ()
274
- if err != nil {
275
- return fmt .Errorf ("getting SQLInstance %s delete root user operation %s failed: %w" , a .desired .Name , op .Name , err )
276
- }
230
+ if err := a .pollForLROCompletion (ctx , op , "delete root user" ); err != nil {
231
+ return err
277
232
}
278
233
}
279
234
}
@@ -303,25 +258,8 @@ func (a *sqlInstanceAdapter) Update(ctx context.Context, updateOp *directbase.Up
303
258
if err != nil {
304
259
return fmt .Errorf ("patching SQLInstance %s version failed: %w" , a .resourceID , err )
305
260
}
306
-
307
- pollingBackoff := gax.Backoff {
308
- Initial : time .Second ,
309
- Max : time .Minute ,
310
- Multiplier : 2 ,
311
- }
312
- for {
313
- log .V (2 ).Info ("polling" , "op" , op )
314
-
315
- if op .Status == "DONE" {
316
- break
317
- }
318
- if err := gax .Sleep (ctx , pollingBackoff .Pause ()); err != nil {
319
- return fmt .Errorf ("waiting for SQLInstance %s version patch failed: %w" , a .resourceID , err )
320
- }
321
- op , err = a .sqlOperationsClient .Get (a .projectID , op .Name ).Do ()
322
- if err != nil {
323
- return fmt .Errorf ("getting SQLInstance %s version patch operation %s failed: %w" , a .resourceID , op .Name , err )
324
- }
261
+ if err := a .pollForLROCompletion (ctx , op , "version patch" ); err != nil {
262
+ return err
325
263
}
326
264
327
265
updated , err := a .sqlInstancesClient .Get (a .projectID , a .resourceID ).Context (ctx ).Do ()
@@ -349,25 +287,8 @@ func (a *sqlInstanceAdapter) Update(ctx context.Context, updateOp *directbase.Up
349
287
if err != nil {
350
288
return fmt .Errorf ("patching SQLInstance %s edition failed: %w" , a .resourceID , err )
351
289
}
352
-
353
- pollingBackoff := gax.Backoff {
354
- Initial : time .Second ,
355
- Max : time .Minute ,
356
- Multiplier : 2 ,
357
- }
358
- for {
359
- log .V (2 ).Info ("polling" , "op" , op )
360
-
361
- if op .Status == "DONE" {
362
- break
363
- }
364
- if err := gax .Sleep (ctx , pollingBackoff .Pause ()); err != nil {
365
- return fmt .Errorf ("waiting for SQLInstance %s edition patch failed: %w" , a .resourceID , err )
366
- }
367
- op , err = a .sqlOperationsClient .Get (a .projectID , op .Name ).Do ()
368
- if err != nil {
369
- return fmt .Errorf ("getting SQLInstance %s edition patch operation %s failed: %w" , a .resourceID , op .Name , err )
370
- }
290
+ if err := a .pollForLROCompletion (ctx , op , "edition patch" ); err != nil {
291
+ return err
371
292
}
372
293
373
294
updated , err := a .sqlInstancesClient .Get (a .projectID , a .resourceID ).Context (ctx ).Do ()
@@ -393,25 +314,8 @@ func (a *sqlInstanceAdapter) Update(ctx context.Context, updateOp *directbase.Up
393
314
if err != nil {
394
315
return fmt .Errorf ("updating SQLInstance %s failed: %w" , desiredGCP .Name , err )
395
316
}
396
-
397
- pollingBackoff := gax.Backoff {
398
- Initial : time .Second ,
399
- Max : time .Minute ,
400
- Multiplier : 2 ,
401
- }
402
- for {
403
- log .V (2 ).Info ("polling" , "op" , op )
404
-
405
- if op .Status == "DONE" {
406
- break
407
- }
408
- if err := gax .Sleep (ctx , pollingBackoff .Pause ()); err != nil {
409
- return fmt .Errorf ("waiting for SQLInstance %s update failed: %w" , desiredGCP .Name , err )
410
- }
411
- op , err = a .sqlOperationsClient .Get (a .projectID , op .Name ).Do ()
412
- if err != nil {
413
- return fmt .Errorf ("getting SQLInstance %s update operation %s failed: %w" , desiredGCP .Name , op .Name , err )
414
- }
317
+ if err := a .pollForLROCompletion (ctx , op , "update" ); err != nil {
318
+ return err
415
319
}
416
320
417
321
updated , err := a .sqlInstancesClient .Get (a .projectID , a .resourceID ).Context (ctx ).Do ()
@@ -473,6 +377,33 @@ func (a *sqlInstanceAdapter) Export(ctx context.Context) (*unstructured.Unstruct
473
377
return u , nil
474
378
}
475
379
380
+ func (a * sqlInstanceAdapter ) pollForLROCompletion (ctx context.Context , op * api.Operation , verb string ) error {
381
+ log := klog .FromContext (ctx ).WithName (ctrlName )
382
+ var err error
383
+
384
+ pollingBackoff := gax.Backoff {
385
+ Initial : time .Second ,
386
+ Max : time .Minute ,
387
+ Multiplier : 2 ,
388
+ }
389
+ for {
390
+ log .V (2 ).Info ("polling" , "op" , op )
391
+
392
+ if op .Status == "DONE" {
393
+ break
394
+ }
395
+ if err := gax .Sleep (ctx , pollingBackoff .Pause ()); err != nil {
396
+ return fmt .Errorf ("waiting for SQLInstance %s %s failed: %w" , a .resourceID , verb , err )
397
+ }
398
+ op , err = a .sqlOperationsClient .Get (a .projectID , op .Name ).Do ()
399
+ if err != nil {
400
+ return fmt .Errorf ("getting SQLInstance %s %s operation %s failed: %w" , a .resourceID , verb , op .Name , err )
401
+ }
402
+ }
403
+
404
+ return nil
405
+ }
406
+
476
407
func setStatus (u * unstructured.Unstructured , typedStatus any ) error {
477
408
status , err := runtime .DefaultUnstructuredConverter .ToUnstructured (typedStatus )
478
409
if err != nil {
0 commit comments