Skip to content

Commit c971bdd

Browse files
Merge pull request #142 from pinikomarov/structured_logging
switch to structured logging
2 parents 343ccf4 + d4b3d89 commit c971bdd

File tree

4 files changed

+37
-50
lines changed

4 files changed

+37
-50
lines changed

controllers/galera_controller.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ type GaleraReconciler struct {
5858
client.Client
5959
Kclient kubernetes.Interface
6060
config *rest.Config
61-
Log logr.Logger
6261
Scheme *runtime.Scheme
6362
}
6463

64+
// GetLog returns a logger object with a prefix of "controller.name" and additional controller context fields
65+
func GetLog(ctx context.Context, controller string) logr.Logger {
66+
return log.FromContext(ctx).WithName("Controllers").WithName(controller)
67+
}
68+
6569
///
6670
// General Galera helper functions
6771
//
@@ -227,7 +231,8 @@ func clearPodAttributes(instance *mariadbv1.Galera, podName string) {
227231

228232
// clearOldPodsAttributesOnScaleDown removes known information from old pods
229233
// that no longer exist after a scale down of the galera CR
230-
func clearOldPodsAttributesOnScaleDown(helper *helper.Helper, instance *mariadbv1.Galera) {
234+
func clearOldPodsAttributesOnScaleDown(helper *helper.Helper, instance *mariadbv1.Galera, ctx context.Context) {
235+
log := GetLog(ctx, "galera")
231236
replicas := int(*instance.Spec.Replicas)
232237

233238
// a pod's name is built as 'statefulsetname-n'
@@ -236,7 +241,7 @@ func clearOldPodsAttributesOnScaleDown(helper *helper.Helper, instance *mariadbv
236241
index, _ := strconv.Atoi(parts[len(parts)-1])
237242
if index >= replicas {
238243
clearPodAttributes(instance, node)
239-
util.LogForObject(helper, "Remove old pod from status after scale-down", instance, "pod", node)
244+
log.Info("Remove old pod from status after scale-down", "instance", instance, "pod", node)
240245
}
241246
}
242247
}
@@ -294,7 +299,7 @@ func assertPodsAttributesValidity(helper *helper.Helper, instance *mariadbv1.Gal
294299

295300
// Reconcile - Galera
296301
func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
297-
_ = log.FromContext(ctx)
302+
log := GetLog(ctx, "galera")
298303

299304
// Fetch the Galera instance
300305
instance := &mariadbv1.Galera{}
@@ -315,7 +320,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
315320
r.Client,
316321
r.Kclient,
317322
r.Scheme,
318-
r.Log,
323+
log,
319324
)
320325
if err != nil {
321326
return ctrl.Result{}, err
@@ -419,7 +424,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
419424
return ctrl.Result{}, err
420425
}
421426
if op != controllerutil.OperationResultNone {
422-
r.Log.Info(fmt.Sprintf("%s %s database endpoints %s - operation: %s", instance.Kind, instance.Name, endpoints.Name, string(op)))
427+
log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database endpoints", endpoints.Name, "operation:", string(op))
423428
}
424429
}
425430

@@ -441,7 +446,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
441446
return ctrl.Result{}, err
442447
}
443448
if op != controllerutil.OperationResultNone {
444-
r.Log.Info(fmt.Sprintf("%s %s database headless service %s - operation: %s", instance.Kind, instance.Name, headless.Name, string(op)))
449+
log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database headless service", headless.Name, "operation", string(op))
445450
}
446451

447452
pkgsvc := mariadb.ServiceForAdoption(instance, "galera", adoption)
@@ -458,7 +463,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
458463
return ctrl.Result{}, err
459464
}
460465
if op != controllerutil.OperationResultNone {
461-
r.Log.Info(fmt.Sprintf("%s %s database service %s - operation: %s", instance.Kind, instance.Name, service.Name, string(op)))
466+
log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database service", service.Name, "operation", string(op))
462467
}
463468

