-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make all the internal validations ... internal
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 <[email protected]>
- Loading branch information
Showing
4 changed files
with
40 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,9 @@ | ||
package reboot | ||
|
||
import ( | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Rebooter is the standard interface to use to execute | ||
// the reboot, after it has been considered as necessary. | ||
// 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() error | ||
} | ||
|
||
// NewRebooter validates the rebootMethod, rebootCommand, and rebootSignal input, | ||
// then chains to the right constructor. This can be made internal later, as | ||
// only the real rebooters constructors should be public (by opposition to this one) | ||
func NewRebooter(rebootMethod string, rebootCommand string, rebootSignal int) (Rebooter, error) { | ||
switch { | ||
case rebootMethod == "command": | ||
log.Infof("Reboot command: %s", rebootCommand) | ||
return NewCommandRebooter(rebootCommand) | ||
case rebootMethod == "signal": | ||
log.Infof("Reboot signal: %d", rebootSignal) | ||
return NewSignalRebooter(rebootSignal) | ||
default: | ||
return nil, fmt.Errorf("invalid reboot-method configured %s, expected signal or command", rebootMethod) | ||
} | ||
} |