Skip to content

Commit f555cd0

Browse files
committed
Merge branch 'DK-2230' into DK-2384
2 parents 28af550 + 672c6d3 commit f555cd0

17 files changed

+242
-99
lines changed

api/v1beta1/database_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type DatabaseSpec struct {
3838
DatabaseName string `json:"databaseName"`
3939

4040
// The connect URI
41-
// +required
42-
Address string `json:"address"`
41+
// +optional
42+
Address string `json:"address,omitempty"`
4343

4444
// Contains a credentials set of a user with enough permission to manage databases and user accounts
4545
// +required
@@ -76,7 +76,7 @@ type Role struct {
7676
Name string `json:"name"`
7777

7878
// +optional
79-
Db *string `json:"db,omitempty"`
79+
Db string `json:"db,omitempty"`
8080
}
8181

8282
// Extension is a resource representing database extension

api/v1beta1/mongodbdatabase_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
// MongoDBDatabaseSpec defines the desired state of MongoDBDatabase
2525
type MongoDBDatabaseSpec struct {
2626
*DatabaseSpec `json:",inline"`
27+
AtlasGroupId string `json:"atlasGroupId,omitempty"`
2728
}
2829

2930
// GetStatusConditions returns a pointer to the Status.Conditions slice
@@ -57,6 +58,10 @@ type MongoDBDatabase struct {
5758
Status MongoDBDatabaseStatus `json:"status,omitempty"`
5859
}
5960

61+
func (in *MongoDBDatabase) GetAtlasGroupId() string {
62+
return in.Spec.AtlasGroupId
63+
}
64+
6065
func (in *MongoDBDatabase) GetAddress() string {
6166
return in.Spec.Address
6267
}

api/v1beta1/postgresqldatabase_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ type PostgreSQLDatabase struct {
5757
Status PostgreSQLDatabaseStatus `json:"status,omitempty"`
5858
}
5959

60+
func (in *PostgreSQLDatabase) GetAtlasGroupId() string {
61+
return ""
62+
}
63+
6064
func (in *PostgreSQLDatabase) GetAddress() string {
6165
return in.Spec.Address
6266
}

common/db/atlas.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ func (m *AtlasRepository) EnableExtension(ctx context.Context, name string) erro
7373
}
7474

7575
func (m *AtlasRepository) doesUserExist(ctx context.Context, database string, username string) (bool, error) {
76-
user, _, err := m.atlas.DatabaseUsers.Get(ctx, database, m.groupId, username)
77-
if user == nil {
78-
return false, err
76+
_, _, err := m.atlas.DatabaseUsers.Get(ctx, database, m.groupId, username)
77+
if err != nil {
78+
return false, nil
7979
}
8080

8181
return true, err
@@ -93,13 +93,13 @@ func (m *AtlasRepository) getRoles(database string, roles []infrav1beta1.Role) [
9393
rs := make([]mongodbatlas.Role, 0)
9494
for _, r := range roles {
9595
db := r.Db
96-
if db == nil {
97-
db = &database
96+
if db == "" {
97+
db = database
9898
}
9999

100100
rs = append(rs, mongodbatlas.Role{
101101
RoleName: r.Name,
102-
DatabaseName: *db,
102+
DatabaseName: db,
103103
})
104104
}
105105

@@ -108,9 +108,10 @@ func (m *AtlasRepository) getRoles(database string, roles []infrav1beta1.Role) [
108108

109109
func (m *AtlasRepository) createUser(ctx context.Context, database string, username string, password string, roles []infrav1beta1.Role) error {
110110
user := &mongodbatlas.DatabaseUser{
111-
Username: username,
112-
Password: password,
113-
Roles: m.getRoles(database, roles),
111+
Username: username,
112+
Password: password,
113+
DatabaseName: database,
114+
Roles: m.getRoles(database, roles),
114115
}
115116

116117
_, _, err := m.atlas.DatabaseUsers.Create(ctx, m.groupId, user)

common/db/handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"context"
55

66
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
7-
v1 "k8s.io/api/core/v1"
87
)
98

109
// Invoke a database handler
11-
type Invoke func(ctx context.Context, db *infrav1beta1.Database, secret *v1.Secret) (Handler, error)
10+
type Invoke func(ctx context.Context, uri, database, username, password string) (Handler, error)
1211

1312
// Handler is a wrapper arround a certain database client
1413
type Handler interface {

common/db/mongodb.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package db
33
import (
44
"context"
55
"errors"
6+
"time"
67

78
"go.mongodb.org/mongo-driver/bson"
89
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -36,7 +37,10 @@ type MongoDBRepository struct {
3637
}
3738

3839
func NewMongoDBRepository(ctx context.Context, uri, database, username, password string) (Handler, error) {
39-
o := options.Client().ApplyURI(uri)
40+
o := options.Client()
41+
o.SetConnectTimeout(time.Duration(2) * time.Second)
42+
o.ApplyURI(uri)
43+
4044
o.SetAuth(options.Credential{
4145
Username: username,
4246
Password: password,
@@ -146,8 +150,8 @@ func (m *MongoDBRepository) getRoles(database string, roles []infrav1beta1.Role)
146150
rs := make([]bson.M, 0)
147151
for _, r := range roles {
148152
db := r.Db
149-
if db == nil {
150-
db = &database
153+
if db == "" {
154+
db = database
151155
}
152156

153157
rs = append(rs, bson.M{

common/db/pool.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

common/db/postgresql.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,25 @@ func NewPostgreSQLRepository(ctx context.Context, uri, database, username, passw
2828

2929
opt.User = url.UserPassword(username, password)
3030

31+
q, _ := url.ParseQuery(opt.RawQuery)
32+
hasConnectTimeout := false
33+
for k, _ := range q {
34+
if k == "connect_timeout" {
35+
hasConnectTimeout = true
36+
break
37+
}
38+
}
39+
40+
if hasConnectTimeout == false {
41+
q.Add("connect_timeout", "2")
42+
}
43+
44+
opt.RawQuery = q.Encode()
45+
3146
if database != "" {
3247
opt.Path = database
3348
}
49+
3450
dbpool, err := pgxpool.Connect(ctx, opt.String())
3551
if err != nil {
3652
return nil, err

config/crd/bases/dbprovisioning.infra.doodle.com_mongodbdatabases.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ spec:
5151
address:
5252
description: The connect URI
5353
type: string
54+
atlasGroupId:
55+
type: string
5456
databaseName:
5557
description: DatabaseName is by default the same as metata.name
5658
type: string
@@ -83,7 +85,6 @@ spec:
8385
- name
8486
type: object
8587
required:
86-
- address
8788
- rootSecret
8889
type: object
8990
status:

config/crd/bases/dbprovisioning.infra.doodle.com_postgresqldatabases.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ spec:
8383
- name
8484
type: object
8585
required:
86-
- address
8786
- rootSecret
8887
type: object
8988
status:

0 commit comments

Comments
 (0)