From 9b1654710a432d248d1ace5d3ac63967db3e61b0 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Ferrero Date: Mon, 4 Oct 2021 10:23:35 +0200 Subject: [PATCH] Add support to get connection data from Trigger Authorization in MySQL Scaler (#2113) Signed-off-by: jorturfer Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com> Signed-off-by: nilayasiktoprak --- CHANGELOG.md | 1 + pkg/scalers/mysql_scaler.go | 45 ++++++++++++++++++++------------ pkg/scalers/mysql_scaler_test.go | 13 ++++++++- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9e6320e45a..8542cb7056b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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)) diff --git a/pkg/scalers/mysql_scaler.go b/pkg/scalers/mysql_scaler.go index 0617341295b..350e761291b 100644 --- a/pkg/scalers/mysql_scaler.go +++ b/pkg/scalers/mysql_scaler.go @@ -78,26 +78,25 @@ func parseMySQLMetadata(config *ScalerConfig) (*mySQLMetadata, 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 } - 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 val, ok := config.TriggerMetadata["dbName"]; ok { - meta.dbName = val - } else { - return nil, fmt.Errorf("no dbName given") + + meta.dbName, err = getFromAuthOrMeta(config, "dbName") + if err != nil { + return nil, err } if config.AuthParams["password"] != "" { @@ -219,3 +218,17 @@ func (s *mySQLScaler) GetMetrics(ctx context.Context, metricName string, metricS return append([]external_metrics.ExternalMetricValue{}, metric), 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 +} diff --git a/pkg/scalers/mysql_scaler_test.go b/pkg/scalers/mysql_scaler_test.go index 8aa3ddddd1a..09f2d090c38 100644 --- a/pkg/scalers/mysql_scaler_test.go +++ b/pkg/scalers/mysql_scaler_test.go @@ -12,6 +12,7 @@ var testMySQLResolvedEnv = map[string]string{ type parseMySQLMetadataTestData struct { metadata map[string]string resolvedEnv map[string]string + authParams map[string]string raisesError bool } @@ -24,18 +25,28 @@ var testMySQLMetadata = []parseMySQLMetadataTestData{ // No metadata { metadata: map[string]string{}, + authParams: map[string]string{}, resolvedEnv: testMySQLResolvedEnv, raisesError: true, }, // connectionString { metadata: map[string]string{"query": "query", "queryValue": "12", "connectionStringFromEnv": "MYSQL_CONN_STR"}, + authParams: map[string]string{}, resolvedEnv: testMySQLResolvedEnv, raisesError: false, }, // Params instead of conn str { metadata: map[string]string{"query": "query", "queryValue": "12", "host": "test_host", "port": "test_port", "username": "test_username", "passwordFromEnv": "MYSQL_PASSWORD", "dbName": "test_dbname"}, + authParams: map[string]string{}, + resolvedEnv: testMySQLResolvedEnv, + raisesError: false, + }, + // Params from trigger authentication + { + metadata: map[string]string{"query": "query", "queryValue": "12"}, + authParams: map[string]string{"host": "test_host", "port": "test_port", "username": "test_username", "password": "MYSQL_PASSWORD", "dbName": "test_dbname"}, resolvedEnv: testMySQLResolvedEnv, raisesError: false, }, @@ -48,7 +59,7 @@ var mySQLMetricIdentifiers = []mySQLMetricIdentifier{ func TestParseMySQLMetadata(t *testing.T) { for _, testData := range testMySQLMetadata { - _, err := parseMySQLMetadata(&ScalerConfig{ResolvedEnv: testMySQLResolvedEnv, TriggerMetadata: testData.metadata, AuthParams: map[string]string{}}) + _, err := parseMySQLMetadata(&ScalerConfig{ResolvedEnv: testMySQLResolvedEnv, TriggerMetadata: testData.metadata, AuthParams: testData.authParams}) if err != nil && !testData.raisesError { t.Error("Expected success but got error", err) }