464469
// Generate the config maps for the various services
@@ -494,7 +499,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
494499
client.MatchingLabels(mariadb.StatefulSetLabels(instance)),
495500
}
496501
if err = r.List(ctx, podList, listOpts...); err != nil {
497-
util.LogErrorForObject(helper, err, "Failed to list pods", instance)
502+
log.Error(err, "Failed to list pods")
498503
return ctrl.Result{}, err
499504
}
500505

@@ -504,7 +509,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
504509

505510
// Ensure status is cleaned up in case of scale down
506511
if *statefulset.Spec.Replicas < statefulset.Status.Replicas {
507-
clearOldPodsAttributesOnScaleDown(helper, instance)
512+
clearOldPodsAttributesOnScaleDown(helper, instance, ctx)
508513
}
509514

510515
// Ensure that all the ongoing galera start actions are still running
@@ -525,7 +530,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
525530
for _, pod := range getReadyPods(podList.Items) {
526531
name := pod.Name
527532
if _, found := instance.Status.Attributes[name]; found {
528-
util.LogForObject(helper, "Galera started on pod "+pod.Name, instance)
533+
log.Info("Galera started on", "pod", pod.Name)
529534
clearPodAttributes(instance, name)
530535
}
531536
}
@@ -534,11 +539,11 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
534539
for _, pod := range getRunningPodsMissingGcomm(ctx, podList.Items, instance, helper, r.config) {
535540
name := pod.Name
536541
joinerURI := buildGcommURI(instance)
537-
util.LogForObject(helper, "Pushing gcomm URI to joiner", instance, "pod", name)
542+
log.Info("Pushing gcomm URI to joiner", "pod", name)
538543
// Setting the gcomm attribute marks this pod as 'currently joining the cluster'
539544
err := injectGcommURI(ctx, helper, r.config, instance, &pod, joinerURI)
540545
if err != nil {
541-
util.LogErrorForObject(helper, err, "Failed to push gcomm URI", instance, "pod", name)
546+
log.Error(err, "Failed to push gcomm URI", "pod", name)
542547
// A failed injection likely means the pod's status has changed.
543548
// drop it from status and reprobe it in another reconcile loop
544549
clearPodAttributes(instance, name)
@@ -561,22 +566,22 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
561566
util.LogForObject(helper, fmt.Sprintf("Pod %s running, retrieve seqno", name), instance)
562567
err := retrieveSequenceNumber(ctx, helper, r.config, instance, &pod)
563568
if err != nil {
564-
util.LogErrorForObject(helper, err, "Failed to retrieve seqno for "+name, instance)
569+
log.Error(err, "Failed to retrieve seqno for ", "name", name)
565570
return ctrl.Result{}, err
566571
}
567-
util.LogForObject(helper, fmt.Sprintf("Pod %s seqno: %s", name, instance.Status.Attributes[name].Seqno), instance)
572+
log.Info("", "Pod", name, "seqno:", instance.Status.Attributes[name].Seqno)
568573
}
569574

570575
// Check if we have enough info to bootstrap the cluster now
571576
if (len(instance.Status.Attributes) > 0) &&
572577
(len(instance.Status.Attributes) == len(podList.Items)) {
573578
node := findBestCandidate(&instance.Status)
574579
pod := getPodFromName(podList.Items, node)
575-
util.LogForObject(helper, "Pushing gcomm URI to bootstrap", instance, "pod", node)
580+
log.Info("Pushing gcomm URI to bootstrap", "pod", node)
576581
// Setting the gcomm attribute marks this pod as 'currently bootstrapping the cluster'
577582
err := injectGcommURI(ctx, helper, r.config, instance, pod, "gcomm://")
578583
if err != nil {
579-
util.LogErrorForObject(helper, err, "Failed to push gcomm URI", instance, "pod", node)
584+
log.Error(err, "Failed to push gcomm URI", "pod", node)
580585
// A failed injection likely means the pod's status has changed.
581586
// drop it from status and reprobe it in another reconcile loop
582587
clearPodAttributes(instance, node)
@@ -591,7 +596,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
591596
// So until all pods become available, we have to requeue this event to get
592597
// a chance to react to all pod's transitions.
593598
if statefulset.Status.AvailableReplicas != statefulset.Status.Replicas {
594-
util.LogForObject(helper, "Requeuing until all replicas are available", instance)
599+
log.Info("Requeuing until all replicas are available")
595600
return ctrl.Result{RequeueAfter: time.Duration(3) * time.Second}, nil
596601
}
597602

@@ -617,6 +622,7 @@ func (r *GaleraReconciler) generateConfigMaps(
617622
instance *mariadbv1.Galera,
618623
envVars *map[string]env.Setter,
619624
) error {
625+
log := GetLog(ctx, "galera")
620626
templateParameters := make(map[string]interface{})
621627
customData := make(map[string]string)
622628
customData[mariadbv1.CustomServiceConfigFile] = instance.Spec.CustomServiceConfig
@@ -644,7 +650,7 @@ func (r *GaleraReconciler) generateConfigMaps(
644650

645651
err := configmap.EnsureConfigMaps(ctx, h, instance, cms, envVars)
646652
if err != nil {
647-
util.LogErrorForObject(h, err, "Unable to retrieve or create config maps", instance)
653+
log.Error(err, "Unable to retrieve or create config maps")
648654
return err
649655
}
650656

controllers/mariadb_controller.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"time"
2323

24-
"github.com/go-logr/logr"
2524
corev1 "k8s.io/api/core/v1"
2625
rbacv1 "k8s.io/api/rbac/v1"
2726
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
@@ -48,7 +47,6 @@ import (
4847
type MariaDBReconciler struct {
4948
Client client.Client
5049
Kclient kubernetes.Interface
51-
Log logr.Logger
5250
Scheme *runtime.Scheme
5351
}
5452

@@ -69,7 +67,7 @@ type MariaDBReconciler struct {
6967

7068
// Reconcile reconcile mariadb API requests
7169
func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
72-
_ = r.Log.WithValues("mariadb", req.NamespacedName)
70+
log := GetLog(ctx, "MariaDBR")
7371

7472
// Fetch the MariaDB instance
7573
instance := &databasev1beta1.MariaDB{}
@@ -110,7 +108,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
110108
r.Client,
111109
r.Kclient,
112110
r.Scheme,
113-
r.Log,
111+
log,
114112
)
115113
if err != nil {
116114
return ctrl.Result{}, err
@@ -179,7 +177,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
179177
return ctrl.Result{}, err
180178
}
181179
if op != controllerutil.OperationResultNone {
182-
r.Log.Info(fmt.Sprintf("%s %s database PVC %s - operation: %s", instance.Kind, instance.Name, pvc.Name, string(op)))
180+
log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database PVC", pvc.Name, "operation:", string(op))
183181
return ctrl.Result{RequeueAfter: time.Duration(5) * time.Second}, err
184182
}
185183

@@ -202,11 +200,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
202200
return ctrl.Result{}, err
203201
}
204202
if op != controllerutil.OperationResultNone {
205-
util.LogForObject(
206-
helper,
207-
fmt.Sprintf("Service %s successfully reconciled - operation: %s", service.Name, string(op)),
208-
instance,
209-
)
203+
log.Info("Service successfully reconciled", "service", service.Name, "operation:", string(op))
210204
}
211205

212206
// Endpoints
@@ -229,11 +223,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
229223
return ctrl.Result{}, err
230224
}
231225
if op != controllerutil.OperationResultNone {
232-
util.LogForObject(
233-
helper,
234-
fmt.Sprintf("Endpoints %s successfully reconciled - operation: %s", endpoints.Name, string(op)),
235-
instance,
236-
)
226+
log.Info("Endpoints successfully reconciled", "endpoint", endpoints.Name, "operation", string(op))
237227
}
238228
}
239229

@@ -296,7 +286,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
296286
if err := r.Client.Status().Update(ctx, instance); err != nil {
297287
return ctrl.Result{}, err
298288
}
299-
r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.DbInitHash))
289+
log.Info("Job hash added", "Job", jobDef.Name, "status", instance.Status.DbInitHash)
300290
}
301291

