diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index cf9ff2b..9baef5f 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -6,7 +6,7 @@ import ( "os/signal" "syscall" - "mutating-webhook/internal/initialize" + "mutating-webhook/internal/config" ) func forever() { @@ -23,7 +23,7 @@ func main() { }() // initialize application configuration - cfg := initialize.Init() + cfg := config.Init() go httpServer(cfg) diff --git a/internal/config/config.go b/internal/config/config.go index bab5ef7..5b31b4d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,7 +3,6 @@ package config import ( "fmt" "log" - "mutating-webhook/internal/envconfig" "os" "reflect" "strconv" @@ -31,6 +30,7 @@ type Config struct { WebServerIdleTimeout int `env:"WEBSERVER_IDLE_TIMEOUT" default:"2"` } +// DefaultConfig initializes the config variable for use with a prepared set of defaults. func DefaultConfig() *Config { return &Config{ Log: &logutils.LevelFilter{ @@ -40,7 +40,7 @@ func DefaultConfig() *Config { } } -func (cfg *Config) Validate() error { +func (cfg *Config) validate() error { checks := []struct { bad bool errMsg string @@ -63,7 +63,7 @@ func (cfg *Config) Validate() error { return nil } -func (cfg *Config) SetLogLevel() { +func (cfg *Config) setLogLevel() { switch { case cfg.LogLevel <= 20: cfg.Log.SetMinLevel(logutils.LogLevel("ERROR")) @@ -79,7 +79,7 @@ func (cfg *Config) SetLogLevel() { log.SetOutput(cfg.Log) } -func (cfg *Config) PrintRunningConfig(cfgInfo []envconfig.StructInfo) { +func (cfg *Config) printRunningConfig(cfgInfo []StructInfo) { log.Printf("[DEBUG] Current Running Configuration Values:") for _, info := range cfgInfo { switch info.Type.String() { diff --git a/internal/envconfig/envconfig.go b/internal/config/envconfig.go similarity index 94% rename from internal/envconfig/envconfig.go rename to internal/config/envconfig.go index 4d1c4d2..4bf5bb4 100644 --- a/internal/envconfig/envconfig.go +++ b/internal/config/envconfig.go @@ -1,4 +1,4 @@ -package envconfig +package config import ( "fmt" @@ -7,12 +7,6 @@ import ( "strings" ) -/* -ignored:"true" -env:"ENVIRONMENT_VARIABLE" -default:"default value" -*/ - type StructInfo struct { Name string Alt string @@ -23,7 +17,7 @@ type StructInfo struct { DefaultValue interface{} } -func GetStructInfo(spec interface{}) ([]StructInfo, error) { +func getStructInfo(spec interface{}) ([]StructInfo, error) { s := reflect.ValueOf(spec) if s.Kind() != reflect.Pointer { diff --git a/internal/initialize/initialize.go b/internal/config/initialize.go similarity index 82% rename from internal/initialize/initialize.go rename to internal/config/initialize.go index 6b07b27..e4c4510 100644 --- a/internal/initialize/initialize.go +++ b/internal/config/initialize.go @@ -1,4 +1,4 @@ -package initialize +package config import ( "flag" @@ -7,9 +7,6 @@ import ( "reflect" "strconv" "time" - - "mutating-webhook/internal/config" - "mutating-webhook/internal/envconfig" ) // getEnvString returns string from environment variable @@ -59,10 +56,13 @@ func getEnvBool(env string, def bool) bool { return retVal } -func Init() *config.Config { - cfg := config.DefaultConfig() +// Init initializes the application configuration by reading default values from the struct's tags +// and environment variables. Tags processed by this process are as follows: +// `ignored:"true" env:"ENVIRONMENT_VARIABLE" default:"default value"` +func Init() *Config { + cfg := DefaultConfig() - cfgInfo, err := envconfig.GetStructInfo(cfg) + cfgInfo, err := getStructInfo(cfg) if err != nil { log.Fatalf("[FATAL] %v", err) } @@ -98,10 +98,11 @@ func Init() *config.Config { flag.Parse() // set logging level - cfg.SetLogLevel() + cfg.setLogLevel() - // - if err = cfg.Validate(); err != nil { + // validate some required values are defined. + // need to break this out to a required:"true" struct tag + if err = cfg.validate(); err != nil { log.Fatalf("[FATAL] %v", err) } @@ -117,7 +118,7 @@ func Init() *config.Config { time.Now().Format(cfg.TimeFormat) // print running config - cfg.PrintRunningConfig(cfgInfo) + cfg.printRunningConfig(cfgInfo) log.Println("[INFO] initialization complete") return cfg