Skip to content

Commit

Permalink
Add support to get connection data from Trigger Authorization in Mong…
Browse files Browse the repository at this point in the history
…oDB Scaler (#2115)

Signed-off-by: jorturfer <jorge_turrado@hotmail.es>

Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com>
  • Loading branch information
Jorge Turrado Ferrero and zroubalik authored Oct 4, 2021
1 parent 392d3ba commit 6148030
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Escape `queueName` and `vhostName` in RabbitMQ Scaler before use them in query string (bug fix) ([#2055](https://github.com/kedacore/keda/pull/2055))
- TriggerAuthentication/Vault: add support for HashiCorp Vault namespace (Vault Enterprise) ([#2085](https://github.com/kedacore/keda/pull/2085))
- Add custom http timeout in RabbitMQ Scaler ([#2086](https://github.com/kedacore/keda/pull/2086))
- Add support to get connection data from Trigger Authorization in MongoDB Scaler ([#2115](https://github.com/kedacore/keda/pull/2115))
- Add support to get connection data from Trigger Authorization in MySQL Scaler ([#2113](https://github.com/kedacore/keda/pull/2113))
- Add support to get connection data from Trigger Authorization in MSSQL Scaler ([#2112](https://github.com/kedacore/keda/pull/2112))
- Add support to provide the metric name in Azure Log Analytics Scaler ([#2106](https://github.com/kedacore/keda/pull/2106))
Expand Down
48 changes: 30 additions & 18 deletions pkg/scalers/mongo_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ func parseMongoDBMetadata(config *ScalerConfig) (*mongoDBMetadata, string, error
return nil, "", fmt.Errorf("no queryValue given")
}

if val, ok := config.TriggerMetadata["dbName"]; ok {
meta.dbName = val
} else {
return nil, "", fmt.Errorf("no dbName given")
}

// Resolve connectionString
switch {
case config.AuthParams["connectionString"] != "":
Expand All @@ -138,21 +132,25 @@ func parseMongoDBMetadata(config *ScalerConfig) (*mongoDBMetadata, string, error
meta.connectionString = config.ResolvedEnv[config.TriggerMetadata["connectionStringFromEnv"]]
default:
meta.connectionString = ""
if val, ok := config.TriggerMetadata["host"]; ok {
meta.host = val
} else {
return nil, "", fmt.Errorf("no host given")
var err error
meta.host, err = getFromAuthOrMeta(config, "host")
if err != nil {
return nil, "", err
}

meta.dbName, err = getFromAuthOrMeta(config, "dbName")
if err != nil {
return nil, "", err
}
if val, ok := config.TriggerMetadata["port"]; ok {
meta.port = val
} else {
return nil, "", fmt.Errorf("no port given")

meta.port, err = getFromAuthOrMeta(config, "port")
if err != nil {
return nil, "", err
}

if val, ok := config.TriggerMetadata["username"]; ok {
meta.username = val
} else {
return nil, "", fmt.Errorf("no username given")
meta.username, err = getFromAuthOrMeta(config, "username")
if err != nil {
return nil, "", err
}

if config.AuthParams["password"] != "" {
Expand Down Expand Up @@ -277,3 +275,17 @@ func json2BsonDoc(js string) (doc bsonx.Doc, err error) {

return doc, nil
}

func getFromAuthOrMeta(config *ScalerConfig, field string) (string, error) {
var result string
var err error
if config.AuthParams[field] != "" {
result = config.AuthParams[field]
} else if config.TriggerMetadata[field] != "" {
result = config.TriggerMetadata[field]
}
if result == "" {
err = fmt.Errorf("no %s given", field)
}
return result, err
}
15 changes: 13 additions & 2 deletions pkg/scalers/mongo_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var testMongoDBResolvedEnv = map[string]string{

type parseMongoDBMetadataTestData struct {
metadata map[string]string
authParams map[string]string
resolvedEnv map[string]string
raisesError bool
}
Expand All @@ -26,18 +27,28 @@ var testMONGODBMetadata = []parseMongoDBMetadataTestData{
// No metadata
{
metadata: map[string]string{},
authParams: map[string]string{},
resolvedEnv: testMongoDBResolvedEnv,
raisesError: true,
},
// connectionStringFromEnv
{
metadata: map[string]string{"query": `{"name":"John"}`, "collection": "demo", "queryValue": "12", "connectionStringFromEnv": "Mongo_CONN_STR", "dbName": "test"},
authParams: map[string]string{},
resolvedEnv: testMongoDBResolvedEnv,
raisesError: false,
},
// with metric name
{
metadata: map[string]string{"query": `{"name":"John"}`, "metricName": "hpa", "collection": "demo", "queryValue": "12", "connectionStringFromEnv": "Mongo_CONN_STR", "dbName": "test"},
authParams: map[string]string{},
resolvedEnv: testMongoDBResolvedEnv,
raisesError: false,
},
// from trigger auth
{
metadata: map[string]string{"query": `{"name":"John"}`, "metricName": "hpa", "collection": "demo", "queryValue": "12"},
authParams: map[string]string{"dbName": "test", "host": "localshot", "port": "1234", "username": "sample", "password": "secure"},
resolvedEnv: testMongoDBResolvedEnv,
raisesError: false,
},
Expand All @@ -49,7 +60,7 @@ var mongoDBMetricIdentifiers = []mongoDBMetricIdentifier{

func TestParseMongoDBMetadata(t *testing.T) {
for _, testData := range testMONGODBMetadata {
_, _, err := parseMongoDBMetadata(&ScalerConfig{TriggerMetadata: testData.metadata})
_, _, err := parseMongoDBMetadata(&ScalerConfig{TriggerMetadata: testData.metadata, AuthParams: testData.authParams})
if err != nil && !testData.raisesError {
t.Error("Expected success but got error:", err)
}
Expand All @@ -61,7 +72,7 @@ func TestParseMongoDBMetadata(t *testing.T) {

func TestMongoDBGetMetricSpecForScaling(t *testing.T) {
for _, testData := range mongoDBMetricIdentifiers {
meta, _, err := parseMongoDBMetadata(&ScalerConfig{ResolvedEnv: testData.metadataTestData.resolvedEnv, TriggerMetadata: testData.metadataTestData.metadata})
meta, _, err := parseMongoDBMetadata(&ScalerConfig{ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams, TriggerMetadata: testData.metadataTestData.metadata})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down

0 comments on commit 6148030

Please sign in to comment.