Skip to content

Commit e984e2d

Browse files
committed
removing vault part / splitting users into their own crd
1 parent f12549f commit e984e2d

29 files changed

+1556
-1664
lines changed

api/v1beta1/database_types.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package v1beta1
2+
3+
import (
4+
apimeta "k8s.io/apimachinery/pkg/api/meta"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
// Status conditions
9+
const (
10+
ProvisionedCondition = "Provisioned"
11+
)
12+
13+
// Status reasons
14+
const (
15+
SecretNotFoundReason = "SecretNotFoundFailed"
16+
ConnectionFailedReason = "ConnectionFailed"
17+
DatabaseProvisioningFailedReason = "DatabaseProvisioningFailed"
18+
DatabaseProvisiningSuccessfulReason = "DatabaseProvisiningSuccessful"
19+
DatabaseNotFoundReason = "DatabaseNotFoundReason"
20+
UserNotProvisionedReason = "UserNotProvisioned"
21+
UserProvisioningSuccessfulReason = "UserProvisioningSuccessful"
22+
CredentialsNotFoundReason = "CredentialsNotFound"
23+
)
24+
25+
// DatabaseSpec defines the desired state of MongoDBDatabase
26+
type DatabaseSpec struct {
27+
// The name of the database, if not set the name is taken from metadata.name
28+
// +optional
29+
DatabaseName string `json:"databaseName"`
30+
31+
// The MongoDB URI
32+
// +required
33+
Address string `json:"address"`
34+
35+
// +required
36+
RootSecret *SecretReference `json:"rootSecret"`
37+
}
38+
39+
type DatabaseReference struct {
40+
Name string `json:"name"`
41+
}
42+
43+
type SecretReference struct {
44+
// +required
45+
Name string `json:"name"`
46+
47+
// +optional
48+
UserField string `json:"userField"`
49+
50+
// +required
51+
PasswordField string `json:"passwordField"`
52+
}
53+
54+
// ConditionalResource is a resource with conditions
55+
type conditionalResource interface {
56+
GetStatusConditions() *[]metav1.Condition
57+
}
58+
59+
func NotProvisioned(in conditionalResource, reason, message string) {
60+
setResourceCondition(in, ProvisionedCondition, metav1.ConditionFalse, reason, message)
61+
}
62+
63+
func Provisioned(in conditionalResource, reason, message string) {
64+
setResourceCondition(in, ProvisionedCondition, metav1.ConditionTrue, reason, message)
65+
}
66+
67+
// setResourceCondition sets the given condition with the given status,
68+
// reason and message on a resource.
69+
func setResourceCondition(resource conditionalResource, condition string, status metav1.ConditionStatus, reason, message string) {
70+
conditions := resource.GetStatusConditions()
71+
72+
newCondition := metav1.Condition{
73+
Type: condition,
74+
Status: status,
75+
Reason: reason,
76+
Message: message,
77+
}
78+
79+
apimeta.SetStatusCondition(conditions, newCondition)
80+
}

api/v1beta1/mongodbdatabase_types.go

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"errors"
2120
"github.com/doodlescheduling/kubedb/common/stringutils"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
)
@@ -33,37 +32,22 @@ const (
3332
MongoSQLDatabaseControllerFinalizer = "infra.finalizers.doodle.com"
3433
)
3534

