Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions internal/controller/databaserequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
"github.com/uselagoon/dbaas-controller/api/v1alpha1"
crdv1alpha1 "github.com/uselagoon/dbaas-controller/api/v1alpha1"
"github.com/uselagoon/dbaas-controller/internal/database"
)
Expand Down Expand Up @@ -187,7 +186,8 @@ func (r *DatabaseRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// Note at the moment we only have one "primary" connection per database request
// Implementing additional users would require to extend the logic here
// check if the database request is already created and the secret and service exist
if databaseRequest.Spec.Type == mysqlType || databaseRequest.Spec.Type == postgresType {
switch databaseRequest.Spec.Type {
case mysqlType, postgresType:
logger.Info("Get relational database info")
// get the database info
var err error
Expand All @@ -196,9 +196,9 @@ func (r *DatabaseRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return r.handleError(
ctx, databaseRequest, fmt.Sprintf("get-%s-database-info", databaseRequest.Spec.Type), err, false)
}
} else if databaseRequest.Spec.Type == mongodbType {
case mongodbType:
logger.Info("Get mongodb database info")
} else {
default:
logger.Error(ErrInvalidDatabaseType, "Unsupported database type", "type", databaseRequest.Spec.Type)
}
}
Expand Down Expand Up @@ -453,18 +453,19 @@ func (r *DatabaseRequestReconciler) deleteDatabase(
// handle deletion logic
logger := log.FromContext(ctx)
if databaseRequest.Spec.Seed == nil && databaseRequest.Spec.DropDatabaseOnDelete {
if databaseRequest.Spec.Type == mysqlType || databaseRequest.Spec.Type == postgresType {
switch databaseRequest.Spec.Type {
case mysqlType, postgresType:
// handle relational database deletion
// Note at the moment we only have one "primary" connection per database request
// Implementing additional users would require to extend the logic here
logger.Info("Dropping relational database")
if err := r.relDBDeletion(ctx, databaseRequest); err != nil {
return r.handleError(ctx, databaseRequest, fmt.Sprintf("%s-drop", databaseRequest.Spec.Type), err, false)
}
} else if databaseRequest.Spec.Type == mongodbType {
case mongodbType:
// handle mongodb deletion
logger.Info("Dropping MongoDB database")
} else {
default:
// this should never happen, but just in case
logger.Error(ErrInvalidDatabaseType, "Unsupported database type", "type", databaseRequest.Spec.Type)
return r.handleError(ctx, databaseRequest, "invalid-database-type", ErrInvalidDatabaseType, false)
Expand Down Expand Up @@ -515,7 +516,8 @@ func (r *DatabaseRequestReconciler) deleteDatabase(
func (r *DatabaseRequestReconciler) createDatabase(
ctx context.Context, databaseRequest *crdv1alpha1.DatabaseRequest) error {
logger := log.FromContext(ctx)
if databaseRequest.Spec.Type == mysqlType || databaseRequest.Spec.Type == postgresType {
switch databaseRequest.Spec.Type {
case mysqlType, postgresType:
// handle relational database creation
// Note at the moment we only have one "primary" connection per database request
// Implementing additional users would require to extend the logic here
Expand All @@ -529,9 +531,9 @@ func (r *DatabaseRequestReconciler) createDatabase(
if databaseRequest.Status.DatabaseInfo == nil {
return fmt.Errorf("%s db creation failed due to missing database info", databaseRequest.Spec.Type)
}
} else if databaseRequest.Spec.Type == mongodbType {
case mongodbType:
logger.Info("Creating MongoDB database")
} else {
default:
// this should never happen, but just in case
logger.Error(ErrInvalidDatabaseType, "Unsupported database type", "type", databaseRequest.Spec.Type)
return fmt.Errorf("failed to create database: %w", ErrInvalidDatabaseType)
Expand Down Expand Up @@ -612,7 +614,7 @@ type dbInfo struct {

// getSecretData returns the secret data for the database
func (m *dbInfo) getSecretData(name, serviceName string) map[string][]byte {
name = strings.ToUpper(strings.Replace(name, "-", "_", -1))
name = strings.ToUpper(strings.ReplaceAll(name, "-", "_"))
return map[string][]byte{
fmt.Sprintf("%s_USERNAME", strings.ToUpper(name)): []byte(m.userName),
fmt.Sprintf("%s_PASSWORD", strings.ToUpper(name)): []byte(m.password),
Expand Down Expand Up @@ -820,7 +822,7 @@ func (r *DatabaseRequestReconciler) relationalDatabaseOperation(
type seedDatabaseInfo struct {
dbInfo *dbInfo
conn *reldbConn
databaseProviderRef *v1alpha1.DatabaseConnectionReference
databaseProviderRef *crdv1alpha1.DatabaseConnectionReference
}

// relationalDatabaseInfoFromSeed finds the relational database provider based on the seed secret
Expand Down Expand Up @@ -848,7 +850,7 @@ func (r *DatabaseRequestReconciler) relationalDatabaseInfoFromSeed(
}

var connection *crdv1alpha1.Connection
var databaseProviderRef *v1alpha1.DatabaseConnectionReference
var databaseProviderRef *crdv1alpha1.DatabaseConnectionReference
for _, dbProvider := range dbProviders.Items {
if dbProvider.Spec.Scope == scope && dbProvider.Spec.Type == dbType {
for _, dbConnection := range dbProvider.Spec.Connections {
Expand Down
5 changes: 3 additions & 2 deletions internal/controller/relationaldatabaseprovider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,13 @@ type reldbConn struct {
// getDSN constructs the DSN string for the MySQL or PostgreSQL connection.
func (rc *reldbConn) getDSN(useDatabase bool) string {
dsn := ""
if rc.dbType == "mysql" {
switch rc.dbType {
case "mysql":
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/", rc.username, rc.password, rc.hostname, rc.port)
if useDatabase {
dsn += rc.name
}
} else if rc.dbType == "postgres" {
case "postgres":
dsn = fmt.Sprintf(
"host=%s port=%d user=%s password=%s sslmode=disable",
rc.hostname, rc.port, rc.username, rc.password,
Expand Down
45 changes: 25 additions & 20 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"fmt"
"math/rand"

_ "github.com/go-sql-driver/mysql"
md "github.com/go-sql-driver/mysql"
"github.com/lib/pq"
_ "github.com/lib/pq"

"sigs.k8s.io/controller-runtime/pkg/log"
)
Expand Down Expand Up @@ -117,7 +115,8 @@ func (ri *RelationalDatabaseImpl) Ping(ctx context.Context, dsn string, dbType s
}

if err := db.PingContext(ctx); err != nil {
if dbType == mysql {
switch dbType {
case mysql:
var driverErr *md.MySQLError
if errors.As(err, &driverErr) {
switch driverErr.Number {
Expand All @@ -129,7 +128,7 @@ func (ri *RelationalDatabaseImpl) Ping(ctx context.Context, dsn string, dbType s
return fmt.Errorf("failed to ping %s database: %w", dbType, err)
}
}
} else if dbType == postgres {
case postgres:
var driverErr *pq.Error
if errors.As(err, &driverErr) {
switch driverErr.Code {
Expand Down Expand Up @@ -175,17 +174,18 @@ func (ri *RelationalDatabaseImpl) Load(ctx context.Context, dsn string, dbType s
}

var totalLoad float64
if dbType == mysql {
switch dbType {
case mysql:
err = db.QueryRowContext(ctx, "SELECT data_length + index_length FROM information_schema.tables").Scan(&totalLoad)
if err != nil {
return 0, fmt.Errorf("load failed to get %s database load: %w", dbType, err)
}
} else if dbType == postgres {
case postgres:
err = db.QueryRowContext(ctx, "SELECT pg_database_size(current_database())").Scan(&totalLoad)
if err != nil {
return 0, fmt.Errorf("load failed to get %s database load: %w", dbType, err)
}
} else {
default:
return 0, fmt.Errorf("load failed to get %s database load: unsupported dbType", dbType)
}
// convert bytes to MB
Expand All @@ -204,7 +204,8 @@ func (ri *RelationalDatabaseImpl) Initialize(ctx context.Context, dsn string, db
return fmt.Errorf("initialize failed to open %s database: %w", dbType, err)
}

if dbType == mysql {
switch dbType {
case mysql:
_, err = db.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS dbaas_controller")
if err != nil {
return fmt.Errorf("initialize failed to create %s database: %w", dbType, err)
Expand All @@ -228,7 +229,7 @@ func (ri *RelationalDatabaseImpl) Initialize(ctx context.Context, dsn string, db
if err != nil {
return fmt.Errorf("initialize failed to create %s table: %w", dbType, err)
}
} else if dbType == postgres {
case postgres:
_, err := db.ExecContext(ctx, "CREATE SCHEMA IF NOT EXISTS dbaas_controller")
if err != nil {
return fmt.Errorf("initialize failed to create %s database: %w", dbType, err)
Expand All @@ -247,7 +248,7 @@ func (ri *RelationalDatabaseImpl) Initialize(ctx context.Context, dsn string, db
if err != nil {
return fmt.Errorf("initialize failed to create %s table: %w", dbType, err)
}
} else {
default:
return fmt.Errorf("initialize failed to initialize %s database: unsupported dbType", dbType)
}

Expand All @@ -267,7 +268,8 @@ func (ri *RelationalDatabaseImpl) CreateDatabase(
}

var info RelationalDatabaseInfo
if dbType == mysql {
switch dbType {
case mysql:
info, err = ri.databaseInfoMySQL(ctx, dsn, name, namespace)
if err != nil {
return info, fmt.Errorf("create %s database failed to get database info: %w", dbType, err)
Expand Down Expand Up @@ -301,7 +303,7 @@ func (ri *RelationalDatabaseImpl) CreateDatabase(
if err != nil {
return info, fmt.Errorf("create %s database error flushing privileges: %w", dbType, err)
}
} else if dbType == postgres {
case postgres:
info, err = ri.databaseInfoPostgreSQL(ctx, dsn, name, namespace)
if err != nil {
return info, fmt.Errorf("create database failed to get %s database info: %w", dbType, err)
Expand Down Expand Up @@ -342,7 +344,7 @@ func (ri *RelationalDatabaseImpl) CreateDatabase(
return info, fmt.Errorf(
"create %s database error in grant privileges in database `%s`: %w", dbType, info.Dbname, err)
}
} else {
default:
return RelationalDatabaseInfo{}, fmt.Errorf(
"create database failed to create %s database: unsupported dbType", dbType)
}
Expand All @@ -359,7 +361,8 @@ func (ri *RelationalDatabaseImpl) DropDatabase(ctx context.Context, dsn, name, n
}

info := RelationalDatabaseInfo{}
if dbType == mysql {
switch dbType {
case mysql:
info, err = ri.databaseInfoMySQL(ctx, dsn, name, namespace)
if err != nil {
return fmt.Errorf("drop database failed to get database info: %w", err)
Expand All @@ -379,7 +382,7 @@ func (ri *RelationalDatabaseImpl) DropDatabase(ctx context.Context, dsn, name, n
if err != nil {
return fmt.Errorf("drop database failed to flush privileges: %w", err)
}
} else if dbType == postgres {
case postgres:
info, err = ri.databaseInfoPostgreSQL(ctx, dsn, name, namespace)
if err != nil {
return fmt.Errorf("drop database failed to get database info: %w", err)
Expand All @@ -404,7 +407,7 @@ func (ri *RelationalDatabaseImpl) DropDatabase(ctx context.Context, dsn, name, n
if err != nil {
return fmt.Errorf("drop database failed to drop user: %w", err)
}
} else {
default:
return fmt.Errorf("drop database failed to drop %s database: unsupported dbType", dbType)
}
return nil
Expand Down Expand Up @@ -575,9 +578,10 @@ func (ri *RelationalDatabaseImpl) GetDatabaseInfo(
dsn, name, namespace, dbType string,
) (RelationalDatabaseInfo, error) {
log.FromContext(ctx).Info("Getting database", "dbType", dbType, "name", name, "namespace", namespace)
if dbType == "mysql" {
switch dbType {
case mysql:
return ri.databaseInfoMySQL(ctx, dsn, name, namespace)
} else if dbType == "postgres" {
case postgres:
return ri.databaseInfoPostgreSQL(ctx, dsn, name, namespace)
}
return RelationalDatabaseInfo{}, fmt.Errorf("get database failed to get %s database: unsupported dbType", dbType)
Expand All @@ -590,9 +594,10 @@ func (ri *RelationalDatabaseImpl) SetDatabaseInfo(
info RelationalDatabaseInfo,
) error {
log.FromContext(ctx).Info("Setting database", "dbType", dbType, "name", name, "namespace", namespace)
if dbType == "mysql" {
switch dbType {
case mysql:
return ri.insertUserInfoIntoMysql(ctx, dsn, name, namespace, info)
} else if dbType == "postgres" {
case postgres:
return ri.insertUserInfoIntoPostgreSQL(ctx, dsn, name, namespace, info)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ var _ = Describe("controller", Ordered, func() {
})

// uncomment to debug ...
//time.Sleep(15 * time.Minute)
// time.Sleep(15 * time.Minute)

})

Expand Down
18 changes: 9 additions & 9 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"os/exec"
"strings"

. "github.com/onsi/ginkgo/v2" //nolint:golint,revive
"github.com/onsi/ginkgo/v2"
)

const (
Expand All @@ -38,7 +38,7 @@ const (
)

func warnError(err error) {
fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
fmt.Fprintf(ginkgo.GinkgoWriter, "warning: %v\n", err)
}

// InstallRelationalDatabases installs both MySQL and PostgreSQL pods to be used for testing.
Expand All @@ -51,7 +51,7 @@ func InstallRelationalDatabases() error {
for _, yaml := range []string{mysqlYaml, postgresYaml} {
cmd := exec.Command("kubectl", "apply", "-f", yaml)
cmd.Dir = dir
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
go func() {
_, err := Run(cmd)
errChan <- err
Expand All @@ -73,7 +73,7 @@ func InstallMongoDB() error {
}
cmd := exec.Command("kubectl", "apply", "-f", "test/e2e/testdata/mongodb.yaml")
cmd.Dir = dir
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
_, err = Run(cmd)
return err
}
Expand All @@ -88,7 +88,7 @@ func UninstallRelationalDatabases() {
for _, yaml := range []string{mysqlYaml, postgresYaml} {
cmd := exec.Command("kubectl", "delete", "-f", yaml)
cmd.Dir = dir
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
go func() {
_, err := Run(cmd)
errChan <- err
Expand Down Expand Up @@ -122,7 +122,7 @@ func InstallMySQL() error {
}
cmd := exec.Command("kubectl", "apply", "-f", mysqlYaml)
cmd.Dir = dir
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
_, err = Run(cmd)
// Note that we don't wait for the pod to be ready here. This is a good test for the controller
// to see if it can handle mysql server not being ready.
Expand Down Expand Up @@ -156,12 +156,12 @@ func Run(cmd *exec.Cmd) ([]byte, error) {
cmd.Dir = dir

if err := os.Chdir(cmd.Dir); err != nil {
fmt.Fprintf(GinkgoWriter, "chdir dir: %s\n", err)
fmt.Fprintf(ginkgo.GinkgoWriter, "chdir dir: %s\n", err)
}

cmd.Env = append(os.Environ(), "GO111MODULE=on")
command := strings.Join(cmd.Args, " ")
fmt.Fprintf(GinkgoWriter, "running: %s\n", command)
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s\n", command)
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
Expand Down Expand Up @@ -239,6 +239,6 @@ func GetProjectDir() (string, error) {
if err != nil {
return wd, err
}
wd = strings.Replace(wd, "/test/e2e", "", -1)
wd = strings.ReplaceAll(wd, "/test/e2e", "")
return wd, nil
}