Skip to content

Commit

Permalink
feat(operator): additional parameters for KeptnTask to support retry …
Browse files Browse the repository at this point in the history
…logic (#1084)

Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed Mar 22, 2023
1 parent 8e93f88 commit eed5568
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 121 deletions.
16 changes: 12 additions & 4 deletions operator/apis/lifecycle/v1alpha3/keptnappversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions operator/apis/lifecycle/v1alpha3/keptnappversion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 8 additions & 5 deletions operator/apis/lifecycle/v1alpha3/keptntask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 12 additions & 4 deletions operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -319,13 +322,17 @@ spec:
type: string
type: object
type: object
retries:
type: integer
secureParameters:
properties:
secret:
type: string
type: object
taskDefinition:
type: string
timeout:
type: string
workload:
type: string
workloadVersion:
Expand All @@ -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
Expand Down
16 changes: 11 additions & 5 deletions operator/controllers/common/evaluationhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
Loading

0 comments on commit eed5568

Please sign in to comment.