new method of working with config

This commit is contained in:
2022-10-07 17:37:40 -05:00
parent 4db753e161
commit 9881d35bce
10 changed files with 367 additions and 273 deletions

View File

@ -1,44 +0,0 @@
package main
import (
"os"
"time"
"github.com/hashicorp/logutils"
)
type configStructure struct {
// time configuration
TimeFormat string
TimeZoneLocal *time.Location
TimeZoneUTC *time.Location
// logging
LogLevel int
Log *logutils.LevelFilter
// webserver
WebSrvPort int
WebSrvIP string
WebSrvReadTimeout int
WebSrvWriteTimeout int
WebSrvIdleTimeout int
}
type patchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
// Set Defaults
var config = configStructure{
TimeFormat: "2006-01-02 15:04:05",
Log: &logutils.LevelFilter{
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARNING", "ERROR"},
Writer: os.Stderr,
},
WebSrvReadTimeout: 5,
WebSrvWriteTimeout: 10,
WebSrvIdleTimeout: 2,
}

View File

@ -1,5 +1,5 @@
package main
/*
import (
"log"
"net/http"
@ -70,3 +70,4 @@ func webHealthCheck(w http.ResponseWriter, r *http.Request) {
tmpltError(w, http.StatusBadRequest, InvalidMethod)
}
}
*/

View File

@ -1,115 +0,0 @@
package main
import (
"flag"
"log"
"os"
"strconv"
"time"
"github.com/hashicorp/logutils"
)
// getEnvString returns string from environment variable
func getEnvString(env, def string) (val string) { //nolint:deadcode
val = os.Getenv(env)
if val == "" {
return def
}
return
}
// getEnvInt returns int from environment variable
func getEnvInt(env string, def int) (ret int) {
val := os.Getenv(env)
if val == "" {
return def
}
ret, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("[ERROR] Environment variable is not numeric: %v\n", env)
}
return
}
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 initialize() {
var (
tz string
err error
)
// 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()
// logging level
setLogLevel(config.LogLevel)
log.SetOutput(config.Log)
// 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")
}

View File

@ -5,6 +5,8 @@ import (
"os"
"os/signal"
"syscall"
"mutating-webhook/internal/initialize"
)
func forever() {
@ -20,9 +22,9 @@ func main() {
log.Println("[DEBUG] shutdown sequence complete")
}()
initialize()
initialize.Init()
go httpServer(config.WebSrvIP, config.WebSrvPort)
//go httpServer(config.WebSrvIP, config.WebSrvPort)
forever()
}

View File

@ -1,5 +1,5 @@
package main
/*
import (
"fmt"
"log"
@ -186,3 +186,4 @@ func mutationRequired(metadata *meta.ObjectMeta) bool {
log.Printf("[TRACE] Mutation policy for %v/%v: required:%v", metadata.Namespace, metadata.Name, required)
return required
}
*/