Skip to content

Commit 8b6a494

Browse files
Merge pull request #358 from ratailor/fix-logger
Use structured logging
2 parents 3331559 + f162b97 commit 8b6a494

9 files changed

+137
-112
lines changed

controllers/designateapi_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func (r *DesignateAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.
399399
func (r *DesignateAPIReconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request {
400400
requests := []reconcile.Request{}
401401

402-
l := log.FromContext(ctx).WithName("Controllers").WithName("DesignateAPI")
402+
Log := r.GetLogger(ctx)
403403

404404
allWatchFields := []string{
405405
passwordSecretField,
@@ -417,12 +417,12 @@ func (r *DesignateAPIReconciler) findObjectsForSrc(ctx context.Context, src clie
417417
}
418418
err := r.Client.List(context.TODO(), crList, listOps)
419419
if err != nil {
420-
l.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GroupVersionKind().Kind, field, src.GetNamespace()))
420+
Log.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GroupVersionKind().Kind, field, src.GetNamespace()))
421421
return requests
422422
}
423423

424424
for _, item := range crList.Items {
425-
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
425+
Log.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
426426

427427
requests = append(requests,
428428
reconcile.Request{
@@ -441,20 +441,20 @@ func (r *DesignateAPIReconciler) findObjectsForSrc(ctx context.Context, src clie
441441
func (r *DesignateAPIReconciler) findObjectForSrc(ctx context.Context, src client.Object) []reconcile.Request {
442442
requests := []reconcile.Request{}
443443

444-
l := log.FromContext(ctx).WithName("Controllers").WithName("DesignateAPI")
444+
Log := r.GetLogger(ctx)
445445

446446
crList := &designatev1beta1.DesignateAPIList{}
447447
listOps := &client.ListOptions{
448448
Namespace: src.GetNamespace(),
449449
}
450450
err := r.Client.List(ctx, crList, listOps)
451451
if err != nil {
452-
l.Error(err, fmt.Sprintf("listing %s for namespace: %s", crList.GroupVersionKind().Kind, src.GetNamespace()))
452+
Log.Error(err, fmt.Sprintf("listing %s for namespace: %s", crList.GroupVersionKind().Kind, src.GetNamespace()))
453453
return requests
454454
}
455455

456456
for _, item := range crList.Items {
457-
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
457+
Log.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
458458

459459
requests = append(requests,
460460
reconcile.Request{

controllers/designatebackendbind9_controller.go

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ func (r *DesignateBackendbind9Reconciler) GetKClient() kubernetes.Interface {
6464
return r.Kclient
6565
}
6666

67-
// GetLogger -
68-
func (r *DesignateBackendbind9Reconciler) GetLogger() logr.Logger {
69-
return r.Log
67+
// GetLogger returns a logger object with a prefix of "controller.name" and additional controller context fields
68+
func (r *DesignateBackendbind9Reconciler) GetLogger(ctx context.Context) logr.Logger {
69+
return log.FromContext(ctx).WithName("Controllers").WithName("DesignateBackendbind9")
7070
}
7171

7272
// GetScheme -
@@ -78,7 +78,6 @@ func (r *DesignateBackendbind9Reconciler) GetScheme() *runtime.Scheme {
7878
type DesignateBackendbind9Reconciler struct {
7979
client.Client
8080
Kclient kubernetes.Interface
81-
Log logr.Logger
8281
Scheme *runtime.Scheme
8382
}
8483

@@ -100,7 +99,7 @@ type DesignateBackendbind9Reconciler struct {
10099
// perform operations to make the cluster state reflect the state specified by
101100
// the user.
102101
func (r *DesignateBackendbind9Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
103-
_ = r.Log.WithValues("designatebackendbind9", req.NamespacedName)
102+
Log := r.GetLogger(ctx)
104103

105104
// Fetch the DesignateBackendbind9 instance
106105
instance := &designatev1beta1.DesignateBackendbind9{}
@@ -121,7 +120,7 @@ func (r *DesignateBackendbind9Reconciler) Reconcile(ctx context.Context, req ctr
121120
r.Client,
122121
r.Kclient,
123122
r.Scheme,
124-
r.Log,
123+
Log,
125124
)
126125
if err != nil {
127126
return ctrl.Result{}, err
@@ -143,7 +142,7 @@ func (r *DesignateBackendbind9Reconciler) Reconcile(ctx context.Context, req ctr
143142
defer func() {
144143
// Don't update the status, if Reconciler Panics
145144
if rc := recover(); rc != nil {
146-
r.Log.Info(fmt.Sprintf("Panic during reconcile %v\n", rc))
145+
Log.Info(fmt.Sprintf("Panic during reconcile %v\n", rc))
147146
panic(rc)
148147
}
149148
condition.RestoreLastTransitionTimes(
@@ -201,7 +200,8 @@ func (r *DesignateBackendbind9Reconciler) Reconcile(ctx context.Context, req ctr
201200
}
202201

203202
// SetupWithManager sets up the controller with the Manager.
204-
func (r *DesignateBackendbind9Reconciler) SetupWithManager(mgr ctrl.Manager) error {
203+
func (r *DesignateBackendbind9Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
204+
Log := r.GetLogger(ctx)
205205
// Watch for changes to any CustomServiceConfigSecrets. Global secrets
206206
// (e.g. TransportURLSecret) are handled by the top designate controller.
207207
// svcSecretFn := func(_ context.Context, o client.Object) []reconcile.Request {
@@ -215,7 +215,7 @@ func (r *DesignateBackendbind9Reconciler) SetupWithManager(mgr ctrl.Manager) err
215215
// client.InNamespace(namespace),
216216
// }
217217
// if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
218-
// r.Log.Error(err, "Unable to retrieve Backendbind9 CRs %v")
218+
// Log.Error(err, "Unable to retrieve Backendbind9 CRs %v")
219219
// return nil
220220
// }
221221
// for _, cr := range apis.Items {
@@ -225,7 +225,7 @@ func (r *DesignateBackendbind9Reconciler) SetupWithManager(mgr ctrl.Manager) err
225225
// Namespace: namespace,
226226
// Name: cr.Name,
227227
// }
228-
// r.Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name))
228+
// Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name))
229229
// result = append(result, reconcile.Request{NamespacedName: name})
230230
// }
231231
// }
@@ -246,7 +246,7 @@ func (r *DesignateBackendbind9Reconciler) SetupWithManager(mgr ctrl.Manager) err
246246
client.InNamespace(o.GetNamespace()),
247247
}
248248
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
249-
r.Log.Error(err, "Unable to retrieve Backendbind9 CRs %v")
249+
Log.Error(err, "Unable to retrieve Backendbind9 CRs %v")
250250
return nil
251251
}
252252

@@ -262,7 +262,7 @@ func (r *DesignateBackendbind9Reconciler) SetupWithManager(mgr ctrl.Manager) err
262262
Namespace: o.GetNamespace(),
263263
Name: cr.Name,
264264
}
265-
r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l))
265+
Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l))
266266
result = append(result, reconcile.Request{NamespacedName: name})
267267
}
268268
}
@@ -301,7 +301,7 @@ func (r *DesignateBackendbind9Reconciler) SetupWithManager(mgr ctrl.Manager) err
301301
func (r *DesignateBackendbind9Reconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request {
302302
requests := []reconcile.Request{}
303303

304-
l := log.FromContext(ctx).WithName("Controllers").WithName("DesignateBackendbind9")
304+
Log := r.GetLogger(ctx)
305305

306306
allWatchFields := []string{
307307
topologyField,
@@ -315,12 +315,12 @@ func (r *DesignateBackendbind9Reconciler) findObjectsForSrc(ctx context.Context,
315315
}
316316
err := r.Client.List(context.TODO(), crList, listOps)
317317
if err != nil {
318-
l.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GroupVersionKind().Kind, field, src.GetNamespace()))
318+
Log.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GroupVersionKind().Kind, field, src.GetNamespace()))
319319
return requests
320320
}
321321

322322
for _, item := range crList.Items {
323-
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
323+
Log.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
324324

325325
requests = append(requests,
326326
reconcile.Request{
@@ -336,7 +336,8 @@ func (r *DesignateBackendbind9Reconciler) findObjectsForSrc(ctx context.Context,
336336
}
337337

338338
func (r *DesignateBackendbind9Reconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.DesignateBackendbind9, helper *helper.Helper) (ctrl.Result, error) {
339-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name))
339+
Log := r.GetLogger(ctx)
340+
Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name))
340341

341342
// Remove finalizer on the Topology CR
342343
if ctrlResult, err := topologyv1.EnsureDeletedTopologyRef(
@@ -350,22 +351,25 @@ func (r *DesignateBackendbind9Reconciler) reconcileDelete(ctx context.Context, i
350351
// We did all the cleanup on the objects we created so we can remove the
351352
// finalizer from ourselves to allow the deletion
352353
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
353-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
354+
Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
354355

355356
return ctrl.Result{}, nil
356357
}
357358

358359
func (r *DesignateBackendbind9Reconciler) reconcileInit(
360+
ctx context.Context,
359361
instance *designatev1beta1.DesignateBackendbind9,
360362
) (ctrl.Result, error) {
361-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))
363+
Log := r.GetLogger(ctx)
364+
Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))
362365

363-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name))
366+
Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name))
364367
return ctrl.Result{}, nil
365368
}
366369

367370
func (r *DesignateBackendbind9Reconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.DesignateBackendbind9, helper *helper.Helper) (ctrl.Result, error) {
368-
r.Log.Info("Reconciling Service")
371+
Log := r.GetLogger(ctx)
372+
Log.Info("Reconciling Service")
369373

370374
// ConfigMap
371375
configMapVars := make(map[string]env.Setter)
@@ -384,7 +388,7 @@ func (r *DesignateBackendbind9Reconciler) reconcileNormal(ctx context.Context, i
384388
//}
385389
// run check service secrets - end
386390
if len(instance.Spec.CustomServiceConfigSecrets) > 0 {
387-
r.Log.Info("warning: CustomServiceConfigSecrets is not supported.")
391+
Log.Info("warning: CustomServiceConfigSecrets is not supported.")
388392
}
389393

390394
//
@@ -455,7 +459,7 @@ func (r *DesignateBackendbind9Reconciler) reconcileNormal(ctx context.Context, i
455459
// create hash over all the different input resources to identify if any those changed
456460
// and a restart/recreate is required.
457461
//
458-
inputHash, hashChanged, err := r.createHashOfInputHashes(instance, configMapVars)
462+
inputHash, hashChanged, err := r.createHashOfInputHashes(ctx, instance, configMapVars)
459463
if err != nil {
460464
instance.Status.Conditions.Set(condition.FalseCondition(
461465
condition.ServiceConfigReadyCondition,
@@ -496,7 +500,7 @@ func (r *DesignateBackendbind9Reconciler) reconcileNormal(ctx context.Context, i
496500
nad, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
497501
if err != nil {
498502
if k8s_errors.IsNotFound(err) {
499-
r.Log.Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt))
503+
Log.Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt))
500504
instance.Status.Conditions.Set(condition.FalseCondition(
501505
condition.NetworkAttachmentsReadyCondition,
502506
condition.RequestedReason,
@@ -526,23 +530,23 @@ func (r *DesignateBackendbind9Reconciler) reconcileNormal(ctx context.Context, i
526530
}
527531

528532
// Handle service init
529-
ctrlResult, err := r.reconcileInit(instance)
533+
ctrlResult, err := r.reconcileInit(ctx, instance)
530534
if err != nil {
531535
return ctrlResult, err
532536
} else if (ctrlResult != ctrl.Result{}) {
533537
return ctrlResult, nil
534538
}
535539

536540
// Handle service update
537-
ctrlResult, err = r.reconcileUpdate(instance)
541+
ctrlResult, err = r.reconcileUpdate(ctx, instance)
538542
if err != nil {
539543
return ctrlResult, err
540544
} else if (ctrlResult != ctrl.Result{}) {
541545
return ctrlResult, nil
542546
}
543547

544548
// Handle service upgrade
545-
ctrlResult, err = r.reconcileUpgrade(instance)
549+
ctrlResult, err = r.reconcileUpgrade(ctx, instance)
546550
if err != nil {
547551
return ctrlResult, err
548552
} else if (ctrlResult != ctrl.Result{}) {
@@ -661,27 +665,29 @@ func (r *DesignateBackendbind9Reconciler) reconcileNormal(ctx context.Context, i
661665
instance.Status.Conditions.MarkTrue(
662666
condition.ReadyCondition, condition.ReadyMessage)
663667
}
664-
r.Log.Info("Reconciled Service successfully")
668+
Log.Info("Reconciled Service successfully")
665669
return ctrl.Result{}, nil
666670
}
667671

668-
func (r *DesignateBackendbind9Reconciler) reconcileUpdate(instance *designatev1beta1.DesignateBackendbind9) (ctrl.Result, error) {
669-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name))
672+
func (r *DesignateBackendbind9Reconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.DesignateBackendbind9) (ctrl.Result, error) {
673+
Log := r.GetLogger(ctx)
674+
Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name))
670675

671676
// TODO: should have minor update tasks if required
672677
// - delete dbsync hash from status to rerun it?
673678

674-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name))
679+
Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name))
675680
return ctrl.Result{}, nil
676681
}
677682

678-
func (r *DesignateBackendbind9Reconciler) reconcileUpgrade(instance *designatev1beta1.DesignateBackendbind9) (ctrl.Result, error) {
679-
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name))
683+
func (r *DesignateBackendbind9Reconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.DesignateBackendbind9) (ctrl.Result, error) {
684+
Log := r.GetLogger(ctx)
685+
Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name))
680686

