Skip to content

Commit 49e37b9

Browse files
committed
switch to structured logging
update logging fix name update test logging update logging update logging
1 parent 6630077 commit 49e37b9

File tree

6 files changed

+152
-116
lines changed

6 files changed

+152
-116
lines changed

controllers/manila_controller.go

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func (r *ManilaReconciler) GetKClient() kubernetes.Interface {
6565
return r.Kclient
6666
}
6767

68-
// GetLogger -
69-
func (r *ManilaReconciler) GetLogger() logr.Logger {
70-
return r.Log
68+
// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields
69+
func (r *ManilaReconciler) GetLogger(ctx context.Context) logr.Logger {
70+
return log.FromContext(ctx).WithName("Controllers").WithName("Manila")
7171
}
7272

7373
// GetScheme -
@@ -79,7 +79,6 @@ func (r *ManilaReconciler) GetScheme() *runtime.Scheme {
7979
type ManilaReconciler struct {
8080
client.Client
8181
Kclient kubernetes.Interface
82-
Log logr.Logger
8382
Scheme *runtime.Scheme
8483
}
8584

@@ -114,7 +113,7 @@ type ManilaReconciler struct {
114113

115114
// Reconcile -
116115
func (r *ManilaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
117-
_ = log.FromContext(ctx)
116+
Log := r.GetLogger(ctx)
118117

119118
instance := &manilav1beta1.Manila{}
120119
err := r.Client.Get(ctx, req.NamespacedName, instance)
@@ -130,7 +129,7 @@ func (r *ManilaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
130129
r.Client,
131130
r.Kclient,
132131
r.Scheme,
133-
r.Log,
132+
Log,
134133
)
135134
if err != nil {
136135
return ctrl.Result{}, err
@@ -205,7 +204,9 @@ func (r *ManilaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
205204
}
206205

207206
// SetupWithManager sets up the controller with the Manager.
208-
func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error {
207+
func (r *ManilaReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
208+
Log := r.GetLogger(ctx)
209+
209210
// transportURLSecretFn - Watch for changes made to the secret associated with the RabbitMQ
210211
// TransportURL created and used by Manila CRs. Watch functions return a list of namespace-scoped
211212
// CRs that then get fed to the reconciler. Hence, in this case, we need to know the name of the
@@ -225,8 +226,8 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error {
225226
listOpts := []client.ListOption{
226227
client.InNamespace(o.GetNamespace()),
227228
}
228-
if err := r.Client.List(context.Background(), manilas, listOpts...); err != nil {
229-
r.Log.Error(err, "Unable to retrieve Manila CRs %v")
229+
if err := r.Client.List(ctx, manilas, listOpts...); err != nil {
230+
Log.Error(err, "Unable to retrieve Manila CRs %v")
230231
return nil
231232
}
232233

@@ -239,7 +240,7 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error {
239240
Namespace: o.GetNamespace(),
240241
Name: cr.Name,
241242
}
242-
r.Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Manila CR %s", o.GetName(), cr.Name))
243+
Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Manila CR %s", o.GetName(), cr.Name))
243244
result = append(result, reconcile.Request{NamespacedName: name})
244245
}
245246
}
@@ -271,7 +272,9 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error {
271272
}
272273

273274
func (r *ManilaReconciler) reconcileDelete(ctx context.Context, instance *manilav1beta1.Manila, helper *helper.Helper) (ctrl.Result, error) {
274-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name))
275+
Log := r.GetLogger(ctx)
276+
277+
Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name))
275278

276279
// remove db finalizer first
277280
db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name)
@@ -287,7 +290,7 @@ func (r *ManilaReconciler) reconcileDelete(ctx context.Context, instance *manila
287290

288291
// Service is deleted so remove the finalizer.
289292
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
290-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
293+
Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
291294

292295
return ctrl.Result{}, nil
293296
}
@@ -299,7 +302,9 @@ func (r *ManilaReconciler) reconcileInit(
299302
serviceLabels map[string]string,
300303
serviceAnnotations map[string]string,
301304
) (ctrl.Result, error) {
302-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))
305+
Log := r.GetLogger(ctx)
306+
307+
Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))
303308

