Skip to content

Commit

Permalink
fix(aws-cloudwatch): Not require uneeded fields (#4331)
Browse files Browse the repository at this point in the history
Co-authored-by: Zbynek Roubalik <zroubalik@gmail.com>
Co-authored-by: Tom Kerkhove <kerkhove.tom@gmail.com>
  • Loading branch information
3 people authored Mar 8, 2023
1 parent 7b7a96b commit c7f3328
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Here is an overview of all new **experimental** features:

- **General**: Fix regression in fallback mechanism ([#4249](https://github.com/kedacore/keda/issues/4249))
- **General**: Prevent a panic that might occur while refreshing a scaler cache ([#4092](https://github.com/kedacore/keda/issues/4092))
- **AWS Cloudwatch Scaler:** Make `metricName` and `namespace` optional when using `expression` ([#4334](https://github.com/kedacore/keda/issues/4262))
- **Azure Pipelines Scaler**: Add new parameter to limit the jobs returned ([#4324](https://github.com/kedacore/keda/issues/4324))
- **Azure Queue Scaler**: Fix azure queue length ([#4002](https://github.com/kedacore/keda/issues/4002))
- **Azure Service Bus Scaler:** Use correct auth flows with pod identity ([#4026](https://github.com/kedacore/keda/issues/4026)|[#4123](https://github.com/kedacore/keda/issues/4123))
Expand Down
37 changes: 13 additions & 24 deletions pkg/scalers/aws_cloudwatch_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/go-logr/logr"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/metrics/pkg/apis/external_metrics"

kedautil "github.com/kedacore/keda/v2/pkg/util"
)

const (
Expand Down Expand Up @@ -120,25 +118,25 @@ func parseAwsCloudwatchMetadata(config *ScalerConfig) (*awsCloudwatchMetadata, e
var err error
meta := awsCloudwatchMetadata{}

if val, ok := config.TriggerMetadata["namespace"]; ok && val != "" {
meta.namespace = val
} else {
return nil, fmt.Errorf("namespace not given")
}

if val, ok := config.TriggerMetadata["metricName"]; ok && val != "" {
meta.metricsName = val
} else {
return nil, fmt.Errorf("metric name not given")
}

if config.TriggerMetadata["expression"] != "" {
if val, ok := config.TriggerMetadata["expression"]; ok && val != "" {
meta.expression = val
} else {
return nil, fmt.Errorf("expression not given")
}
} else {
if val, ok := config.TriggerMetadata["namespace"]; ok && val != "" {
meta.namespace = val
} else {
return nil, fmt.Errorf("namespace not given")
}

if val, ok := config.TriggerMetadata["metricName"]; ok && val != "" {
meta.metricsName = val
} else {
return nil, fmt.Errorf("metric name not given")
}

if val, ok := config.TriggerMetadata["dimensionName"]; ok && val != "" {
meta.dimensionName = strings.Split(val, ";")
} else {
Expand Down Expand Up @@ -294,17 +292,9 @@ func (s *awsCloudwatchScaler) GetMetricsAndActivity(ctx context.Context, metricN
}

func (s *awsCloudwatchScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
var metricNameSuffix string

if s.metadata.expression != "" {
metricNameSuffix = s.metadata.metricsName
} else {
metricNameSuffix = s.metadata.dimensionName[0]
}

externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("aws-cloudwatch-%s", metricNameSuffix))),
Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, "aws-cloudwatch"),
},
Target: GetMetricTargetMili(s.metricType, s.metadata.targetMetricValue),
}
Expand All @@ -331,7 +321,6 @@ func (s *awsCloudwatchScaler) GetCloudwatchMetrics() (float64, error) {
Expression: aws.String(s.metadata.expression),
Id: aws.String("q1"),
Period: aws.Int64(s.metadata.metricStatPeriod),
Label: aws.String(s.metadata.metricsName),
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/scalers/aws_cloudwatch_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ var testAWSCloudwatchMetadata = []parseAWSCloudwatchMetadataTestData{
}

var awsCloudwatchMetricIdentifiers = []awsCloudwatchMetricIdentifier{
{&testAWSCloudwatchMetadata[1], 0, "s0-aws-cloudwatch-QueueName"},
{&testAWSCloudwatchMetadata[1], 3, "s3-aws-cloudwatch-QueueName"},
{&testAWSCloudwatchMetadata[2], 5, "s5-aws-cloudwatch-ApproximateNumberOfMessagesVisible"},
{&testAWSCloudwatchMetadata[1], 0, "s0-aws-cloudwatch"},
{&testAWSCloudwatchMetadata[1], 3, "s3-aws-cloudwatch"},
{&testAWSCloudwatchMetadata[2], 5, "s5-aws-cloudwatch"},
}

var awsCloudwatchGetMetricTestData = []awsCloudwatchMetadata{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type templateData struct {
AwsAccessKeyID string
AwsSecretAccessKey string
AwsRegion string
CloudWatchMetricName string
CloudWatchMetricNamespace string
CloudwatchMetricExpression string
}

Expand Down Expand Up @@ -112,9 +110,7 @@ spec:
name: keda-trigger-auth-aws-credentials
metadata:
awsRegion: {{.AwsRegion}}
namespace: {{.CloudWatchMetricNamespace}}
expression: {{.CloudwatchMetricExpression}}
metricName: {{.CloudWatchMetricName}}
targetMetricValue: "1"
activationTargetMetricValue: "5"
minMetricValue: "0"
Expand Down Expand Up @@ -228,8 +224,6 @@ func getTemplateData() (templateData, []Template) {
AwsAccessKeyID: base64.StdEncoding.EncodeToString([]byte(awsAccessKeyID)),
AwsSecretAccessKey: base64.StdEncoding.EncodeToString([]byte(awsSecretAccessKey)),
AwsRegion: awsRegion,
CloudWatchMetricName: cloudwatchMetricName,
CloudWatchMetricNamespace: cloudwatchMetricNamespace,
CloudwatchMetricExpression: cloudwatchMetricExpression,
}, []Template{
{Name: "secretTemplate", Config: secretTemplate},
Expand Down

0 comments on commit c7f3328

Please sign in to comment.