Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to get connection data from Trigger Authorization in PostgreSQL Scaler #2114

Merged
merged 10 commits into from
Oct 4, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 4 additions & 18 deletions pkg/scalers/mongo_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
29 changes: 7 additions & 22 deletions pkg/scalers/mssql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"]
Expand Down
22 changes: 4 additions & 18 deletions pkg/scalers/mysql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
39 changes: 19 additions & 20 deletions pkg/scalers/postgresql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"] != "" {
Expand Down
55 changes: 55 additions & 0 deletions pkg/scalers/postgresql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
16 changes: 16 additions & 0 deletions pkg/scalers/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package scalers

import (
"context"
"fmt"
"time"

v2beta2 "k8s.io/api/autoscaling/v2beta2"
Expand Down Expand Up @@ -81,3 +82,18 @@ type ScalerConfig struct {
// PodIdentity
PodIdentity kedav1alpha1.PodIdentityProvider
}

// This method helps getting a field from Auth or Meta sections
zroubalik marked this conversation as resolved.
Show resolved Hide resolved
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
}