Revert to use a constructor again

Without this, we have no validation of the data in command/signal
reboot.

This was not a problem in the first refactor, as the constructor
was a dummy one, without validation.

However, as we refactoed, we now have code in the root method
that is validation for the reboot command. This can now be
encompassed in the constructor.

Signed-off-by: Jean-Philippe Evrard <open-source@a.spamming.party>
This commit is contained in:
Jean-Philippe Evrard
2024-10-19 15:51:04 +02:00
parent 3895a2f6d3
commit 42c4b8bc53
3 changed files with 23 additions and 10 deletions
+3 -9
View File
@@ -35,7 +35,6 @@ import (
"github.com/kubereboot/kured/pkg/reboot"
"github.com/kubereboot/kured/pkg/taints"
"github.com/kubereboot/kured/pkg/timewindow"
"github.com/kubereboot/kured/pkg/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
@@ -722,19 +721,14 @@ func root(cmd *cobra.Command, args []string) {
}
log.Infof("Reboot schedule: %v", window)
restartCommand, err := shlex.Split(rebootCommand)
if err != nil {
log.Fatalf("Error parsing provided reboot command: %v", err)
}
var rebooter reboot.Rebooter
switch {
case rebootMethod == "command":
log.Infof("Reboot command: %s", restartCommand)
rebooter = reboot.CommandRebooter{RebootCommand: util.PrivilegedHostCommand(1, restartCommand)}
log.Infof("Reboot command: %s", rebootCommand)
rebooter = reboot.NewCommandRebooter(rebootCommand)
case rebootMethod == "signal":
log.Infof("Reboot signal: %v", rebootSignal)
rebooter = reboot.SignalRebooter{Signal: rebootSignal}
rebooter = reboot.NewSignalRebooter(rebootSignal)
default:
log.Fatalf("Invalid reboot-method configured: %s", rebootMethod)
}
+14 -1
View File
@@ -1,11 +1,12 @@
package reboot
import (
"github.com/google/shlex"
"github.com/kubereboot/kured/pkg/util"
log "github.com/sirupsen/logrus"
)
// CommandRebooter holds context-information for a command reboot.
// CommandRebooter holds context-information for a reboot with command
type CommandRebooter struct {
RebootCommand []string
}
@@ -17,3 +18,15 @@ func (c CommandRebooter) Reboot() {
log.Fatalf("Error invoking reboot command: %v", err)
}
}
// NewCommandRebooter is the constructor to create a CommandRebooter from a string not
// yet shell lexed. You can skip this constructor if you parse the data correctly first
// when instantiating a CommandRebooter instance.
func NewCommandRebooter(rebootCommand string) *CommandRebooter {
cmd, err := shlex.Split(rebootCommand)
if err != nil {
log.Fatalf("Error parsing provided reboot command: %v", err)
}
return &CommandRebooter{RebootCommand: util.PrivilegedHostCommand(1, cmd)}
}
+6
View File
@@ -28,3 +28,9 @@ func (c SignalRebooter) Reboot() {
log.Fatalf("Signal of SIGRTMIN+5 failed: %v", err)
}
}
// NewSignalRebooter is the constructor which sets the signal number.
// The constructor does not yet validate any input. It should be done in a later commit.
func NewSignalRebooter(sig int) *SignalRebooter {
return &SignalRebooter{Signal: sig}
}