Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover dropped commit utility function from pkg/scalers/scaler.go #5314

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ New deprecation(s):
- **General**: Create a common utility function to get parameter value from config ([#5037](https://github.com/kedacore/keda/issues/5037))
- **General**: Fix CVE-2023-45142 in Opentelemetry ([#5089](https://github.com/kedacore/keda/issues/5089))
- **General**: Fix logger in Opentelemetry collector ([#5094](https://github.com/kedacore/keda/issues/5094))
- **General**: Fix lost commit from the newly created utility function ([#5037](https://github.com/kedacore/keda/issues/5037))
- **General**: Reduce amount of gauge creations for OpenTelemetry metrics ([#5101](https://github.com/kedacore/keda/issues/5101))
- **General**: Removed not required RBAC permissions ([#5261](https://github.com/kedacore/keda/issues/5261))
- **General**: Support profiling for KEDA components ([#4789](https://github.com/kedacore/keda/issues/4789))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/segmentio/kafka-go v0.4.43
github.com/segmentio/kafka-go/sasl/aws_msk_iam_v2 v0.1.0
github.com/spf13/cast v1.3.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY52
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
Expand Down
134 changes: 102 additions & 32 deletions pkg/scalers/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"

"github.com/go-logr/logr"
metrics "github.com/rcrowley/go-metrics"
cast "github.com/spf13/cast"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -214,61 +214,131 @@ func GenerateMetricInMili(metricName string, value float64) external_metrics.Ext

// getParameterFromConfigV2 returns the value of the parameter from the config
func getParameterFromConfigV2(config *ScalerConfig, parameter string, useMetadata bool, useAuthentication bool, useResolvedEnv bool, isOptional bool, defaultVal string, targetType reflect.Type) (interface{}, error) {
if val, ok := config.AuthParams[parameter]; useAuthentication && ok && val != "" {
returnedVal, err := convertStringToType(val, targetType)
if err != nil {
return defaultVal, err
foundCount := 0
var foundVal string
var convertedVal interface{}
var foundErr error

if val, ok := config.AuthParams[parameter]; ok && val != "" {
foundCount++
if useAuthentication {
foundVal = val
}
return returnedVal, nil
} else if val, ok := config.TriggerMetadata[parameter]; ok && useMetadata && val != "" {
returnedVal, err := convertStringToType(val, targetType)
if err != nil {
return defaultVal, err
}
if val, ok := config.TriggerMetadata[parameter]; ok && val != "" {
foundCount++
if useMetadata {
foundVal = val
}
return returnedVal, nil
} else if val, ok := config.TriggerMetadata[fmt.Sprintf("%sFromEnv", parameter)]; ok && useResolvedEnv && val != "" {
returnedVal, err := convertStringToType(val, targetType)
if err != nil {
return defaultVal, err
}
if envFromVal, envFromOk := config.TriggerMetadata[fmt.Sprintf("%sFromEnv", parameter)]; envFromOk {
if val, ok := config.ResolvedEnv[envFromVal]; ok && val != "" {
foundCount++
if useResolvedEnv {
foundVal = val
}
}
return returnedVal, nil
}

if isOptional {
convertedVal, foundErr = convertToType(foundVal, targetType)
switch {
case foundCount > 1:
return "", fmt.Errorf("value for parameter '%s' found in more than one place", parameter)
case foundCount == 1:
if foundErr != nil {
return defaultVal, foundErr
}
return convertedVal, nil
case isOptional:
return defaultVal, nil
default:
return "", fmt.Errorf("key not found. Either set the correct key or set isOptional to true and set defaultVal")
}
return "", fmt.Errorf("key not found. Either set the correct key or set isOptional to true and set defaultVal")
}

func convertStringToType(input string, targetType reflect.Type) (interface{}, error) {
func convertToType(input interface{}, targetType reflect.Type) (interface{}, error) {
switch targetType.Kind() {
case reflect.String:
return input, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
result, err := strconv.ParseInt(input, 10, 64)
return fmt.Sprintf("%v", input), nil
case reflect.Int:
val, err := cast.ToIntE(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int8:
val, err := cast.ToInt8E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int16:
val, err := cast.ToInt16E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int32:
val, err := cast.ToInt32E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int64:
val, err := cast.ToInt64E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint:
val, err := cast.ToUintE(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint8:
val, err := cast.ToUint8E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint16:
val, err := cast.ToUint16E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint32:
val, err := cast.ToUint32E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint64:
val, err := cast.ToUint64E(input)
if err != nil {
return nil, err
}
return reflect.ValueOf(result).Convert(targetType).Interface(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
result, err := strconv.ParseUint(input, 10, 64)
return val, nil
case reflect.Float32:
val, err := cast.ToFloat32E(input)
if err != nil {
return nil, err
}
return reflect.ValueOf(result).Convert(targetType).Interface(), nil
case reflect.Float32, reflect.Float64:
result, err := strconv.ParseFloat(input, 64)
return val, nil
case reflect.Float64:
val, err := cast.ToFloat64E(input)
if err != nil {
return nil, err
}
return reflect.ValueOf(result).Convert(targetType).Interface(), nil
return val, nil
case reflect.Bool:
result, err := strconv.ParseBool(input)
val, err := cast.ToBoolE(input)
if err != nil {
return nil, err
}
return result, nil
return val, nil
default:
return nil, fmt.Errorf("unsupported type: %v", targetType)
return nil, fmt.Errorf("unsupported target type: %v", targetType)
}
}
Loading