Skip to content

Commit

Permalink
Add environment variable option to set postgres ssl mode
Browse files Browse the repository at this point in the history
Signed-off-by: Kun Chang <curtis@mail.ustc.edu.cn>
  • Loading branch information
ckcd committed Mar 5, 2024
1 parent fc858d1 commit b04f811
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pkg/db/v1beta1/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ const (
PostgreSQLDBHostEnvName = "KATIB_POSTGRESQL_DB_HOST"
PostgreSQLDBPortEnvName = "KATIB_POSTGRESQL_DB_PORT"
PostgreSQLDatabase = "KATIB_POSTGRESQL_DB_DATABASE"
PostgreSSLMode = "KATIB_POSTGRESQL_SSL_MODE"

DefaultPostgreSQLUser = "katib"
DefaultPostgreSQLDatabase = "katib"
DefaultPostgreSQLHost = "katib-postgres"
DefaultPostgreSQLPort = "5432"
DefaultPostgreSSLMode = "disable"

SkipDbInitializationEnvName = "SKIP_DB_INITIALIZATION"
)
6 changes: 4 additions & 2 deletions pkg/db/v1beta1/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ func getDbName() string {
common.PostgreSQLDBPortEnvName, common.DefaultPostgreSQLPort)
dbName := env.GetEnvOrDefault(common.PostgreSQLDatabase,
common.DefaultPostgreSQLDatabase)
sslMode := env.GetEnvOrDefault(common.PostgreSSLMode,
common.DefaultPostgreSSLMode)

psqlInfo := fmt.Sprintf("host=%s port=%s user=%s "+
"password=%s dbname=%s sslmode=disable",
dbHost, dbPort, dbUser, dbPass, dbName)
"password=%s dbname=%s sslmode=%s",
dbHost, dbPort, dbUser, dbPass, dbName, sslMode)

return psqlInfo
}
Expand Down
60 changes: 55 additions & 5 deletions pkg/db/v1beta1/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/google/go-cmp/cmp"
_ "github.com/lib/pq"

api_pb "github.com/kubeflow/katib/pkg/apis/manager/v1beta1"
Expand Down Expand Up @@ -129,11 +130,60 @@ func TestDeleteObservationLog(t *testing.T) {
}

func TestGetDbName(t *testing.T) {
// dbName := "root:@tcp(katib-mysql:3306)/katib?timeout=5s"
dbName := "host=katib-postgres port=5432 user=katib password= dbname=katib sslmode=disable"

if getDbName() != dbName {
t.Errorf("getDbName returns wrong value %v", getDbName())
cases := map[string]struct {
updateEnvs map[string]string
wantName string
}{
"All parameters are default": {
wantName: "host=katib-postgres port=5432 user=katib password= dbname=katib sslmode=disable",
},
"Set DB_USER": {
updateEnvs: map[string]string{
common.DBUserEnvName: "testUser",
},
wantName: "host=katib-postgres port=5432 user=testUser password= dbname=katib sslmode=disable",
},
"Set KATIB_POSTGRESQL_DB_HOST": {
updateEnvs: map[string]string{
common.PostgreSQLDBHostEnvName: "testHost",
},
wantName: "host=testHost port=5432 user=katib password= dbname=katib sslmode=disable",
},
"Set KATIB_POSTGRESQL_DB_PORT": {
updateEnvs: map[string]string{
common.PostgreSQLDBPortEnvName: "1234",
},
wantName: "host=katib-postgres port=1234 user=katib password= dbname=katib sslmode=disable",
},
"Set KATIB_POSTGRESQL_DB_DATABASE": {
updateEnvs: map[string]string{
common.PostgreSQLDatabase: "testDB",
},
wantName: "host=katib-postgres port=5432 user=katib password= dbname=testDB sslmode=disable",
},
"Set DB_PASSWORD": {
updateEnvs: map[string]string{
common.DBPasswordEnvName: "testPassword",
},
wantName: "host=katib-postgres port=5432 user=katib password=testPassword dbname=katib sslmode=disable",
},
"Set KATIB_POSTGRESQL_SSL_MODE": {
updateEnvs: map[string]string{
common.PostgreSSLMode: "require",
},
wantName: "host=katib-postgres port=5432 user=katib password= dbname=katib sslmode=require",
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
for k, v := range tc.updateEnvs {
t.Setenv(k, v)
}
gotName := getDbName()
if diff := cmp.Diff(tc.wantName, gotName); len(diff) != 0 {
t.Errorf("Unexpected DBName (-want,+got):\n%s", diff)
}
})
}
}

0 comments on commit b04f811

Please sign in to comment.