Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove global logger from db package
Browse files Browse the repository at this point in the history
I am working on a integration test suite for the db package. And the
global logger is not an ideal approach in any case.

This PR moves the logger from package level to TinkDB

Signed-off-by: Gianluca Arbezzano <[email protected]>
Gianluca Arbezzano committed Dec 11, 2020

Unverified

This user has not yet uploaded their public signing key.
1 parent 6d9159b commit 02e4b71
Showing 6 changed files with 34 additions and 32 deletions.
24 changes: 17 additions & 7 deletions cmd/tink-server/main.go
Original file line number Diff line number Diff line change
@@ -2,14 +2,14 @@ package main

import (
"context"
"database/sql"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/packethost/pkg/log"
"github.com/tinkerbell/tink/client/listener"
"github.com/tinkerbell/tink/db"
rpcServer "github.com/tinkerbell/tink/grpc-server"
httpServer "github.com/tinkerbell/tink/http-server"
)
@@ -34,6 +34,22 @@ func main() {
errCh := make(chan error, 2)
facility := os.Getenv("FACILITY")

// TODO(gianarb): I moved this up becuase we need to be sure that both
// connection, the one used for the resources and the one used for
// listening to events and notification are coming in the same way.
// BUT we should be using the right flags
connInfo := fmt.Sprintf("dbname=%s user=%s password=%s sslmode=%s",
os.Getenv("PGDATABASE"),
os.Getenv("PGUSER"),
os.Getenv("PGPASSWORD"),
os.Getenv("PGSSLMODE"),
)

db, err := sql.Open("postgres", connInfo)
if err != nil {
logger.Error(err)
panic(err)
}
tinkDB := db.Connect(logger)

_, onlyMigration := os.LookupEnv("ONLY_MIGRATION")
@@ -48,12 +64,6 @@ func main() {
os.Exit(0)
}

connInfo := fmt.Sprintf("dbname=%s user=%s password=%s sslmode=%s",
os.Getenv("PGDATABASE"),
os.Getenv("PGUSER"),
os.Getenv("PGPASSWORD"),
os.Getenv("PGSSLMODE"),
)
err = listener.Init(connInfo)
if err != nil {
log.Fatal(err)
16 changes: 4 additions & 12 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ import (
pb "github.com/tinkerbell/tink/protos/workflow"
)

var logger log.Logger

// Database interface for tinkerbell database operations
type Database interface {
eventsers
@@ -71,17 +69,12 @@ type eventsers interface {
// TinkDB implements the Database interface
type TinkDB struct {
instance *sql.DB
logger log.Logger
}

// Connect returns a connection to postgres database
func Connect(lg log.Logger) *TinkDB {
logger = lg
db, err := sql.Open("postgres", "")
if err != nil {
logger.Error(err)
panic(err)
}
return &TinkDB{instance: db}
func Connect(db *sql.DB, lg log.Logger) *TinkDB {
return &TinkDB{instance: db, logger: lg}
}

func (t *TinkDB) Migrate() (int, error) {
@@ -100,7 +93,7 @@ func (t *TinkDB) CheckRequiredMigrations() (int, error) {
// PurgeEvents periodically checks the events table and
// purges the events that have passed the defined EVENTS_TTL.
func (t *TinkDB) PurgeEvents(errCh chan<- error) {
err := ev.Purge(t.instance, logger)
err := ev.Purge(t.instance, t.logger)
if err != nil {
errCh <- err
}
@@ -120,7 +113,6 @@ func get(ctx context.Context, db *sql.DB, query string, args ...interface{}) (st
buf := []byte{}
err := row.Scan(&buf)
if err != nil {
logger.Error(err)
return "", errors.Wrap(err, "SELECT")
}
return string(buf), nil
2 changes: 1 addition & 1 deletion db/events.go
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ func (d TinkDB) Events(req *events.WatchRequest, fn func(n informers.Notificatio
err = rows.Scan(&n.ID, &n.ResourceID, &n.ResourceType, &n.EventType, &n.CreatedAt, &n.Data)
if err != nil {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
return err
}
n.Prefix()
2 changes: 1 addition & 1 deletion db/hardware.go
Original file line number Diff line number Diff line change
@@ -167,7 +167,7 @@ func (d TinkDB) GetAll(fn func([]byte) error) error {
err = rows.Scan(&buf)
if err != nil {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
return err
}

6 changes: 3 additions & 3 deletions db/template.go
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string, delet
SELECT id, name, data
FROM template
WHERE
` + getCondition + ` AND
` + getCondition + ` AND
deleted_at IS NULL
`
} else {
@@ -86,7 +86,7 @@ func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string, delet
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
}
return "", "", "", err
}
@@ -143,7 +143,7 @@ func (d TinkDB) ListTemplates(filter string, fn func(id, n string, in, del *time
err = rows.Scan(&id, &name, &createdAt, &updatedAt)
if err != nil {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
return err
}

16 changes: 8 additions & 8 deletions db/workflow.go
Original file line number Diff line number Diff line change
@@ -258,7 +258,7 @@ func (d TinkDB) GetfromWfDataTable(ctx context.Context, req *pb.GetWorkflowDataR

if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
}

return []byte{}, nil
@@ -289,7 +289,7 @@ func (d TinkDB) GetWorkflowMetadata(ctx context.Context, req *pb.GetWorkflowData

if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT from workflow_data")
logger.Error(err)
d.logger.Error(err)
}

return []byte{}, nil
@@ -319,7 +319,7 @@ func (d TinkDB) GetWorkflowsForWorker(id string) ([]string, error) {
err = rows.Scan(&workerID)
if err != nil {
err = errors.Wrap(err, "SELECT from worflow_worker_map")
logger.Error(err)
d.logger.Error(err)
return nil, err
}
wfID = append(wfID, workerID)
@@ -350,7 +350,7 @@ func (d TinkDB) GetWorkflow(ctx context.Context, id string) (Workflow, error) {

if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
}

return Workflow{}, nil
@@ -422,7 +422,7 @@ func (d TinkDB) ListWorkflows(fn func(wf Workflow) error) error {
err = rows.Scan(&id, &tmp, &tar, &crAt, &upAt)
if err != nil {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
return err
}

@@ -541,7 +541,7 @@ func (d TinkDB) GetWorkflowContexts(ctx context.Context, wfID string) (*pb.Workf
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT from worflow_state")
logger.Error(err)
d.logger.Error(err)
}
return &pb.WorkflowContext{}, nil
}
@@ -567,7 +567,7 @@ func (d TinkDB) GetWorkflowActions(ctx context.Context, wfID string) (*pb.Workfl
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT from worflow_state")
logger.Error(err)
d.logger.Error(err)
}
return &pb.WorkflowActionList{}, nil
}
@@ -623,7 +623,7 @@ func (d TinkDB) ShowWorkflowEvents(wfID string, fn func(wfs *pb.WorkflowActionSt
err = rows.Scan(&id, &tName, &aName, &secs, &msg, &status, &evTime)
if err != nil {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
d.logger.Error(err)
return err
}
createdAt, _ := ptypes.TimestampProto(evTime)

0 comments on commit 02e4b71

Please sign in to comment.