diff --git a/pkg/scalers/aws_sqs_queue_scaler.go b/pkg/scalers/aws_sqs_queue_scaler.go index 2f4a647ca1f..9834c8e6266 100644 --- a/pkg/scalers/aws_sqs_queue_scaler.go +++ b/pkg/scalers/aws_sqs_queue_scaler.go @@ -3,7 +3,9 @@ package scalers import ( "context" "fmt" + "net/url" "strconv" + "strings" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" @@ -31,6 +33,7 @@ type awsSqsQueueScaler struct { type awsSqsQueueMetadata struct { targetQueueLength int queueURL string + queueName string awsRegion string awsAuthorization awsAuthorizationMetadata } @@ -69,6 +72,19 @@ func parseAwsSqsQueueMetadata(metadata, resolvedEnv, authParams map[string]strin return nil, fmt.Errorf("no queueURL given") } + queueURL, err := url.ParseRequestURI(meta.queueURL) + if err != nil { + return nil, fmt.Errorf("queueURL is not a valid URL") + } + + queueURLPath := queueURL.Path + queueURLPathParts := strings.Split(queueURLPath, "/") + if len(queueURLPathParts) != 3 || len(queueURLPathParts[2]) <= 0 { + return nil, fmt.Errorf("cannot get queueName from queueURL") + } + + meta.queueName = queueURLPathParts[2] + if val, ok := metadata["awsRegion"]; ok && val != "" { meta.awsRegion = val } else { @@ -102,7 +118,8 @@ func (s *awsSqsQueueScaler) Close() error { func (s *awsSqsQueueScaler) GetMetricSpecForScaling() []v2beta1.MetricSpec { targetQueueLengthQty := resource.NewQuantity(int64(s.metadata.targetQueueLength), resource.DecimalSI) - externalMetric := &v2beta1.ExternalMetricSource{MetricName: awsSqsQueueMetricName, TargetAverageValue: targetQueueLengthQty} + externalMetric := &v2beta1.ExternalMetricSource{MetricName: fmt.Sprintf("%s-%s-%s", "AWS-SQS-Queue", awsSqsQueueMetricName, s.metadata.queueName), + TargetAverageValue: targetQueueLengthQty} metricSpec := v2beta1.MetricSpec{External: externalMetric, Type: externalMetricType} return []v2beta1.MetricSpec{metricSpec} } diff --git a/pkg/scalers/aws_sqs_queue_test.go b/pkg/scalers/aws_sqs_queue_test.go index 1656ebeebcf..7c143a5bd61 100644 --- a/pkg/scalers/aws_sqs_queue_test.go +++ b/pkg/scalers/aws_sqs_queue_test.go @@ -4,10 +4,15 @@ import ( "testing" ) -var testAWSSQSRoleArn = "none" +const ( + testAWSSQSRoleArn = "none" + testAWSSQSAccessKeyID = "none" + testAWSSQSSecretAccessKey = "none" -var testAWSSQSAccessKeyID = "none" -var testAWSSQSSecretAccessKey = "none" + testAWSSQSProperQueueURL = "https://sqs.eu-west-1.amazonaws.com/account_id/DeleteArtifactQ" + testAWSSQSImproperQueueURL1 = "https://sqs.eu-west-1.amazonaws.com/account_id" + testAWSSQSImproperQueueURL2 = "https://sqs.eu-west-1.amazonaws.com" +) var testAWSSQSResolvedEnv = map[string]string{ "AWS_ACCESS_KEY": "none", @@ -32,35 +37,49 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{ true, "metadata empty"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSProperQueueURL, "queueLength": "1", "awsRegion": "eu-west-1"}, testAWSSQSAuthentication, false, "properly formed queue and region"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSImproperQueueURL1, + "queueLength": "1", + "awsRegion": "eu-west-1"}, + testAWSSQSAuthentication, + true, + "improperly formed queue, missing queueName"}, + {map[string]string{ + "queueURL": testAWSSQSImproperQueueURL2, + "queueLength": "1", + "awsRegion": "eu-west-1"}, + testAWSSQSAuthentication, + true, + "improperly formed queue, missing path"}, + {map[string]string{ + "queueURL": testAWSSQSProperQueueURL, "queueLength": "1", "awsRegion": ""}, testAWSSQSAuthentication, true, "properly formed queue, empty region"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSProperQueueURL, "queueLength": "1", "awsRegion": "eu-west-1"}, testAWSSQSAuthentication, false, "properly formed queue, integer queueLength"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSProperQueueURL, "queueLength": "a", "awsRegion": "eu-west-1"}, testAWSSQSAuthentication, false, "properly formed queue, invalid queueLength"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSProperQueueURL, "queueLength": "1", "awsRegion": "eu-west-1"}, map[string]string{ @@ -70,7 +89,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{ false, "with AWS Credentials from TriggerAuthentication"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSProperQueueURL, "queueLength": "1", "awsRegion": "eu-west-1"}, map[string]string{ @@ -80,7 +99,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{ true, "with AWS Credentials from TriggerAuthentication, missing Access Key Id"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSProperQueueURL, "queueLength": "1", "awsRegion": "eu-west-1"}, map[string]string{ @@ -90,7 +109,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{ true, "with AWS Credentials from TriggerAuthentication, missing Secret Access Key"}, {map[string]string{ - "queueURL": "myqueue", + "queueURL": testAWSSQSProperQueueURL, "queueLength": "1", "awsRegion": "eu-west-1"}, map[string]string{