Skip to content

Commit

Permalink
Add support to get connection data from Trigger Authorization in MySQ…
Browse files Browse the repository at this point in the history
…L Scaler (kedacore#2113)


Signed-off-by: jorturfer <jorge_turrado@hotmail.es>

Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com>
Signed-off-by: nilayasiktoprak <nilayasiktoprak@gmail.com>
  • Loading branch information
2 people authored and nilayasiktoprak committed Oct 23, 2021
1 parent 9caa338 commit 9b16547
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
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 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))

Expand Down
45 changes: 29 additions & 16 deletions pkg/scalers/mysql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"] != "" {
Expand Down Expand Up @@ -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
}
13 changes: 12 additions & 1 deletion pkg/scalers/mysql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
},
Expand All @@ -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)
}
Expand Down

0 comments on commit 9b16547

Please sign in to comment.