@@ -206,6 +206,9 @@ func contains(arr []string, target string) bool {
206
206
}
207
207
208
208
func deleteBastion (scope scope.Scope , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
209
+ if openStackCluster .Status .Bastion == nil || openStackCluster .Status .Bastion .ID == "" {
210
+ return nil
211
+ }
209
212
computeService , err := compute .NewService (scope )
210
213
if err != nil {
211
214
return err
@@ -215,8 +218,7 @@ func deleteBastion(scope scope.Scope, cluster *clusterv1.Cluster, openStackClust
215
218
return err
216
219
}
217
220
218
- instanceName := fmt .Sprintf ("%s-bastion" , cluster .Name )
219
- instanceStatus , err := computeService .GetInstanceStatusByName (openStackCluster , instanceName )
221
+ instanceStatus , err := computeService .GetInstanceStatus (openStackCluster .Status .Bastion .ID )
220
222
if err != nil {
221
223
return err
222
224
}
@@ -277,8 +279,8 @@ func reconcileNormal(scope scope.Scope, cluster *clusterv1.Cluster, openStackClu
277
279
return reconcile.Result {}, err
278
280
}
279
281
280
- if err = reconcileBastion (scope , cluster , openStackCluster ); err != nil {
281
- return reconcile. Result {} , err
282
+ if result , err : = reconcileBastion (scope , cluster , openStackCluster ); err != nil {
283
+ return result , err
282
284
}
283
285
284
286
availabilityZones , err := computeService .GetAvailabilityZones ()
@@ -308,82 +310,99 @@ func reconcileNormal(scope scope.Scope, cluster *clusterv1.Cluster, openStackClu
308
310
return reconcile.Result {}, nil
309
311
}
310
312
311
- func reconcileBastion (scope scope.Scope , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
313
+ func reconcileBastion (scope scope.Scope , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) (ctrl. Result , error ) {
312
314
scope .Logger ().Info ("Reconciling Bastion" )
313
315
314
316
if openStackCluster .Spec .Bastion == nil || ! openStackCluster .Spec .Bastion .Enabled {
315
- return deleteBastion (scope , cluster , openStackCluster )
317
+ return reconcile. Result {}, deleteBastion (scope , cluster , openStackCluster )
316
318
}
317
319
318
320
computeService , err := compute .NewService (scope )
319
321
if err != nil {
320
- return err
322
+ return reconcile. Result {}, err
321
323
}
322
324
323
325
instanceSpec := bastionToInstanceSpec (openStackCluster , cluster .Name )
324
326
bastionHash , err := compute .HashInstanceSpec (instanceSpec )
325
327
if err != nil {
326
- return fmt .Errorf ("failed computing bastion hash from instance spec: %w" , err )
328
+ return reconcile. Result {}, fmt .Errorf ("failed computing bastion hash from instance spec: %w" , err )
327
329
}
328
330
329
- instanceStatus , err := computeService .GetInstanceStatusByName (openStackCluster , fmt .Sprintf ("%s-bastion" , cluster .Name ))
330
- if err != nil {
331
- return err
331
+ var instanceStatus * compute.InstanceStatus
332
+ if openStackCluster .Status .Bastion != nil && openStackCluster .Status .Bastion .ID != "" {
333
+ if instanceStatus , err = computeService .GetInstanceStatus (openStackCluster .Status .Bastion .ID ); err != nil {
334
+ return reconcile.Result {}, err
335
+ }
332
336
}
333
- if instanceStatus != nil {
337
+ if instanceStatus == nil {
338
+ instanceStatus , err = computeService .CreateInstance (openStackCluster , openStackCluster , instanceSpec , cluster .Name )
339
+ if err != nil {
340
+ return reconcile.Result {}, fmt .Errorf ("failed to create bastion: %w" , err )
341
+ }
342
+ openStackCluster .Status .Bastion = & infrav1.BastionStatus {
343
+ ID : instanceStatus .ID (),
344
+ }
345
+ } else {
334
346
if ! bastionHashHasChanged (bastionHash , openStackCluster .ObjectMeta .Annotations ) {
335
347
bastion , err := instanceStatus .BastionStatus (openStackCluster )
336
348
if err != nil {
337
- return err
349
+ return reconcile. Result {}, err
338
350
}
339
351
// Add the current hash if no annotation is set.
340
352
if _ , ok := openStackCluster .ObjectMeta .Annotations [BastionInstanceHashAnnotation ]; ! ok {
341
353
annotations .AddAnnotations (openStackCluster , map [string ]string {BastionInstanceHashAnnotation : bastionHash })
342
354
}
343
355
openStackCluster .Status .Bastion = bastion
344
- return nil
356
+ return ctrl. Result {}, nil
345
357
}
346
358
347
359
if err := deleteBastion (scope , cluster , openStackCluster ); err != nil {
348
- return err
360
+ return ctrl. Result {}, err
349
361
}
350
362
}
351
-
352
- instanceStatus , err = computeService .CreateInstance (openStackCluster , openStackCluster , instanceSpec , cluster .Name )
353
- if err != nil {
354
- return fmt .Errorf ("failed to reconcile bastion: %w" , err )
363
+ // Make sure that bastion instance has a valid state
364
+ switch instanceStatus .State () {
365
+ case infrav1 .InstanceStateError :
366
+ return ctrl.Result {}, fmt .Errorf ("failed to reconcile bastion, instance state is ERROR" )
367
+ case infrav1 .InstanceStateBuilding :
368
+ // Requeue until bastion becomes active
369
+ return ctrl.Result {RequeueAfter : waitForBuildingInstanceToReconcile }, nil
370
+ case infrav1 .InstanceStateDeleted :
371
+ // This should normally be handled by deleteBastion
372
+ openStackCluster .Status .Bastion = nil
373
+ return ctrl.Result {}, nil
355
374
}
356
375
357
376
networkingService , err := networking .NewService (scope )
358
377
if err != nil {
359
- return err
378
+ return ctrl. Result {}, err
360
379
}
361
380
clusterName := fmt .Sprintf ("%s-%s" , cluster .Namespace , cluster .Name )
362
381
fp , err := networkingService .GetOrCreateFloatingIP (openStackCluster , openStackCluster , clusterName , openStackCluster .Spec .Bastion .FloatingIP )
363
382
if err != nil {
364
383
handleUpdateOSCError (openStackCluster , fmt .Errorf ("failed to get or create floating IP for bastion: %w" , err ))
365
- return fmt .Errorf ("failed to get or create floating IP for bastion: %w" , err )
384
+ return ctrl. Result {}, fmt .Errorf ("failed to get or create floating IP for bastion: %w" , err )
366
385
}
367
386
port , err := computeService .GetManagementPort (openStackCluster , instanceStatus )
368
387
if err != nil {
369
388
err = fmt .Errorf ("getting management port for bastion: %w" , err )
370
389
handleUpdateOSCError (openStackCluster , err )
371
- return err
390
+ return ctrl. Result {}, err
372
391
}
373
392
err = networkingService .AssociateFloatingIP (openStackCluster , fp , port .ID )
374
393
if err != nil {
375
394
handleUpdateOSCError (openStackCluster , fmt .Errorf ("failed to associate floating IP with bastion: %w" , err ))
376
- return fmt .Errorf ("failed to associate floating IP with bastion: %w" , err )
395
+ return ctrl. Result {}, fmt .Errorf ("failed to associate floating IP with bastion: %w" , err )
377
396
}
378
397
379
398
bastion , err := instanceStatus .BastionStatus (openStackCluster )
380
399
if err != nil {
381
- return err
400
+ return ctrl. Result {}, err
382
401
}
383
402
bastion .FloatingIP = fp .FloatingIP
384
403
openStackCluster .Status .Bastion = bastion
385
404
annotations .AddAnnotations (openStackCluster , map [string ]string {BastionInstanceHashAnnotation : bastionHash })
386
- return nil
405
+ return ctrl. Result {}, nil
387
406
}
388
407
389
408
func bastionToInstanceSpec (openStackCluster * infrav1.OpenStackCluster , clusterName string ) * compute.InstanceSpec {
0 commit comments