From 8ba9e05506341491d703654fcbe0aea184d7e65e Mon Sep 17 00:00:00 2001 From: r Date: Tue, 24 Oct 2023 16:40:07 +0300 Subject: [PATCH] update logging update logging --- controllers/aodh_controller.go | 22 +++++++----- controllers/autoscaling_controller.go | 51 ++++++++++++++++----------- controllers/ceilometer_controller.go | 42 +++++++++++++--------- controllers/prometheus_controller.go | 17 +++++---- controllers/telemetry_controller.go | 30 ++++++++++------ main.go | 8 ++--- 6 files changed, 102 insertions(+), 68 deletions(-) diff --git a/controllers/aodh_controller.go b/controllers/aodh_controller.go index d224c029..f3c01b02 100644 --- a/controllers/aodh_controller.go +++ b/controllers/aodh_controller.go @@ -49,7 +49,8 @@ func (r *AutoscalingReconciler) reconcileDisabledAodh( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service Aodh disabled") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service Aodh disabled") serviceLabels := map[string]string{ common.AppSelector: autoscaling.ServiceName, } @@ -144,7 +145,7 @@ func (r *AutoscalingReconciler) reconcileDisabledAodh( return ctrl.Result{}, err } instance.Status.Conditions = condition.Conditions{} - r.Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' disable successfully", autoscaling.ServiceName)) + Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' disable successfully", autoscaling.ServiceName)) return ctrl.Result{}, nil } @@ -153,7 +154,8 @@ func (r *AutoscalingReconciler) reconcileDeleteAodh( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service Aodh delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service Aodh delete") // remove db finalizer first db, err := mariadbv1.GetDatabaseByName(ctx, helper, autoscaling.ServiceName) @@ -197,7 +199,7 @@ func (r *AutoscalingReconciler) reconcileDeleteAodh( util.LogForObject(helper, "Removed finalizer from our KeystoneEndpoint", instance) } } - r.Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' delete successfully", autoscaling.ServiceName)) + Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' delete successfully", autoscaling.ServiceName)) return ctrl.Result{}, nil } @@ -208,7 +210,8 @@ func (r *AutoscalingReconciler) reconcileInitAodh( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service Aodh init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service Aodh init") _, _, err := secret.GetSecret(ctx, helper, instance.Spec.Aodh.Secret, instance.Namespace) if err != nil { if k8s_errors.IsNotFound(err) { @@ -347,12 +350,12 @@ func (r *AutoscalingReconciler) reconcileInitAodh( } if dbSyncjob.HasChanged() { instance.Status.Hash[telemetryv1.DbSyncHash] = dbSyncjob.GetHash() - r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[telemetryv1.DbSyncHash])) + Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[telemetryv1.DbSyncHash])) } instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage) // run Aodh db sync - end - r.Log.Info("Reconciled Service Aodh init successfully") + Log.Info("Reconciled Service Aodh init successfully") return ctrl.Result{}, nil } @@ -362,7 +365,8 @@ func (r *AutoscalingReconciler) reconcileNormalAodh( helper *helper.Helper, inputHash string, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service Aodh '%s'", autoscaling.ServiceName)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling Service Aodh '%s'", autoscaling.ServiceName)) serviceLabels := map[string]string{ common.AppSelector: autoscaling.ServiceName, } @@ -543,6 +547,6 @@ func (r *AutoscalingReconciler) reconcileNormalAodh( return ctrlResult, nil } - r.Log.Info("Reconciled Service Aodh successfully") + Log.Info("Reconciled Service Aodh successfully") return ctrl.Result{}, nil } diff --git a/controllers/autoscaling_controller.go b/controllers/autoscaling_controller.go index bdc4315f..45fb06ca 100644 --- a/controllers/autoscaling_controller.go +++ b/controllers/autoscaling_controller.go @@ -36,6 +36,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" @@ -64,10 +65,14 @@ import ( type AutoscalingReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *AutoscalingReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Autoscaling") +} + // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=autoscalings,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=autoscalings/status,verbs=get;update;patch // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=autoscalings/finalizers,verbs=update @@ -96,7 +101,7 @@ type AutoscalingReconciler struct { // Reconcile reconciles an Autoscaling func (r *AutoscalingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - _ = r.Log.WithValues(autoscaling.ServiceName, req.NamespacedName) + Log := r.GetLogger(ctx) // Fetch the Autoscaling instance instance := &telemetryv1.Autoscaling{} @@ -117,7 +122,7 @@ func (r *AutoscalingReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -215,7 +220,8 @@ func (r *AutoscalingReconciler) reconcileDisabled( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service disabled") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service disabled") ctrlResult, err := r.reconcileDisabledPrometheus(ctx, instance, helper) if err != nil { return ctrlResult, err @@ -232,7 +238,7 @@ func (r *AutoscalingReconciler) reconcileDisabled( for _, c := range instance.Status.Conditions { instance.Status.Conditions.MarkTrue(c.Type, telemetryv1.AutoscalingReadyDisabledMessage) } - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' disable successfully", autoscaling.ServiceName)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' disable successfully", autoscaling.ServiceName)) return ctrl.Result{}, nil } @@ -241,7 +247,8 @@ func (r *AutoscalingReconciler) reconcileDelete( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service delete") ctrlResult, err := r.reconcileDeletePrometheus(ctx, instance, helper) if err != nil { @@ -257,7 +264,7 @@ func (r *AutoscalingReconciler) reconcileDelete( } // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", autoscaling.ServiceName)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", autoscaling.ServiceName)) return ctrl.Result{}, nil } @@ -268,7 +275,8 @@ func (r *AutoscalingReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service init") ctrlResult, err := r.reconcileInitPrometheus(ctx, instance, helper, serviceLabels) if err != nil { return ctrlResult, err @@ -281,7 +289,7 @@ func (r *AutoscalingReconciler) reconcileInit( } else if (ctrlResult != ctrl.Result{}) { return ctrlResult, nil } - r.Log.Info("Reconciled Service init successfully") + Log.Info("Reconciled Service init successfully") return ctrl.Result{}, nil } @@ -291,7 +299,8 @@ func (r *AutoscalingReconciler) reconcileNormal( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", autoscaling.ServiceName)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling Service '%s'", autoscaling.ServiceName)) // Service account, role, binding rbacRules := []rbacv1.PolicyRule{ @@ -326,7 +335,7 @@ func (r *AutoscalingReconciler) reconcileNormal( transportURL, op, err := r.transportURLCreateOrUpdate(instance) if err != nil { - r.Log.Info("Error getting transportURL. Setting error condition on status and returning") + Log.Info("Error getting transportURL. Setting error condition on status and returning") instance.Status.Conditions.Set(condition.FalseCondition( condition.RabbitMqTransportURLReadyCondition, condition.ErrorReason, @@ -337,13 +346,13 @@ func (r *AutoscalingReconciler) reconcileNormal( } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op))) + Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op))) } instance.Status.TransportURLSecret = transportURL.Status.SecretName if instance.Status.TransportURLSecret == "" { - r.Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name)) + Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name)) instance.Status.Conditions.Set(condition.FalseCondition( condition.RabbitMqTransportURLReadyCondition, condition.RequestedReason, @@ -500,7 +509,7 @@ func (r *AutoscalingReconciler) reconcileNormal( if err != nil { return ctrlResult, err } - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } @@ -513,6 +522,7 @@ func (r *AutoscalingReconciler) createHashOfInputHashes( instance *telemetryv1.Autoscaling, envVars map[string]env.Setter, ) (string, bool, error) { + Log := r.GetLogger(ctx) var hashMap map[string]string changed := false mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) @@ -522,7 +532,7 @@ func (r *AutoscalingReconciler) createHashOfInputHashes( } if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed { instance.Status.Hash = hashMap - r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) + Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) } return hash, changed, nil } @@ -684,7 +694,7 @@ func (r *AutoscalingReconciler) transportURLCreateOrUpdate(instance *telemetryv1 } // SetupWithManager sets up the controller with the Manager. -func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *AutoscalingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // transportURLSecretFn - Watch for changes made to the secret associated with the RabbitMQ // TransportURL created and used by Autoscaling CRs. Watch functions return a list of namespace-scoped // CRs that then get fed to the reconciler. Hence, in this case, we need to know the name of the @@ -696,6 +706,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error { // reconciliation for a Autoscaling CR that does not need it. // // TODO: We also need a watch func to monitor for changes to the secret referenced by Autoscaling.Spec.Secret + Log := r.GetLogger(ctx) transportURLSecretFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -705,7 +716,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), autoscalings, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Autoscaling CRs %v") + Log.Error(err, "Unable to retrieve Autoscaling CRs %v") return nil } @@ -718,7 +729,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Autoscaling CR %s", o.GetName(), cr.Name)) + Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Autoscaling CR %s", o.GetName(), cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -748,7 +759,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), autoscalings, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Autoscaling CRs %w") + Log.Error(err, "Unable to retrieve Autoscaling CRs %w") return nil } @@ -758,7 +769,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Memcached %s is used by Autoscaling CR %s", o.GetName(), cr.Name)) + Log.Info(fmt.Sprintf("Memcached %s is used by Autoscaling CR %s", o.GetName(), cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } diff --git a/controllers/ceilometer_controller.go b/controllers/ceilometer_controller.go index 459aac4c..2386de40 100644 --- a/controllers/ceilometer_controller.go +++ b/controllers/ceilometer_controller.go @@ -32,6 +32,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" @@ -57,10 +58,14 @@ import ( type CeilometerReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *CeilometerReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Ceilometer") +} + // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=ceilometers,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=ceilometers/status,verbs=get;update;patch // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=ceilometers/finalizers,verbs=update @@ -81,7 +86,7 @@ type CeilometerReconciler struct { // Reconcile reconciles a Ceilometer func (r *CeilometerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues(ceilometer.ServiceName, req.NamespacedName) + Log := r.GetLogger(ctx) // Fetch the Ceilometer instance instance := &telemetryv1.Ceilometer{} @@ -102,7 +107,7 @@ func (r *CeilometerReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -172,7 +177,8 @@ func (r *CeilometerReconciler) Reconcile(ctx context.Context, req ctrl.Request) } func (r *CeilometerReconciler) reconcileDelete(ctx context.Context, instance *telemetryv1.Ceilometer, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service delete") // Remove the finalizer from our KeystoneService CR keystoneService, err := keystonev1.GetKeystoneServiceWithName(ctx, helper, ceilometer.ServiceName, instance.Namespace) @@ -192,7 +198,7 @@ func (r *CeilometerReconciler) reconcileDelete(ctx context.Context, instance *te // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", ceilometer.ServiceName)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", ceilometer.ServiceName)) return ctrl.Result{}, nil } @@ -203,7 +209,8 @@ func (r *CeilometerReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service init") // // create Keystone service and users @@ -247,12 +254,13 @@ func (r *CeilometerReconciler) reconcileInit( instance.Status.Hash = map[string]string{} } - r.Log.Info("Reconciled Service init successfully") + Log.Info("Reconciled Service init successfully") return ctrl.Result{}, nil } func (r *CeilometerReconciler) reconcileNormal(ctx context.Context, instance *telemetryv1.Ceilometer, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", ceilometer.ServiceName)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling Service '%s'", ceilometer.ServiceName)) // Service account, role, binding rbacRules := []rbacv1.PolicyRule{ @@ -281,7 +289,7 @@ func (r *CeilometerReconciler) reconcileNormal(ctx context.Context, instance *te transportURL, op, err := r.transportURLCreateOrUpdate(instance) if err != nil { - r.Log.Info("Error getting transportURL. Setting error condition on status and returning") + Log.Info("Error getting transportURL. Setting error condition on status and returning") instance.Status.Conditions.Set(condition.FalseCondition( condition.RabbitMqTransportURLReadyCondition, condition.ErrorReason, @@ -292,13 +300,13 @@ func (r *CeilometerReconciler) reconcileNormal(ctx context.Context, instance *te } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op))) + Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op))) } instance.Status.TransportURLSecret = transportURL.Status.SecretName if instance.Status.TransportURLSecret == "" { - r.Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name)) + Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name)) instance.Status.Conditions.Set(condition.FalseCondition( condition.RabbitMqTransportURLReadyCondition, condition.RequestedReason, @@ -446,7 +454,7 @@ func (r *CeilometerReconciler) reconcileNormal(ctx context.Context, instance *te return ctrl.Result{}, err } - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } @@ -602,6 +610,7 @@ func (r *CeilometerReconciler) createHashOfInputHashes( instance *telemetryv1.Ceilometer, envVars map[string]env.Setter, ) (string, bool, error) { + Log := r.GetLogger(ctx) var hashMap map[string]string changed := false mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) @@ -611,7 +620,7 @@ func (r *CeilometerReconciler) createHashOfInputHashes( } if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed { instance.Status.Hash = hashMap - r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) + Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) } return hash, changed, nil } @@ -632,7 +641,7 @@ func (r *CeilometerReconciler) transportURLCreateOrUpdate(instance *telemetryv1. } // SetupWithManager sets up the controller with the Manager. -func (r *CeilometerReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *CeilometerReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // transportURLSecretFn - Watch for changes made to the secret associated with the RabbitMQ // TransportURL created and used by Ceilometer CRs. Watch functions return a list of namespace-scoped @@ -645,6 +654,7 @@ func (r *CeilometerReconciler) SetupWithManager(mgr ctrl.Manager) error { // reconciliation for a Ceilometer CR that does not need it. // // TODO: We also need a watch func to monitor for changes to the secret referenced by Ceilometer.Spec.Secret + Log := r.GetLogger(ctx) transportURLSecretFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -654,7 +664,7 @@ func (r *CeilometerReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), ceilometers, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Ceilometer CRs %v") + Log.Error(err, "Unable to retrieve Ceilometer CRs %v") return nil } @@ -667,7 +677,7 @@ func (r *CeilometerReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Ceilometer CR %s", o.GetName(), cr.Name)) + Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Ceilometer CR %s", o.GetName(), cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } diff --git a/controllers/prometheus_controller.go b/controllers/prometheus_controller.go index 129f29c8..6da1c5af 100644 --- a/controllers/prometheus_controller.go +++ b/controllers/prometheus_controller.go @@ -39,7 +39,8 @@ func (r *AutoscalingReconciler) reconcileDisabledPrometheus( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service Prometheus disabled") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service Prometheus disabled") serviceLabels := map[string]string{ common.AppSelector: autoscaling.ServiceName, } @@ -56,7 +57,7 @@ func (r *AutoscalingReconciler) reconcileDisabledPrometheus( for _, c := range instance.Status.Conditions { instance.Status.Conditions.MarkTrue(c.Type, telemetryv1.AutoscalingReadyDisabledMessage) } - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' disable successfully", autoscaling.ServiceName)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' disable successfully", autoscaling.ServiceName)) return ctrl.Result{}, nil } @@ -65,9 +66,10 @@ func (r *AutoscalingReconciler) reconcileDeletePrometheus( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service Prometheus delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service Prometheus delete") // TODO: finalizer prometheus - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", autoscaling.ServiceName)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", autoscaling.ServiceName)) return ctrl.Result{}, nil } @@ -87,11 +89,12 @@ func (r *AutoscalingReconciler) reconcileNormalPrometheus( instance *telemetryv1.Autoscaling, helper *helper.Helper, ) (ctrl.Result, error) { + Log := r.GetLogger(ctx) serviceLabels := map[string]string{ common.AppSelector: autoscaling.ServiceName, } prom := autoscaling.Prometheus(instance, serviceLabels) - r.Log.Info(fmt.Sprintf("Reconciling Service Aodh '%s'", prom.Name)) + Log.Info(fmt.Sprintf("Reconciling Service Aodh '%s'", prom.Name)) var promHost string var promPort int32 @@ -105,7 +108,7 @@ func (r *AutoscalingReconciler) reconcileNormalPrometheus( return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("Prometheus %s successfully reconciled - operation: %s", prom.Name, string(op))) + Log.Info(fmt.Sprintf("Prometheus %s successfully reconciled - operation: %s", prom.Name, string(op))) } promReady := true for _, c := range prom.Status.Conditions { @@ -155,6 +158,6 @@ func (r *AutoscalingReconciler) reconcileNormalPrometheus( instance.Status.PrometheusHost = promHost instance.Status.PrometheusPort = promPort - r.Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' successfully", prom.Name)) + Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' successfully", prom.Name)) return ctrl.Result{}, nil } diff --git a/controllers/telemetry_controller.go b/controllers/telemetry_controller.go index d93b8223..6cf93bab 100644 --- a/controllers/telemetry_controller.go +++ b/controllers/telemetry_controller.go @@ -26,6 +26,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/log" logr "github.com/go-logr/logr" common "github.com/openstack-k8s-operators/lib-common/modules/common" @@ -41,10 +42,14 @@ import ( type TelemetryReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *TelemetryReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Telemetry") +} + // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=telemetries,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=telemetries/status,verbs=get;update;patch // +kubebuilder:rbac:groups=telemetry.openstack.org,resources=telemetries/finalizers,verbs=update @@ -58,7 +63,7 @@ type TelemetryReconciler struct { // Reconcile reconciles a Telemetry func (r *TelemetryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("telemetry", req.NamespacedName) + Log := r.GetLogger(ctx) // Fetch the Telemetry instance instance := &telemetryv1.Telemetry{} @@ -79,7 +84,7 @@ func (r *TelemetryReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -154,12 +159,13 @@ func (r *TelemetryReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( } func (r *TelemetryReconciler) reconcileDelete(ctx context.Context, instance *telemetryv1.Telemetry, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete", instance.Name)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciled Service '%s' delete", instance.Name)) // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name)) return ctrl.Result{}, nil } @@ -169,14 +175,16 @@ func (r *TelemetryReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service init") - r.Log.Info("Reconciled Service init successfully") + Log.Info("Reconciled Service init successfully") return ctrl.Result{}, nil } func (r *TelemetryReconciler) reconcileNormal(ctx context.Context, instance *telemetryv1.Telemetry, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name)) // deploy autoscaling autoscaling, op, err := r.autoscalingCreateOrUpdate(instance) @@ -190,7 +198,7 @@ func (r *TelemetryReconciler) reconcileNormal(ctx context.Context, instance *tel return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) + Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) } // Mirror autoscaling's condition status as := autoscaling.Status.Conditions.Mirror(telemetryv1.AutoscalingReadyCondition) @@ -211,7 +219,7 @@ func (r *TelemetryReconciler) reconcileNormal(ctx context.Context, instance *tel return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) + Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) } // Mirror ceilometer's status ReadyCount to this parent CR @@ -224,7 +232,7 @@ func (r *TelemetryReconciler) reconcileNormal(ctx context.Context, instance *tel } // end deploy ceilometer - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } diff --git a/main.go b/main.go index 9a41acbd..0fe474bc 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "flag" "os" "strings" @@ -124,7 +125,6 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Telemetry"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create Telemetry controller") os.Exit(1) @@ -134,8 +134,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Ceilometer"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create Ceilometer controller") os.Exit(1) } @@ -144,8 +143,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Autoscaling"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create Autoscaling controller") os.Exit(1) }