@@ -19,6 +19,7 @@ package cluster
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "strings"
22
23
23
24
"github.com/pkg/errors"
24
25
corev1 "k8s.io/api/core/v1"
@@ -184,7 +185,7 @@ func (r *Reconciler) reconcileControlPlane(ctx context.Context, s *scope.Scope)
184
185
185
186
cpInfraRef , err := contract .ControlPlane ().MachineTemplate ().InfrastructureRef ().Get (s .Desired .ControlPlane .Object )
186
187
if err != nil {
187
- return errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : s .Desired .ControlPlane .InfrastructureMachineTemplate })
188
+ return errors .Wrapf (err , "failed to reconcile %s" , tlog.KObj {Obj : s .Desired .ControlPlane .InfrastructureMachineTemplate })
188
189
}
189
190
190
191
// Create or update the MachineInfrastructureTemplate of the control plane.
@@ -198,13 +199,13 @@ func (r *Reconciler) reconcileControlPlane(ctx context.Context, s *scope.Scope)
198
199
},
199
200
)
200
201
if err != nil {
201
- return errors . Wrapf ( err , "failed to update %s" , tlog. KObj { Obj : s . Desired . ControlPlane . InfrastructureMachineTemplate })
202
+ return err
202
203
}
203
204
204
205
// The controlPlaneObject.Spec.machineTemplate.infrastructureRef has to be updated in the desired object
205
206
err = contract .ControlPlane ().MachineTemplate ().InfrastructureRef ().Set (s .Desired .ControlPlane .Object , refToUnstructured (cpInfraRef ))
206
207
if err != nil {
207
- return errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : s .Desired .ControlPlane .Object })
208
+ return errors .Wrapf (err , "failed to reconcile %s" , tlog.KObj {Obj : s .Desired .ControlPlane .Object })
208
209
}
209
210
}
210
211
@@ -227,7 +228,7 @@ func (r *Reconciler) reconcileControlPlane(ctx context.Context, s *scope.Scope)
227
228
},
228
229
},
229
230
}); err != nil {
230
- return errors . Wrapf ( err , "failed to update %s" , tlog. KObj { Obj : s . Desired . ControlPlane . Object })
231
+ return err
231
232
}
232
233
233
234
// If the ControlPlane has defined a current or desired MachineHealthCheck attempt to reconcile it.
@@ -242,7 +243,7 @@ func (r *Reconciler) reconcileControlPlane(ctx context.Context, s *scope.Scope)
242
243
243
244
// Reconcile the current and desired state of the MachineHealthCheck.
244
245
if err := r .reconcileMachineHealthCheck (ctx , s .Current .ControlPlane .MachineHealthCheck , s .Desired .ControlPlane .MachineHealthCheck ); err != nil {
245
- return errors . Wrapf ( err , "failed to update %s" , tlog. KObj { Obj : s . Desired . ControlPlane . MachineHealthCheck })
246
+ return err
246
247
}
247
248
}
248
249
return nil
@@ -403,21 +404,21 @@ func (r *Reconciler) createMachineDeployment(ctx context.Context, cluster *clust
403
404
cluster : cluster ,
404
405
desired : md .InfrastructureMachineTemplate ,
405
406
}); err != nil {
406
- return errors .Wrapf (err , "failed to create %s" , tlog. KObj { Obj : md .Object } )
407
+ return errors .Wrapf (err , "failed to create %s" , md .Object . Kind )
407
408
}
408
409
409
410
bootstrapCtx , _ := log .WithObject (md .BootstrapTemplate ).Into (ctx )
410
411
if err := r .reconcileReferencedTemplate (bootstrapCtx , reconcileReferencedTemplateInput {
411
412
cluster : cluster ,
412
413
desired : md .BootstrapTemplate ,
413
414
}); err != nil {
414
- return errors .Wrapf (err , "failed to create %s" , tlog. KObj { Obj : md .Object } )
415
+ return errors .Wrapf (err , "failed to create %s" , md .Object . Kind )
415
416
}
416
417
417
418
log = log .WithObject (md .Object )
418
419
log .Infof (fmt .Sprintf ("Creating %s" , tlog.KObj {Obj : md .Object }))
419
420
if err := r .Client .Create (ctx , md .Object .DeepCopy ()); err != nil {
420
- return errors . Wrapf (err , "failed to create %s" , tlog. KObj { Obj : md .Object } )
421
+ return createErrorWithoutObjectName (err , md .Object )
421
422
}
422
423
r .recorder .Eventf (cluster , corev1 .EventTypeNormal , createEventReason , "Created %q" , tlog.KObj {Obj : md .Object })
423
424
@@ -429,7 +430,7 @@ func (r *Reconciler) createMachineDeployment(ctx context.Context, cluster *clust
429
430
* ownerReferenceTo (md .Object ),
430
431
})
431
432
if err := r .reconcileMachineHealthCheck (ctx , nil , md .MachineHealthCheck ); err != nil {
432
- return errors . Wrapf ( err , "failed to create %s" , tlog. KObj { Obj : md . MachineHealthCheck })
433
+ return err
433
434
}
434
435
}
435
436
return nil
@@ -448,7 +449,7 @@ func (r *Reconciler) updateMachineDeployment(ctx context.Context, cluster *clust
448
449
templateNamePrefix : infrastructureMachineTemplateNamePrefix (cluster .Name , mdTopologyName ),
449
450
compatibilityChecker : check .ObjectsAreCompatible ,
450
451
}); err != nil {
451
- return errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : currentMD .Object })
452
+ return errors .Wrapf (err , "failed to reconcile %s" , tlog.KObj {Obj : currentMD .Object })
452
453
}
453
454
454
455
bootstrapCtx , _ := log .WithObject (desiredMD .BootstrapTemplate ).Into (ctx )
@@ -460,13 +461,13 @@ func (r *Reconciler) updateMachineDeployment(ctx context.Context, cluster *clust
460
461
templateNamePrefix : bootstrapTemplateNamePrefix (cluster .Name , mdTopologyName ),
461
462
compatibilityChecker : check .ObjectsAreInTheSameNamespace ,
462
463
}); err != nil {
463
- return errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : currentMD .Object })
464
+ return errors .Wrapf (err , "failed to reconcile %s" , tlog.KObj {Obj : currentMD .Object })
464
465
}
465
466
466
467
// Patch MachineHealthCheck for the MachineDeployment.
467
468
if desiredMD .MachineHealthCheck != nil || currentMD .MachineHealthCheck != nil {
468
469
if err := r .reconcileMachineHealthCheck (ctx , currentMD .MachineHealthCheck , desiredMD .MachineHealthCheck ); err != nil {
469
- return errors . Wrapf ( err , "failed to update %s" , tlog. KObj { Obj : desiredMD . MachineHealthCheck })
470
+ return err
470
471
}
471
472
}
472
473
@@ -525,7 +526,7 @@ func (r *Reconciler) deleteMachineDeployment(ctx context.Context, cluster *clust
525
526
// delete MachineHealthCheck for the MachineDeployment.
526
527
if md .MachineHealthCheck != nil {
527
528
if err := r .reconcileMachineHealthCheck (ctx , md .MachineHealthCheck , nil ); err != nil {
528
- return errors . Wrapf ( err , "failed to delete %s" , tlog. KObj { Obj : md . MachineHealthCheck })
529
+ return err
529
530
}
530
531
}
531
532
log .Infof ("Deleting %s" , tlog.KObj {Obj : md .Object })
@@ -587,7 +588,7 @@ func (r *Reconciler) reconcileReferencedObject(ctx context.Context, in reconcile
587
588
return errors .Wrapf (err , "failed to create a copy of %s with the managed field annotation" , tlog.KObj {Obj : in .desired })
588
589
}
589
590
if err := r .Client .Create (ctx , desiredWithManagedFieldAnnotation ); err != nil {
590
- return errors . Wrapf (err , "failed to create %s" , tlog. KObj { Obj : desiredWithManagedFieldAnnotation } )
591
+ return createErrorWithoutObjectName (err , desiredWithManagedFieldAnnotation )
591
592
}
592
593
r .recorder .Eventf (in .cluster , corev1 .EventTypeNormal , createEventReason , "Created %q" , tlog.KObj {Obj : in .desired })
593
594
return nil
@@ -664,7 +665,7 @@ func (r *Reconciler) reconcileReferencedTemplate(ctx context.Context, in reconci
664
665
return errors .Wrapf (err , "failed to create a copy of %s with the managed field annotation" , tlog.KObj {Obj : in .desired })
665
666
}
666
667
if err := r .Client .Create (ctx , desiredWithManagedFieldAnnotation ); err != nil {
667
- return errors . Wrapf (err , "failed to create %s" , tlog. KObj { Obj : desiredWithManagedFieldAnnotation } )
668
+ return createErrorWithoutObjectName (err , desiredWithManagedFieldAnnotation )
668
669
}
669
670
r .recorder .Eventf (in .cluster , corev1 .EventTypeNormal , createEventReason , "Created %q" , tlog.KObj {Obj : in .desired })
670
671
return nil
@@ -716,7 +717,7 @@ func (r *Reconciler) reconcileReferencedTemplate(ctx context.Context, in reconci
716
717
return errors .Wrapf (err , "failed to create a copy of %s with the managed field annotation" , tlog.KObj {Obj : in .desired })
717
718
}
718
719
if err := r .Client .Create (ctx , desiredWithManagedFieldAnnotation ); err != nil {
719
- return errors . Wrapf (err , "failed to create %s" , tlog. KObj { Obj : desiredWithManagedFieldAnnotation } )
720
+ return createErrorWithoutObjectName (err , desiredWithManagedFieldAnnotation )
720
721
}
721
722
r .recorder .Eventf (in .cluster , corev1 .EventTypeNormal , createEventReason , "Created %q as a replacement for %q (template rotation)" , tlog.KObj {Obj : in .desired }, in .ref .Name )
722
723
@@ -727,3 +728,29 @@ func (r *Reconciler) reconcileReferencedTemplate(ctx context.Context, in reconci
727
728
728
729
return nil
729
730
}
731
+
732
+ // createErrorWithoutObjectName removes the name of the object from the error message. As each new Create call involves an
733
+ // object with a unique generated name each error appears to be a different error. As the errors are being surfaced in a condition
734
+ // on the Cluster, the name is removed here to prevent each creation error from triggering a new reconciliation.
735
+ func createErrorWithoutObjectName (err error , obj client.Object ) error {
736
+ var statusError * apierrors.StatusError
737
+ if errors .As (err , & statusError ) {
738
+ if statusError .Status ().Details != nil {
739
+ var causes []string
740
+ for _ , cause := range statusError .Status ().Details .Causes {
741
+ causes = append (causes , fmt .Sprintf ("%s: %s: %s" , cause .Type , cause .Field , cause .Message ))
742
+ }
743
+ var msg string
744
+ if len (causes ) > 0 {
745
+ msg = fmt .Sprintf ("failed to create %s.%s: %s" , statusError .Status ().Details .Kind , statusError .Status ().Details .Group , strings .Join (causes , " " ))
746
+ } else {
747
+ msg = fmt .Sprintf ("failed to create %s.%s" , statusError .Status ().Details .Kind , statusError .Status ().Details .Group )
748
+ }
749
+ // Replace the statusError message with the constructed message.
750
+ statusError .ErrStatus .Message = msg
751
+ return statusError
752
+ }
753
+ }
754
+ // If this isn't a StatusError return a more generic error with the object details.
755
+ return errors .Wrapf (err , "failed to create %s" , tlog.KObj {Obj : obj })
756
+ }
0 commit comments