Skip to content

Commit 8be552b

Browse files
committed
feat: improve logging
1 parent a526aaf commit 8be552b

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

controllers/database_controller.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func (r *DatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
5454
return ctrl.Result{}, err
5555
}
5656

57+
// add loaded database details
58+
log = log.WithValues("database", database.Spec.Database, "username", database.Spec.Username)
59+
5760
// use maximum of 3 seconds to connect
5861
openCtx, cancel := context.WithTimeout(ctx, time.Duration(time.Second*3))
5962
defer cancel()
@@ -102,18 +105,18 @@ func (r *DatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
102105
// Create database if necessary
103106
hasDatabase, err := db.HasDatabase(ctx, database.Spec.Database)
104107
if err != nil {
105-
log.Error(err, "Couldn't check if database '", database.Spec.Database, "' exists")
108+
log.Error(err, "Couldn't check if database exists")
106109
return ctrl.Result{}, err
107110
} else if !hasDatabase {
108-
log.Info("Creating new database '", database.Spec.Database, "'")
111+
log.Info("Creating new database '" + database.Spec.Database + "'")
109112

110113
err = db.CreateDatabase(ctx, database.Spec.Database)
111114
if err != nil {
112115
log.Error(err, "Can't create database")
113116
return ctrl.Result{}, err
114117
}
115118

116-
log.Info("Created database '", database.Spec.Database, "'")
119+
log.Info("Created database '" + database.Spec.Database + "'")
117120
}
118121

119122
// Create database user with full access if necessary
@@ -122,14 +125,14 @@ func (r *DatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
122125
log.Error(err, "Can't check if user has access to database")
123126
return ctrl.Result{}, err
124127
} else if !hasDatabaseUserWithAccess {
125-
log.Info("Creating new user '" + database.Spec.Username + "' and granting access to database '" + database.Spec.Database + "'")
128+
log.Info("Creating new user and granting access to database")
126129

127130
err = db.CreateDatabaseUser(ctx, database.Spec.Database, database.Spec.Username, database.Spec.Password)
128131
if err != nil {
129132
log.Error(err, "Can't create database user with access to database")
130133
return ctrl.Result{}, err
131134
}
132-
log.Info("Created user '" + database.Spec.Username + "' and granted access to database '" + database.Spec.Database + "'")
135+
log.Info("Created user and granted access to database")
133136
}
134137

135138
return ctrl.Result{}, nil
@@ -177,27 +180,27 @@ func (r *DatabaseReconciler) finalizeDatabase(ctx context.Context, log logr.Logg
177180
if err != nil {
178181
return err
179182
} else if hasDatabaseUserWithAccess {
180-
log.Info("Removing user '" + database.Spec.Username + "' and revoking access to the database '" + database.Spec.Database + "'")
183+
log.Info("Removing user and revoking access to the database")
181184

182185
err = db.DeleteDatabaseUser(ctx, database.Spec.Database, database.Spec.Username)
183186
if err != nil {
184187
return err
185188
}
186-
log.Info("Removed database user '" + database.Spec.Username + "' and revoked access to the database '" + database.Spec.Database + "'")
189+
log.Info("Removed database user and revoked access to the database")
187190
}
188191

189192
// remove database if it exists
190193
hasDatabase, err := db.HasDatabase(ctx, database.Spec.Database)
191194
if err != nil {
192195
return err
193196
} else if hasDatabase {
194-
log.Info("Removing database '" + database.Spec.Database + "'")
197+
log.Info("Removing database")
195198

196199
err = db.DeleteDatabase(ctx, database.Spec.Database)
197200
if err != nil {
198201
return err
199202
}
200-
log.Info("Removed database '" + database.Spec.Database + "'")
203+
log.Info("Removed database")
201204
}
202205

203206
return nil

0 commit comments

Comments
 (0)