Skip to content

Commit

Permalink
add logging info to failure paths
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed May 2, 2023
1 parent 3ca7577 commit aa24c00
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion operator/controllers/common/helperfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/go-logr/logr"
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3"
apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common"
"github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/interfaces"
Expand Down Expand Up @@ -86,12 +87,14 @@ func copyMap[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
}
}

func GetTaskDefinition(k8sclient client.Client, ctx context.Context, definitionName string, namespace string) (*klcv1alpha3.KeptnTaskDefinition, error) {
func GetTaskDefinition(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string) (*klcv1alpha3.KeptnTaskDefinition, error) {
definition := &klcv1alpha3.KeptnTaskDefinition{}
err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: namespace}, definition)
if err != nil {
log.Error(err, "Failed to get KeptnTaskDefinition from application namespace")
if k8serrors.IsNotFound(err) {
if err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: KLTNamespace}, definition); err != nil {
log.Error(err, "Failed to get KeptnTaskDefinition from default KLT namespace")
return nil, err
}
return definition, nil
Expand Down
3 changes: 2 additions & 1 deletion operator/controllers/common/helperfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
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"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
Expand Down Expand Up @@ -472,7 +473,7 @@ func Test_GetTaskDefinition(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewClientBuilder().WithObjects(tt.taskDef).Build()
d, err := GetTaskDefinition(client, context.TODO(), tt.taskDefName, tt.taskDefNamespace)
d, err := GetTaskDefinition(client, ctrl.Log.WithName("testytest"), context.TODO(), tt.taskDefName, tt.taskDefNamespace)
if tt.out != nil && d != nil {
require.Equal(t, tt.out.Name, d.Name)
require.Equal(t, tt.out.Namespace, d.Namespace)
Expand Down
2 changes: 1 addition & 1 deletion operator/controllers/common/taskhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (r TaskHandler) setupTasks(taskCreateAttributes CreateTaskAttributes, piWra
}

func (r TaskHandler) handleTaskNotExists(ctx context.Context, phaseCtx context.Context, taskCreateAttributes CreateTaskAttributes, taskName string, piWrapper *interfaces.PhaseItemWrapper, reconcileObject client.Object, task *klcv1alpha3.KeptnTask, taskStatus *klcv1alpha3.ItemStatus) error {
definition, err := GetTaskDefinition(r.Client, ctx, taskName, piWrapper.GetNamespace())
definition, err := GetTaskDefinition(r.Client, r.Log, ctx, taskName, piWrapper.GetNamespace())
if err != nil {
r.Log.Error(err, "could not find KeptnTaskDefinition")
return controllererrors.ErrCannotGetKeptnTaskDefinition
Expand Down
4 changes: 2 additions & 2 deletions operator/controllers/lifecycle/keptntask/job_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func (r *KeptnTaskReconciler) createJob(ctx context.Context, req ctrl.Request, task *klcv1alpha3.KeptnTask) error {
jobName := ""
definition, err := controllercommon.GetTaskDefinition(r.Client, ctx, task.Spec.TaskDefinition, req.Namespace)
definition, err := controllercommon.GetTaskDefinition(r.Client, r.Log, ctx, task.Spec.TaskDefinition, req.Namespace)
if err != nil {
controllercommon.RecordEvent(r.Recorder, apicommon.PhaseCreateTask, "Warning", task, "TaskDefinitionNotFound", fmt.Sprintf("could not find KeptnTaskDefinition: %s ", task.Spec.TaskDefinition), "")
return err
Expand Down Expand Up @@ -129,7 +129,7 @@ func setupTaskContext(task *klcv1alpha3.KeptnTask) klcv1alpha3.TaskContext {

func (r *KeptnTaskReconciler) handleParent(ctx context.Context, req ctrl.Request, task *klcv1alpha3.KeptnTask, definition *klcv1alpha3.KeptnTaskDefinition, params FunctionExecutionParams) error {
var parentJobParams FunctionExecutionParams
parentDefinition, err := controllercommon.GetTaskDefinition(r.Client, ctx, definition.Spec.Function.FunctionReference.Name, req.Namespace)
parentDefinition, err := controllercommon.GetTaskDefinition(r.Client, r.Log, ctx, definition.Spec.Function.FunctionReference.Name, req.Namespace)
if err != nil {
controllercommon.RecordEvent(r.Recorder, apicommon.PhaseCreateTask, "Warning", task, "TaskDefinitionNotFound", fmt.Sprintf("could not find KeptnTaskDefinition: %s ", task.Spec.TaskDefinition), "")
return err
Expand Down

0 comments on commit aa24c00

Please sign in to comment.