Skip to content

Commit

Permalink
Automatically determine the RabbitMQ protocol when possible, and supp…
Browse files Browse the repository at this point in the history
…ort setting the protocl via TriggerAuthentication.

Signed-off-by: Noah Kantrowitz <[email protected]>
  • Loading branch information
coderanger committed Dec 29, 2020
1 parent fbfa738 commit ea3597b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- Mask password in postgres scaler auto generated metricName. ([PR #1381](https://github.com/kedacore/keda/pull/1381))
- Bug fix for pending jobs in ScaledJob's accurateScalingStrategy . ([#1323](https://github.com/kedacore/keda/issues/1323))
- Fix memory leak because of unclosed scalers. ([#1413](https://github.com/kedacore/keda/issues/1413))
- Automatically determine the RabbitMQ protocol when possible, and support setting the protocl via TriggerAuthentication ([#1459](https://github.com/kedacore/keda/pulls/1459))

### Breaking Changes

Expand Down
31 changes: 25 additions & 6 deletions pkg/scalers/rabbitmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const (
const (
httpProtocol = "http"
amqpProtocol = "amqp"
defaultProtocol = amqpProtocol
autoProtocol = "auto"
defaultProtocol = autoProtocol
)

type rabbitMQScaler struct {
Expand Down Expand Up @@ -87,12 +88,14 @@ func parseRabbitMQMetadata(config *ScalerConfig) (*rabbitMQMetadata, error) {

// Resolve protocol type
meta.protocol = defaultProtocol
if val, ok := config.AuthParams["protocol"]; ok {
meta.protocol = val
}
if val, ok := config.TriggerMetadata["protocol"]; ok {
if val == amqpProtocol || val == httpProtocol {
meta.protocol = val
} else {
return nil, fmt.Errorf("the protocol has to be either `%s` or `%s` but is `%s`", amqpProtocol, httpProtocol, val)
}
meta.protocol = val
}
if meta.protocol != amqpProtocol && meta.protocol != httpProtocol && meta.protocol != autoProtocol {
return nil, fmt.Errorf("the protocol has to be either `%s`, `%s`, or `%s` but is `%s`", amqpProtocol, httpProtocol, autoProtocol, meta.protocol)
}

// Resolve host value
Expand All @@ -107,6 +110,22 @@ func parseRabbitMQMetadata(config *ScalerConfig) (*rabbitMQMetadata, error) {
return nil, fmt.Errorf("no host setting given")
}

// If the protocol is auto, check the host scheme.
if meta.protocol == autoProtocol {
parsedURL, err := url.Parse(meta.host)
if err != nil {
return nil, fmt.Errorf("can't parse host to find protocol: %s", err)
}
switch parsedURL.Scheme {
case "amqp", "amqps":
meta.protocol = amqpProtocol
case "http", "https":
meta.protocol = httpProtocol
default:
return nil, fmt.Errorf("unknown host URL scheme %s", parsedURL.Scheme)
}
}

// Resolve queueName
if val, ok := config.TriggerMetadata["queueName"]; ok {
meta.queueName = val
Expand Down
8 changes: 8 additions & 0 deletions pkg/scalers/rabbitmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ var testRabbitMQMetadata = []parseRabbitMQMetadataTestData{
{map[string]string{"queueLength": "10", "queueName": "sample", "host": host, "protocol": "http"}, false, map[string]string{}},
// queue name with slashes
{map[string]string{"queueLength": "10", "queueName": "namespace/name", "hostFromEnv": host}, false, map[string]string{}},
// protocol defined in authParams
{map[string]string{"queueName": "sample", "hostFromEnv": host}, false, map[string]string{"protocol": "http"}},
// auto protocol and a bad URL
{map[string]string{"queueName": "sample", "host": "something://"}, true, map[string]string{}},
// auto protocol and an HTTP URL
{map[string]string{"queueName": "sample", "host": "http://"}, false, map[string]string{}},
// auto protocol and an HTTPS URL
{map[string]string{"queueName": "sample", "host": "https://"}, false, map[string]string{}},
}

var rabbitMQMetricIdentifiers = []rabbitMQMetricIdentifier{
Expand Down

0 comments on commit ea3597b

Please sign in to comment.