681687
// TODO: should have major version upgrade tasks
682688
// -delete dbsync hash from status to rerun it?
683689

684-
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name))
690+
Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name))
685691
return ctrl.Result{}, nil
686692
}
687693

@@ -693,6 +699,7 @@ func (r *DesignateBackendbind9Reconciler) generateServiceConfigMaps(
693699
envVars *map[string]env.Setter,
694700
serviceLabels map[string]string,
695701
) error {
702+
Log := r.GetLogger(ctx)
696703
//
697704
// create custom Configmap for designate-backendbind9-specific config input
698705
// - %-config-data configmap holding custom config for the service's designate.conf
@@ -712,7 +719,7 @@ func (r *DesignateBackendbind9Reconciler) generateServiceConfigMaps(
712719
nad, err := nad.GetNADWithName(ctx, h, netAtt, instance.Namespace)
713720
if err != nil {
714721
if k8s_errors.IsNotFound(err) {
715-
r.Log.Info(fmt.Sprintf("network-attachment-definition %s not found, cannot configure pod", netAtt))
722+
Log.Info(fmt.Sprintf("network-attachment-definition %s not found, cannot configure pod", netAtt))
716723
instance.Status.Conditions.Set(condition.FalseCondition(
717724
condition.NetworkAttachmentsReadyCondition,
718725
condition.RequestedReason,
@@ -822,9 +829,11 @@ func (r *DesignateBackendbind9Reconciler) generateServiceConfigMaps(
822829
//
823830
// returns the hash, whether the hash changed (as a bool) and any error
824831
func (r *DesignateBackendbind9Reconciler) createHashOfInputHashes(
832+
ctx context.Context,
825833
instance *designatev1beta1.DesignateBackendbind9,
826834
envVars map[string]env.Setter,
827835
) (string, bool, error) {
836+
Log := r.GetLogger(ctx)
828837
var hashMap map[string]string
829838
changed := false
830839
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
@@ -834,7 +843,7 @@ func (r *DesignateBackendbind9Reconciler) createHashOfInputHashes(
834843
}
835844
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
836845
instance.Status.Hash = hashMap
837-
r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
846+
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
838847
}
839848
return hash, changed, nil
840849
}
@@ -846,10 +855,11 @@ func (r *DesignateBackendbind9Reconciler) hasMapChanged(
846855
mapName string,
847856
hashKey string,
848857
) (bool, error) {
858+
Log := r.GetLogger(ctx)
849859
configMap := &corev1.ConfigMap{}
850860
err := h.GetClient().Get(ctx, types.NamespacedName{Name: mapName, Namespace: instance.GetNamespace()}, configMap)
851861
if err != nil {
852-
r.GetLogger().Error(err, fmt.Sprintf("Unable to check config map %s for changes", mapName))
862+
Log.Error(err, fmt.Sprintf("Unable to check config map %s for changes", mapName))
853863
return false, err
854864
}
855865
hashValue, err := configmap.Hash(configMap)
@@ -867,10 +877,11 @@ func (r *DesignateBackendbind9Reconciler) hasSecretChanged(
867877
secretName string,
868878
hashKey string,
869879
) (bool, error) {
880+
Log := r.GetLogger(ctx)
870881
found := &corev1.Secret{}
871882
err := h.GetClient().Get(ctx, types.NamespacedName{Name: secretName, Namespace: instance.GetNamespace()}, found)
872883
if err != nil {
873-
r.GetLogger().Error(err, fmt.Sprintf("Unable to check secret %s for changes", secretName))
884+
Log.Error(err, fmt.Sprintf("Unable to check secret %s for changes", secretName))
874885
return false, err
875886
}
876887
hashValue, err := secret.Hash(found)

controllers/designatecentral_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (r *DesignateCentralReconciler) SetupWithManager(ctx context.Context, mgr c
345345
func (r *DesignateCentralReconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request {
346346
requests := []reconcile.Request{}
347347

348-
l := log.FromContext(ctx).WithName("Controllers").WithName("DesignateCentral")
348+
Log := r.GetLogger(ctx)
349349

350350
allWatchFields := []string{
351351
passwordSecretField,
@@ -361,12 +361,12 @@ func (r *DesignateCentralReconciler) findObjectsForSrc(ctx context.Context, src
361361
}
362362
err := r.Client.List(context.TODO(), crList, listOps)
363363
if err != nil {
364-
l.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GroupVersionKind().Kind, field, src.GetNamespace()))
364+
Log.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GroupVersionKind().Kind, field, src.GetNamespace()))
365365
return requests
366366
}
367367

368368
for _, item := range crList.Items {
369-
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
369+
Log.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
370370

371371
requests = append(requests,
372372
reconcile.Request{

0 commit comments

Comments
 (0)