@@ -58,10 +58,14 @@ type GaleraReconciler struct {
58
58
client.Client
59
59
Kclient kubernetes.Interface
60
60
config * rest.Config
61
- Log logr.Logger
62
61
Scheme * runtime.Scheme
63
62
}
64
63
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
+
65
69
///
66
70
// General Galera helper functions
67
71
//
@@ -227,7 +231,8 @@ func clearPodAttributes(instance *mariadbv1.Galera, podName string) {
227
231
228
232
// clearOldPodsAttributesOnScaleDown removes known information from old pods
229
233
// 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" )
231
236
replicas := int (* instance .Spec .Replicas )
232
237
233
238
// a pod's name is built as 'statefulsetname-n'
@@ -236,7 +241,7 @@ func clearOldPodsAttributesOnScaleDown(helper *helper.Helper, instance *mariadbv
236
241
index , _ := strconv .Atoi (parts [len (parts )- 1 ])
237
242
if index >= replicas {
238
243
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 )
240
245
}
241
246
}
242
247
}
@@ -294,7 +299,7 @@ func assertPodsAttributesValidity(helper *helper.Helper, instance *mariadbv1.Gal
294
299
295
300
// Reconcile - Galera
296
301
func (r * GaleraReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (result ctrl.Result , _err error ) {
297
- _ = log . FromContext (ctx )
302
+ log := GetLog (ctx , "galera" )
298
303
299
304
// Fetch the Galera instance
300
305
instance := & mariadbv1.Galera {}
@@ -315,7 +320,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
315
320
r .Client ,
316
321
r .Kclient ,
317
322
r .Scheme ,
318
- r . Log ,
323
+ log ,
319
324
)
320
325
if err != nil {
321
326
return ctrl.Result {}, err
@@ -419,7 +424,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
419
424
return ctrl.Result {}, err
420
425
}
421
426
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 ))
423
428
}
424
429
}
425
430
@@ -441,7 +446,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
441
446
return ctrl.Result {}, err
442
447
}
443
448
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 ))
445
450
}
446
451
447
452
pkgsvc := mariadb .ServiceForAdoption (instance , "galera" , adoption )
@@ -458,7 +463,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
458
463
return ctrl.Result {}, err
459
464
}
460
465
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 ))
462
467
}
463
468
464
469
// Generate the config maps for the various services
@@ -494,7 +499,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
494
499
client .MatchingLabels (mariadb .StatefulSetLabels (instance )),
495
500
}
496
501
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" )
498
503
return ctrl.Result {}, err
499
504
}
500
505
@@ -504,7 +509,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
504
509
505
510
// Ensure status is cleaned up in case of scale down
506
511
if * statefulset .Spec .Replicas < statefulset .Status .Replicas {
507
- clearOldPodsAttributesOnScaleDown (helper , instance )
512
+ clearOldPodsAttributesOnScaleDown (helper , instance , ctx )
508
513
}
509
514
510
515
// 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
525
530
for _ , pod := range getReadyPods (podList .Items ) {
526
531
name := pod .Name
527
532
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 )
529
534
clearPodAttributes (instance , name )
530
535
}
531
536
}
@@ -534,11 +539,11 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
534
539
for _ , pod := range getRunningPodsMissingGcomm (ctx , podList .Items , instance , helper , r .config ) {
535
540
name := pod .Name
536
541
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 )
538
543
// Setting the gcomm attribute marks this pod as 'currently joining the cluster'
539
544
err := injectGcommURI (ctx , helper , r .config , instance , & pod , joinerURI )
540
545
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 )
542
547
// A failed injection likely means the pod's status has changed.
543
548
// drop it from status and reprobe it in another reconcile loop
544
549
clearPodAttributes (instance , name )
@@ -561,22 +566,22 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
561
566
util .LogForObject (helper , fmt .Sprintf ("Pod %s running, retrieve seqno" , name ), instance )
562
567
err := retrieveSequenceNumber (ctx , helper , r .config , instance , & pod )
563
568
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 )
565
570
return ctrl.Result {}, err
566
571
}
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 )
568
573
}
569
574
570
575
// Check if we have enough info to bootstrap the cluster now
571
576
if (len (instance .Status .Attributes ) > 0 ) &&
572
577
(len (instance .Status .Attributes ) == len (podList .Items )) {
573
578
node := findBestCandidate (& instance .Status )
574
579
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 )
576
581
// Setting the gcomm attribute marks this pod as 'currently bootstrapping the cluster'
577
582
err := injectGcommURI (ctx , helper , r .config , instance , pod , "gcomm://" )
578
583
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 )
580
585
// A failed injection likely means the pod's status has changed.
581
586
// drop it from status and reprobe it in another reconcile loop
582
587
clearPodAttributes (instance , node )
@@ -591,7 +596,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
591
596
// So until all pods become available, we have to requeue this event to get
592
597
// a chance to react to all pod's transitions.
593
598
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" )
595
600
return ctrl.Result {RequeueAfter : time .Duration (3 ) * time .Second }, nil
596
601
}
597
602
@@ -617,6 +622,7 @@ func (r *GaleraReconciler) generateConfigMaps(
617
622
instance * mariadbv1.Galera ,
618
623
envVars * map [string ]env.Setter ,
619
624
) error {
625
+ log := GetLog (ctx , "galera" )
620
626
templateParameters := make (map [string ]interface {})
621
627
customData := make (map [string ]string )
622
628
customData [mariadbv1 .CustomServiceConfigFile ] = instance .Spec .CustomServiceConfig
@@ -644,7 +650,7 @@ func (r *GaleraReconciler) generateConfigMaps(
644
650
645
651
err := configmap .EnsureConfigMaps (ctx , h , instance , cms , envVars )
646
652
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" )
648
654
return err
649
655
}
650
656
0 commit comments