diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index 404c45c..0e6f452 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -22,9 +22,10 @@ func main() { log.Println("[DEBUG] shutdown sequence complete") }() + // initialize application configuration initialize.Init() - //go httpServer(config.WebSrvIP, config.WebSrvPort) + //go httpServer(cfg.WebSrvIP, cfg.WebSrvPort) forever() } diff --git a/internal/config/config.go b/internal/config/config.go index 39a1fa7..bab5ef7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,6 +2,11 @@ package config import ( "fmt" + "log" + "mutating-webhook/internal/envconfig" + "os" + "reflect" + "strconv" "time" "github.com/hashicorp/logutils" @@ -10,8 +15,9 @@ import ( type Config struct { // time configuration TimeFormat string `env:"TIME_FORMAT" default:"2006-01-02 15:04:05"` - TimeZoneLocal *time.Location - TimeZoneUTC *time.Location + TimeZoneLocal string `env:"TIME_ZONE" default:"America/Chicago"` + TZoneLocal *time.Location + TZoneUTC *time.Location // logging LogLevel int `env:"LOG_LEVEL" default:"50"` @@ -25,10 +31,13 @@ type Config struct { WebServerIdleTimeout int `env:"WEBSERVER_IDLE_TIMEOUT" default:"2"` } -func NewConfig() (*Config, error) { - cfg := &Config{} - - return cfg, nil +func DefaultConfig() *Config { + return &Config{ + Log: &logutils.LevelFilter{ + Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARNING", "ERROR"}, + Writer: os.Stderr, + }, + } } func (cfg *Config) Validate() error { @@ -53,3 +62,36 @@ func (cfg *Config) Validate() error { return nil } + +func (cfg *Config) SetLogLevel() { + switch { + case cfg.LogLevel <= 20: + cfg.Log.SetMinLevel(logutils.LogLevel("ERROR")) + case cfg.LogLevel > 20 && cfg.LogLevel <= 40: + cfg.Log.SetMinLevel(logutils.LogLevel("WARNING")) + case cfg.LogLevel > 40 && cfg.LogLevel <= 60: + cfg.Log.SetMinLevel(logutils.LogLevel("INFO")) + case cfg.LogLevel > 60 && cfg.LogLevel <= 80: + cfg.Log.SetMinLevel(logutils.LogLevel("DEBUG")) + case cfg.LogLevel > 80: + cfg.Log.SetMinLevel(logutils.LogLevel("TRACE")) + } + log.SetOutput(cfg.Log) +} + +func (cfg *Config) PrintRunningConfig(cfgInfo []envconfig.StructInfo) { + log.Printf("[DEBUG] Current Running Configuration Values:") + for _, info := range cfgInfo { + switch info.Type.String() { + case "string": + p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*string) + log.Printf("[DEBUG]\t%s\t\t= %s\n", info.Alt, *p) + case "bool": + p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*bool) + log.Printf("[DEBUG]\t%s\t\t= %s\n", info.Alt, strconv.FormatBool(*p)) + case "int": + p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*int) + log.Printf("[DEBUG]\t%s\t\t= %s\n", info.Alt, strconv.FormatInt(int64(*p), 10)) + } + } +} diff --git a/internal/initialize/initialize.go b/internal/initialize/initialize.go index 06bbaa4..6b07b27 100644 --- a/internal/initialize/initialize.go +++ b/internal/initialize/initialize.go @@ -6,6 +6,7 @@ import ( "os" "reflect" "strconv" + "time" "mutating-webhook/internal/config" "mutating-webhook/internal/envconfig" @@ -58,35 +59,8 @@ func getEnvBool(env string, def bool) bool { return retVal } -/* -func setLogLevel(l int) { - switch { - case l <= 20: - config.Log.SetMinLevel(logutils.LogLevel("ERROR")) - case l > 20 && l <= 40: - config.Log.SetMinLevel(logutils.LogLevel("WARNING")) - case l > 40 && l <= 60: - config.Log.SetMinLevel(logutils.LogLevel("INFO")) - case l > 60 && l <= 80: - config.Log.SetMinLevel(logutils.LogLevel("DEBUG")) - case l > 80: - config.Log.SetMinLevel(logutils.LogLevel("TRACE")) - } -} -*/ - -func Init() { - /* - var ( - tz string - err error - ) - */ - - cfg, err := config.NewConfig() - if err != nil { - log.Fatalf("[FATAL] Unable to initialize.") - } +func Init() *config.Config { + cfg := config.DefaultConfig() cfgInfo, err := envconfig.GetStructInfo(cfg) if err != nil { @@ -123,64 +97,28 @@ func Init() { } flag.Parse() + // set logging level + cfg.SetLogLevel() + + // if err = cfg.Validate(); err != nil { log.Fatalf("[FATAL] %v", err) } - /* - // log configuration - flag.IntVar(&config.LogLevel, - "log", - getEnvInt("LOG_LEVEL", 50), - "(LOG_LEVEL)\nlog level") - // local webserver configuration - flag.IntVar(&config.WebSrvPort, - "http-port", - getEnvInt("HTTP_PORT", 8080), - "(HTTP_PORT)\nlisten port for internal webserver") - flag.StringVar(&config.WebSrvIP, - "http-ip", - getEnvString("HTTP_IP", ""), - "(HTTP_IP)\nlisten ip for internal webserver") - flag.IntVar(&config.WebSrvReadTimeout, - "http-read-timeout", - getEnvInt("HTTP_READ_TIMEOUT", 5), - "(HTTP_READ_TIMEOUT)\ninternal http server read timeout in seconds") - flag.IntVar(&config.WebSrvWriteTimeout, - "http-write-timeout", - getEnvInt("HTTP_WRITE_TIMEOUT", 2), - "(HTTP_WRITE_TIMEOUT\ninternal http server write timeout in seconds") - flag.IntVar(&config.WebSrvIdleTimeout, - "http-idle-timeout", - getEnvInt("HTTP_IDLE_TIMEOUT", 2), - "(HTTP_IDLE_TIMEOUT)\ninternal http server idle timeout in seconds") - // timezone - flag.StringVar(&tz, - "timezone", - getEnvString("TZ", "America/Chicago"), - "(TZ)\ntimezone") - // read command line options - flag.Parse() + // timezone & format configuration + cfg.TZoneUTC, _ = time.LoadLocation("UTC") + if err != nil { + log.Fatalf("[ERROR] Unable to parse timezone string. Please use one of the timezone database values listed here: %s", "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones") + } + cfg.TZoneLocal, err = time.LoadLocation(cfg.TimeZoneLocal) + if err != nil { + log.Fatalf("[ERROR] Unable to parse timezone string. Please use one of the timezone database values listed here: %s", "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones") + } + time.Now().Format(cfg.TimeFormat) - // logging level - setLogLevel(config.LogLevel) - log.SetOutput(config.Log) + // print running config + cfg.PrintRunningConfig(cfgInfo) - // timezone configuration - config.TimeZoneUTC, _ = time.LoadLocation("UTC") - if config.TimeZoneLocal, err = time.LoadLocation(tz); err != nil { - log.Fatalf("[ERROR] Unable to parse timezone string. Please use one of the timezone database values listed here: %s", "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones") - } - - // print current configuration - log.Printf("[DEBUG] configuration value set: LOG_LEVEL = %s\n", strconv.Itoa(config.LogLevel)) - log.Printf("[DEBUG] configuration value set: HTTP_PORT = %s\n", strconv.Itoa(config.WebSrvPort)) - log.Printf("[DEBUG] configuration value set: HTTP_IP = %s\n", config.WebSrvIP) - log.Printf("[DEBUG] configuration value set: HTTP_READ_TIMEOUT = %s\n", strconv.Itoa(config.WebSrvReadTimeout)) - log.Printf("[DEBUG] configuration value set: HTTP_WRITE_TIMEOUT = %s\n", strconv.Itoa(config.WebSrvWriteTimeout)) - log.Printf("[DEBUG] configuration value set: HTTP_IDLE_TIMEOUT = %s\n", strconv.Itoa(config.WebSrvIdleTimeout)) - log.Printf("[DEBUG] configuration value set: TZ = %s\n", tz) - - log.Println("[INFO] initialization complete") - */ + log.Println("[INFO] initialization complete") + return cfg }