diff --git a/controllers/galera_controller.go b/controllers/galera_controller.go index a51a60d6..9610b6e6 100644 --- a/controllers/galera_controller.go +++ b/controllers/galera_controller.go @@ -58,10 +58,14 @@ type GaleraReconciler struct { client.Client Kclient kubernetes.Interface config *rest.Config - Log logr.Logger Scheme *runtime.Scheme } +// GetLog returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func GetLog(ctx context.Context, controller string) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName(controller) +} + /// // General Galera helper functions // @@ -227,7 +231,8 @@ func clearPodAttributes(instance *mariadbv1.Galera, podName string) { // clearOldPodsAttributesOnScaleDown removes known information from old pods // that no longer exist after a scale down of the galera CR -func clearOldPodsAttributesOnScaleDown(helper *helper.Helper, instance *mariadbv1.Galera) { +func clearOldPodsAttributesOnScaleDown(helper *helper.Helper, instance *mariadbv1.Galera, ctx context.Context) { + log := GetLog(ctx, "galera") replicas := int(*instance.Spec.Replicas) // a pod's name is built as 'statefulsetname-n' @@ -236,7 +241,7 @@ func clearOldPodsAttributesOnScaleDown(helper *helper.Helper, instance *mariadbv index, _ := strconv.Atoi(parts[len(parts)-1]) if index >= replicas { clearPodAttributes(instance, node) - util.LogForObject(helper, "Remove old pod from status after scale-down", instance, "pod", node) + log.Info("Remove old pod from status after scale-down", "instance", instance, "pod", node) } } } @@ -294,7 +299,7 @@ func assertPodsAttributesValidity(helper *helper.Helper, instance *mariadbv1.Gal // Reconcile - Galera func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + log := GetLog(ctx, "galera") // Fetch the Galera instance instance := &mariadbv1.Galera{} @@ -315,7 +320,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res r.Client, r.Kclient, r.Scheme, - r.Log, + log, ) if err != nil { return ctrl.Result{}, err @@ -419,7 +424,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("%s %s database endpoints %s - operation: %s", instance.Kind, instance.Name, endpoints.Name, string(op))) + log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database endpoints", endpoints.Name, "operation:", string(op)) } } @@ -441,7 +446,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("%s %s database headless service %s - operation: %s", instance.Kind, instance.Name, headless.Name, string(op))) + log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database headless service", headless.Name, "operation", string(op)) } pkgsvc := mariadb.ServiceForAdoption(instance, "galera", adoption) @@ -458,7 +463,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("%s %s database service %s - operation: %s", instance.Kind, instance.Name, service.Name, string(op))) + log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database service", service.Name, "operation", string(op)) } // Generate the config maps for the various services @@ -494,7 +499,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res client.MatchingLabels(mariadb.StatefulSetLabels(instance)), } if err = r.List(ctx, podList, listOpts...); err != nil { - util.LogErrorForObject(helper, err, "Failed to list pods", instance) + log.Error(err, "Failed to list pods") return ctrl.Result{}, err } @@ -504,7 +509,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res // Ensure status is cleaned up in case of scale down if *statefulset.Spec.Replicas < statefulset.Status.Replicas { - clearOldPodsAttributesOnScaleDown(helper, instance) + clearOldPodsAttributesOnScaleDown(helper, instance, ctx) } // 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 for _, pod := range getReadyPods(podList.Items) { name := pod.Name if _, found := instance.Status.Attributes[name]; found { - util.LogForObject(helper, "Galera started on pod "+pod.Name, instance) + log.Info("Galera started on", "pod", pod.Name) clearPodAttributes(instance, name) } } @@ -534,11 +539,11 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res for _, pod := range getRunningPodsMissingGcomm(ctx, podList.Items, instance, helper, r.config) { name := pod.Name joinerURI := buildGcommURI(instance) - util.LogForObject(helper, "Pushing gcomm URI to joiner", instance, "pod", name) + log.Info("Pushing gcomm URI to joiner", "pod", name) // Setting the gcomm attribute marks this pod as 'currently joining the cluster' err := injectGcommURI(ctx, helper, r.config, instance, &pod, joinerURI) if err != nil { - util.LogErrorForObject(helper, err, "Failed to push gcomm URI", instance, "pod", name) + log.Error(err, "Failed to push gcomm URI", "pod", name) // A failed injection likely means the pod's status has changed. // drop it from status and reprobe it in another reconcile loop clearPodAttributes(instance, name) @@ -561,10 +566,10 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res util.LogForObject(helper, fmt.Sprintf("Pod %s running, retrieve seqno", name), instance) err := retrieveSequenceNumber(ctx, helper, r.config, instance, &pod) if err != nil { - util.LogErrorForObject(helper, err, "Failed to retrieve seqno for "+name, instance) + log.Error(err, "Failed to retrieve seqno for ", "name", name) return ctrl.Result{}, err } - util.LogForObject(helper, fmt.Sprintf("Pod %s seqno: %s", name, instance.Status.Attributes[name].Seqno), instance) + log.Info("", "Pod", name, "seqno:", instance.Status.Attributes[name].Seqno) } // Check if we have enough info to bootstrap the cluster now @@ -572,11 +577,11 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res (len(instance.Status.Attributes) == len(podList.Items)) { node := findBestCandidate(&instance.Status) pod := getPodFromName(podList.Items, node) - util.LogForObject(helper, "Pushing gcomm URI to bootstrap", instance, "pod", node) + log.Info("Pushing gcomm URI to bootstrap", "pod", node) // Setting the gcomm attribute marks this pod as 'currently bootstrapping the cluster' err := injectGcommURI(ctx, helper, r.config, instance, pod, "gcomm://") if err != nil { - util.LogErrorForObject(helper, err, "Failed to push gcomm URI", instance, "pod", node) + log.Error(err, "Failed to push gcomm URI", "pod", node) // A failed injection likely means the pod's status has changed. // drop it from status and reprobe it in another reconcile loop clearPodAttributes(instance, node) @@ -591,7 +596,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res // So until all pods become available, we have to requeue this event to get // a chance to react to all pod's transitions. if statefulset.Status.AvailableReplicas != statefulset.Status.Replicas { - util.LogForObject(helper, "Requeuing until all replicas are available", instance) + log.Info("Requeuing until all replicas are available") return ctrl.Result{RequeueAfter: time.Duration(3) * time.Second}, nil } @@ -617,6 +622,7 @@ func (r *GaleraReconciler) generateConfigMaps( instance *mariadbv1.Galera, envVars *map[string]env.Setter, ) error { + log := GetLog(ctx, "galera") templateParameters := make(map[string]interface{}) customData := make(map[string]string) customData[mariadbv1.CustomServiceConfigFile] = instance.Spec.CustomServiceConfig @@ -644,7 +650,7 @@ func (r *GaleraReconciler) generateConfigMaps( err := configmap.EnsureConfigMaps(ctx, h, instance, cms, envVars) if err != nil { - util.LogErrorForObject(h, err, "Unable to retrieve or create config maps", instance) + log.Error(err, "Unable to retrieve or create config maps") return err } diff --git a/controllers/mariadb_controller.go b/controllers/mariadb_controller.go index 50140ec4..ab208ba8 100644 --- a/controllers/mariadb_controller.go +++ b/controllers/mariadb_controller.go @@ -21,7 +21,6 @@ import ( "fmt" "time" - "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" k8s_errors "k8s.io/apimachinery/pkg/api/errors" @@ -48,7 +47,6 @@ import ( type MariaDBReconciler struct { Client client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } @@ -69,7 +67,7 @@ type MariaDBReconciler struct { // Reconcile reconcile mariadb API requests func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("mariadb", req.NamespacedName) + log := GetLog(ctx, "MariaDBR") // Fetch the MariaDB instance instance := &databasev1beta1.MariaDB{} @@ -110,7 +108,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re r.Client, r.Kclient, r.Scheme, - r.Log, + log, ) if err != nil { return ctrl.Result{}, err @@ -179,7 +177,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("%s %s database PVC %s - operation: %s", instance.Kind, instance.Name, pvc.Name, string(op))) + log.Info("", "Kind", instance.Kind, "Name", instance.Name, "database PVC", pvc.Name, "operation:", string(op)) return ctrl.Result{RequeueAfter: time.Duration(5) * time.Second}, err } @@ -202,11 +200,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - util.LogForObject( - helper, - fmt.Sprintf("Service %s successfully reconciled - operation: %s", service.Name, string(op)), - instance, - ) + log.Info("Service successfully reconciled", "service", service.Name, "operation:", string(op)) } // Endpoints @@ -229,11 +223,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - util.LogForObject( - helper, - fmt.Sprintf("Endpoints %s successfully reconciled - operation: %s", endpoints.Name, string(op)), - instance, - ) + log.Info("Endpoints successfully reconciled", "endpoint", endpoints.Name, "operation", string(op)) } } @@ -296,7 +286,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re if err := r.Client.Status().Update(ctx, instance); err != nil { return ctrl.Result{}, err } - r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.DbInitHash)) + log.Info("Job hash added", "Job", jobDef.Name, "status", instance.Status.DbInitHash) } instance.Status.Conditions.MarkTrue(databasev1beta1.MariaDBInitializedCondition, databasev1beta1.MariaDBInitializedReadyMessage) @@ -330,11 +320,7 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re condition.SeverityInfo, condition.DeploymentReadyRunningMessage)) - util.LogForObject( - helper, - fmt.Sprintf("Pod %s successfully reconciled - operation: %s", pod.Name, string(op)), - instance, - ) + log.Info("Pod successfully reconciled", "pod", pod.Name, "operation", string(op)) } if pod.Status.Phase == corev1.PodRunning { diff --git a/controllers/mariadbdatabase_controller.go b/controllers/mariadbdatabase_controller.go index 61bf0d52..02bebf2d 100644 --- a/controllers/mariadbdatabase_controller.go +++ b/controllers/mariadbdatabase_controller.go @@ -21,7 +21,6 @@ import ( "fmt" "time" - "github.com/go-logr/logr" k8s_errors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -40,7 +39,6 @@ import ( type MariaDBDatabaseReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } @@ -53,7 +51,7 @@ type MariaDBDatabaseReconciler struct { // Reconcile reconcile mariadbdatabase API requests func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("mariadbdatabase", req.NamespacedName) + log := GetLog(ctx, "MariaDBDatabase") var err error @@ -69,7 +67,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ r.Client, r.Kclient, r.Scheme, - r.Log, + log, ) if err != nil { return ctrl.Result{}, err @@ -139,7 +137,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ // It is impossible to reach here without either dbGalera or dbMariadb not being nil, due to the checks above if dbGalera != nil { if !dbGalera.Status.Bootstrapped { - r.Log.Info("DB bootstrap not complete. Requeue...") + log.Info("DB bootstrap not complete. Requeue...") return ctrl.Result{RequeueAfter: time.Second * 10}, nil } @@ -149,7 +147,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ serviceAccount = dbGalera.RbacResourceName() } else if dbMariadb != nil { if dbMariadb.Status.DbInitHash == "" { - r.Log.Info("DB initialization not complete. Requeue...") + log.Info("DB initialization not complete. Requeue...") return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil } @@ -188,7 +186,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ instance.Status.Hash = make(map[string]string) } instance.Status.Hash[databasev1beta1.DbCreateHash] = dbCreateJob.GetHash() - r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[databasev1beta1.DbCreateHash])) + log.Info("Job hash added", "Job", jobDef.Name, "Hash", instance.Status.Hash[databasev1beta1.DbCreateHash]) } // database creation finished... okay to set to completed diff --git a/main.go b/main.go index 72788a9d..14364155 100644 --- a/main.go +++ b/main.go @@ -95,7 +95,6 @@ func main() { if err = (&controllers.GaleraReconciler{ Client: mgr.GetClient(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Galera"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Galera") @@ -104,7 +103,6 @@ func main() { if err = (&controllers.MariaDBReconciler{ Client: mgr.GetClient(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("MariaDB"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "MariaDB") @@ -113,7 +111,6 @@ func main() { if err = (&controllers.MariaDBDatabaseReconciler{ Client: mgr.GetClient(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("MariaDBDatabase"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "MariaDBDatabase")