Skip to content

Commit

Permalink
Logger as part of the basereconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
roivaz committed Dec 11, 2023
1 parent 140f621 commit b21a747
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions reconciler/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (

"github.com/3scale-ops/basereconciler/config"
"github.com/3scale-ops/basereconciler/util"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

func (r *Reconciler) pruneOrphaned(ctx context.Context, owner client.Object, managed []corev1.ObjectReference) error {
logger := log.FromContext(ctx)
logger := logr.FromContextOrDiscard(ctx)

ownerGVK, err := apiutil.GVKForObject(owner, r.Scheme)
if err != nil {
Expand Down
29 changes: 24 additions & 5 deletions reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,40 @@ 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/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

// Reconciler computes a list of resources that it needs to keep in place
type Reconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
typeTracker typeTracker
}

// NewFromManager returns a new Reconciler from a controller-runtime manager.Manager
func NewFromManager(mgr manager.Manager) Reconciler {
return Reconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme()}
func NewFromManager(mgr manager.Manager) *Reconciler {
return &Reconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Log: logr.Discard()}
}

func (r *Reconciler) WithLogger(logger logr.Logger) *Reconciler {
r.Log = logger
return r
}

func (r *Reconciler) GetLogger(ctx context.Context) logr.Logger {
if logger, err := logr.FromContext(ctx); err != nil {
return r.Log
} else {
return logger
}
}

func (r *Reconciler) SetLogger(ctx *context.Context, keysAndValues ...interface{}) logr.Logger {
logger := r.GetLogger(*ctx).WithValues(keysAndValues)
*ctx = logr.NewContext(*ctx, logger)
return logger
}

// GetInstance tries to retrieve the custom resource instance and perform some standard
Expand All @@ -42,8 +61,8 @@ func NewFromManager(mgr manager.Manager) Reconciler {
// run when the custom resource is being deleted. Only works with a non-nil finalizer, otherwise
// the custom resource will be immediately deleted and the functions won't run.
func (r *Reconciler) GetInstance(ctx context.Context, key types.NamespacedName,
instance client.Object, finalizer *string, cleanupFns []func()) (*ctrl.Result, error) {
logger := log.FromContext(ctx)
instance client.Object, finalizer *string, cleanupFns ...func()) (*ctrl.Result, error) {
logger := logr.FromContextOrDiscard(ctx)

err := r.Client.Get(ctx, key, instance)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions reconciler/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package reconciler
import (
"context"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// ReconcileStatus can reconcile the status of a custom resource when the resource implements
Expand All @@ -17,7 +17,7 @@ import (
// reconcile the status of the custom resource and return whether update is required or not.
func (r *Reconciler) ReconcileStatus(ctx context.Context, instance ObjectWithAppStatus,
deployments, statefulsets []types.NamespacedName, mutators ...func() bool) error {
logger := log.FromContext(ctx)
logger := logr.FromContextOrDiscard(ctx)
update := false
status := instance.GetStatus()

Expand Down
4 changes: 2 additions & 2 deletions resource/create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/3scale-ops/basereconciler/config"
"github.com/3scale-ops/basereconciler/util"
"github.com/go-logr/logr"
"github.com/nsf/jsondiff"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand All @@ -18,7 +19,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// CreateOrUpdate cretes or updates resources. The function receives several paremters:
Expand All @@ -42,7 +42,7 @@ func CreateOrUpdate(ctx context.Context, cl client.Client, scheme *runtime.Schem
if err != nil {
return nil, err
}
logger := log.FromContext(ctx, "gvk", gvk, "resource", desired.GetName())
logger := logr.FromContextOrDiscard(ctx).WithValues("gvk", gvk, "resource", desired.GetName())

live, err := util.NewObjectFromGVK(gvk, scheme)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ var _ = BeforeSuite(func() {

// Add controllers for testing
err = (&Reconciler{
Reconciler: reconciler.NewFromManager(mgr),
Log: ctrl.Log.WithName("controllers").WithName("Test"),
Reconciler: *reconciler.NewFromManager(mgr).
WithLogger(ctrl.Log.WithName("controllers").WithName("Test")),
}).SetupWithManager(mgr)
Expect(err).ToNot(HaveOccurred())

Expand Down
2 changes: 0 additions & 2 deletions test/test_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/3scale-ops/basereconciler/resource"
"github.com/3scale-ops/basereconciler/test/api/v1alpha1"
"github.com/3scale-ops/basereconciler/util"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
Expand All @@ -43,7 +42,6 @@ import (
// +kubebuilder:object:generate=false
type Reconciler struct {
reconciler.Reconciler
Log logr.Logger
}

// Reconcile is part of the main kubernetes reconciliation loop which aims to
Expand Down

0 comments on commit b21a747

Please sign in to comment.