Skip to content

Commit

Permalink
adding postgres e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Cadetg committed May 6, 2024
1 parent e1a6f26 commit 8db6483
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 276 deletions.
8 changes: 4 additions & 4 deletions api/v1alpha1/relationaldatabaseprovider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ type Connection struct {
type RelationalDatabaseProviderSpec struct {
//+kubebuilder:required
//+kubebuilder:validation:Required
//+kubebuilder:validation:Enum=mysql;postgresql
// Kind is the kind of the relational database provider
// it can be either "mysql" or "postgresql"
Kind string `json:"kind"`
//+kubebuilder:validation:Enum=mysql;postgres
// Type is the type of the relational database provider
// it can be either "mysql" or "postgres"
Type string `json:"type"`

//+kubebuilder:required
//+kubebuilder:validation:Required
Expand Down
18 changes: 9 additions & 9 deletions config/crd/bases/crd.lagoon.sh_relationaldatabaseproviders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ spec:
type: object
minItems: 1
type: array
kind:
description: |-
Kind is the kind of the relational database provider
it can be either "mysql" or "postgresql"
enum:
- mysql
- postgresql
type: string
scope:
default: development
description: |-
Expand All @@ -119,10 +111,18 @@ spec:
- development
- custom
type: string
type:
description: |-
Type is the type of the relational database provider
it can be either "mysql" or "postgres"
enum:
- mysql
- postgres
type: string
required:
- connections
- kind
- scope
- type
type: object
status:
description: RelationalDatabaseProviderStatus defines the observed state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
app.kubernetes.io/managed-by: kustomize
name: relationaldatabaseprovider-mysql-sample
spec:
kind: mysql
type: mysql
scope: development
connections:
- name: primary-test-mysql-connection
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/databaserequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func (r *DatabaseRequestReconciler) relationalDatabaseOperation(
}

conn := reldbConn{
kind: databaseRequest.Spec.Type,
dbType: databaseRequest.Spec.Type,
name: connection.Name,
hostname: connection.Hostname,
username: connection.Username,
Expand Down Expand Up @@ -679,7 +679,7 @@ func (r *DatabaseRequestReconciler) findRelationalDatabaseProvider(
var provider *crdv1alpha1.RelationalDatabaseProvider
var connName string
for _, dbProvider := range dbProviders.Items {
if dbProvider.Spec.Scope == databaseRequest.Spec.Scope {
if dbProvider.Spec.Scope == databaseRequest.Spec.Scope && dbProvider.Spec.Type == databaseRequest.Spec.Type {
log.FromContext(ctx).Info("Found provider", "provider", dbProvider.Name)
for _, dbConnection := range dbProvider.Spec.Connections {
if dbConnection.Enabled {
Expand All @@ -700,7 +700,7 @@ func (r *DatabaseRequestReconciler) findRelationalDatabaseProvider(
}

conn := reldbConn{
kind: databaseRequest.Spec.Type,
dbType: databaseRequest.Spec.Type,
name: dbConnection.Name,
hostname: dbConnection.Hostname,
username: dbConnection.Username,
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/databaserequest_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var _ = Describe("DatabaseRequest Controller", func() {
Name: dbMySQLProviderResource,
},
Spec: crdv1alpha1.RelationalDatabaseProviderSpec{
Kind: "mysql",
Type: "mysql",
Scope: "development",
Connections: []crdv1alpha1.Connection{
{
Expand Down
56 changes: 28 additions & 28 deletions internal/controller/relationaldatabaseprovider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
Name: "relationaldatabaseprovider_reconcile_error_total",
Help: "The total number of reconciled relational database providers errors",
},
[]string{"kind", "name", "scope", "error"},
[]string{"type", "name", "scope", "error"},
)

// promRelationalDatabaseProviderStatus is the gauge for the relational database provider status
Expand All @@ -58,7 +58,7 @@ var (
Name: "relationaldatabaseprovider_status",
Help: "The status of the relational database provider",
},
[]string{"kind", "name", "scope"},
[]string{"type", "name", "scope"},
)

// promRelationalDatabaseProviderConnectionVersion is the gauge for the relational database provider connection version
Expand All @@ -67,7 +67,7 @@ var (
Name: "relationaldatabaseprovider_connection_version",
Help: "The version of the relational database provider connection",
},
[]string{"kind", "name", "scope", "hostname", "username", "version"},
[]string{"type", "name", "scope", "hostname", "username", "version"},
)
)

Expand Down Expand Up @@ -103,7 +103,7 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
"", req.Name, "", "get-relationaldbprovider").Inc()
return ctrl.Result{}, err
}
logger = logger.WithValues("kind", instance.Spec.Kind, "scope", instance.Spec.Scope)
logger = logger.WithValues("type", instance.Spec.Type, "scope", instance.Spec.Scope)
if instance.DeletionTimestamp != nil && !instance.DeletionTimestamp.IsZero() {
// The object is being deleted
// To be discussed whether we need to delete all the database requests using this provider...
Expand Down Expand Up @@ -148,15 +148,15 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-empty-password", instance.Spec.Kind),
fmt.Sprintf("%s-empty-password", instance.Spec.Type),
fmt.Errorf(
"%s connection secret %s in namespace %s has empty password",
instance.Spec.Kind, secret.Name, secret.Namespace,
instance.Spec.Type, secret.Name, secret.Namespace,
),
)
}
conns = append(conns, reldbConn{
kind: instance.Spec.Kind,
dbType: instance.Spec.Type,
name: conn.Name,
hostname: conn.Hostname,
replicaHostnames: conn.ReplicaHostnames,
Expand All @@ -172,8 +172,8 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-unique-name-error", instance.Spec.Kind),
fmt.Errorf("%s database connections must have unique names", instance.Spec.Kind),
fmt.Sprintf("%s-unique-name-error", instance.Spec.Type),
fmt.Errorf("%s database connections must have unique names", instance.Spec.Type),
)
}

Expand All @@ -184,7 +184,7 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
// make a ping to the database to check if it's up and running and we can connect to it
// if not, we should return an error and set the status to 0
// Note we could periodically check the status of the database and update the status accordingly...
if err := r.RelDBClient.Ping(ctx, conn.getDSN(), instance.Spec.Kind); err != nil {
if err := r.RelDBClient.Ping(ctx, conn.getDSN(), instance.Spec.Type); err != nil {
errors = append(errors, err)
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Name: conn.name,
Expand All @@ -194,7 +194,7 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
})
continue
}
version, err := r.RelDBClient.Version(ctx, conn.getDSN(), instance.Spec.Kind)
version, err := r.RelDBClient.Version(ctx, conn.getDSN(), instance.Spec.Type)
if err != nil {
errors = append(errors, err)
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Expand All @@ -207,7 +207,7 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
}

// check if the database is initialized
err = r.RelDBClient.Initialize(ctx, conn.getDSN(), instance.Spec.Kind)
err = r.RelDBClient.Initialize(ctx, conn.getDSN(), instance.Spec.Type)
if err != nil {
errors = append(errors, err)
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Expand All @@ -220,7 +220,7 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
}

promRelationalDatabaseProviderConnectionVersion.WithLabelValues(
instance.Spec.Kind, req.Name, instance.Spec.Scope, conn.hostname, conn.username, version).Set(1)
instance.Spec.Type, req.Name, instance.Spec.Scope, conn.hostname, conn.username, version).Set(1)
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Name: conn.name,
Hostname: conn.hostname,
Expand All @@ -241,16 +241,16 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-connection-error", instance.Spec.Kind),
fmt.Errorf("failed to connect to any of the %s databases: %v", instance.Spec.Kind, errors),
fmt.Sprintf("%s-connection-error", instance.Spec.Type),
fmt.Errorf("failed to connect to any of the %s databases: %v", instance.Spec.Type, errors),
)
}
if !foundEnabledDatabase {
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-connection-not-any-enabled", instance.Spec.Kind),
fmt.Errorf("no enabled working %s database found", instance.Spec.Kind),
fmt.Sprintf("%s-connection-not-any-enabled", instance.Spec.Type),
fmt.Errorf("no enabled working %s database found", instance.Spec.Type),
)
}

Expand All @@ -264,13 +264,13 @@ func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, re
// update the status
if err := r.Status().Update(ctx, instance); err != nil {
promRelationalDatabaseProviderReconcileErrorCounter.WithLabelValues(
instance.Spec.Kind, req.Name, instance.Spec.Scope, "update-status").Inc()
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Kind, req.Name, instance.Spec.Scope).Set(0)
instance.Spec.Type, req.Name, instance.Spec.Scope, "update-status").Inc()
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Type, req.Name, instance.Spec.Scope).Set(0)
return ctrl.Result{}, err
}

