Skip to content

Commit

Permalink
chore(lifecycle-operator): improve logging (keptn#2253)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffrey Israel <[email protected]>
Co-authored-by: Florian Bacher <[email protected]>
Co-authored-by: odubajDT <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2023
1 parent 8443874 commit 8dd3394
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 20 deletions.
9 changes: 9 additions & 0 deletions lifecycle-operator/controllers/common/helperfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
}
}
16 changes: 16 additions & 0 deletions lifecycle-operator/controllers/common/helperfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ type KeptnAppReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ type KeptnEvaluationReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/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")
Expand All @@ -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
}
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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...))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ type KeptnWorkloadReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/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)
Expand All @@ -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{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 8dd3394

Please sign in to comment.