@@ -19,12 +19,12 @@ package controllers
19
19
import (
20
20
"context"
21
21
"encoding/base64"
22
+ "errors"
22
23
"fmt"
23
24
"reflect"
24
25
"time"
25
26
26
27
"github.com/go-logr/logr"
27
- "github.com/pkg/errors"
28
28
corev1 "k8s.io/api/core/v1"
29
29
apierrors "k8s.io/apimachinery/pkg/api/errors"
30
30
"k8s.io/apimachinery/pkg/types"
@@ -261,27 +261,28 @@ func (r *OpenStackMachineReconciler) reconcileDelete(ctx context.Context, scope
261
261
if instanceStatus != nil {
262
262
instanceNS , err := instanceStatus .NetworkStatus ()
263
263
if err != nil {
264
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("error getting network status for OpenStack instance %s with ID %s: %v" , instanceStatus .Name (), instanceStatus .ID (), err ))
264
+ openStackMachine .SetFailure (
265
+ capierrors .UpdateMachineError ,
266
+ fmt .Errorf ("get network status for OpenStack instance %s with ID %s: %v" , instanceStatus .Name (), instanceStatus .ID (), err ),
267
+ )
265
268
return ctrl.Result {}, nil
266
269
}
267
270
268
271
addresses := instanceNS .Addresses ()
269
272
for _ , address := range addresses {
270
273
if address .Type == corev1 .NodeExternalIP {
271
274
if err = networkingService .DeleteFloatingIP (openStackMachine , address .Address ); err != nil {
272
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("error deleting Openstack floating IP: %v" , err ))
273
275
conditions .MarkFalse (openStackMachine , infrav1 .APIServerIngressReadyCondition , infrav1 .FloatingIPErrorReason , clusterv1 .ConditionSeverityError , "Deleting floating IP failed: %v" , err )
274
- return ctrl.Result {}, nil
276
+ return ctrl.Result {}, fmt . Errorf ( "delete floating IP %q: %w" , address . Address , err )
275
277
}
276
278
}
277
279
}
278
280
}
279
281
}
280
282
281
283
if err := computeService .DeleteInstance (openStackMachine , instanceStatus , openStackMachine .Name , openStackMachine .Spec .RootVolume ); err != nil {
282
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("error deleting OpenStack instance %s with ID %s: %v" , instanceStatus .Name (), instanceStatus .ID (), err ))
283
284
conditions .MarkFalse (openStackMachine , infrav1 .InstanceReadyCondition , infrav1 .InstanceDeleteFailedReason , clusterv1 .ConditionSeverityError , "Deleting instance failed: %v" , err )
284
- return ctrl.Result {}, nil
285
+ return ctrl.Result {}, fmt . Errorf ( "delete instance: %w" , err )
285
286
}
286
287
287
288
controllerutil .RemoveFinalizer (openStackMachine , infrav1 .MachineFinalizer )
@@ -338,14 +339,14 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
338
339
339
340
instanceStatus , err := r .getOrCreate (scope .Logger , cluster , openStackCluster , machine , openStackMachine , computeService , userData )
340
341
if err != nil {
341
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("OpenStack instance cannot be created: %v" , err ))
342
342
// Conditions set in getOrCreate
343
343
return ctrl.Result {}, err
344
344
}
345
345
346
346
// Set an error message if we couldn't find the instance.
347
347
if instanceStatus == nil {
348
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .New ("OpenStack instance cannot be found" ))
348
+ err = errors .New ("OpenStack instance not found" )
349
+ openStackMachine .SetFailure (capierrors .UpdateMachineError , err )
349
350
conditions .MarkFalse (openStackMachine , infrav1 .InstanceReadyCondition , infrav1 .InstanceNotFoundReason , clusterv1 .ConditionSeverityError , "" )
350
351
return ctrl.Result {}, nil
351
352
}
@@ -360,26 +361,27 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
360
361
361
362
instanceNS , err := instanceStatus .NetworkStatus ()
362
363
if err != nil {
363
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("Unable to get network status for OpenStack instance %s with ID %s: %v" , instanceStatus .Name (), instanceStatus .ID (), err ))
364
- return ctrl.Result {}, nil
364
+ return ctrl.Result {}, fmt .Errorf ("get network status: %w" , err )
365
365
}
366
366
367
367
addresses := instanceNS .Addresses ()
368
368
openStackMachine .Status .Addresses = addresses
369
369
370
370
switch instanceStatus .State () {
371
371
case infrav1 .InstanceStateActive :
372
- scope .Logger .Info ("Machine instance is ACTIVE" , "instance-id" , instanceStatus .ID ())
372
+ scope .Logger .Info ("Machine instance state is ACTIVE" , "instance-id" , instanceStatus .ID ())
373
373
conditions .MarkTrue (openStackMachine , infrav1 .InstanceReadyCondition )
374
374
openStackMachine .Status .Ready = true
375
375
case infrav1 .InstanceStateError :
376
376
// Error is unexpected, thus we report error and never retry
377
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("OpenStack instance state %q is unexpected" , instanceStatus .State ()))
377
+ scope .Logger .Info ("Machine instance state is ERROR" , "instance-id" , instanceStatus .ID ())
378
+ err = fmt .Errorf ("instance state %q is unexpected" , instanceStatus .State ())
379
+ openStackMachine .SetFailure (capierrors .UpdateMachineError , err )
378
380
conditions .MarkFalse (openStackMachine , infrav1 .InstanceReadyCondition , infrav1 .InstanceStateErrorReason , clusterv1 .ConditionSeverityError , "" )
379
381
return ctrl.Result {}, nil
380
382
case infrav1 .InstanceStateDeleted :
381
383
// we should avoid further actions for DELETED VM
382
- scope .Logger .Info ("Instance state is DELETED, no actions" )
384
+ scope .Logger .Info ("Machine instance state is DELETED, no actions" )
383
385
conditions .MarkFalse (openStackMachine , infrav1 .InstanceReadyCondition , infrav1 .InstanceDeletedReason , clusterv1 .ConditionSeverityError , "" )
384
386
return ctrl.Result {}, nil
385
387
default :
@@ -398,9 +400,8 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
398
400
if openStackCluster .Spec .APIServerLoadBalancer .Enabled {
399
401
err = r .reconcileLoadBalancerMember (scope , openStackCluster , machine , openStackMachine , instanceNS , clusterName )
400
402
if err != nil {
401
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("LoadBalancerMember cannot be reconciled: %v" , err ))
402
403
conditions .MarkFalse (openStackMachine , infrav1 .APIServerIngressReadyCondition , infrav1 .LoadBalancerMemberErrorReason , clusterv1 .ConditionSeverityError , "Reconciling load balancer member failed: %v" , err )
403
- return ctrl.Result {}, nil
404
+ return ctrl.Result {}, fmt . Errorf ( "reconcile load balancer member: %w" , err )
404
405
}
405
406
} else if ! openStackCluster .Spec .DisableAPIServerFloatingIP {
406
407
floatingIPAddress := openStackCluster .Spec .ControlPlaneEndpoint .Host
@@ -409,26 +410,22 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
409
410
}
410
411
fp , err := networkingService .GetOrCreateFloatingIP (openStackMachine , openStackCluster , clusterName , floatingIPAddress )
411
412
if err != nil {
412
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("Floating IP cannot be got or created: %v" , err ))
413
413
conditions .MarkFalse (openStackMachine , infrav1 .APIServerIngressReadyCondition , infrav1 .FloatingIPErrorReason , clusterv1 .ConditionSeverityError , "Floating IP cannot be obtained or created: %v" , err )
414
- return ctrl.Result {}, nil
414
+ return ctrl.Result {}, fmt . Errorf ( "get or create floating IP %q: %w" , floatingIPAddress , err )
415
415
}
416
416
port , err := computeService .GetManagementPort (openStackCluster , instanceStatus )
417
417
if err != nil {
418
- err = errors .Errorf ("getting management port for control plane machine %s: %v" , machine .Name , err )
419
- handleUpdateMachineError (scope .Logger , openStackMachine , err )
420
418
conditions .MarkFalse (openStackMachine , infrav1 .APIServerIngressReadyCondition , infrav1 .FloatingIPErrorReason , clusterv1 .ConditionSeverityError , "Obtaining management port for control plane machine failed: %v" , err )
421
- return ctrl.Result {}, nil
419
+ return ctrl.Result {}, fmt . Errorf ( "get management port for control plane machine: %w" , err )
422
420
}
423
421
424
422
if fp .PortID != "" {
425
423
scope .Logger .Info ("Floating IP already associated to a port:" , "id" , fp .ID , "fixed ip" , fp .FixedIP , "portID" , port .ID )
426
424
} else {
427
425
err = networkingService .AssociateFloatingIP (openStackMachine , fp , port .ID )
428
426
if err != nil {
429
- handleUpdateMachineError (scope .Logger , openStackMachine , errors .Errorf ("Floating IP cannot be associated: %v" , err ))
430
427
conditions .MarkFalse (openStackMachine , infrav1 .APIServerIngressReadyCondition , infrav1 .FloatingIPErrorReason , clusterv1 .ConditionSeverityError , "Associating floating IP failed: %v" , err )
431
- return ctrl.Result {}, nil
428
+ return ctrl.Result {}, fmt . Errorf ( "associate floating IP %q with port %q: %w" , fp . FloatingIP , port . ID , err )
432
429
}
433
430
}
434
431
}
@@ -445,30 +442,19 @@ func (r *OpenStackMachineReconciler) getOrCreate(logger logr.Logger, cluster *cl
445
442
}
446
443
447
444
if instanceStatus == nil {
445
+ instanceSpec := machineToInstanceSpec (openStackCluster , machine , openStackMachine , userData )
448
446
logger .Info ("Machine not exist, Creating Machine" , "Machine" , openStackMachine .Name )
449
- instanceSpec , err := machineToInstanceSpec (openStackCluster , machine , openStackMachine , userData )
450
- if err != nil {
451
- err = errors .Errorf ("machine spec is invalid: %v" , err )
452
- handleUpdateMachineError (logger , openStackMachine , err )
453
- conditions .MarkFalse (openStackMachine , infrav1 .InstanceReadyCondition , infrav1 .InvalidMachineSpecReason , clusterv1 .ConditionSeverityError , err .Error ())
454
- return nil , err
455
- }
456
-
457
447
instanceStatus , err = computeService .CreateInstance (openStackMachine , openStackCluster , instanceSpec , cluster .Name )
458
448
if err != nil {
459
449
conditions .MarkFalse (openStackMachine , infrav1 .InstanceReadyCondition , infrav1 .InstanceCreateFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
460
- return nil , errors .Errorf ("error creating Openstack instance: %v " , err )
450
+ return nil , fmt .Errorf ("create OpenStack instance: %w " , err )
461
451
}
462
452
}
463
453
464
454
return instanceStatus , nil
465
455
}
466
456
467
- func machineToInstanceSpec (openStackCluster * infrav1.OpenStackCluster , machine * clusterv1.Machine , openStackMachine * infrav1.OpenStackMachine , userData string ) (* compute.InstanceSpec , error ) {
468
- if openStackMachine == nil {
469
- return nil , fmt .Errorf ("create Options need be specified to create instace" )
470
- }
471
-
457
+ func machineToInstanceSpec (openStackCluster * infrav1.OpenStackCluster , machine * clusterv1.Machine , openStackMachine * infrav1.OpenStackMachine , userData string ) * compute.InstanceSpec {
472
458
instanceSpec := compute.InstanceSpec {
473
459
Name : openStackMachine .Name ,
474
460
Image : openStackMachine .Spec .Image ,
@@ -530,15 +516,7 @@ func machineToInstanceSpec(openStackCluster *infrav1.OpenStackCluster, machine *
530
516
instanceSpec .Networks = openStackMachine .Spec .Networks
531
517
instanceSpec .Ports = openStackMachine .Spec .Ports
532
518
533
- return & instanceSpec , nil
534
- }
535
-
536
- func handleUpdateMachineError (logger logr.Logger , openstackMachine * infrav1.OpenStackMachine , message error ) {
537
- err := capierrors .UpdateMachineError
538
- openstackMachine .Status .FailureReason = & err
539
- openstackMachine .Status .FailureMessage = pointer .StringPtr (message .Error ())
540
- // TODO remove if this error is logged redundantly
541
- logger .Error (fmt .Errorf ("%s" , string (err )), message .Error ())
519
+ return & instanceSpec
542
520
}
543
521
544
522
func (r * OpenStackMachineReconciler ) reconcileLoadBalancerMember (scope * scope.Scope , openStackCluster * infrav1.OpenStackCluster , machine * clusterv1.Machine , openStackMachine * infrav1.OpenStackMachine , instanceNS * compute.InstanceNetworkStatus , clusterName string ) error {
@@ -591,7 +569,7 @@ func (r *OpenStackMachineReconciler) getBootstrapData(ctx context.Context, machi
591
569
secret := & corev1.Secret {}
592
570
key := types.NamespacedName {Namespace : machine .Namespace , Name : * machine .Spec .Bootstrap .DataSecretName }
593
571
if err := r .Client .Get (ctx , key , secret ); err != nil {
594
- return "" , errors . Wrapf ( err , "failed to retrieve bootstrap data secret for Openstack Machine %s/%s" , machine .Namespace , openStackMachine .Name )
572
+ return "" , fmt . Errorf ( "failed to retrieve bootstrap data secret for Openstack Machine %s/%s: %w " , machine .Namespace , openStackMachine .Name , err )
595
573
}
596
574
597
575
value , ok := secret .Data ["value" ]
0 commit comments