diff --git a/controllers/placementapi_controller.go b/controllers/placementapi_controller.go index 2cf3a84f..2e25041a 100644 --- a/controllers/placementapi_controller.go +++ b/controllers/placementapi_controller.go @@ -58,11 +58,15 @@ import ( k8s_errors "k8s.io/apimachinery/pkg/api/errors" ) +// GetLog returns a logger object with a prefix of "controller.name" and additional controller context fields +func (r *PlacementAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("PlacementAPI") +} + // PlacementAPIReconciler reconciles a PlacementAPI object type PlacementAPIReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } @@ -91,7 +95,7 @@ type PlacementAPIReconciler struct { // Reconcile reconcile placement API requests func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // Fetch the PlacementAPI instance instance := &placementv1.PlacementAPI{} @@ -112,7 +116,7 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -207,7 +211,8 @@ func (r *PlacementAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *PlacementAPIReconciler) reconcileDelete(ctx context.Context, instance *placementv1.PlacementAPI, helper *helper.Helper) (ctrl.Result, error) { - util.LogForObject(helper, "Reconciling Service delete", instance) + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service delete") // remove db finalizer before the placement one db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name) @@ -233,7 +238,7 @@ func (r *PlacementAPIReconciler) reconcileDelete(ctx context.Context, instance * if err != nil && !k8s_errors.IsNotFound(err) { return ctrl.Result{}, err } - util.LogForObject(helper, "Removed finalizer from our KeystoneEndpoint", instance) + Log.Info("Removed finalizer from our KeystoneEndpoint") } } @@ -249,14 +254,14 @@ func (r *PlacementAPIReconciler) reconcileDelete(ctx context.Context, instance * if err != nil && !k8s_errors.IsNotFound(err) { return ctrl.Result{}, err } - util.LogForObject(helper, "Removed finalizer from our KeystoneService", instance) + Log.Info("Removed finalizer from our KeystoneService") } } // We did all the cleanup on the objects we created so we can remove the // finalizer from ourselves to allow the deletion controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - util.LogForObject(helper, "Reconciled Service delete successfully", instance) + Log.Info("Reconciled Service delete successfully") return ctrl.Result{}, nil } @@ -267,8 +272,8 @@ func (r *PlacementAPIReconciler) reconcileInit( serviceLabels map[string]string, serviceAnnotations map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Service init") - + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service init") // Service account, role, binding rbacRules := []rbacv1.PolicyRule{ { @@ -546,39 +551,40 @@ func (r *PlacementAPIReconciler) reconcileInit( } if dbSyncjob.HasChanged() { instance.Status.Hash[placementv1.DbSyncHash] = dbSyncjob.GetHash() - r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[placementv1.DbSyncHash])) + Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[placementv1.DbSyncHash])) } instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage) // run placement db sync - end - r.Log.Info("Reconciled Service init successfully") + Log.Info("Reconciled Service init successfully") return ctrl.Result{}, nil } func (r *PlacementAPIReconciler) reconcileUpdate(ctx context.Context, instance *placementv1.PlacementAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service update") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service update") // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled Service update successfully") + Log.Info("Reconciled Service update successfully") return ctrl.Result{}, nil } func (r *PlacementAPIReconciler) reconcileUpgrade(ctx context.Context, instance *placementv1.PlacementAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service upgrade") - + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service update") // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled Service upgrade successfully") + Log.Info("Reconciled Service upgrade successfully") return ctrl.Result{}, nil } func (r *PlacementAPIReconciler) reconcileNormal(ctx context.Context, instance *placementv1.PlacementAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") - + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -763,7 +769,7 @@ func (r *PlacementAPIReconciler) reconcileNormal(ctx context.Context, instance * } // create Deployment - end - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } @@ -843,6 +849,7 @@ func (r *PlacementAPIReconciler) createHashOfInputHashes( instance *placementv1.PlacementAPI, 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) @@ -852,7 +859,7 @@ func (r *PlacementAPIReconciler) 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 62ad875b..3dbc4ef2 100644 --- a/main.go +++ b/main.go @@ -114,7 +114,6 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("PlacementAPI"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "PlacementAPI") os.Exit(1) diff --git a/tests/functional/suite_test.go b/tests/functional/suite_test.go index e94f337f..d7f8508b 100644 --- a/tests/functional/suite_test.go +++ b/tests/functional/suite_test.go @@ -168,7 +168,6 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("PlacementAPI"), }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred())