Skip to content

Commit

Permalink
Add option to get queueURL from env for aws SQS scaler (#3559)
Browse files Browse the repository at this point in the history
* Add option to get queueURL from env

Signed-off-by: Fahdi Kanavati <fkanavati@gmail.com>
Co-authored-by: Vighnesh Shenoy <vighneshq@gmail.com>
  • Loading branch information
fk128 and v-shenoy authored Oct 21, 2022
1 parent 74d84a6 commit 7eab93b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pkg/scalers/aws_sqs_queue_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func parseAwsSqsQueueMetadata(config *ScalerConfig, logger logr.Logger) (*awsSqs

if val, ok := config.TriggerMetadata["queueURL"]; ok && val != "" {
meta.queueURL = val
} else if val, ok := config.TriggerMetadata["queueURLFromEnv"]; ok && val != "" {
if val, ok := config.ResolvedEnv[val]; ok && val != "" {
meta.queueURL = val
} else {
return nil, fmt.Errorf("queueURLFromEnv `%s` env variable value is empty", config.TriggerMetadata["queueURLFromEnv"])
}
} else {
return nil, fmt.Errorf("no queueURL given")
}
Expand Down
64 changes: 58 additions & 6 deletions pkg/scalers/aws_sqs_queue_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ const (
testAWSSQSBadDataQueueURL = "https://sqs.eu-west-1.amazonaws.com/account_id/BadData"
)

var testAWSSQSEmptyResolvedEnv = map[string]string{}

var testAWSSQSResolvedEnv = map[string]string{
"QUEUE_URL": testAWSSQSProperQueueURL,
}

var testAWSSQSAuthentication = map[string]string{
"awsAccessKeyId": testAWSSQSAccessKeyID,
"awsSecretAccessKey": testAWSSQSSecretAccessKey,
}

type parseAWSSQSMetadataTestData struct {
metadata map[string]string
authParams map[string]string
isError bool
comment string
metadata map[string]string
authParams map[string]string
resolvedEnv map[string]string
isError bool
comment string
}

type awsSQSMetricIdentifier struct {
Expand Down Expand Up @@ -74,48 +81,55 @@ func (m *mockSqs) GetQueueAttributes(input *sqs.GetQueueAttributesInput) (*sqs.G
var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
{map[string]string{},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
true,
"metadata empty"},
{map[string]string{
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue and region"},
{map[string]string{
"queueURL": testAWSSQSImproperQueueURL1,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
true,
"improperly formed queue, missing queueName"},
{map[string]string{
"queueURL": testAWSSQSImproperQueueURL2,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
true,
"improperly formed queue, missing path"},
{map[string]string{
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": ""},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
true,
"properly formed queue, empty region"},
{map[string]string{
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue, integer queueLength"},
{map[string]string{
"queueURL": testAWSSQSProperQueueURL,
"queueLength": "a",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue, invalid queueLength"},
{map[string]string{
Expand All @@ -124,6 +138,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"activationQueueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue, integer activationQueueLength"},
{map[string]string{
Expand All @@ -132,6 +147,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"activationQueueLength": "a",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue, invalid activationQueueLength"},
{map[string]string{
Expand All @@ -142,6 +158,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsAccessKeyId": testAWSSQSAccessKeyID,
"awsSecretAccessKey": testAWSSQSSecretAccessKey,
},
testAWSSQSEmptyResolvedEnv,
false,
"with AWS static credentials from TriggerAuthentication"},
{map[string]string{
Expand All @@ -153,6 +170,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsSecretAccessKey": testAWSSQSSecretAccessKey,
"awsSessionToken": testAWSSQSSessionToken,
},
testAWSSQSEmptyResolvedEnv,
false,
"with AWS temporary credentials from TriggerAuthentication"},
{map[string]string{
Expand All @@ -163,6 +181,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsAccessKeyId": "",
"awsSecretAccessKey": testAWSSQSSecretAccessKey,
},
testAWSSQSEmptyResolvedEnv,
true,
"with AWS static credentials from TriggerAuthentication, missing Access Key Id"},
{map[string]string{
Expand All @@ -173,6 +192,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsAccessKeyId": testAWSSQSAccessKeyID,
"awsSecretAccessKey": "",
},
testAWSSQSEmptyResolvedEnv,
true,
"with AWS temporary credentials from TriggerAuthentication, missing Secret Access Key"},
{map[string]string{
Expand All @@ -184,6 +204,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsSecretAccessKey": testAWSSQSSecretAccessKey,
"awsSessionToken": testAWSSQSSessionToken,
},
testAWSSQSEmptyResolvedEnv,
true,
"with AWS temporary credentials from TriggerAuthentication, missing Access Key Id"},
{map[string]string{
Expand All @@ -195,6 +216,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsSecretAccessKey": "",
"awsSessionToken": testAWSSQSSessionToken,
},
testAWSSQSEmptyResolvedEnv,
true,
"with AWS static credentials from TriggerAuthentication, missing Secret Access Key"},
{map[string]string{
Expand All @@ -204,6 +226,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
map[string]string{
"awsRoleArn": testAWSSQSRoleArn,
},
testAWSSQSEmptyResolvedEnv,
false,
"with AWS Role from TriggerAuthentication"},
{map[string]string{
Expand All @@ -215,13 +238,15 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsAccessKeyId": "",
"awsSecretAccessKey": "",
},
testAWSSQSEmptyResolvedEnv,
false,
"with AWS Role assigned on KEDA operator itself"},
{map[string]string{
"queueURL": testAWSSimpleQueueURL,
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue and region"},
{map[string]string{
Expand All @@ -230,6 +255,7 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsRegion": "eu-west-1",
"scaleOnInFlight": "false"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue and region"},
{map[string]string{
Expand All @@ -238,8 +264,34 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"awsRegion": "eu-west-1",
"scaleOnInFlight": "true"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
false,
"properly formed queue and region"},
{map[string]string{
"queueURLFromEnv": "QUEUE_URL",
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSResolvedEnv,
false,
"properly formed queue loaded from env"},
{map[string]string{
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
testAWSSQSEmptyResolvedEnv,
true,
"missing queue url from both queueURL and queueURLFromEnv"},
{map[string]string{
"queueURLFromEnv": "QUEUE_URL",
"queueLength": "1",
"awsRegion": "eu-west-1"},
testAWSSQSAuthentication,
map[string]string{
"QUEUE_URL": "",
},
true,
"empty QUEUE_URL env value"},
}

var awsSQSMetricIdentifiers = []awsSQSMetricIdentifier{
Expand All @@ -255,7 +307,7 @@ var awsSQSGetMetricTestData = []*awsSqsQueueMetadata{

func TestSQSParseMetadata(t *testing.T) {
for _, testData := range testAWSSQSMetadata {
_, err := parseAwsSqsQueueMetadata(&ScalerConfig{TriggerMetadata: testData.metadata, ResolvedEnv: testAWSSQSAuthentication, AuthParams: testData.authParams}, logr.Discard())
_, err := parseAwsSqsQueueMetadata(&ScalerConfig{TriggerMetadata: testData.metadata, ResolvedEnv: testData.resolvedEnv, AuthParams: testData.authParams}, logr.Discard())
if err != nil && !testData.isError {
t.Errorf("Expected success because %s got error, %s", testData.comment, err)
}
Expand All @@ -268,7 +320,7 @@ func TestSQSParseMetadata(t *testing.T) {
func TestAWSSQSGetMetricSpecForScaling(t *testing.T) {
for _, testData := range awsSQSMetricIdentifiers {
ctx := context.Background()
meta, err := parseAwsSqsQueueMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testAWSSQSAuthentication, AuthParams: testData.metadataTestData.authParams, ScalerIndex: testData.scalerIndex}, logr.Discard())
meta, err := parseAwsSqsQueueMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams, ScalerIndex: testData.scalerIndex}, logr.Discard())
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down

0 comments on commit 7eab93b

Please sign in to comment.