diff --git a/adapter/main.go b/adapter/main.go index 8f14003ac97..b5391555a57 100644 --- a/adapter/main.go +++ b/adapter/main.go @@ -22,7 +22,6 @@ import ( "fmt" "os" "runtime" - "strconv" "sync" "time" @@ -50,6 +49,7 @@ import ( prommetrics "github.com/kedacore/keda/v2/pkg/metrics" kedaprovider "github.com/kedacore/keda/v2/pkg/provider" "github.com/kedacore/keda/v2/pkg/scaling" + kedautil "github.com/kedacore/keda/v2/pkg/util" "github.com/kedacore/keda/v2/version" ) @@ -200,26 +200,17 @@ func main() { ctrl.SetLogger(logger) - globalHTTPTimeoutStr := os.Getenv("KEDA_HTTP_DEFAULT_TIMEOUT") - if globalHTTPTimeoutStr == "" { - // default to 3 seconds if they don't pass the env var - globalHTTPTimeoutStr = "3000" - } - - globalHTTPTimeoutMS, err := strconv.Atoi(globalHTTPTimeoutStr) + // default to 3 seconds if they don't pass the env var + globalHTTPTimeoutMS, err := kedautil.ResolveOsEnvInt("KEDA_HTTP_DEFAULT_TIMEOUT", 3000) if err != nil { logger.Error(err, "Invalid KEDA_HTTP_DEFAULT_TIMEOUT") return } - controllerMaxReconciles := 1 - controllerMaxReconcilesStr := os.Getenv("KEDA_METRICS_CTRL_MAX_RECONCILES") - if controllerMaxReconcilesStr != "" { - controllerMaxReconciles, err = strconv.Atoi(controllerMaxReconcilesStr) - if err != nil { - logger.Error(err, "Invalid KEDA_METRICS_CTRL_MAX_RECONCILES") - return - } + controllerMaxReconciles, err := kedautil.ResolveOsEnvInt("KEDA_METRICS_CTRL_MAX_RECONCILES", 1) + if err != nil { + logger.Error(err, "Invalid KEDA_METRICS_CTRL_MAX_RECONCILES") + return } kedaProvider, stopCh, err := cmd.makeProvider(ctx, time.Duration(globalHTTPTimeoutMS)*time.Millisecond, controllerMaxReconciles) diff --git a/main.go b/main.go index 660e81722f5..2f8caeda0ae 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,6 @@ import ( "fmt" "os" "runtime" - "strconv" "time" apimachineryruntime "k8s.io/apimachinery/pkg/runtime" @@ -35,6 +34,7 @@ import ( kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" kedacontrollers "github.com/kedacore/keda/v2/controllers/keda" + kedautil "github.com/kedacore/keda/v2/pkg/util" "github.com/kedacore/keda/v2/version" //+kubebuilder:scaffold:imports ) @@ -97,36 +97,23 @@ func main() { os.Exit(1) } - globalHTTPTimeoutStr := os.Getenv("KEDA_HTTP_DEFAULT_TIMEOUT") - if globalHTTPTimeoutStr == "" { - // default to 3 seconds if they don't pass the env var - globalHTTPTimeoutStr = "3000" - } - - globalHTTPTimeoutMS, err := strconv.Atoi(globalHTTPTimeoutStr) + // default to 3 seconds if they don't pass the env var + globalHTTPTimeoutMS, err := kedautil.ResolveOsEnvInt("KEDA_HTTP_DEFAULT_TIMEOUT", 3000) if err != nil { setupLog.Error(err, "Invalid KEDA_HTTP_DEFAULT_TIMEOUT") - return + os.Exit(1) } - scaledObjectMaxReconciles := 10 - scaledObjectMaxReconcilesStr := os.Getenv("KEDA_SCALEDOBJECT_CTRL_MAX_RECONCILES") - if scaledObjectMaxReconcilesStr != "" { - scaledObjectMaxReconciles, err = strconv.Atoi(scaledObjectMaxReconcilesStr) - if err != nil { - setupLog.Error(err, "Invalid KEDA_SCALEDOBJECT_CTRL_MAX_RECONCILES") - return - } + scaledObjectMaxReconciles, err := kedautil.ResolveOsEnvInt("KEDA_SCALEDOBJECT_CTRL_MAX_RECONCILES", 5) + if err != nil { + setupLog.Error(err, "Invalid KEDA_SCALEDOBJECT_CTRL_MAX_RECONCILES") + os.Exit(1) } - scaledJobMaxReconciles := 1 - scaledJobMaxReconcilesStr := os.Getenv("KEDA_SCALEDJOB_CTRL_MAX_RECONCILES") - if scaledJobMaxReconcilesStr != "" { - scaledJobMaxReconciles, err = strconv.Atoi(scaledJobMaxReconcilesStr) - if err != nil { - setupLog.Error(err, "Invalid KEDA_SCALEDJOB_CTRL_MAX_RECONCILES") - return - } + scaledJobMaxReconciles, err := kedautil.ResolveOsEnvInt("KEDA_SCALEDJOB_CTRL_MAX_RECONCILES", 1) + if err != nil { + setupLog.Error(err, "Invalid KEDA_SCALEDJOB_CTRL_MAX_RECONCILES") + os.Exit(1) } globalHTTPTimeout := time.Duration(globalHTTPTimeoutMS) * time.Millisecond diff --git a/pkg/util/env_resolver.go b/pkg/util/env_resolver.go new file mode 100644 index 00000000000..761503465dd --- /dev/null +++ b/pkg/util/env_resolver.go @@ -0,0 +1,16 @@ +package util + +import ( + "os" + "strconv" +) + +func ResolveOsEnvInt(envName string, defaultValue int) (int, error) { + valueStr, found := os.LookupEnv(envName) + + if found { + return strconv.Atoi(valueStr) + } + + return defaultValue, nil +}