Skip to content

Commit

Permalink
Refactor events of remediation controllers
Browse files Browse the repository at this point in the history
Signed-off-by: Aniruddha Basak <[email protected]>
  • Loading branch information
aniruddha2000 committed Oct 19, 2023
1 parent cb8f745 commit 0a9d636
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/services/baremetal/remediation/remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *Service) Reconcile(ctx context.Context) (res reconcile.Result, err erro
record.Warn(s.scope.BareMetalRemediation, "FailedSettingConditionOnMachine", err.Error())
return res, err
}
record.Eventf(s.scope.BareMetalRemediation, "ExitRemediation", "exit remediation because bare metal machine has no host annotation")
record.Event(s.scope.BareMetalRemediation, "ExitRemediation", "exit remediation because bare metal machine has no host annotation")
return res, nil
}

Expand Down Expand Up @@ -158,13 +158,16 @@ func (s *Service) remediate(ctx context.Context, host infrav1.HetznerBareMetalHo
// add annotation to host so that it reboots
host.Annotations, err = addRebootAnnotation(host.Annotations)
if err != nil {
record.Warn(s.scope.BareMetalRemediation, "FailedAddingRebootAnnotation", err.Error())
return fmt.Errorf("failed to add reboot annotation: %w", err)
}

if err := patchHelper.Patch(ctx, &host); err != nil {
return fmt.Errorf("failed to patch: %s %s/%s %w", host.Kind, host.Namespace, host.Name, err)
}

record.Event(s.scope.BareMetalRemediation, "AnnotationAdded", "Reboot annotation is added to the BareMetalHost")

// update status of BareMetalRemediation object
now := metav1.Now()
s.scope.BareMetalRemediation.Status.LastRemediated = &now
Expand All @@ -191,6 +194,7 @@ func (s *Service) handlePhaseWaiting(ctx context.Context) (res reconcile.Result,
record.Warn(s.scope.BareMetalRemediation, "FailedSettingConditionOnMachine", err.Error())
return res, err
}
record.Event(s.scope.BareMetalRemediation, "SetOwnerRemediatedCondition", "because retryLimit is reached and reboot timed out")

return res, nil
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/services/hcloud/remediation/remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (s *Service) Reconcile(ctx context.Context) (res reconcile.Result, err erro
s.scope.HCloudRemediation.Status.Phase = infrav1.PhaseDeleting

if err := s.setOwnerRemediatedCondition(ctx); err != nil {
record.Warn(s.scope.HCloudRemediation, "FailedSettingConditionOnMachine", err.Error())
return res, fmt.Errorf("failed to set conditions on CAPI machine: %w", err)
}
record.Warn(s.scope.HCloudRemediation, "ExitRemediation", "exit remediation because bare metal server does not exist")
return res, nil
}

Expand Down Expand Up @@ -93,8 +95,10 @@ func (s *Service) handlePhaseRunning(ctx context.Context, server *hcloud.Server)
if s.scope.HCloudRemediation.Status.LastRemediated == nil {
if err := s.scope.HCloudClient.RebootServer(ctx, server); err != nil {
hcloudutil.HandleRateLimitExceeded(s.scope.HCloudMachine, err, "RebootServer")
record.Warn(s.scope.HCloudRemediation, "FailedRebootServer", err.Error())
return res, fmt.Errorf("failed to reboot server %v: %w", server.ID, err)
}
record.Event(s.scope.HCloudRemediation, "ServerRebooted", "Server has been rebooted")

s.scope.HCloudRemediation.Status.LastRemediated = &now
s.scope.HCloudRemediation.Status.RetryCount++
Expand All @@ -119,8 +123,10 @@ func (s *Service) handlePhaseRunning(ctx context.Context, server *hcloud.Server)
// remediate now
if err := s.scope.HCloudClient.RebootServer(ctx, server); err != nil {
hcloudutil.HandleRateLimitExceeded(s.scope.HCloudMachine, err, "RebootServer")
record.Warn(s.scope.HCloudRemediation, "FailedRebootServer", err.Error())
return res, fmt.Errorf("failed to reboot server %v: %w", server.ID, err)
}
record.Event(s.scope.HCloudRemediation, "ServerRebooted", "Server has been rebooted")

s.scope.HCloudRemediation.Status.LastRemediated = &now
s.scope.HCloudRemediation.Status.RetryCount++
Expand All @@ -143,8 +149,10 @@ func (s *Service) handlePhaseWaiting(ctx context.Context) (res reconcile.Result,
s.scope.HCloudRemediation.Status.Phase = infrav1.PhaseDeleting

if err := s.setOwnerRemediatedCondition(ctx); err != nil {
record.Warn(s.scope.HCloudRemediation, "FailedSettingConditionOnMachine", err.Error())
return res, fmt.Errorf("failed to set conditions on CAPI machine: %w", err)
}
record.Event(s.scope.HCloudRemediation, "SetOwnerRemediatedCondition", "exit remediation because because retryLimit is reached and reboot timed out")

return res, nil
}
Expand Down

0 comments on commit 0a9d636

Please sign in to comment.