Skip to content

Commit

Permalink
Claim only PoweredOff Servers (#159)
Browse files Browse the repository at this point in the history
* Claim only `PoweredOff` Servers

Also do not perform power changes in `Available` state if server is already powered off.

ServerClaim controller may patch the server in parallell to `spec.PowerState: On` which may result in powerin on the server in Available state

* also perform power check in `claimServerByReference`
  • Loading branch information
defo89 authored Nov 1, 2024
1 parent 4fe9dfc commit 6ae1ff4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
19 changes: 10 additions & 9 deletions internal/controller/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,18 @@ func (r *ServerReconciler) handleDiscoveryState(ctx context.Context, log logr.Lo

func (r *ServerReconciler) handleAvailableState(ctx context.Context, log logr.Logger, server *metalv1alpha1.Server) (bool, error) {
serverBase := server.DeepCopy()
server.Spec.Power = metalv1alpha1.PowerOff
if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil {
return false, fmt.Errorf("failed to update server power state: %w", err)
}
log.V(1).Info("Updated Server power state", "PowerState", metalv1alpha1.PowerOff)
if server.Status.PowerState != metalv1alpha1.ServerOffPowerState {
server.Spec.Power = metalv1alpha1.PowerOff
if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil {
return false, fmt.Errorf("failed to update server power state: %w", err)
}
log.V(1).Info("Updated Server power state", "PowerState", metalv1alpha1.PowerOff)

if err := r.ensureServerPowerState(ctx, log, server); err != nil {
return false, fmt.Errorf("failed to ensure server power state: %w", err)
if err := r.ensureServerPowerState(ctx, log, server); err != nil {
return false, fmt.Errorf("failed to ensure server power state: %w", err)
}
log.V(1).Info("Server state set to power off")
}
log.V(1).Info("Server state set to power off")

if err := r.ensureInitialBootConfigurationIsDeleted(ctx, server); err != nil {
return false, fmt.Errorf("failed to ensure server initial boot configuration is deleted: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/controller/serverclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ func (r *ServerClaimReconciler) claimServerByReference(ctx context.Context, log
log.V(1).Info("Server not in a claimable state", "Server", server.Name, "ServerState", server.Status.State)
return nil, nil
}
if server.Status.PowerState != metalv1alpha1.ServerOffPowerState {
log.V(1).Info("Server is not powered off", "Server", server.Name, "PowerState", server.Status.PowerState)
return nil, nil
}
if claim.Spec.ServerSelector == nil {
return server, nil
}
Expand Down Expand Up @@ -385,6 +389,10 @@ func (r *ServerClaimReconciler) claimServerBySelector(ctx context.Context, log l
log.V(1).Info("Server not in a claimable state", "Server", s.Name, "ServerState", s.Status.State)
continue
}
if s.Status.PowerState != metalv1alpha1.ServerOffPowerState {
log.V(1).Info("Server is not powered off", "Server", s.Name, "PowerState", s.Status.PowerState)
continue
}
server = s.DeepCopy()
break
}
Expand Down

0 comments on commit 6ae1ff4

Please sign in to comment.