From 859459d88f43c0e0d87d656986d586454c4f01bc Mon Sep 17 00:00:00 2001 From: Geoffrey Israel Date: Tue, 17 Oct 2023 11:30:13 +0100 Subject: [PATCH] chore(cert-manager): improve logging (#2279) --- .../keptnwebhookcertificate_controller.go | 16 ++++++------- klt-cert-manager/pkg/common/common.go | 11 +++++++++ klt-cert-manager/pkg/common/common_test.go | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 klt-cert-manager/pkg/common/common.go create mode 100644 klt-cert-manager/pkg/common/common_test.go diff --git a/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go b/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go index a002a35962..26d6f42533 100644 --- a/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go +++ b/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go @@ -87,13 +87,13 @@ type KeptnWebhookCertificateReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile func (r *KeptnWebhookCertificateReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) { - r.Log.Info("reconciling webhook certificates", - "namespace", request.Namespace, "name", request.Name) + requestInfo := common.GetRequestInfo(request) + r.Log.Info("reconciling webhook certificates", "requestInfo", requestInfo) r.Log.Info("Retrieving MutatingWebhooks") mutatingWebhookConfigurations, err := r.ResourceRetriever.GetMutatingWebhooks(ctx) if err != nil { - r.Log.Error(err, "could not find mutating webhook configuration") + r.Log.Error(err, "could not find mutating webhook configuration", "requestInfo", requestInfo) } r.Log.Info( "Found MutatingWebhooks to inject certificates", @@ -101,10 +101,10 @@ func (r *KeptnWebhookCertificateReconciler) Reconcile(ctx context.Context, reque "byteSize", mutatingWebhookConfigurations.Size(), ) - r.Log.Info("Retrieving ValidatingWebhooks") + r.Log.Info("Retrieving ValidatingWebhooks", "requestInfo", requestInfo) validatingWebhookConfigurations, err := r.ResourceRetriever.GetValidatingWebhooks(ctx) if err != nil { - r.Log.Error(err, "could not find validating webhook configuration") + r.Log.Error(err, "could not find validating webhook configuration", "requestInfo", requestInfo) } r.Log.Info( "Found ValidatingWebhooks to inject certificates", @@ -112,10 +112,10 @@ func (r *KeptnWebhookCertificateReconciler) Reconcile(ctx context.Context, reque "byteSize", validatingWebhookConfigurations.Size(), ) - r.Log.Info("Retrieving CRDs") + r.Log.Info("Retrieving CRDs", "requestInfo", requestInfo) crds, err := r.ResourceRetriever.GetCRDs(ctx) if err != nil { - r.Log.Error(err, "could not find CRDs") + r.Log.Error(err, "could not find CRDs", "requestInfo", requestInfo) } r.Log.Info( "Found CRDs to inject certificates", @@ -138,7 +138,7 @@ func (r *KeptnWebhookCertificateReconciler) Reconcile(ctx context.Context, reque isCertSecretRecent := certSecret.isRecent() if isCertSecretRecent && areMutatingWebhookConfigsValid && areValidatingWebhookConfigsValid && areCRDConversionsConfigValid { - r.Log.Info("secret for certificates up to date, skipping update") + r.Log.Info("secret for certificates up to date, skipping update", "requestInfo", requestInfo) r.cancelMgr() return reconcile.Result{RequeueAfter: common.SuccessDuration}, nil } diff --git a/klt-cert-manager/pkg/common/common.go b/klt-cert-manager/pkg/common/common.go new file mode 100644 index 0000000000..fdef5d8b8f --- /dev/null +++ b/klt-cert-manager/pkg/common/common.go @@ -0,0 +1,11 @@ +package common + +import ctrl "sigs.k8s.io/controller-runtime" + +// GetRequestInfo extracts name and namespace from a controller request. +func GetRequestInfo(req ctrl.Request) map[string]string { + return map[string]string{ + "name": req.Name, + "namespace": req.Namespace, + } +} diff --git a/klt-cert-manager/pkg/common/common_test.go b/klt-cert-manager/pkg/common/common_test.go new file mode 100644 index 0000000000..dfb6af3bee --- /dev/null +++ b/klt-cert-manager/pkg/common/common_test.go @@ -0,0 +1,24 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" +) + +func TestGetRequestInfo(t *testing.T) { + req := ctrl.Request{ + NamespacedName: types.NamespacedName{ + Name: "example", + Namespace: "test-namespace", + }} + + info := GetRequestInfo(req) + expected := map[string]string{ + "name": "example", + "namespace": "test-namespace", + } + require.Equal(t, expected, info) +}