302292
instance.Status.Conditions.MarkTrue(databasev1beta1.MariaDBInitializedCondition, databasev1beta1.MariaDBInitializedReadyMessage)
@@ -330,11 +320,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
330320
condition.SeverityInfo,
331321
condition.DeploymentReadyRunningMessage))
332322

333-
util.LogForObject(
334-
helper,
335-
fmt.Sprintf("Pod %s successfully reconciled - operation: %s", pod.Name, string(op)),
336-
instance,
337-
)
323+
log.Info("Pod successfully reconciled", "pod", pod.Name, "operation", string(op))
338324
}
339325

340326
if pod.Status.Phase == corev1.PodRunning {

controllers/mariadbdatabase_controller.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"time"
2323

24-
"github.com/go-logr/logr"
2524
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2726
"k8s.io/apimachinery/pkg/runtime"
@@ -40,7 +39,6 @@ import (
4039
type MariaDBDatabaseReconciler struct {
4140
client.Client
4241
Kclient kubernetes.Interface
43-
Log logr.Logger
4442
Scheme *runtime.Scheme
4543
}
4644

@@ -53,7 +51,7 @@ type MariaDBDatabaseReconciler struct {
5351

5452
// Reconcile reconcile mariadbdatabase API requests
5553
func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
56-
_ = r.Log.WithValues("mariadbdatabase", req.NamespacedName)
54+
log := GetLog(ctx, "MariaDBDatabase")
5755

5856
var err error
5957

@@ -69,7 +67,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
6967
r.Client,
7068
r.Kclient,
7169
r.Scheme,
72-
r.Log,
70+
log,
7371
)
7472
if err != nil {
7573
return ctrl.Result{}, err
@@ -139,7 +137,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
139137
// It is impossible to reach here without either dbGalera or dbMariadb not being nil, due to the checks above
140138
if dbGalera != nil {
141139
if !dbGalera.Status.Bootstrapped {
142-
r.Log.Info("DB bootstrap not complete. Requeue...")
140+
log.Info("DB bootstrap not complete. Requeue...")
143141
return ctrl.Result{RequeueAfter: time.Second * 10}, nil
144142
}
145143

@@ -149,7 +147,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
149147
serviceAccount = dbGalera.RbacResourceName()
150148
} else if dbMariadb != nil {
151149
if dbMariadb.Status.DbInitHash == "" {
152-
r.Log.Info("DB initialization not complete. Requeue...")
150+
log.Info("DB initialization not complete. Requeue...")
153151
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
154152
}
155153

@@ -188,7 +186,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
188186
instance.Status.Hash = make(map[string]string)
189187
}
190188
instance.Status.Hash[databasev1beta1.DbCreateHash] = dbCreateJob.GetHash()
191-
r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[databasev1beta1.DbCreateHash]))
189+
log.Info("Job hash added", "Job", jobDef.Name, "Hash", instance.Status.Hash[databasev1beta1.DbCreateHash])
192190
}
193191

