diff --git a/bmc/redfish.go b/bmc/redfish.go index 261e70c..21bf9fb 100644 --- a/bmc/redfish.go +++ b/bmc/redfish.go @@ -73,8 +73,6 @@ func (r *RedfishBMC) PowerOn(systemID string) error { if err := system.Reset(redfish.OnResetType); err != nil { return fmt.Errorf("failed to reset system to power on state: %w", err) } - } else { - fmt.Printf("System %s is already powered on.\n", systemID) } break } diff --git a/internal/controller/serverclaim_controller.go b/internal/controller/serverclaim_controller.go index b4b6de6..59ff8e2 100644 --- a/internal/controller/serverclaim_controller.go +++ b/internal/controller/serverclaim_controller.go @@ -147,21 +147,19 @@ func (r *ServerClaimReconciler) reconcile(ctx context.Context, log logr.Logger, return ctrl.Result{}, fmt.Errorf("failed to get server: %w", err) } - if server.Status.State != metalv1alpha1.ServerStateAvailable { - log.V(1).Info("Failed to claim server in non available state", "Server", server.Name, "ServerState", server.Status.State) - return ctrl.Result{}, nil - } - - // did somebody else claimed this server? + // did somebody else claim this server? if claimRef := server.Spec.ServerClaimRef; claimRef != nil && claimRef.UID != claim.UID { log.V(1).Info("Server claim ref UID does not match claim", "Server", server.Name, "ClaimUID", claimRef.UID) return ctrl.Result{}, nil } - if err := r.applyBootConfiguration(ctx, server, claim); err != nil { - return ctrl.Result{}, fmt.Errorf("failed to apply boot configuration: %w", err) + // Check server state and only proceed if it is Available or Reserved + if server.Status.State != metalv1alpha1.ServerStateAvailable && server.Status.State != metalv1alpha1.ServerStateReserved { + log.V(1).Info("Server not in a claimable state", "Server", server.Name, "ServerState", server.Status.State) + return ctrl.Result{}, nil } + // Prepare the server modification ahead of state-specific actions. serverBase := server.DeepCopy() server.Spec.ServerClaimRef = &v1.ObjectReference{ APIVersion: "metal.ironcore.dev/v1alpha1", @@ -170,6 +168,13 @@ func (r *ServerClaimReconciler) reconcile(ctx context.Context, log logr.Logger, Name: claim.Name, UID: claim.UID, } + + if server.Status.State == metalv1alpha1.ServerStateAvailable { + if err := r.applyBootConfiguration(ctx, server, claim); err != nil { + return ctrl.Result{}, fmt.Errorf("failed to apply boot configuration: %w", err) + } + } + server.Spec.Power = claim.Spec.Power if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil { return ctrl.Result{}, fmt.Errorf("failed to patch claim ref for server: %w", err)