Skip to content

Commit

Permalink
feat: Support env in mysql (#868)
Browse files Browse the repository at this point in the history
* feat: Support env in mysql

Signed-off-by: Ce Gao <gaoce@caicloud.io>

* fix: Reuse code

Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege authored and k8s-ci-robot committed Oct 11, 2019
1 parent cefb6fe commit 89ed82b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
13 changes: 2 additions & 11 deletions pkg/controller.v1alpha3/consts/const.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package consts

import (
"os"
)
import "github.com/kubeflow/katib/pkg/util/v1alpha3/env"

const (
ConfigExperimentSuggestionName = "experiment-suggestion-name"
Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions pkg/db/v1alpha3/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ const (
MySqlDBNameEnvValue = "mysql"

DBPasswordEnvName = "DB_PASSWORD"

MySQLDBHostEnvName = "MYSQL_HOST"
MySQLDBPortEnvName = "MYSQL_PORT"

DefaultMySQLHost = "katib-db"
DefaultMySQLPort = "3306"
)
9 changes: 7 additions & 2 deletions pkg/db/v1alpha3/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ 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 (
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
Expand All @@ -32,7 +33,11 @@ type dbConn struct {
func getDbName() string {
dbPassEnvName := common.DBPasswordEnvName
dbPass := os.Getenv(dbPassEnvName)
return fmt.Sprintf(dbNameTmpl, dbPass)
dbHost := env.GetEnvOrDefault(
common.MySQLDBHostEnvName, common.DefaultMySQLHost)
dbPort := env.GetEnvOrDefault(
common.MySQLDBPortEnvName, common.DefaultMySQLPort)
return fmt.Sprintf(dbNameTmpl, dbPass, dbHost, dbPort)
}

func openSQLConn(driverName string, dataSourceName string, interval time.Duration,
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/v1alpha3/env/env.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions pkg/util/v1alpha3/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package env

import (
"os"
"testing"
)

func TestGetEnvWithDefault(t *testing.T) {
expected := "FAKE"
key := "TEST"
v := GetEnvOrDefault(key, expected)
if v != expected {
t.Errorf("Expected %s, got %s", expected, v)
}
expected = "FAKE1"
os.Setenv(key, expected)
v = GetEnvOrDefault(key, "")
if v != expected {
t.Errorf("Expected %s, got %s", expected, v)
}
}

0 comments on commit 89ed82b

Please sign in to comment.