r.Recorder.Event(instance, "Normal", "Reconciled", "RelationalDatabaseProvider reconciled")
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Kind, req.Name, instance.Spec.Scope).Set(1)
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Type, req.Name, instance.Spec.Scope).Set(1)
return ctrl.Result{}, nil
}

Expand All @@ -282,8 +282,8 @@ func (r *RelationalDatabaseProviderReconciler) handleError(
err error,
) (ctrl.Result, error) {
promRelationalDatabaseProviderReconcileErrorCounter.WithLabelValues(
instance.Spec.Kind, instance.Name, instance.Spec.Scope, promErr).Inc()
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Kind, instance.Name, instance.Spec.Scope).Set(0)
instance.Spec.Type, instance.Name, instance.Spec.Scope, promErr).Inc()
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Type, instance.Name, instance.Spec.Scope).Set(0)
r.Recorder.Event(instance, v1.EventTypeWarning, errTypeToEventReason(promErr), err.Error())

// set the status condition to false
Expand All @@ -297,7 +297,7 @@ func (r *RelationalDatabaseProviderReconciler) handleError(
// update the status
if err := r.Status().Update(ctx, instance); err != nil {
promRelationalDatabaseProviderReconcileErrorCounter.WithLabelValues(
instance.Spec.Kind, instance.Name, instance.Spec.Scope, "update-status").Inc()
instance.Spec.Type, instance.Name, instance.Spec.Scope, "update-status").Inc()
log.FromContext(ctx).Error(err, "Failed to update status")
}

