From 42a1de49b3832bc8301aad9e98952f3f7679bacf Mon Sep 17 00:00:00 2001 From: tico88612 <17496418+tico88612@users.noreply.github.com> Date: Wed, 13 Mar 2024 06:55:27 +0800 Subject: [PATCH] MongoDB Scaler - Scheme field support (#5566) * feat: add schema field in mongodb scaler Signed-off-by: tico88612 <17496418+tico88612@users.noreply.github.com> * test: mongodb scaler support srv scheme Signed-off-by: tico88612 <17496418+tico88612@users.noreply.github.com> * docs: Changelog about MongoDB Scaler scheme field Signed-off-by: tico88612 <17496418+tico88612@users.noreply.github.com> --------- Signed-off-by: tico88612 <17496418+tico88612@users.noreply.github.com> Signed-off-by: Jorge Turrado Ferrero Co-authored-by: Jorge Turrado Ferrero --- CHANGELOG.md | 1 + pkg/scalers/mongo_scaler.go | 12 +++++++++++- pkg/scalers/mongo_scaler_test.go | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97ac7eb2ee7..40e117fcd06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ Here is an overview of all new **experimental** features: - **General**: Add command-line flag in Adapter to allow override of gRPC Authority Header ([#5449](https://github.com/kedacore/keda/issues/5449)) - **General**: Add OPENTELEMETRY flag in e2e test YAML ([#5375](https://github.com/kedacore/keda/issues/5375)) - **General**: Add support for cross tenant/cloud authentication when using Azure Workload Identity for TriggerAuthentication ([#5441](https://github.com/kedacore/keda/issues/5441)) +- **MongoDB Scaler**: Add scheme field support srv record ([#5544](https://github.com/kedacore/keda/issues/5544)) ### Fixes diff --git a/pkg/scalers/mongo_scaler.go b/pkg/scalers/mongo_scaler.go index 8e80b723baf..f7871c8567e 100644 --- a/pkg/scalers/mongo_scaler.go +++ b/pkg/scalers/mongo_scaler.go @@ -34,6 +34,9 @@ type mongoDBMetadata struct { // The string is used by connected with mongoDB. // +optional connectionString string + // Specify the prefix to connect to the mongoDB server, default value `mongodb`, if the connectionString be provided, don't need to specify this param. + // +optional + scheme string // Specify the host to connect to the mongoDB server,if the connectionString be provided, don't need to specify this param. // +optional host string @@ -162,6 +165,13 @@ func parseMongoDBMetadata(config *scalersconfig.ScalerConfig) (*mongoDBMetadata, meta.connectionString = config.ResolvedEnv[config.TriggerMetadata["connectionStringFromEnv"]] default: meta.connectionString = "" + scheme, err := GetFromAuthOrMeta(config, "scheme") + if err != nil { + meta.scheme = "mongodb" + } else { + meta.scheme = scheme + } + host, err := GetFromAuthOrMeta(config, "host") if err != nil { return nil, "", err @@ -196,7 +206,7 @@ func parseMongoDBMetadata(config *scalersconfig.ScalerConfig) (*mongoDBMetadata, // Build connection str addr := net.JoinHostPort(meta.host, meta.port) // nosemgrep: db-connection-string - connStr = fmt.Sprintf("mongodb://%s:%s@%s/%s", url.QueryEscape(meta.username), url.QueryEscape(meta.password), addr, meta.dbName) + connStr = fmt.Sprintf("%s://%s:%s@%s/%s", meta.scheme, url.QueryEscape(meta.username), url.QueryEscape(meta.password), addr, meta.dbName) } meta.triggerIndex = config.TriggerIndex return &meta, connStr, nil diff --git a/pkg/scalers/mongo_scaler_test.go b/pkg/scalers/mongo_scaler_test.go index 4ccb02b30f8..02f1e9479ef 100644 --- a/pkg/scalers/mongo_scaler_test.go +++ b/pkg/scalers/mongo_scaler_test.go @@ -70,6 +70,13 @@ var testMONGODBMetadata = []parseMongoDBMetadataTestData{ resolvedEnv: testMongoDBResolvedEnv, raisesError: false, }, + // mongodb srv support + { + metadata: map[string]string{"query": `{"name":"John"}`, "collection": "demo", "queryValue": "12"}, + authParams: map[string]string{"dbName": "test", "scheme": "mongodb+srv", "host": "localhost", "port": "1234", "username": "sample", "password": "sec@ure"}, + resolvedEnv: testMongoDBResolvedEnv, + raisesError: false, + }, // wrong activationQueryValue { metadata: map[string]string{"query": `{"name":"John"}`, "collection": "demo", "queryValue": "12", "activationQueryValue": "aa", "connectionStringFromEnv": "Mongo_CONN_STR", "dbName": "test"}, @@ -83,6 +90,7 @@ var mongoDBConnectionStringTestDatas = []mongoDBConnectionStringTestData{ {metadataTestData: &testMONGODBMetadata[2], connectionString: "mongodb://mongodb0.example.com:27017"}, {metadataTestData: &testMONGODBMetadata[3], connectionString: "mongodb://sample:test%40password@localhost:1234/test"}, {metadataTestData: &testMONGODBMetadata[4], connectionString: "mongodb://sample:sec%40ure@localhost:1234/test"}, + {metadataTestData: &testMONGODBMetadata[5], connectionString: "mongodb+srv://sample:sec%40ure@localhost:1234/test"}, } var mongoDBMetricIdentifiers = []mongoDBMetricIdentifier{