From b0163c28fb0f1410c9c87ac12a35f0df1d23b388 Mon Sep 17 00:00:00 2001 From: Jeff Mesnil Date: Mon, 7 Aug 2023 12:07:58 +0200 Subject: [PATCH] Update logger calls All logging should be done through the reconciler field Signed-off-by: Jeff Mesnil --- controllers/transaction_recovery.go | 3 +-- controllers/wildflyserver_controller.go | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/controllers/transaction_recovery.go b/controllers/transaction_recovery.go index 1646fe32..6059c3c1 100644 --- a/controllers/transaction_recovery.go +++ b/controllers/transaction_recovery.go @@ -467,7 +467,6 @@ func isJDBCLogStoreInUse(pod *corev1.Pod) (bool, error) { // to be deleted by statefulset update. This is used for cases when recovery process should be skipped. func (r *WildFlyServerReconciler) skipRecoveryAndForceScaleDown(w *wildflyv1alpha1.WildFlyServer, totalNumberOfPods int, numberOfPodsToScaleDown int, podList *corev1.PodList) (mustReconcile int, err error) { - log := r.Log for scaleDownIndex := 1; scaleDownIndex <= numberOfPodsToScaleDown; scaleDownIndex++ { scaleDownPodName := podList.Items[totalNumberOfPods-scaleDownIndex].ObjectMeta.Name @@ -481,7 +480,7 @@ func (r *WildFlyServerReconciler) skipRecoveryAndForceScaleDown(w *wildflyv1alph // process update on the WildFlyServer resource err = resources.UpdateWildFlyServerStatus(w, r.Client) if err != nil { - log.Error(err, "Error on updating WildFlyServer when skipping recovery scale down") + r.Log.Error(err, "Error on updating WildFlyServer when skipping recovery scale down") } return requeueOff, nil } diff --git a/controllers/wildflyserver_controller.go b/controllers/wildflyserver_controller.go index 397f1958..2be50f88 100644 --- a/controllers/wildflyserver_controller.go +++ b/controllers/wildflyserver_controller.go @@ -37,7 +37,6 @@ import ( "k8s.io/client-go/tools/record" "os" - "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "strconv" "strings" @@ -281,7 +280,7 @@ func (r *WildFlyServerReconciler) Reconcile(ctx context.Context, request ctrl.Re // SetupWithManager sets up the controller with the Manager. func (r *WildFlyServerReconciler) SetupWithManager(mgr ctrl.Manager) error { - r.Log.Info("Setting up with manager") + log.Info("Setting up with manager") builder := ctrl.NewControllerManagedBy(mgr). For(&wildflyv1alpha1.WildFlyServer{}) @@ -305,7 +304,6 @@ func (r *WildFlyServerReconciler) SetupWithManager(mgr ctrl.Manager) error { // A non-nil error if an error happens while updating/deleting the statefulset. func (r *WildFlyServerReconciler) checkStatefulSet(wildflyServer *wildflyv1alpha1.WildFlyServer, foundStatefulSet *appsv1.StatefulSet, podList *corev1.PodList) (mustReconcile int, err error) { - log := log.FromContext(context.TODO()) var update bool var requeue = requeueOff @@ -314,14 +312,14 @@ func (r *WildFlyServerReconciler) checkStatefulSet(wildflyServer *wildflyv1alpha desiredStatefulSetReplicaSize := wildflyServerSpecSize // - for scale up if wildflyServerSpecSize > *foundStatefulSet.Spec.Replicas { - log.Info("Scaling up and updating replica size to "+strconv.Itoa(int(wildflyServerSpecSize)), + r.Log.Info("Scaling up and updating replica size to "+strconv.Itoa(int(wildflyServerSpecSize)), "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) foundStatefulSet.Spec.Replicas = &desiredStatefulSetReplicaSize update = true } // - for scale down if wildflyServerSpecSize < *foundStatefulSet.Spec.Replicas { - log.Info("For statefulset scaling down need to verify if pods were cleaned by recovery", + r.Log.Info("For statefulset scaling down need to verify if pods were cleaned by recovery", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) // Change the number of replicas in statefulset, changing based on the pod state nameToPodState := make(map[string]string) @@ -348,14 +346,14 @@ func (r *WildFlyServerReconciler) checkStatefulSet(wildflyServer *wildflyv1alpha calculatedStatefulSetReplicaSize := int32(numberOfPods - numberOfPodsToShutdown) desiredStatefulSetReplicaSize = calculatedStatefulSetReplicaSize if wildflyServerSpecSize <= calculatedStatefulSetReplicaSize && *foundStatefulSet.Spec.Replicas > calculatedStatefulSetReplicaSize { - log.Info("Scaling down and updating replica size to "+strconv.Itoa(int(calculatedStatefulSetReplicaSize)), + r.Log.Info("Scaling down and updating replica size to "+strconv.Itoa(int(calculatedStatefulSetReplicaSize)), "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) foundStatefulSet.Spec.Replicas = &desiredStatefulSetReplicaSize update = true } // There are some unclean pods which can't be scaled down if wildflyServerSpecSize < calculatedStatefulSetReplicaSize { - log.Info("Statefulset was not scaled to the desired replica size "+strconv.Itoa(int(wildflyServerSpecSize))+ + r.Log.Info("Statefulset was not scaled to the desired replica size "+strconv.Itoa(int(wildflyServerSpecSize))+ " (current StatefulSet size: "+strconv.Itoa(int(calculatedStatefulSetReplicaSize))+ "). Transaction recovery scaledown process has not cleaned all pods. Please, check status of the WildflyServer "+wildflyServer.Name, "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) @@ -393,10 +391,10 @@ func (r *WildFlyServerReconciler) checkStatefulSet(wildflyServer *wildflyv1alpha if delete { // VolumeClaimTemplates has changed, the statefulset can not be updated and must be deleted if err = resources.Delete(wildflyServer, r.Client, foundStatefulSet); err != nil { - log.Error(err, "Failed to Delete StatefulSet.", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) + r.Log.Error(err, "Failed to Delete StatefulSet.", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) return requeueNow, err } - log.Info("Deleting StatefulSet that is not up to date with the WildFlyServer StorageSpec", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) + r.Log.Info("Deleting StatefulSet that is not up to date with the WildFlyServer StorageSpec", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) return requeueNow, nil } @@ -405,17 +403,17 @@ func (r *WildFlyServerReconciler) checkStatefulSet(wildflyServer *wildflyv1alpha foundStatefulSet.Spec.Replicas = &desiredStatefulSetReplicaSize foundStatefulSet.Annotations[resources.MarkerServerGeneration] = strconv.FormatInt(wildflyServer.Generation, 10) if err = resources.Update(wildflyServer, r.Client, foundStatefulSet); err != nil { - log.Error(err, "Failed to Update StatefulSet.", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) + r.Log.Error(err, "Failed to Update StatefulSet.", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) return requeueNow, err } - log.Info("Updating StatefulSet to be up to date with the WildFlyServer Spec", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) + r.Log.Info("Updating StatefulSet to be up to date with the WildFlyServer Spec", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) return requeueNow, nil } if update { - log.Info("Updating statefulset", "StatefulSet.Replicas", foundStatefulSet.Spec.Replicas) + r.Log.Info("Updating statefulset", "StatefulSet.Replicas", foundStatefulSet.Spec.Replicas) if err = resources.Update(wildflyServer, r.Client, foundStatefulSet); err != nil { - log.Error(err, "Failed to update StatefulSet.", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) + r.Log.Error(err, "Failed to update StatefulSet.", "StatefulSet.Namespace", foundStatefulSet.Namespace, "StatefulSet.Name", foundStatefulSet.Name) return requeueNow, err } return requeueNow, nil