Expand All @@ -306,7 +306,7 @@ func (r *RelationalDatabaseProviderReconciler) handleError(

// reldbConn is the connection to a MySQL or PostgreSQL database
type reldbConn struct {
kind string
dbType string
name string
hostname string
replicaHostnames []string
Expand All @@ -318,12 +318,12 @@ type reldbConn struct {

// getDSN constructs the DSN string for the MySQL or PostgreSQL connection.
func (rc *reldbConn) getDSN() string {
if rc.kind == "mysql" {
if rc.dbType == "mysql" {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/", rc.username, rc.password, rc.hostname, rc.port)
} else if rc.kind == "postgresql" {
} else if rc.dbType == "postgres" {
return fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
rc.hostname, rc.port, rc.username, rc.password, rc.name,
"host=%s port=%d user=%s password=%s sslmode=disable",
rc.hostname, rc.port, rc.username, rc.password,
)
} else {
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = Describe("RelationalDatabaseProvider Controller", func() {
relationaldatabaseprovider := &crdv1alpha1.RelationalDatabaseProvider{}

BeforeEach(func() {
By("creating the custom resource for the Kind RelationalDatabaseProvider")
By("creating the custom resource for the RelationalDatabaseProvider")
secret := &v1.Secret{}
err := k8sClient.Get(ctx, types.NamespacedName{
Name: "test-rel-db-provider-secret",
Expand Down Expand Up @@ -74,7 +74,7 @@ var _ = Describe("RelationalDatabaseProvider Controller", func() {
Namespace: "default",
},
Spec: crdv1alpha1.RelationalDatabaseProviderSpec{
Kind: "mysql",
Type: "mysql",
Scope: "custom",
Connections: []crdv1alpha1.Connection{
{
Expand Down
Loading

0 comments on commit 8db6483

Please sign in to comment.