Skip to content

Commit 917fc65

Browse files
Merge pull request #22 from uselagoon/address-linting
chore: address linting issues
2 parents b40a5af + cfb96c7 commit 917fc65

File tree

5 files changed

+53
-45
lines changed

5 files changed

+53
-45
lines changed

internal/controller/databaserequest_controller.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141

4242
"github.com/google/go-cmp/cmp"
4343
"github.com/prometheus/client_golang/prometheus"
44-
"github.com/uselagoon/dbaas-controller/api/v1alpha1"
4544
crdv1alpha1 "github.com/uselagoon/dbaas-controller/api/v1alpha1"
4645
"github.com/uselagoon/dbaas-controller/internal/database"
4746
)
@@ -187,7 +186,8 @@ func (r *DatabaseRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ
187186
// Note at the moment we only have one "primary" connection per database request
188187
// Implementing additional users would require to extend the logic here
189188
// check if the database request is already created and the secret and service exist
190-
if databaseRequest.Spec.Type == mysqlType || databaseRequest.Spec.Type == postgresType {
189+
switch databaseRequest.Spec.Type {
190+
case mysqlType, postgresType:
191191
logger.Info("Get relational database info")
192192
// get the database info
193193
var err error
@@ -196,9 +196,9 @@ func (r *DatabaseRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ
196196
return r.handleError(
197197
ctx, databaseRequest, fmt.Sprintf("get-%s-database-info", databaseRequest.Spec.Type), err, false)
198198
}
199-
} else if databaseRequest.Spec.Type == mongodbType {
199+
case mongodbType:
200200
logger.Info("Get mongodb database info")
201-
} else {
201+
default:
202202
logger.Error(ErrInvalidDatabaseType, "Unsupported database type", "type", databaseRequest.Spec.Type)
203203
}
204204
}
@@ -453,18 +453,19 @@ func (r *DatabaseRequestReconciler) deleteDatabase(
453453
// handle deletion logic
454454
logger := log.FromContext(ctx)
455455
if databaseRequest.Spec.Seed == nil && databaseRequest.Spec.DropDatabaseOnDelete {
456-
if databaseRequest.Spec.Type == mysqlType || databaseRequest.Spec.Type == postgresType {
456+
switch databaseRequest.Spec.Type {
457+
case mysqlType, postgresType:
457458
// handle relational database deletion
458459
// Note at the moment we only have one "primary" connection per database request
459460
// Implementing additional users would require to extend the logic here
460461
logger.Info("Dropping relational database")
461462
if err := r.relDBDeletion(ctx, databaseRequest); err != nil {
462463
return r.handleError(ctx, databaseRequest, fmt.Sprintf("%s-drop", databaseRequest.Spec.Type), err, false)
463464
}
464-
} else if databaseRequest.Spec.Type == mongodbType {
465+
case mongodbType:
465466
// handle mongodb deletion
466467
logger.Info("Dropping MongoDB database")
467-
} else {
468+
default:
468469
// this should never happen, but just in case
469470
logger.Error(ErrInvalidDatabaseType, "Unsupported database type", "type", databaseRequest.Spec.Type)
470471
return r.handleError(ctx, databaseRequest, "invalid-database-type", ErrInvalidDatabaseType, false)
@@ -515,7 +516,8 @@ func (r *DatabaseRequestReconciler) deleteDatabase(
515516
func (r *DatabaseRequestReconciler) createDatabase(
516517
ctx context.Context, databaseRequest *crdv1alpha1.DatabaseRequest) error {
517518
logger := log.FromContext(ctx)
518-
if databaseRequest.Spec.Type == mysqlType || databaseRequest.Spec.Type == postgresType {
519+
switch databaseRequest.Spec.Type {
520+
case mysqlType, postgresType:
519521
// handle relational database creation
520522
// Note at the moment we only have one "primary" connection per database request
521523
// Implementing additional users would require to extend the logic here
@@ -529,9 +531,9 @@ func (r *DatabaseRequestReconciler) createDatabase(
529531
if databaseRequest.Status.DatabaseInfo == nil {
530532
return fmt.Errorf("%s db creation failed due to missing database info", databaseRequest.Spec.Type)
531533
}
532-
} else if databaseRequest.Spec.Type == mongodbType {
534+
case mongodbType:
533535
logger.Info("Creating MongoDB database")
534-
} else {
536+
default:
535537
// this should never happen, but just in case
536538
logger.Error(ErrInvalidDatabaseType, "Unsupported database type", "type", databaseRequest.Spec.Type)
537539
return fmt.Errorf("failed to create database: %w", ErrInvalidDatabaseType)
@@ -612,7 +614,7 @@ type dbInfo struct {
612614

613615
// getSecretData returns the secret data for the database
614616
func (m *dbInfo) getSecretData(name, serviceName string) map[string][]byte {
615-
name = strings.ToUpper(strings.Replace(name, "-", "_", -1))
617+
name = strings.ToUpper(strings.ReplaceAll(name, "-", "_"))
616618
return map[string][]byte{
617619
fmt.Sprintf("%s_USERNAME", strings.ToUpper(name)): []byte(m.userName),
618620
fmt.Sprintf("%s_PASSWORD", strings.ToUpper(name)): []byte(m.password),
@@ -820,7 +822,7 @@ func (r *DatabaseRequestReconciler) relationalDatabaseOperation(
820822
type seedDatabaseInfo struct {
821823
dbInfo *dbInfo
822824
conn *reldbConn
823-
databaseProviderRef *v1alpha1.DatabaseConnectionReference
825+
databaseProviderRef *crdv1alpha1.DatabaseConnectionReference
824826
}
825827

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

850852
var connection *crdv1alpha1.Connection
851-
var databaseProviderRef *v1alpha1.DatabaseConnectionReference
853+
var databaseProviderRef *crdv1alpha1.DatabaseConnectionReference
852854
for _, dbProvider := range dbProviders.Items {
853855
if dbProvider.Spec.Scope == scope && dbProvider.Spec.Type == dbType {
854856
for _, dbConnection := range dbProvider.Spec.Connections {

internal/controller/relationaldatabaseprovider_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,13 @@ type reldbConn struct {
329329
// getDSN constructs the DSN string for the MySQL or PostgreSQL connection.
330330
func (rc *reldbConn) getDSN(useDatabase bool) string {
331331
dsn := ""
332-
if rc.dbType == "mysql" {
332+
switch rc.dbType {
333+
case "mysql":
333334
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/", rc.username, rc.password, rc.hostname, rc.port)
334335
if useDatabase {
335336
dsn += rc.name
336337
}
337-
} else if rc.dbType == "postgres" {
338+
case "postgres":
338339
dsn = fmt.Sprintf(
339340
"host=%s port=%d user=%s password=%s sslmode=disable",
340341
rc.hostname, rc.port, rc.username, rc.password,

internal/database/database.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import (
77
"fmt"
88
"math/rand"
99

10-
_ "github.com/go-sql-driver/mysql"
1110
md "github.com/go-sql-driver/mysql"
1211
"github.com/lib/pq"
13-
_ "github.com/lib/pq"
1412

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

119117
if err := db.PingContext(ctx); err != nil {
120-
if dbType == mysql {
118+
switch dbType {
119+
case mysql:
121120
var driverErr *md.MySQLError
122121
if errors.As(err, &driverErr) {
123122
switch driverErr.Number {
@@ -129,7 +128,7 @@ func (ri *RelationalDatabaseImpl) Ping(ctx context.Context, dsn string, dbType s
129128
return fmt.Errorf("failed to ping %s database: %w", dbType, err)
130129
}
131130
}
132-
} else if dbType == postgres {
131+
case postgres:
133132
var driverErr *pq.Error
134133
if errors.As(err, &driverErr) {
135134
switch driverErr.Code {
@@ -175,17 +174,18 @@ func (ri *RelationalDatabaseImpl) Load(ctx context.Context, dsn string, dbType s
175174
}
176175

177176
var totalLoad float64
178-
if dbType == mysql {
177+
switch dbType {
178+
case mysql:
179179
err = db.QueryRowContext(ctx, "SELECT data_length + index_length FROM information_schema.tables").Scan(&totalLoad)
180180
if err != nil {
181181
return 0, fmt.Errorf("load failed to get %s database load: %w", dbType, err)
182182
}
183-
} else if dbType == postgres {
183+
case postgres:
184184
err = db.QueryRowContext(ctx, "SELECT pg_database_size(current_database())").Scan(&totalLoad)
185185
if err != nil {
186186
return 0, fmt.Errorf("load failed to get %s database load: %w", dbType, err)
187187
}
188-
} else {
188+
default:
189189
return 0, fmt.Errorf("load failed to get %s database load: unsupported dbType", dbType)
190190
}
191191
// convert bytes to MB
@@ -204,7 +204,8 @@ func (ri *RelationalDatabaseImpl) Initialize(ctx context.Context, dsn string, db
204204
return fmt.Errorf("initialize failed to open %s database: %w", dbType, err)
205205
}
206206

207-
if dbType == mysql {
207+
switch dbType {
208+
case mysql:
208209
_, err = db.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS dbaas_controller")
209210
if err != nil {
210211
return fmt.Errorf("initialize failed to create %s database: %w", dbType, err)
@@ -228,7 +229,7 @@ func (ri *RelationalDatabaseImpl) Initialize(ctx context.Context, dsn string, db
228229
if err != nil {
229230
return fmt.Errorf("initialize failed to create %s table: %w", dbType, err)
230231
}
231-
} else if dbType == postgres {
232+
case postgres:
232233
_, err := db.ExecContext(ctx, "CREATE SCHEMA IF NOT EXISTS dbaas_controller")
233234
if err != nil {
234235
return fmt.Errorf("initialize failed to create %s database: %w", dbType, err)
@@ -247,7 +248,7 @@ func (ri *RelationalDatabaseImpl) Initialize(ctx context.Context, dsn string, db
247248
if err != nil {
248249
return fmt.Errorf("initialize failed to create %s table: %w", dbType, err)
249250
}
250-
} else {
251+
default:
251252
return fmt.Errorf("initialize failed to initialize %s database: unsupported dbType", dbType)
252253
}
253254

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

269270
var info RelationalDatabaseInfo
270-
if dbType == mysql {
271+
switch dbType {
272+
case mysql:
271273
info, err = ri.databaseInfoMySQL(ctx, dsn, name, namespace)
272274
if err != nil {
273275
return info, fmt.Errorf("create %s database failed to get database info: %w", dbType, err)
@@ -301,7 +303,7 @@ func (ri *RelationalDatabaseImpl) CreateDatabase(
301303
if err != nil {
302304
return info, fmt.Errorf("create %s database error flushing privileges: %w", dbType, err)
303305
}
304-
} else if dbType == postgres {
306+
case postgres:
305307
info, err = ri.databaseInfoPostgreSQL(ctx, dsn, name, namespace)
306308
if err != nil {
307309
return info, fmt.Errorf("create database failed to get %s database info: %w", dbType, err)
@@ -342,7 +344,7 @@ func (ri *RelationalDatabaseImpl) CreateDatabase(
342344
return info, fmt.Errorf(
343345
"create %s database error in grant privileges in database `%s`: %w", dbType, info.Dbname, err)
344346
}
345-
} else {
347+
default:
346348
return RelationalDatabaseInfo{}, fmt.Errorf(
347349
"create database failed to create %s database: unsupported dbType", dbType)
348350
}
@@ -359,7 +361,8 @@ func (ri *RelationalDatabaseImpl) DropDatabase(ctx context.Context, dsn, name, n
359361
}
360362

361363
info := RelationalDatabaseInfo{}
362-
if dbType == mysql {
364+
switch dbType {
365+
case mysql:
363366
info, err = ri.databaseInfoMySQL(ctx, dsn, name, namespace)
364367
if err != nil {
365368
return fmt.Errorf("drop database failed to get database info: %w", err)
@@ -379,7 +382,7 @@ func (ri *RelationalDatabaseImpl) DropDatabase(ctx context.Context, dsn, name, n
379382
if err != nil {
380383
return fmt.Errorf("drop database failed to flush privileges: %w", err)
381384
}
382-
} else if dbType == postgres {
385+
case postgres:
383386
info, err = ri.databaseInfoPostgreSQL(ctx, dsn, name, namespace)
384387
if err != nil {
385388
return fmt.Errorf("drop database failed to get database info: %w", err)
@@ -404,7 +407,7 @@ func (ri *RelationalDatabaseImpl) DropDatabase(ctx context.Context, dsn, name, n
404407
if err != nil {
405408
return fmt.Errorf("drop database failed to drop user: %w", err)
406409
}
407-
} else {
410+
default:
408411
return fmt.Errorf("drop database failed to drop %s database: unsupported dbType", dbType)
409412
}
410413
return nil
@@ -575,9 +578,10 @@ func (ri *RelationalDatabaseImpl) GetDatabaseInfo(
575578
dsn, name, namespace, dbType string,
576579
) (RelationalDatabaseInfo, error) {
577580
log.FromContext(ctx).Info("Getting database", "dbType", dbType, "name", name, "namespace", namespace)
578-
if dbType == "mysql" {
581+
switch dbType {
582+
case mysql:
579583
return ri.databaseInfoMySQL(ctx, dsn, name, namespace)
580-
} else if dbType == "postgres" {
584+
case postgres:
581585
return ri.databaseInfoPostgreSQL(ctx, dsn, name, namespace)
582586
}
583587
return RelationalDatabaseInfo{}, fmt.Errorf("get database failed to get %s database: unsupported dbType", dbType)
@@ -590,9 +594,10 @@ func (ri *RelationalDatabaseImpl) SetDatabaseInfo(
590594
info RelationalDatabaseInfo,
591595
) error {
592596
log.FromContext(ctx).Info("Setting database", "dbType", dbType, "name", name, "namespace", namespace)
593-
if dbType == "mysql" {
597+
switch dbType {
598+
case mysql:
594599
return ri.insertUserInfoIntoMysql(ctx, dsn, name, namespace, info)
595-
} else if dbType == "postgres" {
600+
case postgres:
596601
return ri.insertUserInfoIntoPostgreSQL(ctx, dsn, name, namespace, info)
597602
}
598603
return nil

test/e2e/e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ var _ = Describe("controller", Ordered, func() {
405405
})
406406

407407
// uncomment to debug ...
408-
//time.Sleep(15 * time.Minute)
408+
// time.Sleep(15 * time.Minute)
409409

410410
})
411411

test/utils/utils.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"os/exec"
2323
"strings"
2424

25-
. "github.com/onsi/ginkgo/v2" //nolint:golint,revive
25+
"github.com/onsi/ginkgo/v2"
2626
)
2727

2828
const (
@@ -38,7 +38,7 @@ const (
3838
)
3939

4040
func warnError(err error) {
41-
fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
41+
fmt.Fprintf(ginkgo.GinkgoWriter, "warning: %v\n", err)
4242
}
4343

4444
// InstallRelationalDatabases installs both MySQL and PostgreSQL pods to be used for testing.
@@ -51,7 +51,7 @@ func InstallRelationalDatabases() error {
5151
for _, yaml := range []string{mysqlYaml, postgresYaml} {
5252
cmd := exec.Command("kubectl", "apply", "-f", yaml)
5353
cmd.Dir = dir
54-
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
54+
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
5555
go func() {
5656
_, err := Run(cmd)
5757
errChan <- err
@@ -73,7 +73,7 @@ func InstallMongoDB() error {
7373
}
7474
cmd := exec.Command("kubectl", "apply", "-f", "test/e2e/testdata/mongodb.yaml")
7575
cmd.Dir = dir
76-
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
76+
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
7777
_, err = Run(cmd)
7878
return err
7979
}
@@ -88,7 +88,7 @@ func UninstallRelationalDatabases() {
8888
for _, yaml := range []string{mysqlYaml, postgresYaml} {
8989
cmd := exec.Command("kubectl", "delete", "-f", yaml)
9090
cmd.Dir = dir
91-
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
91+
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
9292
go func() {
9393
_, err := Run(cmd)
9494
errChan <- err
@@ -122,7 +122,7 @@ func InstallMySQL() error {
122122
}
123123
cmd := exec.Command("kubectl", "apply", "-f", mysqlYaml)
124124
cmd.Dir = dir
125-
fmt.Fprintf(GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
125+
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
126126
_, err = Run(cmd)
127127
// Note that we don't wait for the pod to be ready here. This is a good test for the controller
128128
// to see if it can handle mysql server not being ready.
@@ -156,12 +156,12 @@ func Run(cmd *exec.Cmd) ([]byte, error) {
156156
cmd.Dir = dir
157157

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

162162
cmd.Env = append(os.Environ(), "GO111MODULE=on")
163163
command := strings.Join(cmd.Args, " ")
164-
fmt.Fprintf(GinkgoWriter, "running: %s\n", command)
164+
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s\n", command)
165165
output, err := cmd.CombinedOutput()
166166
if err != nil {
167167
return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
@@ -239,6 +239,6 @@ func GetProjectDir() (string, error) {
239239
if err != nil {
240240
return wd, err
241241
}
242-
wd = strings.Replace(wd, "/test/e2e", "", -1)
242+
wd = strings.ReplaceAll(wd, "/test/e2e", "")
243243
return wd, nil
244244
}

0 commit comments

Comments
 (0)