From 6f9f226f2b61afe53f7d7ec125e5d492c4e7c013 Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Thu, 10 Oct 2019 17:58:58 +0800 Subject: [PATCH 1/2] feat: Support env in mysql Signed-off-by: Ce Gao --- pkg/db/v1alpha3/common/const.go | 6 ++++++ pkg/db/v1alpha3/common/helper.go | 11 +++++++++++ pkg/db/v1alpha3/common/helper_test.go | 21 +++++++++++++++++++++ pkg/db/v1alpha3/mysql/mysql.go | 8 ++++++-- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 pkg/db/v1alpha3/common/helper.go create mode 100644 pkg/db/v1alpha3/common/helper_test.go diff --git a/pkg/db/v1alpha3/common/const.go b/pkg/db/v1alpha3/common/const.go index 818d8bab284..2defdaff849 100644 --- a/pkg/db/v1alpha3/common/const.go +++ b/pkg/db/v1alpha3/common/const.go @@ -6,4 +6,10 @@ const ( MySqlDBNameEnvValue = "mysql" DBPasswordEnvName = "DB_PASSWORD" + + MySQLDBHostEnvName = "MYSQL_HOST" + MySQLDBPortEnvName = "MYSQL_PORT" + + DefaultMySQLHost = "katib-db" + DefaultMySQLPort = "3306" ) diff --git a/pkg/db/v1alpha3/common/helper.go b/pkg/db/v1alpha3/common/helper.go new file mode 100644 index 00000000000..d1723b49d18 --- /dev/null +++ b/pkg/db/v1alpha3/common/helper.go @@ -0,0 +1,11 @@ +package common + +import "os" + +func GetEnvWithDefault(name, defaultValue string) string { + v := os.Getenv(name) + if v == "" { + return defaultValue + } + return v +} diff --git a/pkg/db/v1alpha3/common/helper_test.go b/pkg/db/v1alpha3/common/helper_test.go new file mode 100644 index 00000000000..ab3444f102c --- /dev/null +++ b/pkg/db/v1alpha3/common/helper_test.go @@ -0,0 +1,21 @@ +package common + +import ( + "os" + "testing" +) + +func TestGetEnvWithDefault(t *testing.T) { + expected := "FAKE" + key := "TEST" + v := GetEnvWithDefault(key, expected) + if v != expected { + t.Errorf("Expected %s, got %s", expected, v) + } + expected = "FAKE1" + os.Setenv(key, expected) + v = GetEnvWithDefault(key, "") + if v != expected { + t.Errorf("Expected %s, got %s", expected, v) + } +} diff --git a/pkg/db/v1alpha3/mysql/mysql.go b/pkg/db/v1alpha3/mysql/mysql.go index dbdb87ec3de..0cd80b4d580 100644 --- a/pkg/db/v1alpha3/mysql/mysql.go +++ b/pkg/db/v1alpha3/mysql/mysql.go @@ -18,7 +18,7 @@ import ( const ( dbDriver = "mysql" - dbNameTmpl = "root:%s@tcp(katib-db:3306)/katib?timeout=5s" + dbNameTmpl = "root:%s@tcp(%s:%s)/katib?timeout=5s" mysqlTimeFmt = "2006-01-02 15:04:05.999999" connectInterval = 5 * time.Second @@ -32,7 +32,11 @@ type dbConn struct { func getDbName() string { dbPassEnvName := common.DBPasswordEnvName dbPass := os.Getenv(dbPassEnvName) - return fmt.Sprintf(dbNameTmpl, dbPass) + dbHost := common.GetEnvWithDefault( + common.MySQLDBHostEnvName, common.DefaultMySQLHost) + dbPort := common.GetEnvWithDefault( + common.MySQLDBPortEnvName, common.DefaultMySQLPort) + return fmt.Sprintf(dbNameTmpl, dbPass, dbHost, dbPort) } func openSQLConn(driverName string, dataSourceName string, interval time.Duration, From 852d518bf0cf0d1c0c6ac02c0904784d51a9b49a Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Fri, 11 Oct 2019 09:44:46 +0800 Subject: [PATCH 2/2] fix: Reuse code Signed-off-by: Ce Gao --- pkg/controller.v1alpha3/consts/const.go | 13 ++----------- pkg/db/v1alpha3/common/helper.go | 11 ----------- pkg/db/v1alpha3/mysql/mysql.go | 5 +++-- pkg/util/v1alpha3/env/env.go | 10 ++++++++++ .../v1alpha3/env/env_test.go} | 6 +++--- 5 files changed, 18 insertions(+), 27 deletions(-) delete mode 100644 pkg/db/v1alpha3/common/helper.go create mode 100644 pkg/util/v1alpha3/env/env.go rename pkg/{db/v1alpha3/common/helper_test.go => util/v1alpha3/env/env_test.go} (77%) diff --git a/pkg/controller.v1alpha3/consts/const.go b/pkg/controller.v1alpha3/consts/const.go index ba97e9763ad..d2608000d25 100644 --- a/pkg/controller.v1alpha3/consts/const.go +++ b/pkg/controller.v1alpha3/consts/const.go @@ -1,8 +1,6 @@ package consts -import ( - "os" -) +import "github.com/kubeflow/katib/pkg/util/v1alpha3/env" const ( ConfigExperimentSuggestionName = "experiment-suggestion-name" @@ -31,12 +29,5 @@ const ( ) var ( - DefaultKatibNamespace = getEnvOrDefault(DefaultKatibNamespaceEnvName, "kubeflow") + DefaultKatibNamespace = env.GetEnvOrDefault(DefaultKatibNamespaceEnvName, "kubeflow") ) - -func getEnvOrDefault(key string, fallback string) string { - if value, ok := os.LookupEnv(key); ok { - return value - } - return fallback -} diff --git a/pkg/db/v1alpha3/common/helper.go b/pkg/db/v1alpha3/common/helper.go deleted file mode 100644 index d1723b49d18..00000000000 --- a/pkg/db/v1alpha3/common/helper.go +++ /dev/null @@ -1,11 +0,0 @@ -package common - -import "os" - -func GetEnvWithDefault(name, defaultValue string) string { - v := os.Getenv(name) - if v == "" { - return defaultValue - } - return v -} diff --git a/pkg/db/v1alpha3/mysql/mysql.go b/pkg/db/v1alpha3/mysql/mysql.go index 0cd80b4d580..5b7c738933d 100644 --- a/pkg/db/v1alpha3/mysql/mysql.go +++ b/pkg/db/v1alpha3/mysql/mysql.go @@ -14,6 +14,7 @@ import ( v1alpha3 "github.com/kubeflow/katib/pkg/apis/manager/v1alpha3" "github.com/kubeflow/katib/pkg/db/v1alpha3/common" + "github.com/kubeflow/katib/pkg/util/v1alpha3/env" ) const ( @@ -32,9 +33,9 @@ type dbConn struct { func getDbName() string { dbPassEnvName := common.DBPasswordEnvName dbPass := os.Getenv(dbPassEnvName) - dbHost := common.GetEnvWithDefault( + dbHost := env.GetEnvOrDefault( common.MySQLDBHostEnvName, common.DefaultMySQLHost) - dbPort := common.GetEnvWithDefault( + dbPort := env.GetEnvOrDefault( common.MySQLDBPortEnvName, common.DefaultMySQLPort) return fmt.Sprintf(dbNameTmpl, dbPass, dbHost, dbPort) } diff --git a/pkg/util/v1alpha3/env/env.go b/pkg/util/v1alpha3/env/env.go new file mode 100644 index 00000000000..3de45f1865f --- /dev/null +++ b/pkg/util/v1alpha3/env/env.go @@ -0,0 +1,10 @@ +package env + +import "os" + +func GetEnvOrDefault(key string, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} diff --git a/pkg/db/v1alpha3/common/helper_test.go b/pkg/util/v1alpha3/env/env_test.go similarity index 77% rename from pkg/db/v1alpha3/common/helper_test.go rename to pkg/util/v1alpha3/env/env_test.go index ab3444f102c..b1f5d0caf74 100644 --- a/pkg/db/v1alpha3/common/helper_test.go +++ b/pkg/util/v1alpha3/env/env_test.go @@ -1,4 +1,4 @@ -package common +package env import ( "os" @@ -8,13 +8,13 @@ import ( func TestGetEnvWithDefault(t *testing.T) { expected := "FAKE" key := "TEST" - v := GetEnvWithDefault(key, expected) + v := GetEnvOrDefault(key, expected) if v != expected { t.Errorf("Expected %s, got %s", expected, v) } expected = "FAKE1" os.Setenv(key, expected) - v = GetEnvWithDefault(key, "") + v = GetEnvOrDefault(key, "") if v != expected { t.Errorf("Expected %s, got %s", expected, v) }