@@ -113,10 +113,18 @@ func (s *Service) Reconcile(ctx context.Context) (res reconcile.Result, err erro
113113 conditions .MarkTrue (s .scope .BareMetalMachine , infrav1 .HostAssociateSucceededCondition )
114114
115115 // update the machine
116- if err := s .update (ctx ); err != nil {
116+ host , err := s .update (ctx )
117+ if err != nil {
117118 return checkForRequeueError (err , "failed to update machine" )
118119 }
119120
121+ if host .Spec .Status .HasFatalError () {
122+ // hbmm will be deleted soon.
123+ s .scope .BareMetalMachine .Status .Phase = clusterv1 .MachinePhaseDeleting
124+ s .scope .BareMetalMachine .Status .Ready = false
125+ return reconcile.Result {}, nil
126+ }
127+
120128 // set providerID if necessary
121129 if err := s .setProviderID (ctx ); err != nil {
122130 return reconcile.Result {}, fmt .Errorf ("failed to set providerID: %w" , err )
@@ -204,14 +212,19 @@ func (s *Service) Delete(ctx context.Context) (reconcile.Result, error) {
204212}
205213
206214// update updates a machine and is invoked by the Machine Controller.
207- func (s * Service ) update (ctx context.Context ) error {
215+ func (s * Service ) update (ctx context.Context ) ( * infrav1. HetznerBareMetalHost , error ) {
208216 host , helper , err := s .getAssociatedHostAndPatchHelper (ctx )
209217 if err != nil {
210- return fmt .Errorf ("failed to get host: %w" , err )
218+ if apierrors .IsNotFound (err ) {
219+ msg := fmt .Sprintf ("host not found for machine %q. Setting error and deleting machine" , s .scope .Machine .Name )
220+ err = s .scope .SetRemediateMachineAnnotationToDeleteMachine (ctx , msg )
221+ return nil , err
222+ }
223+ return nil , fmt .Errorf ("failed to get host: %w" , err )
211224 }
212225 if host == nil {
213- s .scope .BareMetalMachine . SetFailure ( "UpdateError" , "host not found" )
214- return fmt .Errorf ("host not found for machine %s: %w" , s .scope .Machine .Name , err )
226+ err = errors . Join ( s .scope .SetRemediateMachineAnnotationToDeleteMachine ( ctx , "Reconcile of hbmm: host not found" ) )
227+ return nil , fmt .Errorf ("host not found for machine %s: %w" , s .scope .Machine .Name , err )
215228 }
216229
217230 readyCondition := conditions .Get (host , clusterv1 .ReadyCondition )
@@ -232,28 +245,21 @@ func (s *Service) update(ctx context.Context) error {
232245 }
233246
234247 // maintenance mode on the host is a fatal error for the machine object
235- if host .Spec .MaintenanceMode != nil && * host .Spec .MaintenanceMode && s .scope .BareMetalMachine .Status .FailureReason == nil {
236- s .scope .BareMetalMachine .SetFailure ("UpdateError" , FailureMessageMaintenanceMode )
237- record .Eventf (
238- s .scope .BareMetalMachine ,
239- "BareMetalMachineSetFailure" ,
240- "set failure reason due to maintenance mode of underlying host" ,
241- )
242- return nil
243- }
244-
245- // if host has a fatal error, then it should be set on the machine object as well
246- if (host .Spec .Status .ErrorType == infrav1 .FatalError || host .Spec .Status .ErrorType == infrav1 .PermanentError ) &&
247- s .scope .BareMetalMachine .Status .FailureReason == nil {
248- s .scope .BareMetalMachine .SetFailure ("UpdateError" , host .Spec .Status .ErrorMessage )
249- record .Eventf (s .scope .BareMetalMachine , "BareMetalMachineSetFailure" , host .Spec .Status .ErrorMessage )
250- return nil
248+ if host .Spec .MaintenanceMode != nil && * host .Spec .MaintenanceMode {
249+ err := s .scope .SetRemediateMachineAnnotationToDeleteMachine (ctx , FailureMessageMaintenanceMode )
250+ if err != nil {
251+ return nil , err
252+ }
253+ return host , nil
251254 }
252255
253- // if host is healthy, the machine is healthy as well
254- if host .Spec .Status .ErrorType == infrav1 .ErrorType ("" ) {
255- s .scope .BareMetalMachine .Status .FailureMessage = nil
256- s .scope .BareMetalMachine .Status .FailureReason = nil
256+ // if host has a fatal error, then it should be set on the hbmm object as well
257+ if host .Spec .Status .HasFatalError () {
258+ err := s .scope .SetRemediateMachineAnnotationToDeleteMachine (ctx , host .Spec .Status .ErrorMessage )
259+ if err != nil {
260+ return nil , err
261+ }
262+ return host , nil
257263 }
258264
259265 // ensure that the references are correctly set on host
@@ -266,13 +272,13 @@ func (s *Service) update(ctx context.Context) error {
266272 ensureClusterLabel (host , s .scope .Machine .Spec .ClusterName )
267273
268274 if err := analyzePatchError (helper .Patch (ctx , host ), false ); err != nil {
269- return fmt .Errorf ("failed to patch host: %w" , err )
275+ return nil , fmt .Errorf ("failed to patch host: %w" , err )
270276 }
271277
272278 // if machine is a control plane, the host should be set as target of load balancer
273279 if s .scope .IsControlPlane () {
274280 if err := s .reconcileLoadBalancerAttachment (ctx , host ); err != nil {
275- return fmt .Errorf ("failed to reconcile load balancer attachment: %w" , err )
281+ return nil , fmt .Errorf ("failed to reconcile load balancer attachment: %w" , err )
276282 }
277283 }
278284
@@ -281,7 +287,7 @@ func (s *Service) update(ctx context.Context) error {
281287
282288 // update status of HetznerBareMetalMachine with infos from host
283289 s .updateMachineAddresses (host )
284- return nil
290+ return host , nil
285291}
286292
287293func (s * Service ) associate (ctx context.Context ) error {
@@ -661,8 +667,11 @@ func (s *Service) setProviderID(ctx context.Context) error {
661667 }
662668
663669 if host == nil {
664- s .scope .BareMetalMachine .SetFailure ("UpdateError" , "host not found" )
665- return fmt .Errorf ("host not found for machine %s: %w" , s .scope .Machine .Name , err )
670+ err := s .scope .SetRemediateMachineAnnotationToDeleteMachine (ctx , "setProviderID failed: host not found" )
671+ if err != nil {
672+ return err
673+ }
674+ return fmt .Errorf ("host not found for machine %q" , s .scope .Machine .Name )
666675 }
667676
668677 if host .Spec .Status .ProvisioningState != infrav1 .StateProvisioned {
0 commit comments