Skip to content

Commit

Permalink
Fix DCO signing
Browse files Browse the repository at this point in the history
Signed-off-by: dttung2905 <[email protected]>
  • Loading branch information
dttung2905 committed Jan 30, 2024
1 parent cd57dd9 commit 4f0c4af
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ New deprecation(s):

### Other

- **General**: Create a common utility function to get parameter value from config ([#5037](https://github.com/kedacore/keda/issues/5037))
- **General**: Minor refactor to reduce copy/paste code in ScaledObject webhook ([#5397](https://github.com/kedacore/keda/issues/5397))
- **General**: TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))

Expand Down Expand Up @@ -163,7 +164,6 @@ New deprecation(s):
### Other

- **General**: Bump K8s deps to 0.28.5 ([#5346](https://github.com/kedacore/keda/pull/5346))
- **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))
Expand All @@ -175,6 +175,7 @@ New deprecation(s):
- **CPU scaler**: Wait for metrics window during CPU scaler tests ([#5294](https://github.com/kedacore/keda/pull/5294))
- **Hashicorp Vault**: Improve test coverage in `pkg/scaling/resolver/hashicorpvault_handler` ([#5195](https://github.com/kedacore/keda/issues/5195))
- **Kafka Scaler**: Add more test cases for large value of LagThreshold ([#5354](https://github.com/kedacore/keda/issues/5354))
- **Kafka Scaler**: Improve readability of utility function getParameterFromConfigV2 ([#5037](https://github.com/kedacore/keda/issues/5037))
- **Openstack Scaler**: Use Gophercloud SDK ([#3439](https://github.com/kedacore/keda/issues/3439))

## v2.12.1
Expand Down
63 changes: 54 additions & 9 deletions pkg/scalers/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,74 @@ func GenerateMetricInMili(metricName string, value float64) external_metrics.Ext
}
}

type Option func(*configOptions)

type configOptions struct {
useMetadata bool
useAuthentication bool
useResolvedEnv bool
isOptional bool
defaultVal interface{}
}

func UseMetadata(metadata bool) Option {
return func(opt *configOptions) {
opt.useMetadata = metadata
}
}

func UseAuthentication(auth bool) Option {
return func(opt *configOptions) {
opt.useAuthentication = auth
}
}

func UseResolvedEnv(resolvedEnv bool) Option {
return func(opt *configOptions) {
opt.useResolvedEnv = resolvedEnv
}
}

func IsOptional(optional bool) Option {
return func(opt *configOptions) {
opt.isOptional = optional
}
}

func WithDefaultVal(defaultVal interface{}) Option {
return func(opt *configOptions) {
opt.defaultVal = defaultVal
}
}

// getParameterFromConfigV2 returns the value of the parameter from the config
func getParameterFromConfigV2(config *scalersconfig.ScalerConfig, parameter string, useMetadata bool, useAuthentication bool, useResolvedEnv bool, isOptional bool, defaultVal string, targetType reflect.Type) (interface{}, error) {
func getParameterFromConfigV2(config *scalersconfig.ScalerConfig, parameter string, targetType reflect.Type, options ...Option) (interface{}, error) {
opt := &configOptions{defaultVal: ""}
for _, option := range options {
option(opt)
}

foundCount := 0
var foundVal string
var convertedVal interface{}
var foundErr error

if val, ok := config.AuthParams[parameter]; ok && val != "" {
foundCount++
if useAuthentication {
if opt.useAuthentication {
foundVal = val
}
}
if val, ok := config.TriggerMetadata[parameter]; ok && val != "" {
foundCount++
if useMetadata {
if opt.useMetadata {
foundVal = val
}
}
if envFromVal, envFromOk := config.TriggerMetadata[fmt.Sprintf("%sFromEnv", parameter)]; envFromOk {
if val, ok := config.ResolvedEnv[envFromVal]; ok && val != "" {
foundCount++
if useResolvedEnv {
if opt.useResolvedEnv {
foundVal = val
}
}
Expand All @@ -199,16 +244,16 @@ func getParameterFromConfigV2(config *scalersconfig.ScalerConfig, parameter stri
convertedVal, foundErr = convertToType(foundVal, targetType)
switch {
case foundCount > 1:
return "", fmt.Errorf("value for parameter '%s' found in more than one place", parameter)
return opt.defaultVal, fmt.Errorf("value for parameter '%s' found in more than one place", parameter)
case foundCount == 1:
if foundErr != nil {
return defaultVal, foundErr
return opt.defaultVal, foundErr
}
return convertedVal, nil
case isOptional:
return defaultVal, nil
case opt.isOptional:
return opt.defaultVal, nil
default:
return "", fmt.Errorf("key not found. Either set the correct key or set isOptional to true and set defaultVal")
return opt.defaultVal, fmt.Errorf("key not found. Either set the correct key or set isOptional to true and set defaultVal")
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/scalers/scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ func TestGetParameterFromConfigV2(t *testing.T) {
val, err := getParameterFromConfigV2(
&scalersconfig.ScalerConfig{TriggerMetadata: testData.metadata, AuthParams: testData.authParams, ResolvedEnv: testData.resolvedEnv},
testData.parameter,
testData.useMetadata,
testData.useAuthentication,
testData.useResolvedEnv,
testData.isOptional,
testData.defaultVal,
testData.targetType,
UseMetadata(testData.useMetadata),
UseAuthentication(testData.useAuthentication),
UseResolvedEnv(testData.useResolvedEnv),
IsOptional(testData.isOptional),
WithDefaultVal(testData.defaultVal),
)
if testData.isError {
assert.NotNilf(t, err, "test %s: expected error but got success, testData - %+v", testData.name, testData)
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestConvertStringToType(t *testing.T) {

if testData.isError {
assert.NotNilf(t, err, "test %s: expected error but got success, testData - %+v", testData.name, testData)
assert.Containsf(t, err.Error(), testData.errorMessage, "test %s", testData.name, testData.errorMessage)
assert.Containsf(t, err.Error(), testData.errorMessage, "test %s: %s", testData.name, testData.errorMessage)
} else {
assert.Nil(t, err)
assert.Equalf(t, testData.expectedOutput, val, "test %s: expected %s but got %s", testData.name, testData.expectedOutput, val)
Expand Down

0 comments on commit 4f0c4af

Please sign in to comment.