36-
type MongoDBDatabaseRootSecretLookup struct {
37-
Name string `json:"name"`
38-
Namespace string `json:"namespace"`
39-
Field string `json:"field"`
40-
}
41-
42-
type MongoDBDatabaseCredentials []MongoDBDatabaseCredential
43-
type MongoDBDatabaseCredential struct {
44-
UserName string `json:"username"`
45-
Vault Vault `json:"vault"`
46-
}
47-
4835
// MongoDBDatabaseSpec defines the desired state of MongoDBDatabase
49-
// IMPORTANT: Run "make" to regenerate code after modifying this file
5036
type MongoDBDatabaseSpec struct {
51-
DatabaseName string `json:"databaseName"`
52-
HostName string `json:"hostName"`
53-
// +optional
54-
RootUsername string `json:"rootUsername"`
55-
// +optional
56-
RootAuthenticationDatabase string `json:"rootAuthDatabase"`
57-
RootSecretLookup MongoDBDatabaseRootSecretLookup `json:"rootSecretLookup"`
58-
Credentials MongoDBDatabaseCredentials `json:"credentials"`
37+
*DatabaseSpec `json:",inline"`
38+
}
39+
40+
// GetStatusConditions returns a pointer to the Status.Conditions slice
41+
func (in *MongoDBDatabase) GetStatusConditions() *[]metav1.Condition {
42+
return &in.Status.Conditions
5943
}
6044

6145
// MongoDBDatabaseStatus defines the observed state of MongoDBDatabase
6246
// IMPORTANT: Run "make" to regenerate code after modifying this file
6347
type MongoDBDatabaseStatus struct {
64-
DatabaseStatus DatabaseStatus `json:"database"`
65-
CredentialsStatus CredentialsStatus `json:"credentials"`
66-
LastUpdateTime *metav1.Time `json:"lastUpdateTime"`
48+
// Conditions holds the conditions for the VaultBinding.
49+
// +optional
50+
Conditions []metav1.Condition `json:"conditions,omitempty"`
6751
}
6852

6953
// +kubebuilder:object:root=true
@@ -87,34 +71,6 @@ type MongoDBDatabaseList struct {
8771
Items []MongoDBDatabase `json:"items"`
8872
}
8973

90-
/*
91-
Alignes credentials status with spec by removing unneeded statuses. Mutates the original.
92-
Returns removed statuses.
93-
*/
94-
func (d *MongoDBDatabase) RemoveUnneededCredentialsStatus() *CredentialsStatus {
95-
removedStatuses := make(CredentialsStatus, 0)
96-
statuses := &d.Status.CredentialsStatus
97-
for i := 0; i < len(*statuses); i++ {
98-
status := (*statuses)[i]
99-
found := false
100-
if status != nil {
101-
for _, credential := range d.Spec.Credentials {
102-
if credential.UserName == status.Username {
103-
found = true
104-
}
105-
}
106-
}
107-
if !found {
108-
removedStatuses = append(removedStatuses, status)
109-
s := append((*statuses)[:i], (*statuses)[i+1:]...)
110-
statuses = &s
111-
i--
112-
}
113-
}
114-
d.Status.CredentialsStatus = *statuses
115-
return &removedStatuses
116-
}
117-
11874
/*
11975
If object doesn't contain finalizer, set it and call update function 'updateF'.
12076
Only do this if object is not being deleted (judged by DeletionTimestamp being zero)
@@ -147,7 +103,7 @@ func (d *MongoDBDatabase) Finalize(updateF func() error, finalizeF func() error)
147103
return true, nil
148104
}
149105

150-
func (d *MongoDBDatabase) SetDefaults() error {
106+
/*func (d *MongoDBDatabase) SetDefaults() error {
151107
if d.Spec.RootUsername == "" {
152108
d.Spec.RootUsername = DEFAULT_MONGODB_ROOT_USER
153109
}
@@ -167,7 +123,7 @@ func (d *MongoDBDatabase) SetDefaults() error {
167123
d.Status.CredentialsStatus = make([]*CredentialStatus, 0)
168124
}
169125
return nil
170-
}
126+
}*/
171127

