diff --git a/lifecycle-operator/controllers/common/helperfunctions.go b/lifecycle-operator/controllers/common/helperfunctions.go index 54cb9916d1..16dee36c2f 100644 --- a/lifecycle-operator/controllers/common/helperfunctions.go +++ b/lifecycle-operator/controllers/common/helperfunctions.go @@ -11,6 +11,7 @@ import ( "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/lifecycle/interfaces" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -105,3 +106,11 @@ func getObject(k8sclient client.Client, log logr.Logger, ctx context.Context, de } return nil } + +// 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/lifecycle-operator/controllers/common/helperfunctions_test.go b/lifecycle-operator/controllers/common/helperfunctions_test.go index 5987ed2e4e..efe572b175 100644 --- a/lifecycle-operator/controllers/common/helperfunctions_test.go +++ b/lifecycle-operator/controllers/common/helperfunctions_test.go @@ -9,6 +9,7 @@ import ( "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config" "github.com/stretchr/testify/require" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -525,3 +526,18 @@ func Test_GetEvaluationDefinition(t *testing.T) { }) } } + +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) +} diff --git a/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go b/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go index 7804550c7d..e8a6928bb0 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go @@ -70,7 +70,8 @@ type KeptnAppReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *KeptnAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - r.Log.Info("Searching for App") + requestInfo := controllercommon.GetRequestInfo(req) + r.Log.Info("Searching for App", "requestInfo", requestInfo) app := &klcv1alpha3.KeptnApp{} err := r.Get(ctx, req.NamespacedName, app) diff --git a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go index 4402effdea..fea50631aa 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go @@ -71,15 +71,14 @@ type KeptnAppVersionReconciler struct { // //nolint:gocyclo func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - - r.Log.Info("Searching for Keptn App Version") + requestInfo := controllercommon.GetRequestInfo(req) + r.Log.Info("Searching for Keptn App Version", "requestInfo", requestInfo) appVersion := &klcv1alpha3.KeptnAppVersion{} err := r.Get(ctx, req.NamespacedName, appVersion) if errors.IsNotFound(err) { return reconcile.Result{}, nil } - if err != nil { r.Log.Error(err, "App Version not found") return reconcile.Result{}, fmt.Errorf(controllererrors.ErrCannotFetchAppVersionMsg, err) diff --git a/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go b/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go index 7b7cae48a7..d52ea04e15 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go @@ -70,14 +70,14 @@ type KeptnEvaluationReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - - r.Log.Info("Reconciling KeptnEvaluation") + requestInfo := controllercommon.GetRequestInfo(req) + r.Log.Info("Reconciling KeptnEvaluation", "requestInfo", requestInfo) evaluation := &klcv1alpha3.KeptnEvaluation{} if err := r.Client.Get(ctx, req.NamespacedName, evaluation); err != nil { if errors.IsNotFound(err) { // taking down all associated K8s resources is handled by K8s - r.Log.Info("KeptnEvaluation resource not found. Ignoring since object must be deleted") + r.Log.Info("KeptnEvaluation resource not found. Ignoring since object must be deleted", "requestInfo", requestInfo) return ctrl.Result{}, nil } r.Log.Error(err, "Failed to get the KeptnEvaluation") @@ -96,7 +96,7 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ evaluationDefinition, err := controllercommon.GetEvaluationDefinition(r.Client, r.Log, ctx, evaluation.Spec.EvaluationDefinition, req.NamespacedName.Namespace) if err != nil { if errors.IsNotFound(err) { - r.Log.Info(err.Error() + ", ignoring error since object must be deleted") + r.Log.Info("KeptnEvaluation not found, ignoring error since object must be deleted", "requestInfo", requestInfo) span.SetStatus(codes.Error, err.Error()) return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil } @@ -116,7 +116,7 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{Requeue: true, RequeueAfter: evaluation.Spec.RetryInterval.Duration}, nil } - r.Log.Info("Finished Reconciling KeptnEvaluation") + r.Log.Info("Finished Reconciling KeptnEvaluation", "requestInfo", requestInfo) err := r.updateFinishedEvaluationMetrics(ctx, evaluation, span) diff --git a/lifecycle-operator/controllers/lifecycle/keptntask/controller.go b/lifecycle-operator/controllers/lifecycle/keptntask/controller.go index caf524a57c..23438e00ca 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntask/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptntask/controller.go @@ -58,13 +58,14 @@ type KeptnTaskReconciler struct { // +kubebuilder:rbac:groups=batch,resources=jobs/status,verbs=get;list func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - r.Log.Info("Reconciling KeptnTask") + requestInfo := controllercommon.GetRequestInfo(req) + r.Log.Info("Reconciling KeptnTask", "requestInfo", requestInfo) task := &klcv1alpha3.KeptnTask{} if err := r.Client.Get(ctx, req.NamespacedName, task); err != nil { if errors.IsNotFound(err) { // taking down all associated K8s resources is handled by K8s - r.Log.Info("KeptnTask resource not found. Ignoring since object must be deleted") + r.Log.Info("KeptnTask resource not found. Ignoring since object must be deleted", "requestInfo", requestInfo) return ctrl.Result{}, nil } r.Log.Error(err, "Failed to get the KeptnTask") @@ -111,14 +112,14 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil } - r.Log.Info("Finished Reconciling KeptnTask") + r.Log.Info("Finished Reconciling KeptnTask", "requestInfo", requestInfo) // Task is completed at this place task.SetEndTime() attrs := task.GetMetricsAttributes() - r.Log.Info("Increasing task count") + r.Log.Info("Increasing task count", "requestInfo", requestInfo) // metrics: increment task counter r.Meters.TaskCount.Add(ctx, 1, metric.WithAttributes(attrs...)) diff --git a/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go b/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go index 0dcb3daf01..0973ebb194 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go @@ -45,14 +45,15 @@ type KeptnTaskDefinitionReconciler struct { // +kubebuilder:rbac:groups=core,resources=configmaps,verbs=create;get;update;list;watch func (r *KeptnTaskDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - r.Log.Info("Reconciling KeptnTaskDefinition") + requestInfo := controllercommon.GetRequestInfo(req) + r.Log.Info("Reconciling KeptnTaskDefinition", "requestInfo", requestInfo) definition := &klcv1alpha3.KeptnTaskDefinition{} if err := r.Client.Get(ctx, req.NamespacedName, definition); err != nil { if errors.IsNotFound(err) { // taking down all associated K8s resources is handled by K8s - r.Log.Info("KeptnTaskDefinition resource not found. Ignoring since object must be deleted") + r.Log.Info("KeptnTaskDefinition resource not found. Ignoring since object must be deleted", "requestInfo", requestInfo) return ctrl.Result{}, nil } r.Log.Error(err, "Failed to get the KeptnTaskDefinition") @@ -86,11 +87,11 @@ func (r *KeptnTaskDefinitionReconciler) Reconcile(ctx context.Context, req ctrl. r.Log.Error(err, "could not update configmap status reference for: "+definition.Name) return ctrl.Result{}, nil } - r.Log.Info("updated configmap status reference for: " + definition.Name) + r.Log.Info("updated configmap status reference for: "+definition.Name, "requestInfo", requestInfo) } - r.Log.Info("Finished Reconciling KeptnTaskDefinition") + r.Log.Info("Finished Reconciling KeptnTaskDefinition", "requestInfo", requestInfo) return ctrl.Result{}, nil } diff --git a/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go b/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go index 0cf60216ea..90994b753a 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go @@ -69,7 +69,8 @@ type KeptnWorkloadReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - r.Log.Info("Searching for workload") + requestInfo := controllercommon.GetRequestInfo(req) + r.Log.Info("Searching for workload", "requestInfo", requestInfo) workload := &klcv1alpha3.KeptnWorkload{} err := r.Get(ctx, req.NamespacedName, workload) @@ -88,7 +89,7 @@ func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Reques workload.SetSpanAttributes(span) - r.Log.Info("Reconciling Keptn Workload", "workload", workload.Name) + r.Log.Info("Reconciling Keptn Workload", "workload", workload.Name, "requestInfo", requestInfo) workloadInstance := &klcv1alpha3.KeptnWorkloadInstance{} diff --git a/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go b/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go index ce734b6c61..63068e6196 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go @@ -74,7 +74,8 @@ type KeptnWorkloadInstanceReconciler struct { // //nolint:gocyclo,gocognit func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - r.Log.Info("Searching for KeptnWorkloadInstance") + requestInfo := controllercommon.GetRequestInfo(req) + r.Log.Info("Searching for KeptnWorkloadInstance", "requestInfo", requestInfo) // retrieve workload instance workloadInstance := &klcv1alpha3.KeptnWorkloadInstance{}