mirror of
https://github.com/kubereboot/kured.git
synced 2026-08-21 12:26:40 +00:00
The main is doing flag validation through pflags, then did further validation by involving the constructors. With the recent refactor on the commit "Refactor constructors" in this branch, we moved away from that pattern. However, it means we reintroduced a log dependency into our external API, and the external API now had extra validations regardless of the type. This is unnecessary, so I moved away from that pattern, and moved back all the validation into a central place, internal, which is only doing what kured would desire, without exposing it to users. The users could still theoretically use the proper constructors for each type, as they would validate just fine. The only thing they would lose is the kured internal decision of validation/precedence. Signed-off-by: Jean-Philippe Evrard <open-source@a.spamming.party>
36 lines
1.4 KiB
Go
36 lines
1.4 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/kubereboot/kured/pkg/checkers"
|
|
"github.com/kubereboot/kured/pkg/reboot"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// NewRebooter validates the rebootMethod, rebootCommand, and rebootSignal input,
|
|
// then chains to the right constructor.
|
|
func NewRebooter(rebootMethod string, rebootCommand string, rebootSignal int) (reboot.Rebooter, error) {
|
|
switch {
|
|
case rebootMethod == "command":
|
|
log.Infof("Reboot command: %s", rebootCommand)
|
|
return reboot.NewCommandRebooter(rebootCommand)
|
|
case rebootMethod == "signal":
|
|
log.Infof("Reboot signal: %d", rebootSignal)
|
|
return reboot.NewSignalRebooter(rebootSignal)
|
|
default:
|
|
return nil, fmt.Errorf("invalid reboot-method configured %s, expected signal or command", rebootMethod)
|
|
}
|
|
}
|
|
|
|
// NewRebootChecker validates the rebootSentinelCommand, rebootSentinelFile input,
|
|
// then chains to the right constructor.
|
|
func NewRebootChecker(rebootSentinelCommand string, rebootSentinelFile string) (checkers.Checker, error) {
|
|
// An override of rebootSentinelCommand means a privileged command
|
|
if rebootSentinelCommand != "" {
|
|
log.Infof("Sentinel checker is (privileged) user provided command: %s", rebootSentinelCommand)
|
|
return checkers.NewCommandChecker(rebootSentinelCommand)
|
|
}
|
|
log.Infof("Sentinel checker is (unprivileged) testing for the presence of: %s", rebootSentinelFile)
|
|
return checkers.NewFileRebootChecker(rebootSentinelFile)
|
|
}
|