diff options
author | Valery Piashchynski <[email protected]> | 2023-08-06 22:57:16 +0200 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2023-08-06 22:57:16 +0200 |
commit | d5886b45fa3d8e65f6217f8f37fe848d6355c851 (patch) | |
tree | 6634496990556b9f1cf86a6e652d559632095299 /container | |
parent | 3bf88db8a91fcb79fb46c80abdbf55ab8d4a9f8f (diff) |
feature: sd_notify support
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'container')
-rw-r--r-- | container/config.go | 56 |
1 files changed, 20 insertions, 36 deletions
diff --git a/container/config.go b/container/config.go index ce1edfa1..9364ecf8 100644 --- a/container/config.go +++ b/container/config.go @@ -8,14 +8,18 @@ import ( "golang.org/x/exp/slog" ) +// Config defines endure container configuration. type Config struct { - GracePeriod time.Duration - PrintGraph bool - LogLevel slog.Leveler + GracePeriod time.Duration `mapstructure:"grace_period"` + LogLevel string `mapstructure:"log_level"` + EnableWatchdog bool `mapstructure:"enable_watchdog"` + PrintGraph bool `mapstructure:"print_graph"` } const ( - endureKey = "endure" + // endure config key + endureKey = "endure" + // overall grace period, after which container will be stopped forcefully defaultGracePeriod = time.Second * 30 ) @@ -29,46 +33,26 @@ func NewConfig(cfgFile string) (*Config, error) { return nil, err } - if !v.IsSet(endureKey) { - return &Config{ // return config with defaults - GracePeriod: defaultGracePeriod, - PrintGraph: false, - LogLevel: slog.LevelError, - }, nil - } - - rrCfgEndure := struct { - GracePeriod time.Duration `mapstructure:"grace_period"` - PrintGraph bool `mapstructure:"print_graph"` - LogLevel string `mapstructure:"log_level"` - }{} - - err = v.UnmarshalKey(endureKey, &rrCfgEndure) - if err != nil { - return nil, err - } - - if rrCfgEndure.GracePeriod == 0 { - rrCfgEndure.GracePeriod = defaultGracePeriod + cfg := &Config{ + GracePeriod: defaultGracePeriod, + LogLevel: "error", + PrintGraph: false, + EnableWatchdog: false, } - if rrCfgEndure.LogLevel == "" { - rrCfgEndure.LogLevel = "error" + if !v.IsSet(endureKey) { + return cfg, nil } - logLevel, err := parseLogLevel(rrCfgEndure.LogLevel) + err = v.UnmarshalKey(endureKey, cfg) if err != nil { return nil, err } - return &Config{ - GracePeriod: rrCfgEndure.GracePeriod, - PrintGraph: rrCfgEndure.PrintGraph, - LogLevel: logLevel, - }, nil + return cfg, nil } -func parseLogLevel(s string) (slog.Leveler, error) { +func ParseLogLevel(s string) (slog.Leveler, error) { switch s { case "debug": return slog.LevelDebug, nil @@ -78,7 +62,7 @@ func parseLogLevel(s string) (slog.Leveler, error) { return slog.LevelWarn, nil case "error": return slog.LevelError, nil + default: + return slog.LevelError, fmt.Errorf(`unknown log level "%s" (allowed: debug, info, warn, error)`, s) } - - return slog.LevelInfo, fmt.Errorf(`unknown log level "%s" (allowed: debug, info, warn, error)`, s) } |