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
53 changes: 33 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 Expand Up @@ -225,3 +224,17 @@ func (s *postgreSQLScaler) GetMetrics(ctx context.Context, metricName string, me

return append([]external_metrics.ExternalMetricValue{}, metric), nil
}

func getFromAuthOrMeta(config *ScalerConfig, field string) (string, error) {
zroubalik marked this conversation as resolved.
Show resolved Hide resolved
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
}
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")
}
}
}