From 626db871581aee0f7d5346d75e96b6d349ea08e6 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Evrard Date: Sat, 12 Oct 2024 17:13:25 +0200 Subject: [PATCH] Add error to reboot interface Without this, impossible to bubble up errors to main Signed-off-by: Jean-Philippe Evrard --- cmd/kured/main.go | 6 +++++- pkg/reboot/command.go | 5 +++-- pkg/reboot/reboot.go | 2 +- pkg/reboot/signal.go | 11 ++++------- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cmd/kured/main.go b/cmd/kured/main.go index 1ff59d7..3c77c8b 100644 --- a/cmd/kured/main.go +++ b/cmd/kured/main.go @@ -710,7 +710,11 @@ func rebootAsRequired(nodeID string, rebooter reboot.Rebooter, checker checkers. } } log.Infof("Triggering reboot for node %v", nodeID) - rebooter.Reboot() + + err = rebooter.Reboot() + if err != nil { + log.Fatalf("Unable to reboot node: %v", err) + } for { log.Infof("Waiting for reboot") time.Sleep(time.Minute) diff --git a/pkg/reboot/command.go b/pkg/reboot/command.go index 7414302..6cfb61d 100644 --- a/pkg/reboot/command.go +++ b/pkg/reboot/command.go @@ -13,11 +13,12 @@ type CommandRebooter struct { } // Reboot triggers the reboot command -func (c CommandRebooter) Reboot() { +func (c CommandRebooter) Reboot() error { log.Infof("Invoking command: %s", c.RebootCommand) if err := util.NewCommand(c.RebootCommand[0], c.RebootCommand[1:]...).Run(); err != nil { - log.Fatalf("Error invoking reboot command: %v", err) + return fmt.Errorf("error invoking reboot command %s: %v", c.RebootCommand, err) } + return nil } // NewCommandRebooter is the constructor to create a CommandRebooter from a string not diff --git a/pkg/reboot/reboot.go b/pkg/reboot/reboot.go index 171004d..9ce65d2 100644 --- a/pkg/reboot/reboot.go +++ b/pkg/reboot/reboot.go @@ -10,7 +10,7 @@ import ( // The Reboot method does not expect any return, yet should // most likely be refactored in the future to return an error type Rebooter interface { - Reboot() + Reboot() error } // NewRebooter validates the rebootMethod, rebootCommand, and rebootSignal input, diff --git a/pkg/reboot/signal.go b/pkg/reboot/signal.go index ef2595f..92b7e04 100644 --- a/pkg/reboot/signal.go +++ b/pkg/reboot/signal.go @@ -4,8 +4,6 @@ import ( "fmt" "os" "syscall" - - log "github.com/sirupsen/logrus" ) // SignalRebooter holds context-information for a signal reboot. @@ -14,20 +12,19 @@ type SignalRebooter struct { } // Reboot triggers the reboot signal -func (c SignalRebooter) Reboot() { - log.Infof("Invoking signal: %v", c.Signal) - +func (c SignalRebooter) Reboot() error { process, err := os.FindProcess(1) if err != nil { - log.Fatalf("Not running on Unix: %v", err) + return fmt.Errorf("not running on Unix: %v", err) } err = process.Signal(syscall.Signal(c.Signal)) // Either PID does not exist, or the signal does not work. Hoping for // a decent enough error. if err != nil { - log.Fatalf("Signal of SIGRTMIN+5 failed: %v", err) + return fmt.Errorf("signal of SIGRTMIN+5 failed: %v", err) } + return nil } // NewSignalRebooter is the constructor which sets the signal number.