From 99c3606956384a2b12b316fe90929e52442e9024 Mon Sep 17 00:00:00 2001 From: Matthias Linhuber Date: Fri, 22 Sep 2023 22:23:04 +0200 Subject: [PATCH] Add environment config options (#3) * Update from main * Add basic docker compose config * Implement env parser * Implement config load for hadesAPI * Implement config load for hadesScheduler * Add env variables * Add API port to env variables * Remove hardcoded queue value in API * Improve log messages --- .env | 3 +++ HadesAPI/go.mod | 2 ++ HadesAPI/go.sum | 4 ++++ HadesAPI/main.go | 33 +++++++++++++++++++++------------ HadesScheduler/main.go | 13 +++++++------ docker-compose.yml | 31 ++++++++++++++++++++++++++++++- shared/utils/config.go | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 .env create mode 100644 shared/utils/config.go diff --git a/.env b/.env new file mode 100644 index 0000000..e52448c --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +RABBITMQ_URL=localhost:5672 +RABBITMQ_DEFAULT_USER=admin +RABBITMQ_DEFAULT_PASS=admin \ No newline at end of file diff --git a/HadesAPI/go.mod b/HadesAPI/go.mod index 6d298d5..e5e55f3 100644 --- a/HadesAPI/go.mod +++ b/HadesAPI/go.mod @@ -12,6 +12,7 @@ replace github.com/Mtze/HadesCI/shared => ../shared require ( github.com/bytedance/sonic v1.9.1 // indirect + github.com/caarlos0/env/v9 v9.0.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect @@ -19,6 +20,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.14.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/leodido/go-urn v1.2.4 // indirect diff --git a/HadesAPI/go.sum b/HadesAPI/go.sum index edd5fda..d55dbd4 100644 --- a/HadesAPI/go.sum +++ b/HadesAPI/go.sum @@ -1,6 +1,8 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc= +github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= @@ -27,6 +29,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= diff --git a/HadesAPI/main.go b/HadesAPI/main.go index a0fce02..9fa1b2c 100644 --- a/HadesAPI/main.go +++ b/HadesAPI/main.go @@ -1,31 +1,40 @@ package main import ( - "os" + "fmt" "github.com/Mtze/HadesCI/shared/queue" + "github.com/Mtze/HadesCI/shared/utils" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" ) var BuildQueue *queue.Queue +type HadesAPIConfig struct { + APIPort uint `env:"API_PORT,notEmpty" envDefault:"8080"` + RabbitMQConfig utils.RabbitMQConfig +} + func main() { - if is_debug := os.Getenv("DEBUG"); is_debug == "true" { - log.SetLevel(log.DebugLevel) - log.Warn("DEBUG MODE ENABLED") - } - // var err error - // BuildQueue, err = queue.Init("builds", "amqp://admin:admin@localhost:5672/") - // if err != nil { - // log.Panic(err) - // } + var cfg HadesAPIConfig + utils.LoadConfig(&cfg) - log.Info("Starting HadesAPI") + var err error + rabbitmqURL := fmt.Sprintf("amqp://%s:%s@%s/", cfg.RabbitMQConfig.User, cfg.RabbitMQConfig.Password, cfg.RabbitMQConfig.Url) + log.Debug("Connecting to RabbitMQ: ", rabbitmqURL) + BuildQueue, err = queue.Init("builds", rabbitmqURL) + if err != nil { + log.Panic(err) + } + + log.Infof("Starting HadesAPI on port %d", cfg.APIPort) gin.SetMode(gin.ReleaseMode) + r := gin.Default() r.GET("/ping", ping) r.POST("/build", AddBuildToQueue) - log.Panic(r.Run(":8080")) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") + + log.Panic(r.Run(fmt.Sprintf(":%d", cfg.APIPort))) } diff --git a/HadesScheduler/main.go b/HadesScheduler/main.go index e3888c9..553f85c 100644 --- a/HadesScheduler/main.go +++ b/HadesScheduler/main.go @@ -9,6 +9,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "github.com/Mtze/HadesCI/shared/queue" + "github.com/Mtze/HadesCI/shared/utils" amqp "github.com/rabbitmq/amqp091-go" log "github.com/sirupsen/logrus" @@ -48,14 +49,14 @@ func initializeKubeconfig() *kubernetes.Clientset { } func main() { - - if is_debug := os.Getenv("DEBUG"); is_debug == "true" { - log.SetLevel(log.DebugLevel) - log.Warn("DEBUG MODE ENABLED") - } + var cfg utils.RabbitMQConfig + utils.LoadConfig(&cfg) var err error - BuildQueue, err = queue.Init("builds", "amqp://admin:admin@localhost:5672/") + rabbitmqURL := fmt.Sprintf("amqp://%s:%s@%s/", cfg.User, cfg.Password, cfg.Url) + log.Debug("Connecting to RabbitMQ: ", rabbitmqURL) + BuildQueue, err = queue.Init("builds", rabbitmqURL) + if err != nil { log.Panic(err) } diff --git a/docker-compose.yml b/docker-compose.yml index b3a5480..7e1a9d5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,37 @@ services: dockerfile: ./HadesAPI/Dockerfile ports: - "8080:8080" + networks: + - hades + depends_on: + - rabbitmq + environment: + - RABBITMQ_URL=rabbitmq:5672 + hadesScheduler: image: hades-scheduler build: context: . - dockerfile: ./HadesScheduler/Dockerfile \ No newline at end of file + dockerfile: ./HadesScheduler/Dockerfile + networks: + - hades + depends_on: + - rabbitmq + environment: + - RABBITMQ_URL=rabbitmq:5672 + + rabbitmq: + container_name: rabbitmq + image: rabbitmq:3-management + ports: + - "15672:15672" + - "5672:5672" + networks: + - hades + healthcheck: + test: ["CMD", "rabbitmq-diagnostics", "ping"] + interval: 30s + timeout: 10s + retries: 5 +networks: + hades: diff --git a/shared/utils/config.go b/shared/utils/config.go new file mode 100644 index 0000000..7b3e561 --- /dev/null +++ b/shared/utils/config.go @@ -0,0 +1,35 @@ +package utils + +import ( + "os" + + "github.com/caarlos0/env/v9" + "github.com/joho/godotenv" + log "github.com/sirupsen/logrus" +) + +type RabbitMQConfig struct { + Url string `env:"RABBITMQ_URL,notEmpty"` + User string `env:"RABBITMQ_DEFAULT_USER,notEmpty"` + Password string `env:"RABBITMQ_DEFAULT_PASS,notEmpty"` +} + +func LoadConfig(cfg interface{}) { + + if is_debug := os.Getenv("DEBUG"); is_debug == "true" { + log.SetLevel(log.DebugLevel) + log.Warn("DEBUG MODE ENABLED") + } + + err := godotenv.Load() + if err != nil { + log.WithError(err).Warn("Error loading .env file") + } + + err = env.Parse(cfg) + if err != nil { + log.WithError(err).Fatal("Error parsing environment variables") + } + + log.Debug("Config loaded: ", cfg) +}