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 username from env mysql scaler #5952

Merged
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 @@ -77,6 +77,7 @@ Here is an overview of all new **experimental** features:
- **GCP Scalers**: Added custom time horizon in GCP scalers ([#5778](https://github.com/kedacore/keda/issues/5778))
- **GitHub Scaler**: Fixed pagination, fetching repository list ([#5738](https://github.com/kedacore/keda/issues/5738))
- **Kafka**: Fix logic to scale to zero on invalid offset even with earliest offsetResetPolicy ([#5689](https://github.com/kedacore/keda/issues/5689))
- **MYSQL Scaler**: Add support to fetch username from env ([#5883](https://github.com/kedacore/keda/issues/5883))

### Fixes

Expand Down
2 changes: 1 addition & 1 deletion pkg/scalers/mysql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func parseMySQLMetadata(config *scalersconfig.ScalerConfig) (*mySQLMetadata, err
}
meta.port = port

username, err := GetFromAuthOrMeta(config, "username")
username, err := GetFromAuthOrMetaOrEnv(config, "username", "usernameFromEnv")
if err != nil {
return nil, err
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/scalers/mysql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

var testMySQLResolvedEnv = map[string]string{
"MYSQL_USERNAME": "test_username",
"MYSQL_PASSWORD": "pass",
"MYSQL_CONN_STR": "user@tcp(http://my.mysql.dev:3306)/stats_db",
}
Expand Down Expand Up @@ -46,6 +47,13 @@ var testMySQLMetadata = []parseMySQLMetadataTestData{
resolvedEnv: testMySQLResolvedEnv,
raisesError: false,
},
// Params instead of conn str with userFromEnv
{
metadata: map[string]string{"query": "query", "queryValue": "12", "host": "test_host", "port": "test_port", "usernameFromEnv": "MYSQL_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"},
Expand All @@ -60,6 +68,13 @@ var testMySQLMetadata = []parseMySQLMetadataTestData{
resolvedEnv: testMySQLResolvedEnv,
raisesError: true,
},
// No username provided in authParams, metadata, resolvedEnv
{
metadata: map[string]string{"query": "query", "queryValue": "12", "activationQueryValue": "AA"},
authParams: map[string]string{"host": "test_host", "port": "test_port", "password": "MYSQL_PASSWORD", "dbName": "test_dbname"},
resolvedEnv: map[string]string{},
raisesError: true,
},
}

var mySQLMetricIdentifiers = []mySQLMetricIdentifier{
Expand Down
18 changes: 18 additions & 0 deletions pkg/scalers/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@
return result, err
}

// GetFromAuthOrMetaOrEnv helps to get a field from Auth or Meta or Env sections
func GetFromAuthOrMetaOrEnv(config *scalersconfig.ScalerConfig, field string, envField string) (string, error) {
var result string
var err error
switch {
case config.AuthParams[field] != "":
return config.AuthParams[field], err
case config.TriggerMetadata[field] != "":
return config.TriggerMetadata[field], err
case config.ResolvedEnv[config.TriggerMetadata[envField]] != "":
return config.TriggerMetadata[envField], err
}
if result == "" {
Fixed Show fixed Hide fixed
err = fmt.Errorf("%w: no %s or %s given", ErrScalerConfigMissingField, field, envField)
}
return result, err
}
Indresh2410 marked this conversation as resolved.
Show resolved Hide resolved

// GenerateMetricNameWithIndex helps to add the index prefix to the metric name
func GenerateMetricNameWithIndex(triggerIndex int, metricName string) string {
return fmt.Sprintf("s%d-%s", triggerIndex, metricName)
Expand Down
Loading