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

feat: Support env in mysql #868

Merged
merged 2 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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)
}
}