From 6f9b48a7a02b75f95eee60b2d81794e729ba2cca Mon Sep 17 00:00:00 2001 From: Stefan Hipfel Date: Thu, 12 Sep 2024 16:38:36 +0200 Subject: [PATCH 1/5] implements adhoc operations --- bmc/bmc.go | 2 +- bmc/redfish.go | 10 ++++++-- internal/controller/server_controller.go | 31 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/bmc/bmc.go b/bmc/bmc.go index e9592a9..fa6c8e5 100644 --- a/bmc/bmc.go +++ b/bmc/bmc.go @@ -17,7 +17,7 @@ type BMC interface { PowerOff(systemUUID string) error // Reset performs a reset on the system. - Reset() error + Reset(systemUUID string, resetType redfish.ResetType) error // SetPXEBootOnce sets the boot device for the next system boot. SetPXEBootOnce(systemUUID string) error diff --git a/bmc/redfish.go b/bmc/redfish.go index 2edf848..24c9f06 100644 --- a/bmc/redfish.go +++ b/bmc/redfish.go @@ -87,8 +87,14 @@ func (r *RedfishBMC) PowerOff(systemUUID string) error { } // Reset performs a reset on the system using Redfish. -func (r *RedfishBMC) Reset() error { - // Implementation details... +func (r *RedfishBMC) Reset(systemUUID string, resetType redfish.ResetType) error { + system, err := r.getSystemByUUID(systemUUID) + if err != nil { + return fmt.Errorf("failed to get systems: %w", err) + } + if err := system.Reset(resetType); err != nil { + return fmt.Errorf("failed to reset system to power on state: %w", err) + } return nil } diff --git a/internal/controller/server_controller.go b/internal/controller/server_controller.go index 2e1d870..af4d0b1 100644 --- a/internal/controller/server_controller.go +++ b/internal/controller/server_controller.go @@ -17,6 +17,7 @@ import ( metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1" "github.com/ironcore-dev/metal-operator/internal/api/registry" "github.com/ironcore-dev/metal-operator/internal/ignition" + "github.com/stmcginnis/gofish/redfish" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" meta "k8s.io/apimachinery/pkg/api/meta" @@ -38,6 +39,7 @@ const ( ServerFinalizer = "metal.ironcore.dev/server" InternalAnnotationTypeKeyName = "metal.ironcore.dev/type" InternalAnnotationTypeValue = "Internal" + AnnotationOperationKey = "metal.ironcore.dev/operation" ) const ( @@ -125,6 +127,10 @@ func (r *ServerReconciler) reconcile(ctx context.Context, log logr.Logger, serve return ctrl.Result{}, err } } + if modified, err := r.handleAnnotionOperations(ctx, log, server); err != nil || modified { + return ctrl.Result{}, err + } + log.V(1).Info("Handled annotation operations") if modified, err := clientutils.PatchEnsureFinalizer(ctx, r.Client, server, ServerFinalizer); err != nil || modified { return ctrl.Result{}, err @@ -812,6 +818,31 @@ func (r *ServerReconciler) applyBiosSettings(ctx context.Context, log logr.Logge return nil } +func (r *ServerReconciler) handleAnnotionOperations(ctx context.Context, log logr.Logger, server *metalv1alpha1.Server) (bool, error) { + annotations := server.GetAnnotations() + operation, ok := annotations[metalv1alpha1.OperationAnnotation] + if !ok { + return false, nil + } + bmcClient, err := GetBMCClientForServer(ctx, r.Client, server, r.Insecure) + if err != nil { + return false, fmt.Errorf("failed to create BMC client: %w", err) + } + defer bmcClient.Logout() + log.Info("Handling operation", "Operation", operation) + if err := bmcClient.Reset(server.Spec.UUID, redfish.ResetType(operation)); err != nil { + return false, fmt.Errorf("failed to reset server: %w", err) + } + log.Info("Successfully executed operation", "Operation", operation) + serverBase := server.DeepCopy() + delete(annotations, metalv1alpha1.OperationAnnotation) + server.SetAnnotations(annotations) + if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil { + return false, fmt.Errorf("failed to patch Server Annotations: %w", err) + } + return true, nil +} + // SetupWithManager sets up the controller with the Manager. func (r *ServerReconciler) SetupWithManager(mgr ctrl.Manager) error { // Create a channel to send periodic events From 3f0a639d3b2f77cf2748e98daaf2b46f7ba96147 Mon Sep 17 00:00:00 2001 From: Stefan Hipfel Date: Thu, 12 Sep 2024 16:40:25 +0200 Subject: [PATCH 2/5] removes AnnotationOperationKey --- internal/controller/server_controller.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/controller/server_controller.go b/internal/controller/server_controller.go index af4d0b1..9f5ac2a 100644 --- a/internal/controller/server_controller.go +++ b/internal/controller/server_controller.go @@ -39,7 +39,6 @@ const ( ServerFinalizer = "metal.ironcore.dev/server" InternalAnnotationTypeKeyName = "metal.ironcore.dev/type" InternalAnnotationTypeValue = "Internal" - AnnotationOperationKey = "metal.ironcore.dev/operation" ) const ( From bcbe00bf1bb0b7fcc42cfc7f062b666f34b09cbc Mon Sep 17 00:00:00 2001 From: Stefan Hipfel Date: Thu, 12 Sep 2024 16:44:01 +0200 Subject: [PATCH 3/5] corrects error message in bmc reset --- bmc/redfish.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bmc/redfish.go b/bmc/redfish.go index 24c9f06..cf10dca 100644 --- a/bmc/redfish.go +++ b/bmc/redfish.go @@ -93,7 +93,7 @@ func (r *RedfishBMC) Reset(systemUUID string, resetType redfish.ResetType) error return fmt.Errorf("failed to get systems: %w", err) } if err := system.Reset(resetType); err != nil { - return fmt.Errorf("failed to reset system to power on state: %w", err) + return fmt.Errorf("failed to reset system: %w", err) } return nil } From 8fb0b78b03ed793253b2aeee7c7533fc75e91f45 Mon Sep 17 00:00:00 2001 From: Stefan Hipfel Date: Thu, 12 Sep 2024 16:45:11 +0200 Subject: [PATCH 4/5] adds mising V(1) to logs --- internal/controller/server_controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/controller/server_controller.go b/internal/controller/server_controller.go index 9f5ac2a..e4e8053 100644 --- a/internal/controller/server_controller.go +++ b/internal/controller/server_controller.go @@ -828,11 +828,11 @@ func (r *ServerReconciler) handleAnnotionOperations(ctx context.Context, log log return false, fmt.Errorf("failed to create BMC client: %w", err) } defer bmcClient.Logout() - log.Info("Handling operation", "Operation", operation) + log.V(1).Info("Handling operation", "Operation", operation) if err := bmcClient.Reset(server.Spec.UUID, redfish.ResetType(operation)); err != nil { return false, fmt.Errorf("failed to reset server: %w", err) } - log.Info("Successfully executed operation", "Operation", operation) + log.V(1).Info("Successfully executed operation", "Operation", operation) serverBase := server.DeepCopy() delete(annotations, metalv1alpha1.OperationAnnotation) server.SetAnnotations(annotations) From 7fa8a59669a2df70f3a581d70fab9b6c18e0ddd6 Mon Sep 17 00:00:00 2001 From: Stefan Hipfel Date: Fri, 13 Sep 2024 10:03:36 +0200 Subject: [PATCH 5/5] fixes log messages --- internal/controller/server_controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/controller/server_controller.go b/internal/controller/server_controller.go index e4e8053..22a7d9b 100644 --- a/internal/controller/server_controller.go +++ b/internal/controller/server_controller.go @@ -832,12 +832,12 @@ func (r *ServerReconciler) handleAnnotionOperations(ctx context.Context, log log if err := bmcClient.Reset(server.Spec.UUID, redfish.ResetType(operation)); err != nil { return false, fmt.Errorf("failed to reset server: %w", err) } - log.V(1).Info("Successfully executed operation", "Operation", operation) + log.V(1).Info("Operation completed", "Operation", operation) serverBase := server.DeepCopy() delete(annotations, metalv1alpha1.OperationAnnotation) server.SetAnnotations(annotations) if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil { - return false, fmt.Errorf("failed to patch Server Annotations: %w", err) + return false, fmt.Errorf("failed to patch server annotations: %w", err) } return true, nil }