Skip to content

Commit

Permalink
support extra parameters for mysql connection (#2668)
Browse files Browse the repository at this point in the history
* support extra parameters for mysql connection

* added missing const
  • Loading branch information
xaniasd authored and k8s-ci-robot committed Dec 24, 2019
1 parent 5c27fa8 commit d945bf3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
21 changes: 14 additions & 7 deletions backend/src/apiserver/client/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,25 @@ import (
)

func CreateMySQLConfig(user, password string, mysqlServiceHost string,
mysqlServicePort string, dbName string, mysqlGroupConcatMaxLen string) *mysql.Config {
mysqlServicePort string, dbName string, mysqlGroupConcatMaxLen string, mysqlExtraParams map[string]string) *mysql.Config {

params := map[string]string{
"charset": "utf8",
"parseTime": "True",
"loc": "Local",
"group_concat_max_len": mysqlGroupConcatMaxLen,
}

for k, v := range mysqlExtraParams {
params[k] = v
}

return &mysql.Config{
User: user,
Passwd: password,
Net: "tcp",
Addr: fmt.Sprintf("%s:%s", mysqlServiceHost, mysqlServicePort),
Params: map[string]string{
"charset": "utf8",
"parseTime": "True",
"loc": "Local",
"group_concat_max_len": mysqlGroupConcatMaxLen,
},
Params: params,
DBName: dbName,
AllowNativePasswords: true,
}
Expand Down
81 changes: 81 additions & 0 deletions backend/src/apiserver/client/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"reflect"
"testing"

"github.com/go-sql-driver/mysql"
)

func TestCreateMySQLConfig(t *testing.T) {
type args struct {
user string
password string
host string
port string
dbName string
mysqlGroupConcatMaxLen string
mysqlExtraParams map[string]string
}
tests := []struct {
name string
args args
want *mysql.Config
}{
{
name: "default config",
args: args{
user: "root",
host: "mysql",
port: "3306",
mysqlGroupConcatMaxLen: "1024",
mysqlExtraParams: nil,
},
want: &mysql.Config{
User: "root",
Net: "tcp",
Addr: "mysql:3306",
Params: map[string]string{"charset": "utf8", "parseTime": "True", "loc": "Local", "group_concat_max_len": "1024"},
AllowNativePasswords: true,
},
},
{
name: "extra parameters",
args: args{
user: "root",
host: "mysql",
port: "3306",
mysqlGroupConcatMaxLen: "1024",
mysqlExtraParams: map[string]string{"tls": "true"},
},
want: &mysql.Config{
User: "root",
Net: "tcp",
Addr: "mysql:3306",
Params: map[string]string{"charset": "utf8", "parseTime": "True", "loc": "Local", "group_concat_max_len": "1024", "tls": "true"},
AllowNativePasswords: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateMySQLConfig(tt.args.user, tt.args.password, tt.args.host, tt.args.port, tt.args.dbName, tt.args.mysqlGroupConcatMaxLen, tt.args.mysqlExtraParams); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateMySQLConfig() = %#v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions backend/src/apiserver/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
mysqlGroupConcatMaxLen = "DBConfig.GroupConcatMaxLen"
kfamServiceHost = "PROFILES_KFAM_SERVICE_HOST"
kfamServicePort = "PROFILES_KFAM_SERVICE_PORT"
mysqlExtraParams = "DBConfig.ExtraParams"

visualizationServiceHost = "ML_PIPELINE_VISUALIZATIONSERVER_SERVICE_HOST"
visualizationServicePort = "ML_PIPELINE_VISUALIZATIONSERVER_SERVICE_PORT"
Expand Down Expand Up @@ -263,6 +264,7 @@ func initMysql(driverName string, initConnectionTimeout time.Duration) string {
common.GetStringConfigWithDefault(mysqlServicePort, "3306"),
"",
common.GetStringConfigWithDefault(mysqlGroupConcatMaxLen, "1024"),
common.GetMapConfig(mysqlExtraParams),
)

var db *sql.DB
Expand Down
8 changes: 8 additions & 0 deletions backend/src/apiserver/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func GetStringConfigWithDefault(configName, value string) string {
return viper.GetString(configName)
}

func GetMapConfig(configName string) map[string]string {
if !viper.IsSet(configName) {
glog.Infof("Config %s not specified, skipping", configName)
return nil
}
return viper.GetStringMapString(configName)
}

func GetBoolConfigWithDefault(configName string, value bool) bool {
if !viper.IsSet(configName) {
return value
Expand Down

0 comments on commit d945bf3

Please sign in to comment.