From 2f78826be2485daa211085890b60f70f74e9b4a7 Mon Sep 17 00:00:00 2001 From: Warren Fernandes Date: Wed, 27 Jan 2021 20:23:26 -0700 Subject: [PATCH] Use logger from context --- operator/controllers/coreprovider_controller.go | 6 ++---- operator/main.go | 9 ++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/operator/controllers/coreprovider_controller.go b/operator/controllers/coreprovider_controller.go index a10b7f11bc76..177a01ca9cad 100644 --- a/operator/controllers/coreprovider_controller.go +++ b/operator/controllers/coreprovider_controller.go @@ -19,7 +19,6 @@ package controllers import ( "context" - "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -30,7 +29,6 @@ import ( // CoreProviderReconciler reconciles a CoreProvider object type CoreProviderReconciler struct { client.Client - Log logr.Logger Scheme *runtime.Scheme } @@ -38,8 +36,8 @@ type CoreProviderReconciler struct { // +kubebuilder:rbac:groups=operator.cluster.x-k8s.io,resources=coreproviders/status,verbs=get;update;patch func (r *CoreProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - _ = r.Log.WithValues("coreprovider", req.NamespacedName) - + log := ctrl.LoggerFrom(ctx) + _ = log // your logic here return ctrl.Result{}, nil diff --git a/operator/main.go b/operator/main.go index 9886b5f49e0d..304592a132f8 100644 --- a/operator/main.go +++ b/operator/main.go @@ -26,6 +26,7 @@ import ( "k8s.io/klog/klogr" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/cluster-api/cmd/version" operatorv1alpha4 "sigs.k8s.io/cluster-api/operator/api/v1alpha4" "sigs.k8s.io/cluster-api/operator/controllers" // +kubebuilder:scaffold:imports @@ -66,9 +67,11 @@ func main() { os.Exit(1) } + // Setup the context that's going to be used in controllers and for the manager. + ctx := ctrl.SetupSignalHandler() + if err = (&controllers.CoreProviderReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("CoreProvider"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "CoreProvider") @@ -76,8 +79,8 @@ func main() { } // +kubebuilder:scaffold:builder - setupLog.Info("starting manager") - if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { + setupLog.Info("starting manager", "version", version.Get().String()) + if err := mgr.Start(ctx); err != nil { setupLog.Error(err, "problem running manager") os.Exit(1) }