Skip to content

Commit 86c24e9

Browse files
authored
Merge branch 'master' into fix-cond-DK-1738
2 parents f42ff03 + 42d526d commit 86c24e9

25 files changed

+237
-431
lines changed

api/v1beta1/database_types.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ const (
1212

1313
// Status conditions
1414
const (
15-
DatabaseReadyConditionType = "DatabaseReady"
16-
UserReadyConditionType = "UserReady"
15+
DatabaseReadyConditionType = "DatabaseReady"
16+
UserReadyConditionType = "UserReady"
17+
ExtensionReadyConditionType = "ExtensionReady"
1718
)
1819

1920
// Status reasons
@@ -27,6 +28,7 @@ const (
2728
UserProvisioningSuccessfulReason = "UserProvisioningSuccessful"
2829
CredentialsNotFoundReason = "CredentialsNotFound"
2930
CreateDatabaseFailedReason = "CreateDatabaseFailed"
31+
CreateExtensionFailedReason = "CreateExtensionFailed"
3032
)
3133

3234
// DatabaseSpec defines the desired state of MongoDBDatabase
@@ -42,6 +44,10 @@ type DatabaseSpec struct {
4244
// Contains a credentials set of a user with enough permission to manage databases and user accounts
4345
// +required
4446
RootSecret *SecretReference `json:"rootSecret"`
47+
48+
// Database extensions
49+
// +optional
50+
Extensions Extensions `json:"extensions,omitempty"`
4551
}
4652

4753
// DatabaseReference is a named reference to a database kind
@@ -66,6 +72,14 @@ type SecretReference struct {
6672
PasswordField string `json:"passwordField"`
6773
}
6874

75+
// Extension is a resource representing database extension
76+
type Extension struct {
77+
Name string `json:"name"`
78+
}
79+
80+
// Extensions is a collection of Extension types
81+
type Extensions []Extension
82+
6983
// conditionalResource is a resource with conditions
7084
type conditionalResource interface {
7185
GetStatusConditions() *[]metav1.Condition
@@ -79,6 +93,10 @@ func DatabaseReadyCondition(in conditionalResource, reason, message string) {
7993
setResourceCondition(in, DatabaseReadyConditionType, metav1.ConditionTrue, reason, message)
8094
}
8195

96+
func ExtensionNotReadyCondition(in conditionalResource, reason, message string) {
97+
setResourceCondition(in, ExtensionReadyConditionType, metav1.ConditionFalse, reason, message)
98+
}
99+
82100
func UserNotReadyCondition(in conditionalResource, reason, message string) {
83101
setResourceCondition(in, UserReadyConditionType, metav1.ConditionFalse, reason, message)
84102
}

api/v1beta1/mongodbdatabase_types.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ import (
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
)
2323

24-
// defaults
25-
const (
26-
DEFAULT_MONGODB_ROOT_USER = "root"
27-
DEFAULT_MONGODB_ROOT_AUTHENTICATION_DATABASE = "admin"
28-
)
29-
3024
// MongoDBDatabaseSpec defines the desired state of MongoDBDatabase
3125
type MongoDBDatabaseSpec struct {
3226
*DatabaseSpec `json:",inline"`
@@ -40,7 +34,7 @@ func (in *MongoDBDatabase) GetStatusConditions() *[]metav1.Condition {
4034
// MongoDBDatabaseStatus defines the observed state of MongoDBDatabase
4135
// IMPORTANT: Run "make" to regenerate code after modifying this file
4236
type MongoDBDatabaseStatus struct {
43-
// Conditions holds the conditions for the VaultBinding.
37+
// Conditions holds the conditions for the MongoDBDatabase.
4438
// +optional
4539
Conditions []metav1.Condition `json:"conditions,omitempty"`
4640
}
@@ -79,6 +73,14 @@ func (in *MongoDBDatabase) GetDatabaseName() string {
7973
return in.GetName()
8074
}
8175

76+
func (in *MongoDBDatabase) GetRootDatabaseName() string {
77+
return ""
78+
}
79+
80+
func (in *MongoDBDatabase) GetExtensions() Extensions {
81+
return in.Spec.Extensions
82+
}
83+
8284
// +kubebuilder:object:root=true
8385

8486
// MongoDBDatabaseList contains a list of MongoDBDatabase

api/v1beta1/mongodbuser_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (in *MongoDBUser) GetStatusConditions() *[]metav1.Condition {
5252
// MongoDBUserStatus defines the observed state of MongoDBUser
5353
// IMPORTANT: Run "make" to regenerate code after modifying this file
5454
type MongoDBUserStatus struct {
55-
// Conditions holds the conditions for the VaultBinding.
55+
// Conditions holds the conditions for the MongoDBUser.
5656
// +optional
5757
Conditions []metav1.Condition `json:"conditions,omitempty"`
5858
}

api/v1beta1/postgresqldatabase_types.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ import (
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
)
2323

24-
// defaults
25-
const (
26-
DEFAULT_POSTGRESQL_ROOT_USER = "postgres"
27-
DEFAULT_POSTGRESQL_ROOT_AUTHENTICATION_DATABASE = "postgres"
28-
)
29-
3024
// PostgreSQLDatabaseSpec defines the desired state of PostgreSQLDatabase
3125
type PostgreSQLDatabaseSpec struct {
3226
*DatabaseSpec `json:",inline"`
@@ -40,7 +34,7 @@ func (in *PostgreSQLDatabase) GetStatusConditions() *[]metav1.Condition {
4034
// PostgreSQLDatabaseStatus defines the observed state of PostgreSQLDatabase
4135
// IMPORTANT: Run "make" to regenerate code after modifying this file
4236
type PostgreSQLDatabaseStatus struct {
43-
// Conditions holds the conditions for the VaultBinding.
37+
// Conditions holds the conditions for the PostgreSQLDatabase.
4438
// +optional
4539
Conditions []metav1.Condition `json:"conditions,omitempty"`
4640
}
@@ -79,6 +73,14 @@ func (in *PostgreSQLDatabase) GetDatabaseName() string {
7973
return in.GetName()
8074
}
8175

76+
func (in *PostgreSQLDatabase) GetRootDatabaseName() string {
77+
return ""
78+
}
79+
80+
func (in *PostgreSQLDatabase) GetExtensions() Extensions {
81+
return in.Spec.Extensions
82+
}
83+
8284
// +kubebuilder:object:root=true
8385

8486
// PostgreSQLDatabaseList contains a list of PostgreSQLDatabase

api/v1beta1/postgresqluser_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (in *PostgreSQLUser) GetStatusConditions() *[]metav1.Condition {
3434
// PostgreSQLUserStatus defines the observed state of PostgreSQLUser
3535
// IMPORTANT: Run "make" to regenerate code after modifying this file
3636
type PostgreSQLUserStatus struct {
37-
// Conditions holds the conditions for the VaultBinding.
37+
// Conditions holds the conditions for the PostgreSQLUser.
3838
// +optional
3939
Conditions []metav1.Condition `json:"conditions,omitempty"`
4040
}

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chart/k8sdb-controller/Chart.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ keywords:
66
- kubernetes-controller
77
- mongodb
88
- postgresql
9-
- vault
109
name: k8sdb-controller
1110
sources:
1211
- https://github.com/DoodleScheduling/k8sdb-controller

common/db/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
)
66

77
// Invoke a database handler
8-
type Invoke (func(ctx context.Context, uri, username, password string) (Handler, error))
8+
type Invoke func(ctx context.Context, uri, database, username, password string) (Handler, error)
99

1010
// Handler is a wrapper arround a certain database client
1111
type Handler interface {
1212
Close() error
1313
SetupUser(database string, username string, password string) error
1414
DropUser(database string, username string) error
1515
CreateDatabaseIfNotExists(database string) error
16+
EnableExtension(name string) error
1617
}

common/db/mongodb.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ type User struct {
3030
Roles Roles `json:"roles" bson:"roles"`
3131
}
3232

33-
type MongoDBServer struct {
33+
type MongoDBRepository struct {
3434
client *mongo.Client
3535
}
3636

37-
func NewMongoDBServer(ctx context.Context, uri, username, password string) (Handler, error) {
37+
func NewMongoDBRepository(ctx context.Context, uri, database, username, password string) (Handler, error) {
3838
o := options.Client().ApplyURI(uri)
3939
o.SetAuth(options.Credential{
4040
Username: username,
@@ -50,24 +50,24 @@ func NewMongoDBServer(ctx context.Context, uri, username, password string) (Hand
5050
return nil, err
5151
}
5252

53-
return &MongoDBServer{
53+
return &MongoDBRepository{
5454
client: client,
5555
}, nil
5656
}
5757

58-
func (m *MongoDBServer) Close() error {
58+
func (m *MongoDBRepository) Close() error {
5959
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
6060
defer cancel()
6161
return m.client.Disconnect(ctx)
6262
}
6363

6464
// CreateDatabaseIfNotExists is a dummy to apply to fullfill the contract,
6565
// we don't need to create the database on MongoDB
66-
func (m *MongoDBServer) CreateDatabaseIfNotExists(database string) error {
66+
func (m *MongoDBRepository) CreateDatabaseIfNotExists(database string) error {
6767
return nil
6868
}
6969

70-
func (m *MongoDBServer) SetupUser(database string, username string, password string) error {
70+
func (m *MongoDBRepository) SetupUser(database string, username string, password string) error {
7171
doesUserExist, err := m.doesUserExist(database, username)
7272
if err != nil {
7373
return err
@@ -91,7 +91,21 @@ func (m *MongoDBServer) SetupUser(database string, username string, password str
9191
return nil
9292
}
9393

94-
func (m *MongoDBServer) doesUserExist(database string, username string) (bool, error) {
94+
func (m *MongoDBRepository) DropUser(database string, username string) error {
95+
command := &bson.D{primitive.E{Key: "dropUser", Value: username}}
96+
r := m.runCommand(database, command)
97+
if _, err := r.DecodeBytes(); err != nil {
98+
return err
99+
}
100+
return nil
101+
}
102+
103+
func (m *MongoDBRepository) EnableExtension(name string) error {
104+
// NOOP
105+
return nil
106+
}
107+
108+
func (m *MongoDBRepository) doesUserExist(database string, username string) (bool, error) {
95109
users, err := m.getAllUsers(database, username)
96110
if err != nil {
97111
return false, err
@@ -100,7 +114,7 @@ func (m *MongoDBServer) doesUserExist(database string, username string) (bool, e
100114
return users != nil && len(users) > 0, nil
101115
}
102116

103-
func (m *MongoDBServer) getAllUsers(database string, username string) (Users, error) {
117+
func (m *MongoDBRepository) getAllUsers(database string, username string) (Users, error) {
104118
users := make(Users, 0)
105119
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
106120
defer cancel()
@@ -124,7 +138,7 @@ func (m *MongoDBServer) getAllUsers(database string, username string) (Users, er
124138
return users, nil
125139
}
126140

127-
func (m *MongoDBServer) createUser(database string, username string, password string) error {
141+
func (m *MongoDBRepository) createUser(database string, username string, password string) error {
128142
command := &bson.D{primitive.E{Key: "createUser", Value: username}, primitive.E{Key: "pwd", Value: password},
129143
primitive.E{Key: "roles", Value: []bson.M{{"role": "readWrite", "db": database}}}}
130144
r := m.runCommand(database, command)
@@ -134,7 +148,7 @@ func (m *MongoDBServer) createUser(database string, username string, password st
134148
return nil
135149
}
136150

137-
func (m *MongoDBServer) updateUserPasswordAndRoles(database string, username string, password string) error {
151+
func (m *MongoDBRepository) updateUserPasswordAndRoles(database string, username string, password string) error {
138152
command := &bson.D{primitive.E{Key: "updateUser", Value: username}, primitive.E{Key: "pwd", Value: password},
139153
primitive.E{Key: "roles", Value: []bson.M{{"role": "readWrite", "db": database}}}}
140154
r := m.runCommand(database, command)
@@ -144,16 +158,7 @@ func (m *MongoDBServer) updateUserPasswordAndRoles(database string, username str
144158
return nil
145159
}
146160

147-
func (m *MongoDBServer) DropUser(database string, username string) error {
148-
command := &bson.D{primitive.E{Key: "dropUser", Value: username}}
149-
r := m.runCommand(database, command)
150-
if _, err := r.DecodeBytes(); err != nil {
151-
return err
152-
}
153-
return nil
154-
}
155-
156-
func (m *MongoDBServer) runCommand(database string, command *bson.D) *mongo.SingleResult {
161+
func (m *MongoDBRepository) runCommand(database string, command *bson.D) *mongo.SingleResult {
157162
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
158163
defer cancel()
159164
return m.client.Database(database).RunCommand(ctx, *command)

common/db/pool.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func NewClientPool() *ClientPool {
1717
}
1818
}
1919

20-
func (c *ClientPool) FromURI(ctx context.Context, invoke Invoke, uri, username, password string) (Handler, error) {
20+
func (c *ClientPool) FromURI(ctx context.Context, invoke Invoke, uri, database, username, password string) (Handler, error) {
2121
c.mutex.Lock()
2222
defer c.mutex.Unlock()
23-
key := fmt.Sprintf("%s/%s/%s", uri, username, password)
23+
key := fmt.Sprintf("%s/%s/%s/%s", uri, database, username, password)
2424

2525
if _, ok := c.pool[key]; !ok {
26-
if server, err := invoke(ctx, uri, username, password); err != nil {
26+
if server, err := invoke(ctx, uri, database, username, password); err != nil {
2727
return nil, err
2828
} else {
2929
c.pool[key] = server

0 commit comments

Comments
 (0)