Skip to content

Commit f73d6e5

Browse files
committed
added db to role
1 parent 72541ce commit f73d6e5

File tree

8 files changed

+33
-27
lines changed

8 files changed

+33
-27
lines changed

api/v1beta1/database_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ type SecretReference struct {
7373
}
7474

7575
type Role struct {
76-
Name string `json:"name"`
76+
Name string `json:"name"`
77+
Db *string `json:"db,omitempty"`
7778
}
7879

7980
// Extension is a resource representing database extension

api/v1beta1/mongodbuser_type.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ func (in *MongoDBUser) GetCredentials() *SecretReference {
7676
return in.Spec.Credentials
7777
}
7878

79-
func (in *MongoDBUser) GetRoles() *[]Role {
80-
return in.Spec.Roles
79+
func (in *MongoDBUser) GetRoles() []Role {
80+
if in.Spec.Roles == nil {
81+
return []Role{}
82+
}
83+
84+
return *in.Spec.Roles
8185
}
8286

8387
// +kubebuilder:object:root=true

api/v1beta1/postgresqluser_type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func (in *PostgreSQLUser) GetCredentials() *SecretReference {
6868
return in.Spec.Credentials
6969
}
7070

71-
func (in *PostgreSQLUser) GetRoles() *[]Role {
71+
func (in *PostgreSQLUser) GetRoles() []Role {
7272
// NOOP
73-
return nil
73+
return []Role{}
7474
}
7575

7676
// +kubebuilder:object:root=true

common/db/handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package db
22

33
import (
44
"context"
5+
6+
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
57
)
68

79
// Invoke a database handler
@@ -10,7 +12,7 @@ type Invoke func(ctx context.Context, uri, database, username, password string)
1012
// Handler is a wrapper arround a certain database client
1113
type Handler interface {
1214
Close() error
13-
SetupUser(database string, username string, password string, roles []string) error
15+
SetupUser(database string, username string, password string, roles []infrav1beta1.Role) error
1416
DropUser(database string, username string) error
1517
CreateDatabaseIfNotExists(database string) error
1618
EnableExtension(name string) error

common/db/mongodb.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"go.mongodb.org/mongo-driver/mongo"
1111
"go.mongodb.org/mongo-driver/mongo/options"
1212
"go.mongodb.org/mongo-driver/mongo/readpref"
13+
14+
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
1315
)
1416

1517
const (
@@ -67,7 +69,7 @@ func (m *MongoDBRepository) CreateDatabaseIfNotExists(database string) error {
6769
return nil
6870
}
6971

70-
func (m *MongoDBRepository) SetupUser(database string, username string, password string, roles []string) error {
72+
func (m *MongoDBRepository) SetupUser(database string, username string, password string, roles []infrav1beta1.Role) error {
7173
doesUserExist, err := m.doesUserExist(database, username)
7274
if err != nil {
7375
return err
@@ -138,25 +140,30 @@ func (m *MongoDBRepository) getAllUsers(database string, username string) (Users
138140
return users, nil
139141
}
140142

141-
func (m *MongoDBRepository) getRoles(database string, roles []string) []bson.M {
143+
func (m *MongoDBRepository) getRoles(database string, roles []infrav1beta1.Role) []bson.M {
142144
// by default, assign readWrite role (backward compatibility)
143-
if roles == nil || len(roles) == 0 {
145+
if len(roles) == 0 {
144146
return []bson.M{{
145147
"role": "readWrite",
146148
"db": database,
147149
}}
148150
}
149151
rs := make([]bson.M, 0)
150152
for _, r := range roles {
153+
db := r.Db
154+
if db == nil {
155+
db = &database
156+
}
157+
151158
rs = append(rs, bson.M{
152-
"role": r,
153-
"db": database,
159+
"role": r.Name,
160+
"db": db,
154161
})
155162
}
156163
return rs
157164
}
158165

159-
func (m *MongoDBRepository) createUser(database string, username string, password string, roles []string) error {
166+
func (m *MongoDBRepository) createUser(database string, username string, password string, roles []infrav1beta1.Role) error {
160167
command := &bson.D{primitive.E{Key: "createUser", Value: username}, primitive.E{Key: "pwd", Value: password},
161168
primitive.E{Key: "roles", Value: m.getRoles(database, roles)}}
162169
r := m.runCommand(database, command)
@@ -166,7 +173,7 @@ func (m *MongoDBRepository) createUser(database string, username string, passwor
166173
return nil
167174
}
168175

169-
func (m *MongoDBRepository) updateUserPasswordAndRoles(database string, username string, password string, roles []string) error {
176+
func (m *MongoDBRepository) updateUserPasswordAndRoles(database string, username string, password string, roles []infrav1beta1.Role) error {
170177
command := &bson.D{primitive.E{Key: "updateUser", Value: username}, primitive.E{Key: "pwd", Value: password},
171178
primitive.E{Key: "roles", Value: m.getRoles(database, roles)}}
172179
r := m.runCommand(database, command)

common/db/postgresql.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/url"
88

9+
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
910
"github.com/jackc/pgx/v4"
1011
"github.com/jackc/pgx/v4/pgxpool"
1112
)
@@ -69,7 +70,7 @@ func (s *PostgreSQLRepository) CreateDatabaseIfNotExists(database string) error
6970
}
7071
}
7172

72-
func (s *PostgreSQLRepository) SetupUser(database string, user string, password string, roles []string) error {
73+
func (s *PostgreSQLRepository) SetupUser(database string, user string, password string, roles []infrav1beta1.Role) error {
7374
if err := s.createUserIfNotExists(user); err != nil {
7475
return err
7576
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ spec:
8080
- name: readWrite
8181
items:
8282
properties:
83+
db:
84+
type: string
8385
name:
8486
type: string
8587
required:

controllers/handler.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type user interface {
4040
runtime.Object
4141
GetStatusConditions() *[]metav1.Condition
4242
GetCredentials() *infrav1beta1.SecretReference
43-
GetRoles() *[]infrav1beta1.Role
43+
GetRoles() []infrav1beta1.Role
4444
GetDatabase() string
4545
}
4646

@@ -73,17 +73,6 @@ func extractCredentials(credentials *infrav1beta1.SecretReference, secret *corev
7373
return user, pw, nil
7474
}
7575

76-
func extractRoles(roles *[]infrav1beta1.Role) []string {
77-
if roles == nil || len(*roles) == 0 {
78-
return nil
79-
}
80-
rolesToReturn := make([]string, 0)
81-
for _, r := range *roles {
82-
rolesToReturn = append(rolesToReturn, r.Name)
83-
}
84-
return rolesToReturn
85-
}
86-
8776
func reconcileDatabase(c client.Client, pool *db.ClientPool, invoke db.Invoke, database database, recorder record.EventRecorder) (database, ctrl.Result) {
8877
// Fetch referencing root secret
8978
secret := &corev1.Secret{}
@@ -215,7 +204,7 @@ func reconcileUser(database database, c client.Client, pool *db.ClientPool, invo
215204
return user, ctrl.Result{Requeue: true}
216205
}
217206

218-
err = dbHandler.SetupUser(database.GetDatabaseName(), usr, pw, extractRoles(user.GetRoles()))
207+
err = dbHandler.SetupUser(database.GetDatabaseName(), usr, pw, user.GetRoles())
219208
if err != nil {
220209
msg := fmt.Sprintf("Failed to provison user account: %s", err.Error())
221210
recorder.Event(user, "Normal", "error", msg)

0 commit comments

Comments
 (0)