Skip to content

Commit

Permalink
feat(config): default configuration and optional config via env
Browse files Browse the repository at this point in the history
closes #100

Signed-off-by: Robin Opletal <[email protected]>
  • Loading branch information
fourstepper committed Jun 5, 2024
1 parent 670ee5e commit 2874253
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
12 changes: 3 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"flag"
monitoringcoreoscom "github.com/oskoperator/osko/internal/controller/monitoring.coreos.com"
"os"
"time"

"github.com/oskoperator/osko/internal/config"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand Down Expand Up @@ -54,14 +54,8 @@ func main() {
opts := zap.Options{}
opts.BindFlags(flag.CommandLine)

var mimirRuleRequeuePeriod time.Duration
mimirRuleRequeuePeriodDefault, err := time.ParseDuration("1m")
if err != nil {
setupLog.Error(err, "can't parse default \"mimirRuleRequeuePeriodDefault\"")
os.Exit(1)
}
flag.DurationVar(&mimirRuleRequeuePeriod, "mimirrule-requeue-period", mimirRuleRequeuePeriodDefault, "The requeue period for MimirRule object reconcilation.")
flag.Parse()
cfg := config.NewConfig()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

Expand Down Expand Up @@ -145,7 +139,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("mimirrule-controller"),
RequeueAfterPeriod: mimirRuleRequeuePeriod,
RequeueAfterPeriod: cfg.MimirRuleRequeuePeriod,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MimirRule")
os.Exit(1)
Expand Down
Empty file added internal/config/config.go
Empty file.
31 changes: 31 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

import (
"github.com/oskoperator/osko/internal/utils"
"time"
)

type Config struct {
MimirRuleRequeuePeriod time.Duration
AlertingBurnRates AlertingBurnRates
}

type AlertingBurnRates struct {
PageShortWindow float64
PageLongWindow float64
TicketShortWindow float64
TicketLongWindow float64
}

func NewConfig() Config {
config := Config{
MimirRuleRequeuePeriod: utils.GetEnvAsDuration("MIMIR_RULE_REQUEUE_PERIOD", 60*time.Second),
AlertingBurnRates: AlertingBurnRates{
PageShortWindow: utils.GetEnvAsFloat64("ABR_PAGE_SHORT_WINDOW", 14.4),
PageLongWindow: utils.GetEnvAsFloat64("ABR_PAGE_LONG_WINDOW", 6),
TicketShortWindow: utils.GetEnvAsFloat64("ABR_TICKET_SHORT_WINDOW", 3),
TicketLongWindow: utils.GetEnvAsFloat64("ABR_TICKET_LONG_WINDOW", 1),
},
}
return config
}
45 changes: 45 additions & 0 deletions internal/utils/env_parse_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package utils

import (
"os"
"strconv"
"time"
)

// Helper function to read an environment variable or return a default value
func GetEnv(key, defaultValue string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultValue
}

// Helper function to read an environment variable as an integer or return a default value
func GetEnvAsInt(key string, defaultValue int) int {
if valueStr, exists := os.LookupEnv(key); exists {
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
}
return defaultValue
}

// Helper function to read an environment variable as a float64 or return a default value
func GetEnvAsFloat64(key string, defaultValue float64) float64 {
if valueStr, exists := os.LookupEnv(key); exists {
if value, err := strconv.ParseFloat(valueStr, 64); err == nil {
return value
}
}
return defaultValue
}

// Helper function to read an environment variable as a time.Duration or return a default value
func GetEnvAsDuration(key string, defaultValue time.Duration) time.Duration {
if valueStr, exists := os.LookupEnv(key); exists {
if value, err := time.ParseDuration(valueStr); err == nil {
return value
}
}
return defaultValue
}

0 comments on commit 2874253

Please sign in to comment.