diff --git a/operator/apis/lifecycle/v1alpha3/keptnappversion_test.go b/operator/apis/lifecycle/v1alpha3/keptnappversion_test.go index 94fa1ca4b9..b68d1a6c28 100644 --- a/operator/apis/lifecycle/v1alpha3/keptnappversion_test.go +++ b/operator/apis/lifecycle/v1alpha3/keptnappversion_test.go @@ -178,21 +178,29 @@ func TestKeptnAppVersion(t *testing.T) { require.Equal(t, "trace1.appname.version.phase", app.GetSpanKey("phase")) - task := app.GenerateTask("taskdef", common.PostDeploymentCheckType) + task := app.GenerateTask(KeptnTaskDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "task-def", + }, + }, common.PostDeploymentCheckType) require.Equal(t, KeptnTaskSpec{ AppVersion: app.GetVersion(), AppName: app.GetParentName(), - TaskDefinition: "taskdef", + TaskDefinition: "task-def", Parameters: TaskParameters{}, SecureParameters: SecureParameters{}, Type: common.PostDeploymentCheckType, }, task.Spec) - evaluation := app.GenerateEvaluation("taskdef", common.PostDeploymentCheckType) + evaluation := app.GenerateEvaluation(KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "eval-def", + }, + }, common.PostDeploymentCheckType) require.Equal(t, KeptnEvaluationSpec{ AppVersion: app.GetVersion(), AppName: app.GetParentName(), - EvaluationDefinition: "taskdef", + EvaluationDefinition: "eval-def", Type: common.PostDeploymentCheckType, RetryInterval: metav1.Duration{ Duration: 5 * time.Second, diff --git a/operator/apis/lifecycle/v1alpha3/keptnappversion_types.go b/operator/apis/lifecycle/v1alpha3/keptnappversion_types.go index 5d53c585a0..1b4bc38733 100644 --- a/operator/apis/lifecycle/v1alpha3/keptnappversion_types.go +++ b/operator/apis/lifecycle/v1alpha3/keptnappversion_types.go @@ -310,33 +310,35 @@ func (a KeptnAppVersion) GetVersion() string { return a.Spec.Version } -func (a KeptnAppVersion) GenerateTask(taskDefinition string, checkType common.CheckType) KeptnTask { +func (a KeptnAppVersion) GenerateTask(taskDefinition KeptnTaskDefinition, checkType common.CheckType) KeptnTask { return KeptnTask{ ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition), + Name: common.GenerateTaskName(checkType, taskDefinition.Name), Namespace: a.Namespace, }, Spec: KeptnTaskSpec{ AppVersion: a.GetVersion(), AppName: a.GetParentName(), - TaskDefinition: taskDefinition, + TaskDefinition: taskDefinition.Name, Parameters: TaskParameters{}, SecureParameters: SecureParameters{}, Type: checkType, + Retries: taskDefinition.Spec.Retries, + Timeout: taskDefinition.Spec.Timeout, }, } } -func (a KeptnAppVersion) GenerateEvaluation(evaluationDefinition string, checkType common.CheckType) KeptnEvaluation { +func (a KeptnAppVersion) GenerateEvaluation(evaluationDefinition KeptnEvaluationDefinition, checkType common.CheckType) KeptnEvaluation { return KeptnEvaluation{ ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition), + Name: common.GenerateEvaluationName(checkType, evaluationDefinition.Name), Namespace: a.Namespace, }, Spec: KeptnEvaluationSpec{ AppVersion: a.Spec.Version, AppName: a.Spec.AppName, - EvaluationDefinition: evaluationDefinition, + EvaluationDefinition: evaluationDefinition.Name, Type: checkType, RetryInterval: metav1.Duration{ Duration: 5 * time.Second, diff --git a/operator/apis/lifecycle/v1alpha3/keptntask_types.go b/operator/apis/lifecycle/v1alpha3/keptntask_types.go index 3f994f22ec..6e4a4ccabf 100644 --- a/operator/apis/lifecycle/v1alpha3/keptntask_types.go +++ b/operator/apis/lifecycle/v1alpha3/keptntask_types.go @@ -41,6 +41,8 @@ type KeptnTaskSpec struct { Parameters TaskParameters `json:"parameters,omitempty"` SecureParameters SecureParameters `json:"secureParameters,omitempty"` Type common.CheckType `json:"checkType,omitempty"` + Retries int `json:"retries,omitempty"` + Timeout metav1.Duration `json:"timeout,omitempty"` } type TaskContext struct { @@ -68,18 +70,19 @@ type KeptnTaskStatus struct { Message string `json:"message,omitempty"` StartTime metav1.Time `json:"startTime,omitempty"` EndTime metav1.Time `json:"endTime,omitempty"` - // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster - // Important: Run "make" to regenerate code after modifying this file + // +kubebuilder:default:=0 + RetryCount int `json:"retryCount"` } -//+kubebuilder:object:root=true -//+kubebuilder:subresource:status -//+kubebuilder:storageversion +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:storageversion // +kubebuilder:printcolumn:name="AppName",type=string,JSONPath=`.spec.app` // +kubebuilder:printcolumn:name="AppVersion",type=string,JSONPath=`.spec.appVersion` // +kubebuilder:printcolumn:name="WorkloadName",type=string,JSONPath=`.spec.workload` // +kubebuilder:printcolumn:name="WorkloadVersion",type=string,JSONPath=`.spec.workloadVersion` // +kubebuilder:printcolumn:name="Job Name",type=string,JSONPath=`.status.jobName` +// +kubebuilder:printcolumn:name="RetryCount",type=string,JSONPath=`.status.retryCount` // +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.status` // KeptnTask is the Schema for the keptntasks API diff --git a/operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go b/operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go index d590e723f7..8f267428ab 100644 --- a/operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go +++ b/operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go @@ -26,6 +26,14 @@ import ( // KeptnTaskDefinitionSpec defines the desired state of KeptnTaskDefinition type KeptnTaskDefinitionSpec struct { Function FunctionSpec `json:"function,omitempty"` + // +kubebuilder:default:=10 + Retries int `json:"retries,omitempty"` + // +optional + // +kubebuilder:default:="5m" + // +kubebuilder:validation:Pattern="^0|([0-9]+(\\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string + // +optional + Timeout metav1.Duration `json:"timeout,omitempty"` } type FunctionSpec struct { diff --git a/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_test.go b/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_test.go index 47d14d57a9..5cdb3f7b26 100644 --- a/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_test.go +++ b/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_test.go @@ -182,23 +182,31 @@ func TestKeptnWorkloadInstance(t *testing.T) { require.Equal(t, "trace1.workloadname.version.phase", workload.GetSpanKey("phase")) - task := workload.GenerateTask("taskdef", common.PostDeploymentCheckType) + task := workload.GenerateTask(KeptnTaskDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "task-def", + }, + }, common.PostDeploymentCheckType) require.Equal(t, KeptnTaskSpec{ AppName: workload.GetAppName(), WorkloadVersion: workload.GetVersion(), Workload: workload.GetParentName(), - TaskDefinition: "taskdef", + TaskDefinition: "task-def", Parameters: TaskParameters{}, SecureParameters: SecureParameters{}, Type: common.PostDeploymentCheckType, }, task.Spec) - evaluation := workload.GenerateEvaluation("taskdef", common.PostDeploymentCheckType) + evaluation := workload.GenerateEvaluation(KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "eval-def", + }, + }, common.PostDeploymentCheckType) require.Equal(t, KeptnEvaluationSpec{ AppName: workload.GetAppName(), WorkloadVersion: workload.GetVersion(), Workload: workload.GetParentName(), - EvaluationDefinition: "taskdef", + EvaluationDefinition: "eval-def", Type: common.PostDeploymentCheckType, RetryInterval: metav1.Duration{ Duration: 5 * time.Second, diff --git a/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go b/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go index 1569b45a01..bfc48fb32b 100644 --- a/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go +++ b/operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go @@ -318,35 +318,37 @@ func (w KeptnWorkloadInstance) GetVersion() string { return w.Spec.Version } -func (w KeptnWorkloadInstance) GenerateTask(taskDefinition string, checkType common.CheckType) KeptnTask { +func (w KeptnWorkloadInstance) GenerateTask(taskDefinition KeptnTaskDefinition, checkType common.CheckType) KeptnTask { return KeptnTask{ ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition), + Name: common.GenerateTaskName(checkType, taskDefinition.Name), Namespace: w.Namespace, }, Spec: KeptnTaskSpec{ AppName: w.GetAppName(), WorkloadVersion: w.GetVersion(), Workload: w.GetParentName(), - TaskDefinition: taskDefinition, + TaskDefinition: taskDefinition.Name, Parameters: TaskParameters{}, SecureParameters: SecureParameters{}, Type: checkType, + Retries: taskDefinition.Spec.Retries, + Timeout: taskDefinition.Spec.Timeout, }, } } -func (w KeptnWorkloadInstance) GenerateEvaluation(evaluationDefinition string, checkType common.CheckType) KeptnEvaluation { +func (w KeptnWorkloadInstance) GenerateEvaluation(evaluationDefinition KeptnEvaluationDefinition, checkType common.CheckType) KeptnEvaluation { return KeptnEvaluation{ ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition), + Name: common.GenerateEvaluationName(checkType, evaluationDefinition.Name), Namespace: w.Namespace, }, Spec: KeptnEvaluationSpec{ AppName: w.GetAppName(), WorkloadVersion: w.GetVersion(), Workload: w.GetParentName(), - EvaluationDefinition: evaluationDefinition, + EvaluationDefinition: evaluationDefinition.Name, Type: checkType, RetryInterval: metav1.Duration{ Duration: 5 * time.Second, diff --git a/operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go b/operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go index 6dfdb7ee57..6beeaac8cb 100644 --- a/operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go +++ b/operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go @@ -821,6 +821,7 @@ func (in *KeptnTaskDefinitionList) DeepCopyObject() runtime.Object { func (in *KeptnTaskDefinitionSpec) DeepCopyInto(out *KeptnTaskDefinitionSpec) { *out = *in in.Function.DeepCopyInto(&out.Function) + out.Timeout = in.Timeout } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KeptnTaskDefinitionSpec. @@ -887,6 +888,7 @@ func (in *KeptnTaskSpec) DeepCopyInto(out *KeptnTaskSpec) { out.Context = in.Context in.Parameters.DeepCopyInto(&out.Parameters) out.SecureParameters = in.SecureParameters + out.Timeout = in.Timeout } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KeptnTaskSpec. diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml index a7ce4a3f01..cddfac6c7d 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml @@ -225,6 +225,13 @@ spec: type: string type: object type: object + retries: + default: 10 + type: integer + timeout: + default: 5m + pattern: ^0|([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$ + type: string type: object status: description: KeptnTaskDefinitionStatus defines the observed state of KeptnTaskDefinition diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml index cea25279af..d281bea55e 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml @@ -261,6 +261,9 @@ spec: - jsonPath: .status.jobName name: Job Name type: string + - jsonPath: .status.retryCount + name: RetryCount + type: string - jsonPath: .status.status name: Status type: string @@ -319,6 +322,8 @@ spec: type: string type: object type: object + retries: + type: integer secureParameters: properties: secret: @@ -326,6 +331,8 @@ spec: type: object taskDefinition: type: string + timeout: + type: string workload: type: string workloadVersion: @@ -348,12 +355,17 @@ spec: type: string message: type: string + retryCount: + default: 0 + type: integer startTime: format: date-time type: string status: default: Pending type: string + required: + - retryCount type: object type: object served: true diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index 91394acf5f..e2829390ef 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -29,8 +29,14 @@ type EvaluationHandler struct { SpanHandler ISpanHandler } +type CreateEvaluationAttributes struct { + SpanName string + Definition klcv1alpha3.KeptnEvaluationDefinition + CheckType apicommon.CheckType +} + //nolint:gocognit,gocyclo -func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, evaluationCreateAttributes CreateAttributes) ([]klcv1alpha3.ItemStatus, apicommon.StatusSummary, error) { +func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, evaluationCreateAttributes CreateEvaluationAttributes) ([]klcv1alpha3.ItemStatus, apicommon.StatusSummary, error) { piWrapper, err := interfaces.NewPhaseItemWrapperFromClientObject(reconcileObject) if err != nil { return nil, apicommon.StatusSummary{}, err @@ -107,7 +113,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co } //nolint:dupl -func (r EvaluationHandler) CreateKeptnEvaluation(ctx context.Context, namespace string, reconcileObject client.Object, evaluationCreateAttributes CreateAttributes) (string, error) { +func (r EvaluationHandler) CreateKeptnEvaluation(ctx context.Context, namespace string, reconcileObject client.Object, evaluationCreateAttributes CreateEvaluationAttributes) (string, error) { piWrapper, err := interfaces.NewPhaseItemWrapperFromClientObject(reconcileObject) if err != nil { return "", err @@ -143,7 +149,7 @@ func (r EvaluationHandler) emitEvaluationFailureEvents(evaluation *klcv1alpha3.K RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "Failed", k8sEventMessage, piWrapper.GetVersion()) } -func (r EvaluationHandler) setupEvaluations(evaluationCreateAttributes CreateAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha3.ItemStatus) { +func (r EvaluationHandler) setupEvaluations(evaluationCreateAttributes CreateEvaluationAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha3.ItemStatus) { var evaluations []string var statuses []klcv1alpha3.ItemStatus @@ -158,8 +164,8 @@ func (r EvaluationHandler) setupEvaluations(evaluationCreateAttributes CreateAtt return evaluations, statuses } -func (r EvaluationHandler) handleEvaluationNotExists(ctx context.Context, phaseCtx context.Context, evaluationCreateAttributes CreateAttributes, evaluationName string, piWrapper *interfaces.PhaseItemWrapper, reconcileObject client.Object, evaluation *klcv1alpha3.KeptnEvaluation, evaluationStatus *klcv1alpha3.ItemStatus) error { - evaluationCreateAttributes.Definition = evaluationName +func (r EvaluationHandler) handleEvaluationNotExists(ctx context.Context, phaseCtx context.Context, evaluationCreateAttributes CreateEvaluationAttributes, evaluationName string, piWrapper *interfaces.PhaseItemWrapper, reconcileObject client.Object, evaluation *klcv1alpha3.KeptnEvaluation, evaluationStatus *klcv1alpha3.ItemStatus) error { + evaluationCreateAttributes.Definition.Name = evaluationName evaluationName, err := r.CreateKeptnEvaluation(ctx, piWrapper.GetNamespace(), reconcileObject, evaluationCreateAttributes) if err != nil { return err diff --git a/operator/controllers/common/evaluationhandler_test.go b/operator/controllers/common/evaluationhandler_test.go index cc60cddffe..95a878f638 100644 --- a/operator/controllers/common/evaluationhandler_test.go +++ b/operator/controllers/common/evaluationhandler_test.go @@ -24,7 +24,7 @@ func TestEvaluationHandler(t *testing.T) { tests := []struct { name string object client.Object - createAttr CreateAttributes + createAttr CreateEvaluationAttributes wantStatus []v1alpha3.ItemStatus wantSummary apicommon.StatusSummary evalObj v1alpha3.KeptnEvaluation @@ -37,7 +37,7 @@ func TestEvaluationHandler(t *testing.T) { name: "cannot unwrap object", object: &v1alpha3.KeptnEvaluation{}, evalObj: v1alpha3.KeptnEvaluation{}, - createAttr: CreateAttributes{}, + createAttr: CreateEvaluationAttributes{}, wantStatus: nil, wantSummary: apicommon.StatusSummary{}, wantErr: controllererrors.ErrCannotWrapToPhaseItem, @@ -48,10 +48,14 @@ func TestEvaluationHandler(t *testing.T) { name: "no evaluations", object: &v1alpha3.KeptnAppVersion{}, evalObj: v1alpha3.KeptnEvaluation{}, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "", - CheckType: apicommon.PreDeploymentEvaluationCheckType, + createAttr: CreateEvaluationAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "", + }, + }, + CheckType: apicommon.PreDeploymentEvaluationCheckType, }, wantStatus: []v1alpha3.ItemStatus(nil), wantSummary: apicommon.StatusSummary{}, @@ -69,10 +73,14 @@ func TestEvaluationHandler(t *testing.T) { }, }, evalObj: v1alpha3.KeptnEvaluation{}, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "eval-def", - CheckType: apicommon.PreDeploymentEvaluationCheckType, + createAttr: CreateEvaluationAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "eval-def", + }, + }, + CheckType: apicommon.PreDeploymentEvaluationCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -106,10 +114,14 @@ func TestEvaluationHandler(t *testing.T) { }, }, evalObj: v1alpha3.KeptnEvaluation{}, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "eval-def", - CheckType: apicommon.PreDeploymentEvaluationCheckType, + createAttr: CreateEvaluationAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "eval-def", + }, + }, + CheckType: apicommon.PreDeploymentEvaluationCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -161,10 +173,14 @@ func TestEvaluationHandler(t *testing.T) { }, }, }, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "eval-def", - CheckType: apicommon.PreDeploymentEvaluationCheckType, + createAttr: CreateEvaluationAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "eval-def", + }, + }, + CheckType: apicommon.PreDeploymentEvaluationCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -212,10 +228,14 @@ func TestEvaluationHandler(t *testing.T) { OverallStatus: apicommon.StateSucceeded, }, }, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "eval-def", - CheckType: apicommon.PreDeploymentEvaluationCheckType, + createAttr: CreateEvaluationAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "eval-def", + }, + }, + CheckType: apicommon.PreDeploymentEvaluationCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -287,14 +307,14 @@ func TestEvaluationHandler_createEvaluation(t *testing.T) { tests := []struct { name string object client.Object - createAttr CreateAttributes + createAttr CreateEvaluationAttributes wantName string wantErr error }{ { name: "cannot unwrap object", object: &v1alpha3.KeptnEvaluation{}, - createAttr: CreateAttributes{}, + createAttr: CreateEvaluationAttributes{}, wantName: "", wantErr: controllererrors.ErrCannotWrapToPhaseItem, }, @@ -310,10 +330,14 @@ func TestEvaluationHandler_createEvaluation(t *testing.T) { }, }, }, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "eval-def", - CheckType: apicommon.PreDeploymentEvaluationCheckType, + createAttr: CreateEvaluationAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnEvaluationDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "eval-def", + }, + }, + CheckType: apicommon.PreDeploymentEvaluationCheckType, }, wantName: "pre-eval-eval-def-", wantErr: nil, diff --git a/operator/controllers/common/helperfunctions.go b/operator/controllers/common/helperfunctions.go index 7bd463c933..e4a5f2e2e7 100644 --- a/operator/controllers/common/helperfunctions.go +++ b/operator/controllers/common/helperfunctions.go @@ -11,12 +11,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -type CreateAttributes struct { - SpanName string - Definition string - CheckType apicommon.CheckType -} - // GetItemStatus retrieves the state of the task/evaluation, if it does not exists, it creates a default one func GetItemStatus(name string, instanceStatus []klcv1alpha3.ItemStatus) klcv1alpha3.ItemStatus { for _, status := range instanceStatus { diff --git a/operator/controllers/common/taskhandler.go b/operator/controllers/common/taskhandler.go index eec1d798e6..356a12fc8c 100644 --- a/operator/controllers/common/taskhandler.go +++ b/operator/controllers/common/taskhandler.go @@ -29,8 +29,14 @@ type TaskHandler struct { SpanHandler ISpanHandler } +type CreateTaskAttributes struct { + SpanName string + Definition klcv1alpha3.KeptnTaskDefinition + CheckType apicommon.CheckType +} + //nolint:gocognit,gocyclo -func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, taskCreateAttributes CreateAttributes) ([]klcv1alpha3.ItemStatus, apicommon.StatusSummary, error) { +func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, taskCreateAttributes CreateTaskAttributes) ([]klcv1alpha3.ItemStatus, apicommon.StatusSummary, error) { piWrapper, err := interfaces.NewPhaseItemWrapperFromClientObject(reconcileObject) if err != nil { return nil, apicommon.StatusSummary{}, err @@ -109,7 +115,7 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex } //nolint:dupl -func (r TaskHandler) CreateKeptnTask(ctx context.Context, namespace string, reconcileObject client.Object, taskCreateAttributes CreateAttributes) (string, error) { +func (r TaskHandler) CreateKeptnTask(ctx context.Context, namespace string, reconcileObject client.Object, taskCreateAttributes CreateTaskAttributes) (string, error) { piWrapper, err := interfaces.NewPhaseItemWrapperFromClientObject(reconcileObject) if err != nil { return "", err @@ -137,7 +143,7 @@ func (r TaskHandler) setTaskFailureEvents(task *klcv1alpha3.KeptnTask, spanTrace spanTrace.AddEvent(fmt.Sprintf("task '%s' failed with reason: '%s'", task.Name, task.Status.Message), trace.WithTimestamp(time.Now().UTC())) } -func (r TaskHandler) setupTasks(taskCreateAttributes CreateAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha3.ItemStatus) { +func (r TaskHandler) setupTasks(taskCreateAttributes CreateTaskAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha3.ItemStatus) { var tasks []string var statuses []klcv1alpha3.ItemStatus @@ -152,8 +158,8 @@ func (r TaskHandler) setupTasks(taskCreateAttributes CreateAttributes, piWrapper return tasks, statuses } -func (r TaskHandler) handleTaskNotExists(ctx context.Context, phaseCtx context.Context, taskCreateAttributes CreateAttributes, taskName string, piWrapper *interfaces.PhaseItemWrapper, reconcileObject client.Object, task *klcv1alpha3.KeptnTask, taskStatus *klcv1alpha3.ItemStatus) error { - taskCreateAttributes.Definition = taskName +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 { + taskCreateAttributes.Definition.Name = taskName taskName, err := r.CreateKeptnTask(ctx, piWrapper.GetNamespace(), reconcileObject, taskCreateAttributes) if err != nil { return err diff --git a/operator/controllers/common/taskhandler_test.go b/operator/controllers/common/taskhandler_test.go index 1a274c97d2..71ca1cb7d0 100644 --- a/operator/controllers/common/taskhandler_test.go +++ b/operator/controllers/common/taskhandler_test.go @@ -24,7 +24,7 @@ func TestTaskHandler(t *testing.T) { tests := []struct { name string object client.Object - createAttr CreateAttributes + createAttr CreateTaskAttributes wantStatus []v1alpha3.ItemStatus wantSummary apicommon.StatusSummary taskObj v1alpha3.KeptnTask @@ -36,7 +36,7 @@ func TestTaskHandler(t *testing.T) { name: "cannot unwrap object", object: &v1alpha3.KeptnTask{}, taskObj: v1alpha3.KeptnTask{}, - createAttr: CreateAttributes{}, + createAttr: CreateTaskAttributes{}, wantStatus: nil, wantSummary: apicommon.StatusSummary{}, wantErr: controllererrors.ErrCannotWrapToPhaseItem, @@ -47,9 +47,9 @@ func TestTaskHandler(t *testing.T) { name: "no tasks", object: &v1alpha3.KeptnAppVersion{}, taskObj: v1alpha3.KeptnTask{}, - createAttr: CreateAttributes{ + createAttr: CreateTaskAttributes{ SpanName: "", - Definition: "", + Definition: v1alpha3.KeptnTaskDefinition{}, CheckType: apicommon.PreDeploymentCheckType, }, wantStatus: []v1alpha3.ItemStatus(nil), @@ -68,10 +68,14 @@ func TestTaskHandler(t *testing.T) { }, }, taskObj: v1alpha3.KeptnTask{}, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "task-def", - CheckType: apicommon.PreDeploymentCheckType, + createAttr: CreateTaskAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnTaskDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "task-def", + }, + }, + CheckType: apicommon.PreDeploymentCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -105,10 +109,14 @@ func TestTaskHandler(t *testing.T) { }, }, taskObj: v1alpha3.KeptnTask{}, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "task-def", - CheckType: apicommon.PreDeploymentCheckType, + createAttr: CreateTaskAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnTaskDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "task-def", + }, + }, + CheckType: apicommon.PreDeploymentCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -153,10 +161,14 @@ func TestTaskHandler(t *testing.T) { Status: apicommon.StateFailed, }, }, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "task-def", - CheckType: apicommon.PreDeploymentCheckType, + createAttr: CreateTaskAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnTaskDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "task-def", + }, + }, + CheckType: apicommon.PreDeploymentCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -201,10 +213,14 @@ func TestTaskHandler(t *testing.T) { Status: apicommon.StateSucceeded, }, }, - createAttr: CreateAttributes{ - SpanName: "", - Definition: "task-def", - CheckType: apicommon.PreDeploymentCheckType, + createAttr: CreateTaskAttributes{ + SpanName: "", + Definition: v1alpha3.KeptnTaskDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "task-def", + }, + }, + CheckType: apicommon.PreDeploymentCheckType, }, wantStatus: []v1alpha3.ItemStatus{ { @@ -262,14 +278,14 @@ func TestTaskHandler_createTask(t *testing.T) { tests := []struct { name string object client.Object - createAttr CreateAttributes + createAttr CreateTaskAttributes wantName string wantErr error }{ { name: "cannot unwrap object", object: &v1alpha3.KeptnEvaluation{}, - createAttr: CreateAttributes{}, + createAttr: CreateTaskAttributes{}, wantName: "", wantErr: controllererrors.ErrCannotWrapToPhaseItem, }, @@ -285,10 +301,14 @@ func TestTaskHandler_createTask(t *testing.T) { }, }, }, - createAttr: CreateAttributes{ - SpanName: "", - CheckType: apicommon.PreDeploymentCheckType, - Definition: "task-def", + createAttr: CreateTaskAttributes{ + SpanName: "", + CheckType: apicommon.PreDeploymentCheckType, + Definition: v1alpha3.KeptnTaskDefinition{ + ObjectMeta: v1.ObjectMeta{ + Name: "task-def", + }, + }, }, wantName: "pre-task-def-", wantErr: nil, diff --git a/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go b/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go index 412a13ba73..ba2d753f70 100644 --- a/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go +++ b/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go @@ -24,10 +24,10 @@ import ( // DeprecateRemainingPhasesFunc: func(phase apicommon.KeptnPhaseType) { // panic("mock out the DeprecateRemainingPhases method") // }, -// GenerateEvaluationFunc: func(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation { +// GenerateEvaluationFunc: func(evaluationDefinition klcv1alpha3.KeptnEvaluationDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation { // panic("mock out the GenerateEvaluation method") // }, -// GenerateTaskFunc: func(taskDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnTask { +// GenerateTaskFunc: func(taskDefinition klcv1alpha3.KeptnTaskDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnTask { // panic("mock out the GenerateTask method") // }, // GetAppNameFunc: func() string { @@ -110,10 +110,10 @@ type PhaseItemMock struct { DeprecateRemainingPhasesFunc func(phase apicommon.KeptnPhaseType) // GenerateEvaluationFunc mocks the GenerateEvaluation method. - GenerateEvaluationFunc func(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation + GenerateEvaluationFunc func(evaluationDefinition klcv1alpha3.KeptnEvaluationDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation // GenerateTaskFunc mocks the GenerateTask method. - GenerateTaskFunc func(taskDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnTask + GenerateTaskFunc func(taskDefinition klcv1alpha3.KeptnTaskDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnTask // GetAppNameFunc mocks the GetAppName method. GetAppNameFunc func() string @@ -194,14 +194,14 @@ type PhaseItemMock struct { // GenerateEvaluation holds details about calls to the GenerateEvaluation method. GenerateEvaluation []struct { // EvaluationDefinition is the evaluationDefinition argument value. - EvaluationDefinition string + EvaluationDefinition klcv1alpha3.KeptnEvaluationDefinition // CheckType is the checkType argument value. CheckType apicommon.CheckType } // GenerateTask holds details about calls to the GenerateTask method. GenerateTask []struct { // TaskDefinition is the taskDefinition argument value. - TaskDefinition string + TaskDefinition klcv1alpha3.KeptnTaskDefinition // CheckType is the checkType argument value. CheckType apicommon.CheckType } @@ -364,12 +364,12 @@ func (mock *PhaseItemMock) DeprecateRemainingPhasesCalls() []struct { } // GenerateEvaluation calls GenerateEvaluationFunc. -func (mock *PhaseItemMock) GenerateEvaluation(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation { +func (mock *PhaseItemMock) GenerateEvaluation(evaluationDefinition klcv1alpha3.KeptnEvaluationDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation { if mock.GenerateEvaluationFunc == nil { panic("PhaseItemMock.GenerateEvaluationFunc: method is nil but PhaseItem.GenerateEvaluation was just called") } callInfo := struct { - EvaluationDefinition string + EvaluationDefinition klcv1alpha3.KeptnEvaluationDefinition CheckType apicommon.CheckType }{ EvaluationDefinition: evaluationDefinition, @@ -385,11 +385,11 @@ func (mock *PhaseItemMock) GenerateEvaluation(evaluationDefinition string, check // Check the length with: // len(mockedPhaseItem.GenerateEvaluationCalls()) func (mock *PhaseItemMock) GenerateEvaluationCalls() []struct { - EvaluationDefinition string + EvaluationDefinition klcv1alpha3.KeptnEvaluationDefinition CheckType apicommon.CheckType } { var calls []struct { - EvaluationDefinition string + EvaluationDefinition klcv1alpha3.KeptnEvaluationDefinition CheckType apicommon.CheckType } mock.lockGenerateEvaluation.RLock() @@ -399,12 +399,12 @@ func (mock *PhaseItemMock) GenerateEvaluationCalls() []struct { } // GenerateTask calls GenerateTaskFunc. -func (mock *PhaseItemMock) GenerateTask(taskDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnTask { +func (mock *PhaseItemMock) GenerateTask(taskDefinition klcv1alpha3.KeptnTaskDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnTask { if mock.GenerateTaskFunc == nil { panic("PhaseItemMock.GenerateTaskFunc: method is nil but PhaseItem.GenerateTask was just called") } callInfo := struct { - TaskDefinition string + TaskDefinition klcv1alpha3.KeptnTaskDefinition CheckType apicommon.CheckType }{ TaskDefinition: taskDefinition, @@ -420,11 +420,11 @@ func (mock *PhaseItemMock) GenerateTask(taskDefinition string, checkType apicomm // Check the length with: // len(mockedPhaseItem.GenerateTaskCalls()) func (mock *PhaseItemMock) GenerateTaskCalls() []struct { - TaskDefinition string + TaskDefinition klcv1alpha3.KeptnTaskDefinition CheckType apicommon.CheckType } { var calls []struct { - TaskDefinition string + TaskDefinition klcv1alpha3.KeptnTaskDefinition CheckType apicommon.CheckType } mock.lockGenerateTask.RLock() diff --git a/operator/controllers/lifecycle/interfaces/phaseitem.go b/operator/controllers/lifecycle/interfaces/phaseitem.go index afe1ca1a51..cb6b8fe2b7 100644 --- a/operator/controllers/lifecycle/interfaces/phaseitem.go +++ b/operator/controllers/lifecycle/interfaces/phaseitem.go @@ -36,8 +36,8 @@ type PhaseItem interface { GetPostDeploymentEvaluations() []string GetPreDeploymentEvaluationTaskStatus() []klcv1alpha3.ItemStatus GetPostDeploymentEvaluationTaskStatus() []klcv1alpha3.ItemStatus - GenerateTask(taskDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnTask - GenerateEvaluation(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation + GenerateTask(taskDefinition klcv1alpha3.KeptnTaskDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnTask + GenerateEvaluation(evaluationDefinition klcv1alpha3.KeptnEvaluationDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation GetSpanAttributes() []attribute.KeyValue SetSpanAttributes(span trace.Span) DeprecateRemainingPhases(phase apicommon.KeptnPhaseType) @@ -139,11 +139,11 @@ func (pw PhaseItemWrapper) GetPostDeploymentEvaluationTaskStatus() []klcv1alpha3 return pw.Obj.GetPostDeploymentEvaluationTaskStatus() } -func (pw PhaseItemWrapper) GenerateTask(taskDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnTask { +func (pw PhaseItemWrapper) GenerateTask(taskDefinition klcv1alpha3.KeptnTaskDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnTask { return pw.Obj.GenerateTask(taskDefinition, checkType) } -func (pw PhaseItemWrapper) GenerateEvaluation(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation { +func (pw PhaseItemWrapper) GenerateEvaluation(evaluationDefinition klcv1alpha3.KeptnEvaluationDefinition, checkType apicommon.CheckType) klcv1alpha3.KeptnEvaluation { return pw.Obj.GenerateEvaluation(evaluationDefinition, checkType) } diff --git a/operator/controllers/lifecycle/interfaces/phaseitem_test.go b/operator/controllers/lifecycle/interfaces/phaseitem_test.go index ceaec84079..87abc9c5de 100644 --- a/operator/controllers/lifecycle/interfaces/phaseitem_test.go +++ b/operator/controllers/lifecycle/interfaces/phaseitem_test.go @@ -95,10 +95,10 @@ func TestPhaseItem(t *testing.T) { GetPostDeploymentEvaluationTaskStatusFunc: func() []v1alpha3.ItemStatus { return nil }, - GenerateTaskFunc: func(taskDefinition string, checkType apicommon.CheckType) v1alpha3.KeptnTask { + GenerateTaskFunc: func(taskDefinition v1alpha3.KeptnTaskDefinition, checkType apicommon.CheckType) v1alpha3.KeptnTask { return v1alpha3.KeptnTask{} }, - GenerateEvaluationFunc: func(evaluationDefinition string, checkType apicommon.CheckType) v1alpha3.KeptnEvaluation { + GenerateEvaluationFunc: func(evaluationDefinition v1alpha3.KeptnEvaluationDefinition, checkType apicommon.CheckType) v1alpha3.KeptnEvaluation { return v1alpha3.KeptnEvaluation{} }, SetSpanAttributesFunc: func(span trace.Span) { @@ -175,10 +175,10 @@ func TestPhaseItem(t *testing.T) { _ = wrapper.GetPostDeploymentEvaluationTaskStatus() require.Len(t, phaseItemMock.GetPostDeploymentEvaluationTaskStatusCalls(), 1) - _ = wrapper.GenerateTask("", apicommon.PostDeploymentCheckType) + _ = wrapper.GenerateTask(v1alpha3.KeptnTaskDefinition{}, apicommon.PostDeploymentCheckType) require.Len(t, phaseItemMock.GenerateTaskCalls(), 1) - _ = wrapper.GenerateEvaluation("", apicommon.PostDeploymentCheckType) + _ = wrapper.GenerateEvaluation(v1alpha3.KeptnEvaluationDefinition{}, apicommon.PostDeploymentCheckType) require.Len(t, phaseItemMock.GenerateEvaluationCalls(), 1) wrapper.SetSpanAttributes(nil) diff --git a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go index 9be3b16f3c..c66d8f003a 100644 --- a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go +++ b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go @@ -20,7 +20,7 @@ func (r *KeptnAppVersionReconciler) reconcilePrePostDeployment(ctx context.Conte SpanHandler: r.SpanHandler, } - taskCreateAttributes := controllercommon.CreateAttributes{ + taskCreateAttributes := controllercommon.CreateTaskAttributes{ SpanName: fmt.Sprintf(apicommon.CreateAppTaskSpanName, checkType), CheckType: checkType, } diff --git a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go index 7652417317..cc9531efd2 100644 --- a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go +++ b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go @@ -20,7 +20,7 @@ func (r *KeptnAppVersionReconciler) reconcilePrePostEvaluation(ctx context.Conte SpanHandler: r.SpanHandler, } - evaluationCreateAttributes := controllercommon.CreateAttributes{ + evaluationCreateAttributes := controllercommon.CreateEvaluationAttributes{ SpanName: fmt.Sprintf(apicommon.CreateAppEvalSpanName, checkType), CheckType: checkType, } diff --git a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go index d4ef12d7a4..7e4d29c77f 100644 --- a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go +++ b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go @@ -20,7 +20,7 @@ func (r *KeptnWorkloadInstanceReconciler) reconcilePrePostDeployment(ctx context SpanHandler: r.SpanHandler, } - taskCreateAttributes := controllercommon.CreateAttributes{ + taskCreateAttributes := controllercommon.CreateTaskAttributes{ SpanName: fmt.Sprintf(apicommon.CreateWorkloadTaskSpanName, checkType), CheckType: checkType, } diff --git a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go index 117e246736..ac63e4aaf7 100644 --- a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go +++ b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go @@ -20,7 +20,7 @@ func (r *KeptnWorkloadInstanceReconciler) reconcilePrePostEvaluation(ctx context SpanHandler: r.SpanHandler, } - evaluationCreateAttributes := controllercommon.CreateAttributes{ + evaluationCreateAttributes := controllercommon.CreateEvaluationAttributes{ SpanName: fmt.Sprintf(apicommon.CreateWorkloadEvalSpanName, checkType), CheckType: checkType, }