From a164ffa5343af145f462ced12e7532a7cfe444b4 Mon Sep 17 00:00:00 2001 From: r Date: Sun, 25 Jun 2023 02:45:13 +0300 Subject: [PATCH] controplane getlog func and mariah log update --- controllers/core/openstackcontrolplane_controller.go | 12 +++++++++--- pkg/openstack/mariadb.go | 7 +++++-- pkg/openstack/rabbitmq.go | 10 ++-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/controllers/core/openstackcontrolplane_controller.go b/controllers/core/openstackcontrolplane_controller.go index 19bb8b465..3bc496466 100644 --- a/controllers/core/openstackcontrolplane_controller.go +++ b/controllers/core/openstackcontrolplane_controller.go @@ -90,9 +90,15 @@ type OpenStackControlPlaneReconciler struct { // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.1/pkg/reconcile + +// GetLog returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func GetLog(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("OpenstackControlPlane") +} + func (r *OpenStackControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + l := GetLog(ctx) // Fetch the OpenStackControlPlane instance instance := &corev1beta1.OpenStackControlPlane{} err := r.Client.Get(ctx, req.NamespacedName, instance) @@ -101,7 +107,7 @@ func (r *OpenStackControlPlaneReconciler) Reconcile(ctx context.Context, req ctr // Request object not found, could have been deleted after reconcile request. // Owned objects are automatically garbage collected. // For additional cleanup logic use finalizers. Return and don't requeue. - r.Log.Info("OpenStackControlPlane instance is not found, probaby deleted. Nothing to do.") + l.Info("OpenStackControlPlane instance is not found, probaby deleted. Nothing to do.") return ctrl.Result{}, nil } // Error reading the object - requeue the request. @@ -117,7 +123,7 @@ func (r *OpenStackControlPlaneReconciler) Reconcile(ctx context.Context, req ctr ) if err != nil { // helper might be nil, so can't use util.LogErrorForObject since it requires helper as first arg - r.Log.Error(err, fmt.Sprintf("unable to acquire helper for OpenStackControlPlane %s", instance.Name)) + l.Error(err, fmt.Sprintf("unable to acquire helper for OpenStackControlPlane %s", instance.Name)) return ctrl.Result{}, err } diff --git a/pkg/openstack/mariadb.go b/pkg/openstack/mariadb.go index db939c3f0..89851d13b 100644 --- a/pkg/openstack/mariadb.go +++ b/pkg/openstack/mariadb.go @@ -12,6 +12,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" corev1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1" + "github.com/openstack-k8s-operators/openstack-operator/controllers/core" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" ) @@ -94,6 +95,8 @@ func reconcileMariaDB( }, } + l := core.GetLog(ctx) + if !instance.Spec.Mariadb.Enabled { if _, err := EnsureDeleted(ctx, helper, mariadb); err != nil { return mariadbFailed, err @@ -102,7 +105,7 @@ func reconcileMariaDB( return mariadbReady, nil } - helper.GetLogger().Info("Reconciling MariaDB", "MariaDB.Namespace", instance.Namespace, "Mariadb.Name", name) + l.Info("Reconciling MariaDB", "MariaDB.Namespace", instance.Namespace, "Mariadb.Name", name) op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), mariadb, func() error { spec.DeepCopyInto(&mariadb.Spec) if mariadb.Spec.Secret == "" { @@ -123,7 +126,7 @@ func reconcileMariaDB( return mariadbFailed, err } if op != controllerutil.OperationResultNone { - helper.GetLogger().Info(fmt.Sprintf("MariaDB %s - %s", mariadb.Name, op)) + l.Info(fmt.Sprintf("MariaDB %s - %s", mariadb.Name, op)) } if mariadb.IsReady() { diff --git a/pkg/openstack/rabbitmq.go b/pkg/openstack/rabbitmq.go index 029a81d73..6e6e3f99a 100644 --- a/pkg/openstack/rabbitmq.go +++ b/pkg/openstack/rabbitmq.go @@ -18,12 +18,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" corev1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1" + "github.com/openstack-k8s-operators/openstack-operator/controllers/core" corev1 "k8s.io/api/core/v1" - "github.com/go-logr/logr" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/log" ) type mqStatus int @@ -34,11 +33,6 @@ const ( mqReady mqStatus = iota ) -// GetLog returns a logger object with a prefix of "conroller.name" and aditional controller context fields -func GetLog(ctx context.Context) logr.Logger { - return log.FromContext(ctx).WithName("Controllers").WithName("OpenstackControlplane") -} - // ReconcileRabbitMQs - func ReconcileRabbitMQs( ctx context.Context, @@ -104,7 +98,7 @@ func reconcileRabbitMQ( }, } - l := GetLog(ctx) + l := core.GetLog(ctx) l.Info("Reconciling RabbitMQ", "RabbitMQ.Namespace", instance.Namespace, "RabbitMQ.Name", name) if !instance.Spec.Rabbitmq.Enabled { if _, err := EnsureDeleted(ctx, helper, rabbitmq); err != nil {