Skip to content

Commit

Permalink
Use logger from context
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren Fernandes committed Jan 28, 2021
1 parent 8b8a6b4 commit 2f78826
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 2 additions & 4 deletions operator/controllers/coreprovider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,16 +29,15 @@ import (
// CoreProviderReconciler reconciles a CoreProvider object
type CoreProviderReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups=operator.cluster.x-k8s.io,resources=coreproviders,verbs=get;list;watch;create;update;patch;delete
// +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
Expand Down
9 changes: 6 additions & 3 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,18 +67,20 @@ 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")
os.Exit(1)
}
// +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)
}
Expand Down

0 comments on commit 2f78826

Please sign in to comment.