From 5b710443a8b419db10360e0a68ed6f232421aa90 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Ferrero Date: Mon, 4 Oct 2021 14:37:16 +0200 Subject: [PATCH] Add support to get connection data from Trigger Authorization in PostgreSQL Scaler (#2114) Signed-off-by: jorturfer Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com> --- CHANGELOG.md | 1 + pkg/scalers/mongo_scaler.go | 22 ++--------- pkg/scalers/mssql_scaler.go | 29 ++++---------- pkg/scalers/mysql_scaler.go | 22 ++--------- pkg/scalers/postgresql_scaler.go | 39 +++++++++---------- pkg/scalers/postgresql_scaler_test.go | 55 +++++++++++++++++++++++++++ pkg/scalers/scaler.go | 16 ++++++++ 7 files changed, 106 insertions(+), 78 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9f12c4a333..597e4c6399a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ - 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 get connection data from Trigger Authorization in PostgreSQL Scaler ([#2114](https://github.com/kedacore/keda/pull/2114)) - Add support to provide the metric name in Azure Log Analytics Scaler ([#2106](https://github.com/kedacore/keda/pull/2106)) ### Breaking Changes diff --git a/pkg/scalers/mongo_scaler.go b/pkg/scalers/mongo_scaler.go index b2ce5074e8c..232e6458c7d 100644 --- a/pkg/scalers/mongo_scaler.go +++ b/pkg/scalers/mongo_scaler.go @@ -133,22 +133,22 @@ func parseMongoDBMetadata(config *ScalerConfig) (*mongoDBMetadata, string, error default: meta.connectionString = "" var err error - meta.host, err = getFromAuthOrMeta(config, "host") + meta.host, err = GetFromAuthOrMeta(config, "host") if err != nil { return nil, "", err } - meta.dbName, err = getFromAuthOrMeta(config, "dbName") + meta.dbName, err = GetFromAuthOrMeta(config, "dbName") if err != nil { return nil, "", err } - meta.port, err = getFromAuthOrMeta(config, "port") + meta.port, err = GetFromAuthOrMeta(config, "port") if err != nil { return nil, "", err } - meta.username, err = getFromAuthOrMeta(config, "username") + meta.username, err = GetFromAuthOrMeta(config, "username") if err != nil { return nil, "", err } @@ -275,17 +275,3 @@ 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 -} diff --git a/pkg/scalers/mssql_scaler.go b/pkg/scalers/mssql_scaler.go index 981327b887e..20c6b66ac30 100644 --- a/pkg/scalers/mssql_scaler.go +++ b/pkg/scalers/mssql_scaler.go @@ -108,22 +108,15 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) { meta.connectionString = config.ResolvedEnv[config.TriggerMetadata["connectionStringFromEnv"]] default: meta.connectionString = "" + var err error - if config.AuthParams["host"] != "" { - meta.host = config.AuthParams["host"] - } else if config.TriggerMetadata["host"] != "" { - meta.host = config.TriggerMetadata["host"] - } - if meta.host == "" { - return nil, fmt.Errorf("no host given") + meta.host, err = GetFromAuthOrMeta(config, "host") + if err != nil { + return nil, err } var paramPort string - if config.AuthParams["port"] != "" { - paramPort = config.AuthParams["port"] - } else if config.TriggerMetadata["port"] != "" { - paramPort = config.TriggerMetadata["port"] - } + paramPort, _ = GetFromAuthOrMeta(config, "port") if paramPort != "" { port, err := strconv.Atoi(paramPort) if err != nil { @@ -132,18 +125,10 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) { meta.port = port } - if config.AuthParams["username"] != "" { - meta.username = config.AuthParams["username"] - } else if config.TriggerMetadata["username"] != "" { - meta.username = config.TriggerMetadata["username"] - } + meta.username, _ = GetFromAuthOrMeta(config, "username") // database is optional in SQL s - if config.AuthParams["database"] != "" { - meta.database = config.AuthParams["database"] - } else if config.TriggerMetadata["database"] != "" { - meta.database = config.TriggerMetadata["database"] - } + meta.database, _ = GetFromAuthOrMeta(config, "database") if config.AuthParams["password"] != "" { meta.password = config.AuthParams["password"] diff --git a/pkg/scalers/mysql_scaler.go b/pkg/scalers/mysql_scaler.go index 350e761291b..a7445925b48 100644 --- a/pkg/scalers/mysql_scaler.go +++ b/pkg/scalers/mysql_scaler.go @@ -79,22 +79,22 @@ func parseMySQLMetadata(config *ScalerConfig) (*mySQLMetadata, error) { default: meta.connectionString = "" var err error - meta.host, err = getFromAuthOrMeta(config, "host") + meta.host, err = GetFromAuthOrMeta(config, "host") if err != nil { return nil, err } - meta.port, err = getFromAuthOrMeta(config, "port") + meta.port, err = GetFromAuthOrMeta(config, "port") if err != nil { return nil, err } - meta.username, err = getFromAuthOrMeta(config, "username") + meta.username, err = GetFromAuthOrMeta(config, "username") if err != nil { return nil, err } - meta.dbName, err = getFromAuthOrMeta(config, "dbName") + meta.dbName, err = GetFromAuthOrMeta(config, "dbName") if err != nil { return nil, err } @@ -218,17 +218,3 @@ 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/postgresql_scaler.go b/pkg/scalers/postgresql_scaler.go index 998b44a3dcc..5dc20b6e247 100644 --- a/pkg/scalers/postgresql_scaler.go +++ b/pkg/scalers/postgresql_scaler.go @@ -81,31 +81,30 @@ func parsePostgreSQLMetadata(config *ScalerConfig) (*postgreSQLMetadata, error) meta.connection = config.ResolvedEnv[config.TriggerMetadata["connectionFromEnv"]] default: meta.connection = "" - 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.port, 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 val, ok := config.TriggerMetadata["sslmode"]; ok { - meta.sslmode = val - } else { - return nil, fmt.Errorf("no sslmode name given") + + meta.sslmode, err = GetFromAuthOrMeta(config, "sslmode") + if err != nil { + return nil, err } if config.AuthParams["password"] != "" { diff --git a/pkg/scalers/postgresql_scaler_test.go b/pkg/scalers/postgresql_scaler_test.go index 24be355880d..388c6858596 100644 --- a/pkg/scalers/postgresql_scaler_test.go +++ b/pkg/scalers/postgresql_scaler_test.go @@ -54,3 +54,58 @@ func TestPosgresSQLGetMetricSpecForScaling(t *testing.T) { } } } + +var testPostgresResolvedEnv = map[string]string{ + "POSTGRE_PASSWORD": "pass", + "POSTGRE_CONN_STR": "test_conn_str", +} + +type parsePostgresMetadataTestData struct { + metadata map[string]string + authParams map[string]string + resolvedEnv map[string]string + raisesError bool +} + +var testPostgresMetadata = []parsePostgresMetadataTestData{ + // No metadata + { + metadata: map[string]string{}, + authParams: map[string]string{}, + resolvedEnv: map[string]string{}, + raisesError: true, + }, + // connectionString + { + metadata: map[string]string{"query": "query", "targetQueryValue": "12", "connectionFromEnv": "POSTGRE_CONN_STR"}, + authParams: map[string]string{}, + resolvedEnv: testPostgresResolvedEnv, + raisesError: false, + }, + // Params instead of conn str + { + metadata: map[string]string{"query": "query", "targetQueryValue": "12", "host": "test_host", "port": "test_port", "userName": "test_username", "passwordFromEnv": "POSTGRE_PASSWORD", "dbName": "test_dbname", "sslmode": "require"}, + authParams: map[string]string{}, + resolvedEnv: testPostgresResolvedEnv, + raisesError: false, + }, + // Params from trigger authentication + { + metadata: map[string]string{"query": "query", "targetQueryValue": "12"}, + authParams: map[string]string{"host": "test_host", "port": "test_port", "userName": "test_username", "password": "POSTGRE_PASSWORD", "dbName": "test_dbname", "sslmode": "disable"}, + resolvedEnv: testPostgresResolvedEnv, + raisesError: false, + }, +} + +func TestParsePosgresSQLMetadata(t *testing.T) { + for _, testData := range testPostgresMetadata { + _, err := parsePostgreSQLMetadata(&ScalerConfig{ResolvedEnv: testData.resolvedEnv, TriggerMetadata: testData.metadata, AuthParams: testData.authParams}) + if err != nil && !testData.raisesError { + t.Error("Expected success but got error", err) + } + if err == nil && testData.raisesError { + t.Error("Expected error but got success") + } + } +} diff --git a/pkg/scalers/scaler.go b/pkg/scalers/scaler.go index ddaead64a99..1a7cc9e171f 100644 --- a/pkg/scalers/scaler.go +++ b/pkg/scalers/scaler.go @@ -18,6 +18,7 @@ package scalers import ( "context" + "fmt" "time" v2beta2 "k8s.io/api/autoscaling/v2beta2" @@ -81,3 +82,18 @@ type ScalerConfig struct { // PodIdentity PodIdentity kedav1alpha1.PodIdentityProvider } + +// GetFromAuthOrMeta helps getting a field from Auth or Meta sections +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 +}