Skip to content

Commit

Permalink
Read triggerAuthentication values for Rabbitmq (#385)
Browse files Browse the repository at this point in the history
* Read triggerAuthentication values for Rabbitmq

Closes #341

* Address PR comment
  • Loading branch information
ahmelsayed authored Nov 9, 2019
1 parent 6bd52e0 commit eff8948
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/handler/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (h *ScaleHandler) getScaler(name, namespace, triggerType string, resolvedEn
case "kafka":
return scalers.NewKafkaScaler(resolvedEnv, triggerMetadata)
case "rabbitmq":
return scalers.NewRabbitMQScaler(resolvedEnv, triggerMetadata)
return scalers.NewRabbitMQScaler(resolvedEnv, triggerMetadata, authParams)
case "azure-eventhub":
return scalers.NewAzureEventHubScaler(resolvedEnv, triggerMetadata)
case "prometheus":
Expand Down
10 changes: 6 additions & 4 deletions pkg/scalers/rabbitmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type rabbitMQMetadata struct {
var rabbitmqLog = logf.Log.WithName("rabbitmq_scaler")

// NewRabbitMQScaler creates a new rabbitMQ scaler
func NewRabbitMQScaler(resolvedEnv, metadata map[string]string) (Scaler, error) {
meta, err := parseRabbitMQMetadata(resolvedEnv, metadata)
func NewRabbitMQScaler(resolvedEnv, metadata, authParams map[string]string) (Scaler, error) {
meta, err := parseRabbitMQMetadata(resolvedEnv, metadata, authParams)
if err != nil {
return nil, fmt.Errorf("error parsing rabbitmq metadata: %s", err)
}
Expand All @@ -52,10 +52,12 @@ func NewRabbitMQScaler(resolvedEnv, metadata map[string]string) (Scaler, error)
}, nil
}

func parseRabbitMQMetadata(resolvedEnv, metadata map[string]string) (*rabbitMQMetadata, error) {
func parseRabbitMQMetadata(resolvedEnv, metadata, authParams map[string]string) (*rabbitMQMetadata, error) {
meta := rabbitMQMetadata{}

if val, ok := metadata["host"]; ok {
if val, ok := authParams["host"]; ok {
meta.host = val
} else if val, ok := metadata["host"]; ok {
hostSetting := val

if val, ok := resolvedEnv[hostSetting]; ok {
Expand Down
19 changes: 11 additions & 8 deletions pkg/scalers/rabbitmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const (
)

type parseRabbitMQMetadataTestData struct {
metadata map[string]string
isError bool
metadata map[string]string
isError bool
authParams map[string]string
}

var sampleRabbitMqResolvedEnv = map[string]string{
Expand All @@ -19,20 +20,22 @@ var sampleRabbitMqResolvedEnv = map[string]string{

var testRabbitMQMetadata = []parseRabbitMQMetadataTestData{
// nothing passed
{map[string]string{}, true},
{map[string]string{}, true, map[string]string{}},
// properly formed metadata
{map[string]string{"queueLength": "10", "queueName": "sample", "host": host}, false},
{map[string]string{"queueLength": "10", "queueName": "sample", "host": host}, false, map[string]string{}},
// malformed queueLength
{map[string]string{"queueLength": "AA", "queueName": "sample", "host": host}, true},
{map[string]string{"queueLength": "AA", "queueName": "sample", "host": host}, true, map[string]string{}},
// missing host
{map[string]string{"queueLength": "AA", "queueName": "sample"}, true},
{map[string]string{"queueLength": "AA", "queueName": "sample"}, true, map[string]string{}},
// missing queueName
{map[string]string{"queueLength": "10", "host": host}, true},
{map[string]string{"queueLength": "10", "host": host}, true, map[string]string{}},
// host defined in authParams
{map[string]string{"queueLength": "10"}, true, map[string]string{"host": host}},
}

func TestRabbitMQParseMetadata(t *testing.T) {
for _, testData := range testRabbitMQMetadata {
_, err := parseRabbitMQMetadata(sampleRabbitMqResolvedEnv, testData.metadata)
_, err := parseRabbitMQMetadata(sampleRabbitMqResolvedEnv, testData.metadata, testData.authParams)
if err != nil && !testData.isError {
t.Error("Expected success but got error", err)
}
Expand Down
24 changes: 24 additions & 0 deletions spec/triggers/rabbit-mq-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ This specification describes the `rabbitmq` trigger for Rabbit MQ Queue.
The `host` value is the name of the environment variable your deployment uses to get the connection string. This is usually resolved from a `Secret V1` or a `ConfigMap V1` collections. `env` and `envFrom` are both supported. The resolved host should follow a format like `amqp://guest:password@localhost:5672/`

You can also use `TriggerAuthentication` CRD with `rabbitmq`. The trigger will look like:

```yaml
triggers:
- type: rabbitmq
metadata:
queueName: testqueue
authenticationRef:
name: rabbitmq-auth
```
and a `TriggerAuthentication` object

```yaml
apiVersion: keda.k8s.io/v1alpha1
kind: TriggerAuthentication
metadata:
name: rabbitmq-auth
spec:
secretTargetRef:
- parameter: host
name: test-auth-secrets
key: connectionString
```

## Example

[`examples/rabbitmq_scaledobject.yaml`](./../../examples/rabbitmq_scaledobject.yaml)

0 comments on commit eff8948

Please sign in to comment.