Skip to content

Commit 67ca089

Browse files
committed
refactoring
1 parent 875adf8 commit 67ca089

37 files changed

+973
-722
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.13 as builder
2+
FROM golang:1.15 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests

Jenkinsfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ podTemplate(label: 'kubedb',
55
containers: [
66
containerTemplate(
77
name: 'golang',
8-
image: 'bitnami/golang:1.13',
8+
image: 'bitnami/golang:1.15',
99
ttyEnabled: true
1010
),
1111
containerTemplate(
@@ -28,7 +28,7 @@ podTemplate(label: 'kubedb',
2828
ansiColor("xterm") {
2929
stage('checkout') {
3030
checkout(scm)
31-
31+
3232
container('docker') {
3333
dockerAuth()
3434
}
@@ -66,8 +66,8 @@ podTemplate(label: 'kubedb',
6666
bumpImageVersion(env.TAG_NAME)
6767

6868
tgz="kubedb-${version}.tgz"
69+
sh "cp config/crd/bases/* chart/kubedb/crds"
6970
sh "helm package chart/kubedb"
70-
7171
}
7272

7373
container('golang') {

PROJECT

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
domain: doodle.com
22
repo: github.com/doodlescheduling/kubedb
33
resources:
4-
- group: infra
5-
kind: MongoDB
4+
- group: dbprovisioning.infra.doodle.com
5+
kind: MongoDBDatabase
66
version: v1beta1
7-
- group: infra
8-
kind: PostgreSQL
7+
- group: dbprovisioning.infra.doodle.com
8+
kind: PostgreSQLDatabase
9+
version: v1beta1
10+
- group: dbprovisioning.infra.doodle.com
11+
kind: MongoDBUser
12+
version: v1beta1
13+
- group: dbprovisioning.infra.doodle.com
14+
kind: PostgreSQLUser
915
version: v1beta1
1016
version: "2"

README.md

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,110 @@
1-
# KUBEDB
1+
# Database controller
22

3-
Kubernetes Controller that sets up databases, credentials and permissions in Doodle databases.
3+
Kubernetes Controller that deals with database and user provisioning.
44

5-
Build with [kubebuilder](https://github.com/kubernetes-sigs/kubebuilder).
5+
## Example for PostgreSQL
66

7-
Work in progress.
7+
A `VaultBinding` binds a kubernetes vanialla secret to a vault path.
8+
Following a secret which fields shall be placed into vault:
89

9-
Name "kubedb" clashes with an open-source project: https://github.com/kubedb
10-
We're going to have naming "clashes" (not technical, but on human level) if we ever decide to use that one.
10+
```yaml
11+
apiVersion: v1
12+
kind: Secret
13+
metadata:
14+
name: postgresql-admin-credentials
15+
namespace: default
16+
data:
17+
password: MTIzNA==
18+
username: MTIzNA==
19+
---
20+
apiVersion: dbprovisioning.infra.doodle.com/v1beta1
21+
kind: PostgreSQLDatabase
22+
metadata:
23+
name: my-app
24+
namespace: default
25+
spec:
26+
address: "postgres://localhost:5432"
27+
rootSecret:
28+
name: postgresql-admin-credentials
29+
---
30+
apiVersion: dbprovisioning.infra.doodle.com/v1beta1
31+
kind: PostgreSQLUser
32+
metadata:
33+
name: my-app
34+
namespace: default
35+
spec:
36+
database:
37+
name: my-app
38+
credentials:
39+
name: my-app-postgresql-credentials
40+
---
41+
apiVersion: v1
42+
kind: Secret
43+
metadata:
44+
name: my-app-postgresql-credentials
45+
namespace: default
46+
data:
47+
password: MTIzNA==
48+
username: MTIzNA==
49+
```
1150
12-
TODO: Write proper README file.
51+
## Example for MongoDB
52+
```yaml
53+
apiVersion: v1
54+
kind: Secret
55+
metadata:
56+
name: mongodb-admin-credentials
57+
namespace: default
58+
data:
59+
password: MTIzNA==
60+
username: MTIzNA==
61+
---
62+
apiVersion: dbprovisioning.infra.doodle.com/v1beta1
63+
kind: MongoDBDatabase
64+
metadata:
65+
name: my-app
66+
namespace: default
67+
spec:
68+
address: "mongodb://localhost:27017"
69+
rootSecret:
70+
name: mongodb-admin-credentials
71+
---
72+
apiVersion: dbprovisioning.infra.doodle.com/v1beta1
73+
kind: MongoDBUser
74+
metadata:
75+
name: my-app
76+
namespace: default
77+
spec:
78+
database:
79+
name: my-app-mongodb-credentials
80+
credentials:
81+
name: my-app-mongodb
82+
---
83+
apiVersion: v1
84+
kind: Secret
85+
metadata:
86+
name: my-app-mongodb-credentials
87+
namespace: default
88+
data:
89+
password: MTIzNA==
90+
username: MTIzNA==
91+
```
1392
14-
Config options:
93+
## Helm chart
94+
95+
Please see [chart/kubedb](https://github.com/DoodleScheduling/kubedb) for the helm chart docs.
96+
97+
## Limitations
98+
99+
Currently there is no garbage collection implemented, meaning all the things created are not removed.
100+
This will be at least implemented for user provisioning. Discussion will stay open for databases.
101+
102+
## Configure the controller
15103
16104
ENV Variable | Argument | Default value | Example | Purpose |
17105
-------------|----------|---------------|---------|---------|
18106
METRICS_ADDR | --metrics-addr | :8080 | :8080 | Metrics port |
19107
ENABLE_LEADER_ELECTION | --enable-leader-election | false | true | Enable leader election |
20108
LEADER_ELECTION_NAMESPACE | --leader-election-namespace | "" | devops | Leader election namespace. Default is the same as controller.
21109
NAMESPACES | --namespaces | "" | devops,default | Namespaces to watch. Default: watch all namespaces |
22-
MAX_CONCURRENT_RECONCILES | --max-concurrent-reconciles | 1 | 5 | Maximum concurrent reconciles per controller. This config covers all controllers. TODO maybe have a separate flag for each controller? |
23-
24-
25-
26-
110+
MAX_CONCURRENT_RECONCILES | --max-concurrent-reconciles | 1 | 5 | Maximum concurrent reconciles per controller. This config covers all controllers. |

api/v1beta1/database_types.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import (
55
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
66
)
77

8+
// Finalizer
9+
const (
10+
Finalizer = "infra.finalizers.doodle.com"
11+
)
12+
813
// Status conditions
914
const (
1015
DatabaseReadyConditionType = "DatabaseReady"
@@ -21,6 +26,7 @@ const (
2126
UserNotProvisionedReason = "UserNotProvisioned"
2227
UserProvisioningSuccessfulReason = "UserProvisioningSuccessful"
2328
CredentialsNotFoundReason = "CredentialsNotFound"
29+
CreateDatabaseFailedReason = "CreateDatabaseFailed"
2430
)
2531

2632
// DatabaseSpec defines the desired state of MongoDBDatabase
@@ -60,7 +66,7 @@ type SecretReference struct {
6066
PasswordField string `json:"passwordField"`
6167
}
6268

63-
// ConditionalResource is a resource with conditions
69+
// conditionalResource is a resource with conditions
6470
type conditionalResource interface {
6571
GetStatusConditions() *[]metav1.Condition
6672
}

api/v1beta1/groupversion_info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
// Package v1beta1 contains API Schema definitions for the infra v1beta1 API group
1818
// +kubebuilder:object:generate=true
19-
// +groupName=infra.doodle.com
19+
// +groupName=dbprovisioning.infra.doodle.com
2020
package v1beta1
2121

2222
import (
@@ -26,7 +26,7 @@ import (
2626

2727
var (
2828
// GroupVersion is group version used to register these objects
29-
GroupVersion = schema.GroupVersion{Group: "infra.doodle.com", Version: "v1beta1"}
29+
GroupVersion = schema.GroupVersion{Group: "dbprovisioning.infra.doodle.com", Version: "v1beta1"}
3030

3131
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
3232
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

api/v1beta1/mongodbdatabase_types.go

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ const (
2727
DEFAULT_MONGODB_ROOT_AUTHENTICATION_DATABASE = "admin"
2828
)
2929

30-
// Finalizer
31-
const (
32-
MongoSQLDatabaseControllerFinalizer = "infra.finalizers.doodle.com"
33-
)
34-
3530
// MongoDBDatabaseSpec defines the desired state of MongoDBDatabase
3631
type MongoDBDatabaseSpec struct {
3732
*DatabaseSpec `json:",inline"`
@@ -55,8 +50,8 @@ type MongoDBDatabaseStatus struct {
5550
// +kubebuilder:object:root=true
5651
// +kubebuilder:resource:shortName=mdb
5752
// +kubebuilder:subresource:status
58-
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Provisioned\")].status",description=""
59-
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Provisioned\")].message",description=""
53+
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"DatabaseReady\")].status",description=""
54+
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"DatabaseReady\")].message",description=""
6055
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
6156

6257
// MongoDBDatabase is the Schema for the mongodbs API
@@ -68,6 +63,22 @@ type MongoDBDatabase struct {
6863
Status MongoDBDatabaseStatus `json:"status,omitempty"`
6964
}
7065

66+
func (in *MongoDBDatabase) GetAddress() string {
67+
return in.Spec.Address
68+
}
69+
70+
func (in *MongoDBDatabase) GetRootSecret() *SecretReference {
71+
return in.Spec.RootSecret
72+
}
73+
74+
func (in *MongoDBDatabase) GetDatabaseName() string {
75+
if in.Spec.DatabaseName != "" {
76+
return in.Spec.DatabaseName
77+
}
78+
79+
return in.GetName()
80+
}
81+
7182
// +kubebuilder:object:root=true
7283

7384
// MongoDBDatabaseList contains a list of MongoDBDatabase
@@ -77,59 +88,41 @@ type MongoDBDatabaseList struct {
7788
Items []MongoDBDatabase `json:"items"`
7889
}
7990

80-
/*
81-
If object doesn't contain finalizer, set it and call update function 'updateF'.
82-
Only do this if object is not being deleted (judged by DeletionTimestamp being zero)
83-
*/
91+
// If object doesn't contain finalizer, set it and call update function 'updateF'.
92+
// Only do this if object is not being deleted (judged by DeletionTimestamp being zero)
8493
func (d *MongoDBDatabase) SetFinalizer(updateF func() error) error {
8594
if !d.ObjectMeta.DeletionTimestamp.IsZero() {
8695
return nil
8796
}
88-
if !stringutils.ContainsString(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer) {
89-
d.ObjectMeta.Finalizers = append(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer)
97+
if !stringutils.ContainsString(d.ObjectMeta.Finalizers, Finalizer) {
98+
d.ObjectMeta.Finalizers = append(d.ObjectMeta.Finalizers, Finalizer)
9099
return updateF()
91100
}
92101
return nil
93102
}
94103

95-
/*
96-
Finalize object if deletion timestamp is not zero (i.e. object is being deleted).
97-
Call finalize function 'finalizeF', which should handle finalization logic.
98-
Remove finalizer from the object (so that object can be deleted), and update by calling update function 'updateF'.
99-
*/
104+
// Finalize object if deletion timestamp is not zero (i.e. object is being deleted).
105+
// Call finalize function 'finalizeF', which should handle finalization logic.
106+
// Remove finalizer from the object (so that object can be deleted), and update by calling update function 'updateF'.
100107
func (d *MongoDBDatabase) Finalize(updateF func() error, finalizeF func() error) (bool, error) {
101108
if d.ObjectMeta.DeletionTimestamp.IsZero() {
102109
return false, nil
103110
}
104-
if stringutils.ContainsString(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer) {
111+
if stringutils.ContainsString(d.ObjectMeta.Finalizers, Finalizer) {
105112
_ = finalizeF()
106-
d.ObjectMeta.Finalizers = stringutils.RemoveString(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer)
113+
d.ObjectMeta.Finalizers = stringutils.RemoveString(d.ObjectMeta.Finalizers, Finalizer)
107114
return true, updateF()
108115
}
109116
return true, nil
110117
}
111118

112-
/*func (d *MongoDBDatabase) SetDefaults() error {
113-
if d.Spec.RootUsername == "" {
114-
d.Spec.RootUsername = DEFAULT_MONGODB_ROOT_USER
115-
}
116-
if d.Spec.RootAuthenticationDatabase == "" {
117-
d.Spec.RootAuthenticationDatabase = DEFAULT_MONGODB_ROOT_AUTHENTICATION_DATABASE
118-
}
119-
if d.Spec.RootSecretLookup.Name == "" {
120-
return errors.New("must specify root secret")
121-
}
122-
if d.Spec.RootSecretLookup.Field == "" {
123-
return errors.New("must specify root secret field")
124-
}
125-
if d.Spec.RootSecretLookup.Namespace == "" {
126-
d.Spec.RootSecretLookup.Namespace = d.ObjectMeta.Namespace
127-
}
128-
if d.Status.CredentialsStatus == nil || len(d.Status.CredentialsStatus) == 0 {
129-
d.Status.CredentialsStatus = make([]*CredentialStatus, 0)
119+
func (d *MongoDBDatabase) SetDefaults() error {
120+
if d.Spec.DatabaseName == "" {
121+
d.Spec.DatabaseName = d.GetName()
130122
}
123+
131124
return nil
132-
}*/
125+
}
133126

134127
func init() {
135128
SchemeBuilder.Register(&MongoDBDatabase{}, &MongoDBDatabaseList{})

0 commit comments

Comments
 (0)