194192
// database creation finished... okay to set to completed

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func main() {
105105
if err = (&controllers.GaleraReconciler{
106106
Client: mgr.GetClient(),
107107
Kclient: kclient,
108-
Log: ctrl.Log.WithName("controllers").WithName("Galera"),
109108
Scheme: mgr.GetScheme(),
110109
}).SetupWithManager(mgr); err != nil {
111110
setupLog.Error(err, "unable to create controller", "controller", "Galera")
@@ -114,7 +113,6 @@ func main() {
114113
if err = (&controllers.MariaDBReconciler{
115114
Client: mgr.GetClient(),
116115
Kclient: kclient,
117-
Log: ctrl.Log.WithName("controllers").WithName("MariaDB"),
118116
Scheme: mgr.GetScheme(),
119117
}).SetupWithManager(mgr); err != nil {
120118
setupLog.Error(err, "unable to create controller", "controller", "MariaDB")
@@ -123,7 +121,6 @@ func main() {
123121
if err = (&controllers.MariaDBDatabaseReconciler{
124122
Client: mgr.GetClient(),
125123
Kclient: kclient,
126-
Log: ctrl.Log.WithName("controllers").WithName("MariaDBDatabase"),
127124
Scheme: mgr.GetScheme(),
128125
}).SetupWithManager(mgr); err != nil {
129126
setupLog.Error(err, "unable to create controller", "controller", "MariaDBDatabase")

0 commit comments

Comments
 (0)