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 1, 2024
1 parent 8df3c5c commit 3160836
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 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
10 changes: 10 additions & 0 deletions pkg/db/v1beta1/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ func TestGetDbName(t *testing.T) {
}

}

func TestSslMode(t *testing.T) {
sslMode := "require"
t.Setenv(common.PostgreSslMode, sslMode)

dbName := "host=katib-postgres port=5432 user=katib password= dbname=katib sslmode=require"
if getDbName() != dbName {
t.Errorf("getDbName returns wrong value %v", getDbName())
}
}

0 comments on commit 3160836

Please sign in to comment.