304309
//
305310
// create service DB instance
@@ -396,18 +401,20 @@ func (r *ManilaReconciler) reconcileInit(
396401
}
397402
if dbSyncjob.HasChanged() {
398403
instance.Status.Hash[manilav1beta1.DbSyncHash] = dbSyncjob.GetHash()
399-
r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[manilav1beta1.DbSyncHash]))
404+
Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[manilav1beta1.DbSyncHash]))
400405
}
401406
instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage)
402407

403408
// run Manila db sync - end
404409

405-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name))
410+
Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name))
406411
return ctrl.Result{}, nil
407412
}
408413

409414
func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manilav1beta1.Manila, helper *helper.Helper) (ctrl.Result, error) {
410-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name))
415+
Log := r.GetLogger(ctx)
416+
417+
Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name))
411418

412419
// Service account, role, binding
413420
rbacRules := []rbacv1.PolicyRule{
@@ -450,13 +457,13 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
450457
}
451458

452459
if op != controllerutil.OperationResultNone {
453-
r.Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op)))
460+
Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op)))
454461
}
455462

456463
instance.Status.TransportURLSecret = transportURL.Status.SecretName
457464

458465
if instance.Status.TransportURLSecret == "" {
459-
r.Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name))
466+
Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name))
460467
instance.Status.Conditions.Set(condition.FalseCondition(
461468
condition.RabbitMqTransportURLReadyCondition,
462469
condition.RequestedReason,
@@ -613,7 +620,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
613620
return ctrl.Result{}, err
614621
}
615622
if op != controllerutil.OperationResultNone {
616-
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
623+
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
617624
}
618625

619626
// Mirror ManilaAPI status' ReadyCount to this parent CR
@@ -638,7 +645,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
638645
return ctrl.Result{}, err
639646
}
640647
if op != controllerutil.OperationResultNone {
641-
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
648+
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
642649
}
643650

644651
// Mirror ManilaScheduler status' ReadyCount to this parent CR
@@ -665,7 +672,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
665672
return ctrl.Result{}, err
666673
}
667674
if op != controllerutil.OperationResultNone {
668-
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
675+
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
669676
}
670677

671678
// Mirror ManilaShare status' ReadyCount to this parent CR
@@ -716,27 +723,31 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
716723
instance.Status.Conditions.MarkTrue(condition.CronJobReadyCondition, condition.CronJobReadyMessage)
717724
// create CronJob - end
718725

719-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name))
726+
Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name))
720727
return ctrl.Result{}, nil
721728
}
722729

723730
func (r *ManilaReconciler) reconcileUpdate(ctx context.Context, instance *manilav1beta1.Manila, helper *helper.Helper) (ctrl.Result, error) {
724-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name))
731+
Log := r.GetLogger(ctx)
732+
733+
Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name))
725734

726735
// TODO: should have minor update tasks if required
727736
// - delete dbsync hash from status to rerun it?
728737

729-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name))
738+
Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name))
730739
return ctrl.Result{}, nil
731740
}
732741

733742
func (r *ManilaReconciler) reconcileUpgrade(ctx context.Context, instance *manilav1beta1.Manila, helper *helper.Helper) (ctrl.Result, error) {
734-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name))
743+
Log := r.GetLogger(ctx)
744+
745+
Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name))
735746

736747
// TODO: should have major version upgrade tasks
737748
// -delete dbsync hash from status to rerun it?
738749

739-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name))
750+
Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name))
740751
return ctrl.Result{}, nil
741752
}
742753

@@ -836,6 +847,8 @@ func (r *ManilaReconciler) createHashOfInputHashes(
836847
instance *manilav1beta1.Manila,
837848
envVars map[string]env.Setter,
838849
) (string, bool, error) {
850+
Log := r.GetLogger(ctx)
851+
839852
var hashMap map[string]string
840853
changed := false
841854
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
@@ -845,7 +858,7 @@ func (r *ManilaReconciler) createHashOfInputHashes(
845858
}
846859
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
847860
instance.Status.Hash = hashMap
848-
r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
861+
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
849862
}
850863
return hash, changed, nil
851864
}

0 commit comments

Comments
 (0)