Skip to content

Commit

Permalink
Fix nil pointer deref in PXE boot (#26)
Browse files Browse the repository at this point in the history
- Fix nil pointer deref in PXE boot
- Add proper error handling when polling the registry
  • Loading branch information
afritzler authored Apr 26, 2024
1 parent 845087b commit 0ca787c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions internal/controller/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (r *ServerReconciler) ensureServerStateTransition(ctx context.Context, log
}
log.V(1).Info("Server boot configuration is ready")

if err := r.pxeBootServer(ctx, server); err != nil {
if err := r.pxeBootServer(ctx, log, server); err != nil {
return fmt.Errorf("failed to boot server: %w", err)
}
log.V(1).Info("Booted Server in PXE")
Expand Down Expand Up @@ -371,7 +371,16 @@ func (r *ServerReconciler) serverBootConfigurationIsReady(ctx context.Context, s
return config.Status.State == metalv1alpha1.ServerBootConfigurationStateReady, nil
}

func (r *ServerReconciler) pxeBootServer(ctx context.Context, server *metalv1alpha1.Server) error {
func (r *ServerReconciler) pxeBootServer(ctx context.Context, log logr.Logger, server *metalv1alpha1.Server) error {
if server == nil {
log.V(1).Info("PXE boot server is nil")
return nil
}

if server.Spec.BMCRef == nil {
return fmt.Errorf("can only PXE boot server with valid BMC ref")
}

bmcClient, err := GetBMCClientFromBMCName(ctx, r.Client, server.Spec.BMCRef.Name, r.Insecure)
defer bmcClient.Logout()

Expand All @@ -392,13 +401,12 @@ func (r *ServerReconciler) pxeBootServer(ctx context.Context, server *metalv1alp
func (r *ServerReconciler) extractServerDetailsFromRegistry(ctx context.Context, server *metalv1alpha1.Server) error {
resp, err := http.Get(fmt.Sprintf("%s/systems/%s", r.RegistryURL, server.Spec.UUID))
if err != nil {
if resp != nil && resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("could not find server details: %s", resp.Status)
}
return fmt.Errorf("failed to fetch server details: %w", err)
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("could not find server details: %s", resp.Status)
}

serverDetails := &registry.Server{}
if err := json.NewDecoder(resp.Body).Decode(serverDetails); err != nil {
return fmt.Errorf("failed to decode server details: %w", err)
Expand Down

0 comments on commit 0ca787c

Please sign in to comment.