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 MSSQL Scaler #2112

Merged
merged 4 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 MSSQL Scaler ([#2112](https://github.com/kedacore/keda/pull/2112))

### Breaking Changes

Expand Down
33 changes: 23 additions & 10 deletions pkg/scalers/mssql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,41 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) {
meta.connectionString = config.ResolvedEnv[config.TriggerMetadata["connectionStringFromEnv"]]
default:
meta.connectionString = ""
if val, ok := config.TriggerMetadata["host"]; ok {
meta.host = val
} else {

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")
}

if val, ok := config.TriggerMetadata["port"]; ok {
port, err := strconv.Atoi(val)
var paramPort string
if config.AuthParams["port"] != "" {
paramPort = config.AuthParams["port"]
} else if config.TriggerMetadata["port"] != "" {
paramPort = config.TriggerMetadata["port"]
}
if paramPort != "" {
port, err := strconv.Atoi(paramPort)
if err != nil {
return nil, fmt.Errorf("port parsing error %s", err.Error())
}

meta.port = port
}

if val, ok := config.TriggerMetadata["username"]; ok {
meta.username = val
if config.AuthParams["username"] != "" {
meta.username = config.AuthParams["username"]
} else if config.TriggerMetadata["username"] != "" {
meta.username = config.TriggerMetadata["username"]
}

// database is optional in SQL s
if val, ok := config.TriggerMetadata["database"]; ok {
meta.database = val
if config.AuthParams["database"] != "" {
meta.database = config.AuthParams["database"]
} else if config.TriggerMetadata["database"] != "" {
meta.database = config.TriggerMetadata["database"]
}

if config.AuthParams["password"] != "" {
Expand Down
8 changes: 8 additions & 0 deletions pkg/scalers/mssql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ var testInputs = []mssqlTestData{
expectedMetricName: "mssql-AdventureWorks",
expectedConnectionString: "sqlserver://user2:Password%232@example.database.windows.net?database=AdventureWorks",
},
// connection string generated from full authParams
{
metadata: map[string]string{"query": "SELECT 1", "targetValue": "1"},
resolvedEnv: map[string]string{},
authParams: map[string]string{"password": "Password#2", "host": "example.database.windows.net", "username": "user2", "database": "AdventureWorks", "port": "1433"},
expectedMetricName: "mssql-AdventureWorks",
expectedConnectionString: "sqlserver://user2:Password%232@example.database.windows.net:1433?database=AdventureWorks",
},
// variation of previous: no database name, metricName from host
{
metadata: map[string]string{"query": "SELECT 1", "targetValue": "1", "host": "example.database.windows.net", "username": "user3"},
Expand Down