172128
func init() {
173129
SchemeBuilder.Register(&MongoDBDatabase{}, &MongoDBDatabaseList{})

api/v1beta1/mongodbuser_type.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"github.com/doodlescheduling/kubedb/common/stringutils"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
type MongoDBUserSpec struct {
25+
Database *DatabaseReference `json:"database"`
26+
Credentials *SecretReference `json:"credentials"`
27+
}
28+
29+
// GetStatusConditions returns a pointer to the Status.Conditions slice
30+
func (in *MongoDBUser) GetStatusConditions() *[]metav1.Condition {
31+
return &in.Status.Conditions
32+
}
33+
34+
// MongoDBUserStatus defines the observed state of MongoDBUser
35+
// IMPORTANT: Run "make" to regenerate code after modifying this file
36+
type MongoDBUserStatus struct {
37+
// Conditions holds the conditions for the VaultBinding.
38+
// +optional
39+
Conditions []metav1.Condition `json:"conditions,omitempty"`
40+
}
41+
42+
// +kubebuilder:object:root=true
43+
// +kubebuilder:subresource:status
44+
45+
// MongoDBUser is the Schema for the mongodbs API
46+
type MongoDBUser struct {
47+
metav1.TypeMeta `json:",inline"`
48+
metav1.ObjectMeta `json:"metadata,omitempty"`
49+
50+
Spec MongoDBUserSpec `json:"spec,omitempty"`
51+
Status MongoDBUserStatus `json:"status,omitempty"`
52+
}
53+
54+
// +kubebuilder:object:root=true
55+
56+
// MongoDBUserList contains a list of MongoDBUser
57+
type MongoDBUserList struct {
58+
metav1.TypeMeta `json:",inline"`
59+
metav1.ListMeta `json:"metadata,omitempty"`
60+
Items []MongoDBUser `json:"items"`
61+
}
62+
63+
/*
64+
If object doesn't contain finalizer, set it and call update function 'updateF'.
65+
Only do this if object is not being deleted (judged by DeletionTimestamp being zero)
66+
*/
67+
func (d *MongoDBUser) SetFinalizer(updateF func() error) error {
68+
if !d.ObjectMeta.DeletionTimestamp.IsZero() {
69+
return nil
70+
}
71+
if !stringutils.ContainsString(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer) {
72+
d.ObjectMeta.Finalizers = append(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer)
73+
return updateF()
74+
}
75+
return nil
76+
}
77+
78+
/*
79+
Finalize object if deletion timestamp is not zero (i.e. object is being deleted).
80+
Call finalize function 'finalizeF', which should handle finalization logic.
81+
Remove finalizer from the object (so that object can be deleted), and update by calling update function 'updateF'.
82+
*/
83+
func (d *MongoDBUser) Finalize(updateF func() error, finalizeF func() error) (bool, error) {
84+
if d.ObjectMeta.DeletionTimestamp.IsZero() {
85+
return false, nil
86+
}
87+
if stringutils.ContainsString(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer) {
88+
_ = finalizeF()
89+
d.ObjectMeta.Finalizers = stringutils.RemoveString(d.ObjectMeta.Finalizers, MongoSQLDatabaseControllerFinalizer)
90+
return true, updateF()
91+
}
92+
return true, nil
93+
}
94+
95+
func init() {
96+
SchemeBuilder.Register(&MongoDBUser{}, &MongoDBUserList{})
97+
}

api/v1beta1/postgresqldatabase_types.go

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"errors"
2120
"github.com/doodlescheduling/kubedb/common/stringutils"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
)
@@ -33,37 +32,22 @@ const (
3332
PostgreSQLDatabaseControllerFinalizer = "infra.finalizers.doodle.com"
3433
)
3534

