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

Add environment variable option to set postgres ssl mode #2266

Merged
merged 1 commit into from
Mar 5, 2024
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
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)
}
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	cases := map[string]struct{
		updateEnvs map[string]string
		wantName string	
	}{
		"All parameters are default": {
		...
		},
		"Set KATIB_POSTGRESQL_SSL_MODE": {
		...
		}
	}
	for name, tc := range cases {
		t.Run(name, func(t *testsing.T) {
			// TEST
		})
	}

Please could you refine these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tenzen-y Oh I got your point. I made the change based on your suggestion. And add more test cases for each env. Please help to review it. Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ckcd Thanks for your great refining! I'd be appreciated for your effort!

}
Loading