diff --git a/controllers/controllers/networking/cfdomain_controller.go b/controllers/controllers/networking/cfdomain_controller.go index 83e2c6587..3b53d7c3c 100644 --- a/controllers/controllers/networking/cfdomain_controller.go +++ b/controllers/controllers/networking/cfdomain_controller.go @@ -59,8 +59,7 @@ func (r *CFDomainReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builder //+kubebuilder:rbac:groups=korifi.cloudfoundry.org,resources=cfdomains/finalizers,verbs=update func (r *CFDomainReconciler) ReconcileResource(ctx context.Context, cfDomain *korifiv1alpha1.CFDomain) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfDomain) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) if !cfDomain.GetDeletionTimestamp().IsZero() { return r.finalizeCFDomain(ctx, cfDomain) diff --git a/controllers/controllers/networking/cfroute_controller.go b/controllers/controllers/networking/cfroute_controller.go index f5499b877..a615f7142 100644 --- a/controllers/controllers/networking/cfroute_controller.go +++ b/controllers/controllers/networking/cfroute_controller.go @@ -23,7 +23,6 @@ import ( korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/controllers/config" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/tools/k8s" "github.com/go-logr/logr" @@ -75,8 +74,7 @@ func (r *CFRouteReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builder //+kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;patch;delete func (r *CFRouteReconciler) ReconcileResource(ctx context.Context, cfRoute *korifiv1alpha1.CFRoute) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfRoute) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) var err error diff --git a/controllers/controllers/services/cfservicebinding_controller.go b/controllers/controllers/services/cfservicebinding_controller.go index f2a5d9382..089e1c554 100644 --- a/controllers/controllers/services/cfservicebinding_controller.go +++ b/controllers/controllers/services/cfservicebinding_controller.go @@ -23,7 +23,6 @@ import ( "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/tools/k8s" "github.com/go-logr/logr" @@ -73,8 +72,7 @@ func (r *CFServiceBindingReconciler) SetupWithManager(mgr ctrl.Manager) *builder //+kubebuilder:rbac:groups=servicebinding.io,resources=servicebindings,verbs=get;list;create;update;patch;watch func (r *CFServiceBindingReconciler) ReconcileResource(ctx context.Context, cfServiceBinding *korifiv1alpha1.CFServiceBinding) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfServiceBinding) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfServiceBinding.Status.ObservedGeneration = cfServiceBinding.Generation log.V(1).Info("set observed generation", "generation", cfServiceBinding.Status.ObservedGeneration) diff --git a/controllers/controllers/services/cfserviceinstance_controller.go b/controllers/controllers/services/cfserviceinstance_controller.go index 73bb1071b..da050dd38 100644 --- a/controllers/controllers/services/cfserviceinstance_controller.go +++ b/controllers/controllers/services/cfserviceinstance_controller.go @@ -21,7 +21,6 @@ import ( "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/tools/k8s" "github.com/go-logr/logr" @@ -62,8 +61,7 @@ func (r *CFServiceInstanceReconciler) SetupWithManager(mgr ctrl.Manager) *builde //+kubebuilder:rbac:groups=korifi.cloudfoundry.org,resources=cfserviceinstances/finalizers,verbs=update func (r *CFServiceInstanceReconciler) ReconcileResource(ctx context.Context, cfServiceInstance *korifiv1alpha1.CFServiceInstance) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfServiceInstance) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfServiceInstance.Status.ObservedGeneration = cfServiceInstance.Generation log.V(1).Info("set observed generation", "generation", cfServiceInstance.Status.ObservedGeneration) diff --git a/controllers/controllers/shared/index.go b/controllers/controllers/shared/index.go index f49d261f5..a613148db 100644 --- a/controllers/controllers/shared/index.go +++ b/controllers/controllers/shared/index.go @@ -7,7 +7,6 @@ import ( korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "github.com/go-logr/logr" - "github.com/google/uuid" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -140,7 +139,3 @@ func RemovePackageManagerKeys(src map[string]string, log logr.Logger) map[string return dest } - -func ObjectLogger(log logr.Logger, obj client.Object) logr.Logger { - return log.WithValues("namespace", obj.GetNamespace(), "name", obj.GetName(), "logID", uuid.NewString()) -} diff --git a/controllers/controllers/workloads/build/cf_build_controller.go b/controllers/controllers/workloads/build/cf_build_controller.go index c05c8109e..0868fdaf3 100644 --- a/controllers/controllers/workloads/build/cf_build_controller.go +++ b/controllers/controllers/workloads/build/cf_build_controller.go @@ -56,8 +56,7 @@ func (r *CFBuildReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builder } func (r *CFBuildReconciler) ReconcileResource(ctx context.Context, cfBuild *korifiv1alpha1.CFBuild) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfBuild) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfBuild.Status.ObservedGeneration = cfBuild.Generation log.V(1).Info("set observed generation", "generation", cfBuild.Status.ObservedGeneration) diff --git a/controllers/controllers/workloads/cfapp_controller.go b/controllers/controllers/workloads/cfapp_controller.go index 077fd7f8b..6ac78ba3f 100644 --- a/controllers/controllers/workloads/cfapp_controller.go +++ b/controllers/controllers/workloads/cfapp_controller.go @@ -101,8 +101,7 @@ func serviceBindingToApp(ctx context.Context, o client.Object) []reconcile.Reque //+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;patch func (r *CFAppReconciler) ReconcileResource(ctx context.Context, cfApp *korifiv1alpha1.CFApp) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfApp) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfApp.Status.ObservedGeneration = cfApp.Generation log.V(1).Info("set observed generation", "generation", cfApp.Status.ObservedGeneration) diff --git a/controllers/controllers/workloads/cforg_controller.go b/controllers/controllers/workloads/cforg_controller.go index f5af9d333..4132311d6 100644 --- a/controllers/controllers/workloads/cforg_controller.go +++ b/controllers/controllers/workloads/cforg_controller.go @@ -122,8 +122,7 @@ func (r *CFOrgReconciler) enqueueCFOrgRequests(ctx context.Context, object clien //+kubebuilder:rbac:groups="policy",resources=podsecuritypolicies,verbs=use func (r *CFOrgReconciler) ReconcileResource(ctx context.Context, cfOrg *korifiv1alpha1.CFOrg) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfOrg) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfOrg.Status.ObservedGeneration = cfOrg.Generation log.V(1).Info("set observed generation", "generation", cfOrg.Status.ObservedGeneration) diff --git a/controllers/controllers/workloads/cfpackage_controller.go b/controllers/controllers/workloads/cfpackage_controller.go index b1d2b6df2..09c7c7ca5 100644 --- a/controllers/controllers/workloads/cfpackage_controller.go +++ b/controllers/controllers/workloads/cfpackage_controller.go @@ -89,8 +89,7 @@ func (r *CFPackageReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builde //+kubebuilder:rbac:groups=korifi.cloudfoundry.org,resources=cfpackages/finalizers,verbs=get;update;patch func (r *CFPackageReconciler) ReconcileResource(ctx context.Context, cfPackage *korifiv1alpha1.CFPackage) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfPackage) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfPackage.Status.ObservedGeneration = cfPackage.Generation log.V(1).Info("set observed generation", "generation", cfPackage.Status.ObservedGeneration) diff --git a/controllers/controllers/workloads/cfprocess_controller.go b/controllers/controllers/workloads/cfprocess_controller.go index 20734deee..d537ed579 100644 --- a/controllers/controllers/workloads/cfprocess_controller.go +++ b/controllers/controllers/workloads/cfprocess_controller.go @@ -103,8 +103,7 @@ func (r *CFProcessReconciler) enqueueCFProcessRequests(ctx context.Context, o cl //+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;patch func (r *CFProcessReconciler) ReconcileResource(ctx context.Context, cfProcess *korifiv1alpha1.CFProcess) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfProcess) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfProcess.Status.ObservedGeneration = cfProcess.Generation log.V(1).Info("set observed generation", "generation", cfProcess.Status.ObservedGeneration) diff --git a/controllers/controllers/workloads/cfspace_controller.go b/controllers/controllers/workloads/cfspace_controller.go index 9074452a7..e4d6efaf0 100644 --- a/controllers/controllers/workloads/cfspace_controller.go +++ b/controllers/controllers/workloads/cfspace_controller.go @@ -137,8 +137,7 @@ func (r *CFSpaceReconciler) enqueueCFSpaceRequestsForServiceAccount(ctx context. //+kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;patch;delete func (r *CFSpaceReconciler) ReconcileResource(ctx context.Context, cfSpace *korifiv1alpha1.CFSpace) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfSpace) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfSpace.Status.ObservedGeneration = cfSpace.Generation log.V(1).Info("set observed generation", "generation", cfSpace.Status.ObservedGeneration) diff --git a/controllers/controllers/workloads/cftask_controller.go b/controllers/controllers/workloads/cftask_controller.go index eadf3b1eb..ad771fcec 100644 --- a/controllers/controllers/workloads/cftask_controller.go +++ b/controllers/controllers/workloads/cftask_controller.go @@ -88,8 +88,7 @@ func (r *CFTaskReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builder { //+kubebuilder:rbac:groups="",resources=events,verbs=create;patch func (r *CFTaskReconciler) ReconcileResource(ctx context.Context, cfTask *korifiv1alpha1.CFTask) (ctrl.Result, error) { - log := shared.ObjectLogger(r.log, cfTask) - ctx = logr.NewContext(ctx, log) + log := logr.FromContextOrDiscard(ctx) cfTask.Status.ObservedGeneration = cfTask.Generation log.V(1).Info("set observed generation", "generation", cfTask.Status.ObservedGeneration) diff --git a/job-task-runner/controllers/taskworkload_controller.go b/job-task-runner/controllers/taskworkload_controller.go index 3c282be5b..047e6d538 100644 --- a/job-task-runner/controllers/taskworkload_controller.go +++ b/job-task-runner/controllers/taskworkload_controller.go @@ -89,12 +89,12 @@ func (r *TaskWorkloadReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Bui //+kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch func (r *TaskWorkloadReconciler) ReconcileResource(ctx context.Context, taskWorkload *korifiv1alpha1.TaskWorkload) (ctrl.Result, error) { - logger := r.logger.WithValues("namespace", taskWorkload.Namespace, "name", taskWorkload.Name) + log := logr.FromContextOrDiscard(ctx) taskWorkload.Status.ObservedGeneration = taskWorkload.Generation - logger.V(1).Info("set observed generation", "generation", taskWorkload.Status.ObservedGeneration) + log.V(1).Info("set observed generation", "generation", taskWorkload.Status.ObservedGeneration) - job, err := r.getOrCreateJob(ctx, logger, taskWorkload) + job, err := r.getOrCreateJob(ctx, log, taskWorkload) if err != nil { return ctrl.Result{}, err } @@ -104,7 +104,7 @@ func (r *TaskWorkloadReconciler) ReconcileResource(ctx context.Context, taskWork } if err = r.updateTaskWorkloadStatus(ctx, taskWorkload, job); err != nil { - logger.Info("failed to update task workload status", "reason", err) + log.Info("failed to update task workload status", "reason", err) return ctrl.Result{}, err } diff --git a/kpack-image-builder/controllers/builderinfo_controller.go b/kpack-image-builder/controllers/builderinfo_controller.go index b7eeba5a3..5ac734289 100644 --- a/kpack-image-builder/controllers/builderinfo_controller.go +++ b/kpack-image-builder/controllers/builderinfo_controller.go @@ -107,7 +107,7 @@ func (r *BuilderInfoReconciler) filterBuilderInfos(object client.Object) bool { //+kubebuilder:rbac:groups=kpack.io,resources=clusterbuilders/status,verbs=get func (r *BuilderInfoReconciler) ReconcileResource(ctx context.Context, info *korifiv1alpha1.BuilderInfo) (ctrl.Result, error) { - log := r.log.WithValues("namespace", info.Namespace, "name", info.Name) + log := logr.FromContextOrDiscard(ctx) info.Status.ObservedGeneration = info.Generation log.V(1).Info("set observed generation", "generation", info.Status.ObservedGeneration) diff --git a/kpack-image-builder/controllers/buildworkload_controller.go b/kpack-image-builder/controllers/buildworkload_controller.go index 759f70b66..a3eda5c2d 100644 --- a/kpack-image-builder/controllers/buildworkload_controller.go +++ b/kpack-image-builder/controllers/buildworkload_controller.go @@ -171,7 +171,7 @@ func filterBuildWorkloads(object client.Object) bool { //+kubebuilder:rbac:groups="",resources=serviceaccounts/status;secrets/status,verbs=get func (r *BuildWorkloadReconciler) ReconcileResource(ctx context.Context, buildWorkload *korifiv1alpha1.BuildWorkload) (ctrl.Result, error) { - log := r.log.WithValues("namespace", buildWorkload.Namespace, "name", buildWorkload.Name) + log := logr.FromContextOrDiscard(ctx) buildWorkload.Status.ObservedGeneration = buildWorkload.Generation log.V(1).Info("set observed generation", "generation", buildWorkload.Status.ObservedGeneration) diff --git a/kpack-image-builder/controllers/kpack_build_controller.go b/kpack-image-builder/controllers/kpack_build_controller.go index 30f7724f5..7a31d0e35 100644 --- a/kpack-image-builder/controllers/kpack_build_controller.go +++ b/kpack-image-builder/controllers/kpack_build_controller.go @@ -67,8 +67,7 @@ func (c KpackBuildController) SetupWithManager(mgr manager.Manager) *ctrl.Builde //+kubebuilder:rbac:groups=kpack.io,resources=builds/finalizers,verbs=get;patch func (c KpackBuildController) ReconcileResource(ctx context.Context, kpackBuild *kpackv1alpha2.Build) (ctrl.Result, error) { - log := c.log.WithValues("namespace", kpackBuild.Namespace, "name", kpackBuild.Name, "deletionTimestamp", kpackBuild.DeletionTimestamp) - log.Info("in finalizer") + log := logr.FromContextOrDiscard(ctx) if !kpackBuild.GetDeletionTimestamp().IsZero() { if !controllerutil.ContainsFinalizer(kpackBuild, KpackBuildFinalizer) { diff --git a/statefulset-runner/controllers/appworkload_controller.go b/statefulset-runner/controllers/appworkload_controller.go index 19c81ab42..c5ceb17c6 100644 --- a/statefulset-runner/controllers/appworkload_controller.go +++ b/statefulset-runner/controllers/appworkload_controller.go @@ -149,7 +149,7 @@ func filterAppWorkloads(object client.Object) bool { //+kubebuilder:rbac:groups=policy,resources=poddisruptionbudgets,verbs=create;patch;deletecollection func (r *AppWorkloadReconciler) ReconcileResource(ctx context.Context, appWorkload *korifiv1alpha1.AppWorkload) (ctrl.Result, error) { - log := r.log.WithValues("namespace", appWorkload.Namespace, "name", appWorkload.Name) + log := logr.FromContextOrDiscard(ctx) if appWorkload.Spec.RunnerName != AppWorkloadReconcilerName { return ctrl.Result{}, nil diff --git a/statefulset-runner/controllers/appworkload_controller_test.go b/statefulset-runner/controllers/appworkload_controller_test.go index e422672e4..69e557e38 100644 --- a/statefulset-runner/controllers/appworkload_controller_test.go +++ b/statefulset-runner/controllers/appworkload_controller_test.go @@ -21,7 +21,6 @@ import ( "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/log/zap" ) const ( @@ -89,7 +88,7 @@ var _ = Describe("AppWorkload Reconcile", func() { } } - fakeClient.CreateStub = func(ctx context.Context, obj client.Object, option ...client.CreateOption) error { + fakeClient.CreateStub = func(_ context.Context, obj client.Object, _ ...client.CreateOption) error { switch obj.(type) { case *v1.StatefulSet: return createStatefulSetError @@ -98,7 +97,13 @@ var _ = Describe("AppWorkload Reconcile", func() { } } - reconciler = controllers.NewAppWorkloadReconciler(fakeClient, scheme.Scheme, fakeWorkloadToStSet, fakePDB, zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + reconciler = controllers.NewAppWorkloadReconciler( + fakeClient, + scheme.Scheme, + fakeWorkloadToStSet, + fakePDB, + ctrl.Log.WithName("controllers").WithName("TestAppWorkload"), + ) }) JustBeforeEach(func() { diff --git a/statefulset-runner/controllers/runnerinfo_controller.go b/statefulset-runner/controllers/runnerinfo_controller.go index 6a0a5454e..d37460604 100644 --- a/statefulset-runner/controllers/runnerinfo_controller.go +++ b/statefulset-runner/controllers/runnerinfo_controller.go @@ -69,7 +69,7 @@ func filterRunnerInfos(object client.Object) bool { //+kubebuilder:rbac:groups=korifi.cloudfoundry.org,resources=runnerinfos/status,verbs=get;patch func (r *RunnerInfoReconciler) ReconcileResource(ctx context.Context, runnerInfo *korifiv1alpha1.RunnerInfo) (ctrl.Result, error) { - log := r.log.WithValues("namespace", runnerInfo.Namespace, "name", runnerInfo.Name) + log := logr.FromContextOrDiscard(ctx) runnerInfo.Status.ObservedGeneration = runnerInfo.Generation log.V(1).Info("set observed generation", "generation", runnerInfo.Status.ObservedGeneration) diff --git a/statefulset-runner/controllers/runnerinfo_controller_test.go b/statefulset-runner/controllers/runnerinfo_controller_test.go index 46ed008e3..bc04a3b17 100644 --- a/statefulset-runner/controllers/runnerinfo_controller_test.go +++ b/statefulset-runner/controllers/runnerinfo_controller_test.go @@ -14,7 +14,6 @@ import ( "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/log/zap" ) const ( @@ -26,7 +25,6 @@ var _ = Describe("RunnerInfo Reconcile", func() { reconciler *k8s.PatchingReconciler[korifiv1alpha1.RunnerInfo, *korifiv1alpha1.RunnerInfo] reconcileResult ctrl.Result reconcileErr error - ctx context.Context req ctrl.Request getRunnerInfoError error runnerInfo *korifiv1alpha1.RunnerInfo @@ -58,9 +56,12 @@ var _ = Describe("RunnerInfo Reconcile", func() { } } - reconciler = controllers.NewRunnerInfoReconciler(fakeClient, scheme.Scheme, zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - - reconcileResult, reconcileErr = reconciler.Reconcile(ctx, req) + reconciler = controllers.NewRunnerInfoReconciler( + fakeClient, + scheme.Scheme, + ctrl.Log.WithName("controllers").WithName("TestRunnerInfo"), + ) + reconcileResult, reconcileErr = reconciler.Reconcile(context.Background(), req) }) When("the RunnerInfo is being reconciled", func() { diff --git a/statefulset-runner/controllers/suite_test.go b/statefulset-runner/controllers/suite_test.go index e24f8c46e..de6b8f8aa 100644 --- a/statefulset-runner/controllers/suite_test.go +++ b/statefulset-runner/controllers/suite_test.go @@ -3,7 +3,9 @@ package controllers_test import ( "testing" + "go.uber.org/zap/zapcore" "k8s.io/apimachinery/pkg/util/intstr" + "sigs.k8s.io/controller-runtime/pkg/log/zap" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/statefulset-runner/fake" @@ -13,6 +15,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + logf "sigs.k8s.io/controller-runtime/pkg/log" ) func TestAppWorkloadsController(t *testing.T) { @@ -25,6 +28,10 @@ var ( fakeStatusWriter *fake.StatusWriter ) +var _ = BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), zap.Level(zapcore.DebugLevel))) +}) + var _ = BeforeEach(func() { fakeClient = new(fake.Client) fakeStatusWriter = &fake.StatusWriter{} diff --git a/tools/k8s/reconcile.go b/tools/k8s/reconcile.go index 4a8940a4e..ad53b8d91 100644 --- a/tools/k8s/reconcile.go +++ b/tools/k8s/reconcile.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/go-logr/logr" + "github.com/google/uuid" apierrors "k8s.io/apimachinery/pkg/api/errors" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" @@ -24,14 +25,15 @@ type PatchingReconciler[T any, PT ObjectWithDeepCopy[T]] struct { func NewPatchingReconciler[T any, PT ObjectWithDeepCopy[T]](log logr.Logger, k8sClient client.Client, objectReconciler ObjectReconciler[T, PT]) *PatchingReconciler[T, PT] { return &PatchingReconciler[T, PT]{ - log: log.WithName("patching-reconciler"), + log: log, k8sClient: k8sClient, objectReconciler: objectReconciler, } } func (r *PatchingReconciler[T, PT]) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := r.log.WithValues("namespace", req.Namespace, "name", req.Name) + log := r.log.WithValues("namespace", req.Namespace, "name", req.Name, "logID", uuid.NewString()) + ctx = logr.NewContext(ctx, log) obj := PT(new(T)) err := r.k8sClient.Get(ctx, req.NamespacedName, obj) diff --git a/tools/k8s/reconcile_test.go b/tools/k8s/reconcile_test.go index 1dc6e3069..287b6e46c 100644 --- a/tools/k8s/reconcile_test.go +++ b/tools/k8s/reconcile_test.go @@ -4,9 +4,11 @@ import ( "context" "errors" + "github.com/go-logr/logr" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/onsi/gomega/gbytes" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -15,6 +17,8 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" "code.cloudfoundry.org/korifi/controllers/fake" "code.cloudfoundry.org/korifi/tools/k8s" @@ -27,6 +31,9 @@ type fakeObjectReconciler struct { } func (f *fakeObjectReconciler) ReconcileResource(ctx context.Context, obj *corev1.Pod) (ctrl.Result, error) { + log := logr.FromContextOrDiscard(ctx) + log.V(1).Info("fake reconciler reconciling") + f.reconcileResourceCallCount++ f.reconcileResourceObj = obj @@ -189,4 +196,23 @@ var _ = Describe("Reconcile", func() { Expect(fakeStatusWriter.PatchCallCount()).To(Equal(1)) }) }) + + Describe("logging", func() { + var logOutput *gbytes.Buffer + + BeforeEach(func() { + logOutput = gbytes.NewBuffer() + GinkgoWriter.TeeTo(logOutput) + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + }) + + It("captures logs from object reconciler", func() { + Eventually(logOutput).Should(SatisfyAll( + gbytes.Say("fake reconciler reconciling"), + gbytes.Say(`"namespace":`), + gbytes.Say(`"name":`), + gbytes.Say(`"logID":`), + )) + }) + }) })