36-
type PostgreSQLDatabaseRootSecretLookup struct {
37-
Name string `json:"name"`
38-
Namespace string `json:"namespace"`
39-
Field string `json:"field"`
40-
}
41-
42-
type PostgreSQLDatabaseCredentials []PostgreSQLDatabaseCredential
43-
type PostgreSQLDatabaseCredential struct {
44-
UserName string `json:"username"`
45-
Vault Vault `json:"vault"`
46-
}
47-
4835
// PostgreSQLDatabaseSpec defines the desired state of PostgreSQLDatabase
49-
// IMPORTANT: Run "make" to regenerate code after modifying this file
5036
type PostgreSQLDatabaseSpec struct {
51-
DatabaseName string `json:"databaseName"`
52-
HostName string `json:"hostName"`
53-
// +optional
54-
RootUsername string `json:"rootUsername"`
55-
// +optional
56-
RootAuthenticationDatabase string `json:"rootAuthDatabase"`
57-
RootSecretLookup PostgreSQLDatabaseRootSecretLookup `json:"rootSecretLookup"`
58-
Credentials PostgreSQLDatabaseCredentials `json:"credentials"`
37+
*DatabaseSpec `json:",inline"`
38+
}
39+
40+
// GetStatusConditions returns a pointer to the Status.Conditions slice
41+
func (in *PostgreSQLDatabase) GetStatusConditions() *[]metav1.Condition {
42+
return &in.Status.Conditions
5943
}
6044

6145
// PostgreSQLDatabaseStatus defines the observed state of PostgreSQLDatabase
6246
// IMPORTANT: Run "make" to regenerate code after modifying this file
6347
type PostgreSQLDatabaseStatus struct {
64-
DatabaseStatus DatabaseStatus `json:"database"`
65-
CredentialsStatus CredentialsStatus `json:"credentials"`
66-
LastUpdateTime *metav1.Time `json:"lastUpdateTime"`
48+
// Conditions holds the conditions for the VaultBinding.
49+
// +optional
50+
Conditions []metav1.Condition `json:"conditions,omitempty"`
6751
}
6852

6953
// +kubebuilder:object:root=true
@@ -87,34 +71,6 @@ type PostgreSQLDatabaseList struct {
8771
Items []PostgreSQLDatabase `json:"items"`
8872
}
8973

90-
/*
91-
Alignes credentials status with spec by removing unneeded statuses. Mutates the original.
92-
Returns removed statuses.
93-
*/
94-
func (d *PostgreSQLDatabase) RemoveUnneededCredentialsStatus() *CredentialsStatus {
95-
removedStatuses := make(CredentialsStatus, 0)
96-
statuses := &d.Status.CredentialsStatus
97-
for i := 0; i < len(*statuses); i++ {
98-
status := (*statuses)[i]
99-
found := false
100-
if status != nil {
101-
for _, credential := range d.Spec.Credentials {
102-
if credential.UserName == status.Username {
103-
found = true
104-
}
105-
}
106-
}
107-
if !found {
108-
removedStatuses = append(removedStatuses, status)
109-
s := append((*statuses)[:i], (*statuses)[i+1:]...)
110-
statuses = &s
111-
i--
112-
}
113-
}
114-
d.Status.CredentialsStatus = *statuses
115-
return &removedStatuses
116-
}
117-
11874
/*
11975
If object doesn't contain finalizer, set it and call update function 'updateF'.
12076
Only do this if object is not being deleted (judged by DeletionTimestamp being zero)
@@ -147,7 +103,7 @@ func (d *PostgreSQLDatabase) Finalize(updateF func() error, finalizeF func() err
147103
return true, nil
148104
}
149105

150-
func (d *PostgreSQLDatabase) SetDefaults() error {
106+
/*func (d *PostgreSQLDatabase) SetDefaults() error {
151107
if d.Spec.RootUsername == "" {
152108
d.Spec.RootUsername = DEFAULT_POSTGRESQL_ROOT_USER
153109
}
@@ -167,7 +123,7 @@ func (d *PostgreSQLDatabase) SetDefaults() error {
167123
d.Status.CredentialsStatus = make([]*CredentialStatus, 0)
168124
}
169125
return nil
170-
}
126+
}*/
171127

172128
func init() {
173129
SchemeBuilder.Register(&PostgreSQLDatabase{}, &PostgreSQLDatabaseList{})

0 commit comments

Comments
 (0)