openbao-monitor: align the log levels with helm chart
The helm chart uses numeric values representing the following log levels: DEBUG, INFO, WARNING, ERROR and FATAL Update the validation to accept numeric levels, and interpet 5 (FATAL) as ERROR. Test Plan: PASS test valid/invalid loglevels, numeric bounds Change-Id: Ifbebe30e487f30ba11ada398056252e80a07ba94 Signed-off-by: Michel Thebeau <Michel.Thebeau@windriver.com>
This commit is contained in:

committed by
Michel Thebeau

parent
6244fc7974
commit
bc138e563c
@@ -60,11 +60,7 @@ func setupCmd(cmd *cobra.Command, args []string) error {
|
||||
|
||||
// Set default configuration for logs if no custum configs are given
|
||||
logFile := globalConfig.LogPath
|
||||
logLevel := globalConfig.LogLevel
|
||||
if logLevel == "" {
|
||||
// Default log level if no log level was set
|
||||
logLevel = "INFO"
|
||||
}
|
||||
logLevel := globalConfig.InterpretLogLevel()
|
||||
|
||||
// Set default to stderr if no log file was specified.
|
||||
logWriter = os.Stderr
|
||||
|
@@ -65,6 +65,7 @@ type MonitorConfig struct {
|
||||
|
||||
// The default log level
|
||||
// Available log levels: DEBUG, INFO, WARN and ERROR
|
||||
// Accepts numeric values matching helm chart log levels (4-5 are interpreted as ERROR)
|
||||
LogLevel string `yaml:"logLevel"`
|
||||
|
||||
// The time in seconds waited between each unseal check in the run command.
|
||||
@@ -295,3 +296,26 @@ func (configInstance *MonitorConfig) ParseInitResponse(dnshost string, responce
|
||||
slog.Debug("Parsing init response complete")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Interpret numeric or text log level
|
||||
// Always returns a log level in string format
|
||||
func (configInstance *MonitorConfig) InterpretLogLevel() string {
|
||||
// Check if the log level is a number
|
||||
if converted, err := strconv.Atoi(configInstance.LogLevel); err == nil {
|
||||
if level, exists := availableLogLevels[converted]; exists {
|
||||
return level
|
||||
}
|
||||
|
||||
// error, but this code should not be reached if validateLogConfig works
|
||||
fmt.Errorf("the numeric LogLevel %v is not a valid log level", configInstance.LogLevel)
|
||||
return "INFO" // Default to INFO if the numeric level is invalid
|
||||
}
|
||||
|
||||
// Default to INFO if no log level was set
|
||||
if configInstance.LogLevel == "" {
|
||||
return "INFO"
|
||||
}
|
||||
|
||||
// validateLogConfig already validated the LogLevel
|
||||
return configInstance.LogLevel
|
||||
}
|
@@ -6,9 +6,18 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Available log levels; these should match the levels available in helm chart
|
||||
var availableLogLevels = map[int]string{
|
||||
1: "DEBUG",
|
||||
2: "INFO",
|
||||
3: "WARN",
|
||||
4: "ERROR",
|
||||
5: "ERROR",
|
||||
}
|
||||
|
||||
func (configInstance MonitorConfig) validateDNS() error {
|
||||
for domain_name, url := range configInstance.ServerAddresses {
|
||||
// If Host is empty, then the domain entry is invalid
|
||||
@@ -63,6 +72,8 @@ func (configInstance MonitorConfig) validateKeyShards() error {
|
||||
}
|
||||
|
||||
func (configInstance MonitorConfig) validateLogConfig() error {
|
||||
var found bool = false
|
||||
|
||||
if configInstance.LogPath != "" {
|
||||
_, err := os.Stat(path.Dir(configInstance.LogPath))
|
||||
if err != nil {
|
||||
@@ -71,10 +82,27 @@ func (configInstance MonitorConfig) validateLogConfig() error {
|
||||
}
|
||||
}
|
||||
if configInstance.LogLevel != "" {
|
||||
availableLogLevels := []string{"DEBUG", "INFO", "WARN", "ERROR"}
|
||||
if !slices.Contains(availableLogLevels, configInstance.LogLevel) {
|
||||
return fmt.Errorf(
|
||||
"the listed LogLevel %v is not a valid log level", configInstance.LogLevel)
|
||||
if converted, err := strconv.Atoi(configInstance.LogLevel); err == nil {
|
||||
// convert the numeric log level to string
|
||||
if _, exists := availableLogLevels[converted]; exists {
|
||||
// pass, Accept the numeric LogLevel
|
||||
} else {
|
||||
return fmt.Errorf(
|
||||
"the numeric LogLevel %v is not a valid log level", configInstance.LogLevel)
|
||||
}
|
||||
} else {
|
||||
// Check if the LogLevel is one of the available log levels
|
||||
for _, value := range availableLogLevels {
|
||||
if value == configInstance.LogLevel {
|
||||
// Accept LogLevel
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf(
|
||||
"the listed LogLevel %v is not a valid log level", configInstance.LogLevel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user