From eff894848946d3447a970ed3317c201399c521d5 Mon Sep 17 00:00:00 2001 From: Ahmed ElSayed Date: Sat, 9 Nov 2019 15:12:39 -0800 Subject: [PATCH] Read triggerAuthentication values for Rabbitmq (#385) * Read triggerAuthentication values for Rabbitmq Closes #341 * Address PR comment --- pkg/handler/scale_handler.go | 2 +- pkg/scalers/rabbitmq_scaler.go | 10 ++++++---- pkg/scalers/rabbitmq_scaler_test.go | 19 +++++++++++-------- spec/triggers/rabbit-mq-queue.md | 24 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/pkg/handler/scale_handler.go b/pkg/handler/scale_handler.go index 062935893c6..bfeb334654e 100644 --- a/pkg/handler/scale_handler.go +++ b/pkg/handler/scale_handler.go @@ -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": diff --git a/pkg/scalers/rabbitmq_scaler.go b/pkg/scalers/rabbitmq_scaler.go index 037ddb8b0f7..97cd220c5a1 100644 --- a/pkg/scalers/rabbitmq_scaler.go +++ b/pkg/scalers/rabbitmq_scaler.go @@ -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) } @@ -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 { diff --git a/pkg/scalers/rabbitmq_scaler_test.go b/pkg/scalers/rabbitmq_scaler_test.go index 7711d6e0b87..157f9c9cecb 100644 --- a/pkg/scalers/rabbitmq_scaler_test.go +++ b/pkg/scalers/rabbitmq_scaler_test.go @@ -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{ @@ -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) } diff --git a/spec/triggers/rabbit-mq-queue.md b/spec/triggers/rabbit-mq-queue.md index 07b31a39d77..56d9488035c 100644 --- a/spec/triggers/rabbit-mq-queue.md +++ b/spec/triggers/rabbit-mq-queue.md @@ -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)