Skip to content

Commit

Permalink
switch to structured logging
Browse files Browse the repository at this point in the history
update logging implementation

update main.go with logging
  • Loading branch information
pinikomarov committed Oct 17, 2023
1 parent efbae20 commit 302b360
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 50 deletions.
44 changes: 25 additions & 19 deletions controllers/galera_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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'
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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{}
Expand All @@ -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
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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)
}
}
Expand All @@ -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)
Expand All @@ -561,22 +566,22 @@ 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
if (len(instance.Status.Attributes) > 0) &&
(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)
Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
28 changes: 7 additions & 21 deletions controllers/mariadb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -48,7 +47,6 @@ import (
type MariaDBReconciler struct {
Client client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -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{}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions controllers/mariadbdatabase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -40,7 +39,6 @@ import (
type MariaDBDatabaseReconciler struct {
client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 302b360

Please sign in to comment.