From 7582147092b4a97ea64956fc25ca025e003efea0 Mon Sep 17 00:00:00 2001 From: r Date: Sun, 3 Sep 2023 09:33:49 +0300 Subject: [PATCH] switch to structured logging update logging fix name update test logging update logging update logging --- controllers/manila_controller.go | 67 ++++++++++++++--------- controllers/manilaapi_controller.go | 61 ++++++++++++--------- controllers/manilascheduler_controller.go | 60 ++++++++++++-------- controllers/manilashare_controller.go | 55 +++++++++++-------- main.go | 13 ++--- test/functional/suite_test.go | 12 ++-- 6 files changed, 152 insertions(+), 116 deletions(-) diff --git a/controllers/manila_controller.go b/controllers/manila_controller.go index 207c07fe..d99d7a24 100644 --- a/controllers/manila_controller.go +++ b/controllers/manila_controller.go @@ -65,9 +65,9 @@ func (r *ManilaReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *ManilaReconciler) GetLogger() logr.Logger { - return r.Log +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *ManilaReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Manila") } // GetScheme - @@ -79,7 +79,6 @@ func (r *ManilaReconciler) GetScheme() *runtime.Scheme { type ManilaReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } @@ -114,7 +113,7 @@ type ManilaReconciler struct { // Reconcile - func (r *ManilaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) instance := &manilav1beta1.Manila{} err := r.Client.Get(ctx, req.NamespacedName, instance) @@ -130,7 +129,7 @@ func (r *ManilaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -205,7 +204,9 @@ func (r *ManilaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res } // SetupWithManager sets up the controller with the Manager. -func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *ManilaReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { + Log := r.GetLogger(ctx) + // transportURLSecretFn - Watch for changes made to the secret associated with the RabbitMQ // TransportURL created and used by Manila 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 @@ -225,8 +226,8 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(o.GetNamespace()), } - if err := r.Client.List(context.Background(), manilas, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Manila CRs %v") + if err := r.Client.List(ctx, manilas, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve Manila CRs %v") return nil } @@ -239,7 +240,7 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Manila CR %s", o.GetName(), cr.Name)) + Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Manila CR %s", o.GetName(), cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -271,7 +272,9 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *ManilaReconciler) reconcileDelete(ctx context.Context, instance *manilav1beta1.Manila, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) // remove db finalizer first db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name) @@ -287,7 +290,7 @@ func (r *ManilaReconciler) reconcileDelete(ctx context.Context, instance *manila // 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 } @@ -299,7 +302,9 @@ func (r *ManilaReconciler) reconcileInit( serviceLabels map[string]string, serviceAnnotations map[string]string, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) // // create service DB instance @@ -396,18 +401,20 @@ func (r *ManilaReconciler) reconcileInit( } if dbSyncjob.HasChanged() { instance.Status.Hash[manilav1beta1.DbSyncHash] = dbSyncjob.GetHash() - r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[manilav1beta1.DbSyncHash])) + Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[manilav1beta1.DbSyncHash])) } instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage) // run Manila db sync - end - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manilav1beta1.Manila, 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)) // Service account, role, binding rbacRules := []rbacv1.PolicyRule{ @@ -450,13 +457,13 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila } 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, @@ -613,7 +620,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila 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 ManilaAPI status' ReadyCount to this parent CR @@ -638,7 +645,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila 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 ManilaScheduler status' ReadyCount to this parent CR @@ -665,7 +672,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila 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 ManilaShare status' ReadyCount to this parent CR @@ -716,27 +723,31 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila instance.Status.Conditions.MarkTrue(condition.CronJobReadyCondition, condition.CronJobReadyMessage) // create CronJob - end - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaReconciler) reconcileUpdate(ctx context.Context, instance *manilav1beta1.Manila, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaReconciler) reconcileUpgrade(ctx context.Context, instance *manilav1beta1.Manila, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) return ctrl.Result{}, nil } @@ -836,6 +847,8 @@ func (r *ManilaReconciler) createHashOfInputHashes( instance *manilav1beta1.Manila, 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) @@ -845,7 +858,7 @@ func (r *ManilaReconciler) 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 } diff --git a/controllers/manilaapi_controller.go b/controllers/manilaapi_controller.go index 52e5c643..d8ac68d1 100644 --- a/controllers/manilaapi_controller.go +++ b/controllers/manilaapi_controller.go @@ -28,7 +28,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "github.com/go-logr/logr" - keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1" "github.com/openstack-k8s-operators/lib-common/modules/common" "github.com/openstack-k8s-operators/lib-common/modules/common/condition" @@ -58,7 +57,6 @@ type ManilaAPIReconciler struct { client.Client Scheme *runtime.Scheme Kclient kubernetes.Interface - Log logr.Logger } var ( @@ -77,6 +75,11 @@ var ( } ) +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *ManilaAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("ManilaAPI") +} + //+kubebuilder:rbac:groups=manila.openstack.org,resources=manilaapis,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=manila.openstack.org,resources=manilaapis/status,verbs=get;update;patch //+kubebuilder:rbac:groups=manila.openstack.org,resources=manilaapis/finalizers,verbs=update @@ -98,11 +101,6 @@ func (r *ManilaAPIReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *ManilaAPIReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *ManilaAPIReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -110,7 +108,7 @@ func (r *ManilaAPIReconciler) GetScheme() *runtime.Scheme { // Reconcile - func (r *ManilaAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // Fetch the ManilaAPI instance instance := &manilav1beta1.ManilaAPI{} @@ -131,7 +129,7 @@ func (r *ManilaAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -202,7 +200,8 @@ func (r *ManilaAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( } // SetupWithManager sets up the controller with the Manager. -func (r *ManilaAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *ManilaAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { + Log := r.GetLogger(ctx) // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top Manila controller. @@ -216,8 +215,8 @@ func (r *ManilaAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(namespace), } - if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve API CRs %v") + if err := r.Client.List(ctx, apis, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve API CRs %v") return nil } // Watch for changes to secrets where the owner label AND the @@ -232,7 +231,7 @@ func (r *ManilaAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("Secret %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } @@ -245,7 +244,7 @@ func (r *ManilaAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Manila CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Manila CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -270,7 +269,9 @@ func (r *ManilaAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *ManilaAPIReconciler) reconcileDelete(ctx context.Context, instance *manilav1beta1.ManilaAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) for _, ksSvc := range keystoneServices { @@ -305,7 +306,7 @@ func (r *ManilaAPIReconciler) reconcileDelete(ctx context.Context, instance *man // 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 } @@ -316,7 +317,9 @@ func (r *ManilaAPIReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) // // expose the service (create service and return the created endpoint URLs) @@ -505,12 +508,14 @@ func (r *ManilaAPIReconciler) reconcileInit( } } - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaAPIReconciler) reconcileNormal(ctx context.Context, instance *manilav1beta1.ManilaAPI, 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)) // ConfigVars configVars := make(map[string]env.Setter) @@ -727,27 +732,31 @@ func (r *ManilaAPIReconciler) reconcileNormal(ctx context.Context, instance *man } // create Deployment - end - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaAPIReconciler) reconcileUpdate(ctx context.Context, instance *manilav1beta1.ManilaAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaAPIReconciler) reconcileUpgrade(ctx context.Context, instance *manilav1beta1.ManilaAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) return ctrl.Result{}, nil } @@ -859,6 +868,8 @@ func (r *ManilaAPIReconciler) createHashOfInputHashes( instance *manilav1beta1.ManilaAPI, 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) @@ -868,7 +879,7 @@ func (r *ManilaAPIReconciler) 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 } diff --git a/controllers/manilascheduler_controller.go b/controllers/manilascheduler_controller.go index 581ef177..3ef083d7 100644 --- a/controllers/manilascheduler_controller.go +++ b/controllers/manilascheduler_controller.go @@ -59,11 +59,6 @@ func (r *ManilaSchedulerReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *ManilaSchedulerReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *ManilaSchedulerReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -73,10 +68,14 @@ func (r *ManilaSchedulerReconciler) GetScheme() *runtime.Scheme { type ManilaSchedulerReconciler struct { client.Client Scheme *runtime.Scheme - Log logr.Logger Kclient kubernetes.Interface } +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *ManilaSchedulerReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("ManilaScheduler") +} + //+kubebuilder:rbac:groups=manila.openstack.org,resources=manilaschedulers,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=manila.openstack.org,resources=manilaschedulers/status,verbs=get;update;patch //+kubebuilder:rbac:groups=manila.openstack.org,resources=manilaschedulers/finalizers,verbs=update @@ -87,7 +86,7 @@ type ManilaSchedulerReconciler struct { // Reconcile -- func (r *ManilaSchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // Fetch the ManilaScheduler instance instance := &manilav1beta1.ManilaScheduler{} @@ -108,7 +107,7 @@ func (r *ManilaSchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Requ r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -172,7 +171,8 @@ func (r *ManilaSchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Requ } // SetupWithManager sets up the controller with the Manager. -func (r *ManilaSchedulerReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *ManilaSchedulerReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { + Log := r.GetLogger(ctx) // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top Manila controller. @@ -186,8 +186,8 @@ func (r *ManilaSchedulerReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(namespace), } - if err := r.Client.List(context.Background(), schedulers, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve API CRs %v") + if err := r.Client.List(ctx, schedulers, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve API CRs %v") return nil } @@ -203,7 +203,7 @@ func (r *ManilaSchedulerReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } @@ -216,7 +216,7 @@ func (r *ManilaSchedulerReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Manila CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Manila CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -238,11 +238,13 @@ func (r *ManilaSchedulerReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *ManilaSchedulerReconciler) reconcileDelete(ctx context.Context, instance *manilav1beta1.ManilaScheduler, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling 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 } @@ -253,14 +255,18 @@ func (r *ManilaSchedulerReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + Log := r.GetLogger(ctx) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + + Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaSchedulerReconciler) reconcileNormal(ctx context.Context, instance *manilav1beta1.ManilaScheduler, 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)) // ConfigMap configVars := make(map[string]env.Setter) @@ -475,27 +481,31 @@ func (r *ManilaSchedulerReconciler) reconcileNormal(ctx context.Context, instanc } // create StatefulSet - end - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaSchedulerReconciler) reconcileUpdate(ctx context.Context, instance *manilav1beta1.ManilaScheduler, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaSchedulerReconciler) reconcileUpgrade(ctx context.Context, instance *manilav1beta1.ManilaScheduler, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) return ctrl.Result{}, nil } @@ -604,6 +614,8 @@ func (r *ManilaSchedulerReconciler) createHashOfInputHashes( instance *manilav1beta1.ManilaScheduler, 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) @@ -613,7 +625,7 @@ func (r *ManilaSchedulerReconciler) 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 } diff --git a/controllers/manilashare_controller.go b/controllers/manilashare_controller.go index 20c842af..3a58ca40 100644 --- a/controllers/manilashare_controller.go +++ b/controllers/manilashare_controller.go @@ -59,11 +59,6 @@ func (r *ManilaShareReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *ManilaShareReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *ManilaShareReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -74,7 +69,11 @@ type ManilaShareReconciler struct { client.Client Scheme *runtime.Scheme Kclient kubernetes.Interface - Log logr.Logger +} + +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *ManilaShareReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("ManilaShare") } //+kubebuilder:rbac:groups=manila.openstack.org,resources=manilashares,verbs=get;list;watch;create;update;patch;delete @@ -88,7 +87,7 @@ type ManilaShareReconciler struct { // Reconcile - func (r *ManilaShareReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // Fetch the ManilaShare instance instance := &manilav1beta1.ManilaShare{} @@ -109,7 +108,7 @@ func (r *ManilaShareReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -173,7 +172,9 @@ func (r *ManilaShareReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // SetupWithManager sets up the controller with the Manager. -func (r *ManilaShareReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *ManilaShareReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { + + Log := r.GetLogger(ctx) // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top Manila controller. @@ -187,8 +188,8 @@ func (r *ManilaShareReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(namespace), } - if err := r.Client.List(context.Background(), shares, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve API CRs %v") + if err := r.Client.List(ctx, shares, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve API CRs %v") return nil } @@ -202,7 +203,7 @@ func (r *ManilaShareReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("Secret object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -214,7 +215,7 @@ func (r *ManilaShareReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Manila CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Manila CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -236,11 +237,12 @@ func (r *ManilaShareReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *ManilaShareReconciler) reconcileDelete(ctx context.Context, instance *manilav1beta1.ManilaShare, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling 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 } @@ -251,14 +253,16 @@ func (r *ManilaShareReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaShareReconciler) reconcileNormal(ctx context.Context, instance *manilav1beta1.ManilaShare, 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)) // configVars configVars := make(map[string]env.Setter) @@ -472,27 +476,29 @@ func (r *ManilaShareReconciler) reconcileNormal(ctx context.Context, instance *m } // create StatefulSet - end - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaShareReconciler) reconcileUpdate(ctx context.Context, instance *manilav1beta1.ManilaShare, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) return ctrl.Result{}, nil } func (r *ManilaShareReconciler) reconcileUpgrade(ctx context.Context, instance *manilav1beta1.ManilaShare, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) + Log := r.GetLogger(ctx) + Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) return ctrl.Result{}, nil } @@ -602,6 +608,7 @@ func (r *ManilaShareReconciler) createHashOfInputHashes( instance *manilav1beta1.ManilaShare, 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 +618,7 @@ func (r *ManilaShareReconciler) 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 } diff --git a/main.go b/main.go index bd270cbd..f06cdd76 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "flag" "os" "strings" @@ -114,8 +115,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("ManilaAPI"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "ManilaAPI") os.Exit(1) } @@ -123,8 +123,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Manila"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Manila") os.Exit(1) } @@ -132,8 +131,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("ManilaScheduler"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "ManilaScheduler") os.Exit(1) } @@ -141,8 +139,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("ManilaShare"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "ManilaShare") os.Exit(1) } diff --git a/test/functional/suite_test.go b/test/functional/suite_test.go index cf499333..586606f6 100644 --- a/test/functional/suite_test.go +++ b/test/functional/suite_test.go @@ -190,8 +190,7 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Manila"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(context.Background(), k8sManager) Expect(err).ToNot(HaveOccurred()) @@ -202,24 +201,21 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("ManilaAPI"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(context.Background(), k8sManager) Expect(err).ToNot(HaveOccurred()) err = (&controllers.ManilaSchedulerReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("ManilaScheduler"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(context.Background(), k8sManager) Expect(err).ToNot(HaveOccurred()) err = (&controllers.ManilaShareReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("ManilaShare"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(context.Background(), k8sManager) Expect(err).ToNot(HaveOccurred()) go func() {