Skip to content

Commit

Permalink
util fn for ENV resolving
Browse files Browse the repository at this point in the history
Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>
  • Loading branch information
Zbynek Roubalik committed Nov 23, 2021
1 parent 30ffeb5 commit 316dfa6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
23 changes: 7 additions & 16 deletions adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"os"
"runtime"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
37 changes: 12 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"os"
"runtime"
"strconv"
"time"

apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
Expand All @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/env_resolver.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 316dfa6

Please sign in to comment.