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

MySQL Scaler: don't expose connection string in metricName #2171

Merged
merged 7 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -41,6 +41,7 @@
- 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))
- MySQL Scaler: don't expose connection string in `metricName` ([#2171](https://github.com/kedacore/keda/pull/2171))
- Add `pageSize` (using regex) in RabbitMQ Scaler ([#2162](https://github.com/kedacore/keda/pull/2162))
- Add `unsafeSsl` parameter in InfluxDB scaler ([#2157](https://github.com/kedacore/keda/pull/2157))
- Improve metric name creation to be unique using scaler index inside the scaler ([#2161](https://github.com/kedacore/keda/pull/2161))
Expand Down
28 changes: 21 additions & 7 deletions pkg/scalers/mysql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"strconv"
"strings"

"github.com/go-sql-driver/mysql"
"k8s.io/api/autoscaling/v2beta2"
Expand Down Expand Up @@ -32,6 +33,7 @@ type mySQLMetadata struct {
query string
queryValue int
scalerIndex int
metricName string
}

var mySQLLog = logf.Log.WithName("mysql_scaler")
Expand Down Expand Up @@ -111,6 +113,12 @@ func parseMySQLMetadata(config *ScalerConfig) (*mySQLMetadata, error) {
}
}
meta.scalerIndex = config.ScalerIndex
zroubalik marked this conversation as resolved.
Show resolved Hide resolved

if meta.connectionString != "" {
meta.dbName = parseMySQLDbNameFromConnectionStr(meta.connectionString)
}
meta.metricName = GenerateMetricNameWithIndex(meta.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("mysql-%s", meta.dbName)))

return &meta, nil
}

Expand Down Expand Up @@ -149,6 +157,17 @@ func newMySQLConnection(meta *mySQLMetadata) (*sql.DB, error) {
return db, nil
}

// parseMySQLDbNameFromConnectionStr returns dbname from connection string
// in it is not able to parse it, it returns "dbname" string
func parseMySQLDbNameFromConnectionStr(connectionString string) string {
splitted := strings.Split(connectionString, "/")

if size := len(splitted); size > 0 {
return splitted[size-1]
}
return "dbname"
}

// Close disposes of MySQL connections
func (s *mySQLScaler) Close() error {
err := s.connection.Close()
Expand Down Expand Up @@ -183,15 +202,10 @@ func (s *mySQLScaler) getQueryResult() (int, error) {
// GetMetricSpecForScaling returns the MetricSpec for the Horizontal Pod Autoscaler
func (s *mySQLScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetQueryValue := resource.NewQuantity(int64(s.metadata.queryValue), resource.DecimalSI)
metricName := "mysql"
if s.metadata.connectionString != "" {
metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", metricName, s.metadata.connectionString))
} else {
metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", metricName, s.metadata.dbName))
}

externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, metricName),
Name: s.metadata.metricName,
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
16 changes: 6 additions & 10 deletions pkg/scalers/mysql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var testMySQLResolvedEnv = map[string]string{
"MYSQL_PASSWORD": "pass",
"MYSQL_CONN_STR": "test_conn_str",
"MYSQL_CONN_STR": "user@tcp(http://my.mysql.dev:3306)/stats_db",
}

type parseMySQLMetadataTestData struct {
Expand All @@ -19,7 +19,7 @@ type parseMySQLMetadataTestData struct {
type mySQLMetricIdentifier struct {
metadataTestData *parseMySQLMetadataTestData
scalerIndex int
name string
metricName string
}

var testMySQLMetadata = []parseMySQLMetadataTestData{
Expand Down Expand Up @@ -54,8 +54,8 @@ var testMySQLMetadata = []parseMySQLMetadataTestData{
}

var mySQLMetricIdentifiers = []mySQLMetricIdentifier{
{metadataTestData: &testMySQLMetadata[1], scalerIndex: 0, name: "s0-mysql-test_conn_str"},
{metadataTestData: &testMySQLMetadata[2], scalerIndex: 1, name: "s1-mysql-test_dbname"},
{metadataTestData: &testMySQLMetadata[1], scalerIndex: 0, metricName: "s0-mysql-stats_db"},
{metadataTestData: &testMySQLMetadata[2], scalerIndex: 1, metricName: "s1-mysql-test_dbname"},
}

func TestParseMySQLMetadata(t *testing.T) {
Expand Down Expand Up @@ -97,12 +97,8 @@ func TestMySQLGetMetricSpecForScaling(t *testing.T) {
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockMySQLScaler := mySQLScaler{meta, nil}

metricSpec := mockMySQLScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
if meta.metricName != testData.metricName {
t.Error("Wrong External metric source name:", meta.metricName)
}
}
}