diff --git a/.github/scripts/.helm-tests/default/result.yaml b/.github/scripts/.helm-tests/default/result.yaml index b2ed4c4968..30d989618e 100644 --- a/.github/scripts/.helm-tests/default/result.yaml +++ b/.github/scripts/.helm-tests/default/result.yaml @@ -1710,6 +1710,8 @@ spec: MapCarrier is a TextMapCarrier that uses a map held in memory as a storage medium for propagated key-value pairs. type: object + description: PhaseTraceID is a map storing TraceIDs of OpenTelemetry + spans in lifecycle phases type: object postDeploymentEvaluationStatus: default: Pending diff --git a/.github/scripts/.helm-tests/lifecycle-only/result.yaml b/.github/scripts/.helm-tests/lifecycle-only/result.yaml index e865ee5d09..85f5b33afe 100644 --- a/.github/scripts/.helm-tests/lifecycle-only/result.yaml +++ b/.github/scripts/.helm-tests/lifecycle-only/result.yaml @@ -1656,6 +1656,8 @@ spec: MapCarrier is a TextMapCarrier that uses a map held in memory as a storage medium for propagated key-value pairs. type: object + description: PhaseTraceID is a map storing TraceIDs of OpenTelemetry + spans in lifecycle phases type: object postDeploymentEvaluationStatus: default: Pending diff --git a/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml b/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml index 72dbb3af78..0f4b4bb2b8 100644 --- a/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml +++ b/.github/scripts/.helm-tests/lifecycle-with-certs/result.yaml @@ -1671,6 +1671,8 @@ spec: MapCarrier is a TextMapCarrier that uses a map held in memory as a storage medium for propagated key-value pairs. type: object + description: PhaseTraceID is a map storing TraceIDs of OpenTelemetry + spans in lifecycle phases type: object postDeploymentEvaluationStatus: default: Pending diff --git a/.github/scripts/.helm-tests/local-global-precedence/result.yaml b/.github/scripts/.helm-tests/local-global-precedence/result.yaml index 229d3ec99c..de18970d6e 100644 --- a/.github/scripts/.helm-tests/local-global-precedence/result.yaml +++ b/.github/scripts/.helm-tests/local-global-precedence/result.yaml @@ -1774,6 +1774,8 @@ spec: MapCarrier is a TextMapCarrier that uses a map held in memory as a storage medium for propagated key-value pairs. type: object + description: PhaseTraceID is a map storing TraceIDs of OpenTelemetry + spans in lifecycle phases type: object postDeploymentEvaluationStatus: default: Pending diff --git a/docs/docs/reference/api-reference/lifecycle/v1/index.md b/docs/docs/reference/api-reference/lifecycle/v1/index.md index a811581c9b..7353ea1187 100644 --- a/docs/docs/reference/api-reference/lifecycle/v1/index.md +++ b/docs/docs/reference/api-reference/lifecycle/v1/index.md @@ -1091,7 +1091,7 @@ _Appears in:_ _Underlying type:_ _[MapCarrier](https://pkg.go.dev/go.opentelemetry.io/otel/propagation#MapCarrier)_ - +PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases diff --git a/docs/docs/reference/api-reference/lifecycle/v1alpha1/index.md b/docs/docs/reference/api-reference/lifecycle/v1alpha1/index.md index caadf58560..faa0d7d205 100644 --- a/docs/docs/reference/api-reference/lifecycle/v1alpha1/index.md +++ b/docs/docs/reference/api-reference/lifecycle/v1alpha1/index.md @@ -156,10 +156,6 @@ _Appears in:_ | `configMap` _string_ | || ✓ | | - - - - #### HttpReference @@ -499,10 +495,6 @@ _Appears in:_ | `endTime` _[Time](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#time-v1-meta)_ | || ✓ | | - - - - #### KeptnState _Underlying type:_ _string_ @@ -931,8 +923,6 @@ _Appears in:_ | `secret` _string_ | || ✓ | | - - #### TaskContext diff --git a/docs/docs/reference/api-reference/lifecycle/v1alpha2/index.md b/docs/docs/reference/api-reference/lifecycle/v1alpha2/index.md index f11c2e58b7..3cd9c6e05b 100644 --- a/docs/docs/reference/api-reference/lifecycle/v1alpha2/index.md +++ b/docs/docs/reference/api-reference/lifecycle/v1alpha2/index.md @@ -497,10 +497,6 @@ _Appears in:_ | `endTime` _[Time](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#time-v1-meta)_ | || ✓ | | - - - - #### KeptnState _Underlying type:_ _string_ @@ -882,7 +878,7 @@ _Appears in:_ _Underlying type:_ _[MapCarrier](https://pkg.go.dev/go.opentelemetry.io/otel/propagation#MapCarrier)_ - +PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases @@ -928,8 +924,6 @@ _Appears in:_ | `secret` _string_ | || ✓ | | - - #### TaskContext diff --git a/docs/docs/reference/api-reference/lifecycle/v1alpha3/index.md b/docs/docs/reference/api-reference/lifecycle/v1alpha3/index.md index 457d80f32a..79b1584f28 100644 --- a/docs/docs/reference/api-reference/lifecycle/v1alpha3/index.md +++ b/docs/docs/reference/api-reference/lifecycle/v1alpha3/index.md @@ -34,8 +34,6 @@ Package v1alpha3 contains API Schema definitions for the lifecycle v1alpha3 API - - #### AutomountServiceAccountTokenSpec @@ -560,8 +558,6 @@ _Appears in:_ | `endTime` _[Time](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#time-v1-meta)_ | EndTime represents the time at which the KeptnEvaluation finished. || ✓ | | - - #### KeptnMetricReference @@ -579,8 +575,6 @@ _Appears in:_ | `namespace` _string_ | Namespace is the namespace where the referenced KeptnMetric is located. || ✓ | | - - #### KeptnState _Underlying type:_ _string_ @@ -969,7 +963,7 @@ _Appears in:_ _Underlying type:_ _[MapCarrier](https://pkg.go.dev/go.opentelemetry.io/otel/propagation#MapCarrier)_ - +PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases @@ -1053,8 +1047,6 @@ _Appears in:_ | `name` _string_ | || x | | - - #### TaskContext diff --git a/docs/docs/reference/api-reference/lifecycle/v1beta1/index.md b/docs/docs/reference/api-reference/lifecycle/v1beta1/index.md index a869642b0c..db9711b1ae 100644 --- a/docs/docs/reference/api-reference/lifecycle/v1beta1/index.md +++ b/docs/docs/reference/api-reference/lifecycle/v1beta1/index.md @@ -36,8 +36,6 @@ Package v1beta1 contains API Schema definitions for the lifecycle v1beta1 API gr - - #### AutomountServiceAccountTokenSpec @@ -679,8 +677,6 @@ _Appears in:_ | `endTime` _[Time](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#time-v1-meta)_ | EndTime represents the time at which the KeptnEvaluation finished. || ✓ | | - - #### KeptnMetricReference @@ -698,8 +694,6 @@ _Appears in:_ | `namespace` _string_ | Namespace is the namespace where the referenced KeptnMetric is located. || ✓ | | - - #### KeptnState _Underlying type:_ _string_ @@ -1092,7 +1086,7 @@ _Appears in:_ _Underlying type:_ _[MapCarrier](https://pkg.go.dev/go.opentelemetry.io/otel/propagation#MapCarrier)_ - +PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases @@ -1176,8 +1170,6 @@ _Appears in:_ | `name` _string_ | || x | | - - #### TaskContext diff --git a/lifecycle-operator/apis/lifecycle/v1/common/phases.go b/lifecycle-operator/apis/lifecycle/v1/common/phases.go index 875e193a8f..33466d7ea8 100644 --- a/lifecycle-operator/apis/lifecycle/v1/common/phases.go +++ b/lifecycle-operator/apis/lifecycle/v1/common/phases.go @@ -115,6 +115,7 @@ var ( PhaseDeprecated = KeptnPhaseType{LongName: "Deprecated", ShortName: "Deprecated"} ) +// PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases type PhaseTraceID map[string]propagation.MapCarrier func (pid PhaseTraceID) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { diff --git a/lifecycle-operator/apis/lifecycle/v1/keptnapp_types.go b/lifecycle-operator/apis/lifecycle/v1/keptnapp_types.go index 69fc0b0d92..164e4913e9 100644 --- a/lifecycle-operator/apis/lifecycle/v1/keptnapp_types.go +++ b/lifecycle-operator/apis/lifecycle/v1/keptnapp_types.go @@ -24,9 +24,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppSpec defines the desired state of KeptnApp type KeptnAppSpec struct { // Version defines the version of the application. For automatically created KeptnApps, diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/common/common.go b/lifecycle-operator/apis/lifecycle/v1alpha1/common/common.go index 6dc05c4e48..65dfa974b3 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/common/common.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/common/common.go @@ -5,179 +5,17 @@ package common import ( "errors" - "fmt" - "math/rand" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/propagation" ) -const WorkloadAnnotation = "keptn.sh/workload" -const VersionAnnotation = "keptn.sh/version" -const AppAnnotation = "keptn.sh/app" -const PreDeploymentTaskAnnotation = "keptn.sh/pre-deployment-tasks" -const PostDeploymentTaskAnnotation = "keptn.sh/post-deployment-tasks" -const K8sRecommendedWorkloadAnnotations = "app.kubernetes.io/name" -const K8sRecommendedVersionAnnotations = "app.kubernetes.io/version" -const K8sRecommendedAppAnnotations = "app.kubernetes.io/part-of" -const PreDeploymentEvaluationAnnotation = "keptn.sh/pre-deployment-evaluations" -const PostDeploymentEvaluationAnnotation = "keptn.sh/post-deployment-evaluations" -const TaskNameAnnotation = "keptn.sh/task-name" -const NamespaceEnabledAnnotation = "keptn.sh/lifecycle-toolkit" - -const CreateAppTaskSpanName = "create_%s_app_task" -const CreateWorkloadTaskSpanName = "create_%s_deployment_task" -const CreateAppEvalSpanName = "create_%s_app_evaluation" -const CreateWorkloadEvalSpanName = "create_%s_deployment_evaluation" - -const MaxAppNameLength = 25 -const MaxWorkloadNameLength = 25 -const MaxTaskNameLength = 25 -const MaxVersionLength = 12 - var ErrCannotCastKeptnAppVersion = errors.New("cannot cast KeptnAppVersion to v1") var ErrCannotCastKeptnApp = errors.New("cannot cast KeptnApp to v1") // KeptnState is a string containing current Phase state (Progressing/Succeeded/Failed/Unknown/Pending/Cancelled) type KeptnState string -const ( - StateProgressing KeptnState = "Progressing" - StateSucceeded KeptnState = "Succeeded" - StateFailed KeptnState = "Failed" - StateUnknown KeptnState = "Unknown" - StatePending KeptnState = "Pending" - StateCancelled KeptnState = "Cancelled" -) - -func (k KeptnState) IsCompleted() bool { - return k == StateSucceeded || k == StateFailed || k == StateCancelled -} - -func (k KeptnState) IsSucceeded() bool { - return k == StateSucceeded -} - -func (k KeptnState) IsFailed() bool { - return k == StateFailed -} - -func (k KeptnState) IsCancelled() bool { - return k == StateCancelled -} - -func (k KeptnState) IsPending() bool { - return k == StatePending -} - -type StatusSummary struct { - Total int - Progressing int - Failed int - Succeeded int - Pending int - Unknown int - Cancelled int -} - -func UpdateStatusSummary(status KeptnState, summary StatusSummary) StatusSummary { - switch status { - case StateFailed: - summary.Failed++ - case StateCancelled: - summary.Cancelled++ - case StateSucceeded: - summary.Succeeded++ - case StateProgressing: - summary.Progressing++ - case StatePending, "": - summary.Pending++ - case StateUnknown: - summary.Unknown++ - } - return summary -} - -func (s StatusSummary) GetTotalCount() int { - return s.Failed + s.Succeeded + s.Progressing + s.Pending + s.Unknown + s.Cancelled -} - -func GetOverallState(s StatusSummary) KeptnState { - if s.Failed > 0 || s.Cancelled > 0 { - return StateFailed - } - if s.Progressing > 0 { - return StateProgressing - } - if s.Pending > 0 { - return StatePending - } - if s.Unknown > 0 || s.GetTotalCount() != s.Total { - return StateUnknown - } - return StateSucceeded -} - -func TruncateString(s string, max int) string { - if len(s) > max { - return s[:max] - } - return s -} - type CheckType string -const PreDeploymentCheckType CheckType = "pre" -const PostDeploymentCheckType CheckType = "post" -const PreDeploymentEvaluationCheckType CheckType = "pre-eval" -const PostDeploymentEvaluationCheckType CheckType = "post-eval" - -type KeptnMeters struct { - TaskCount metric.Int64Counter - TaskDuration metric.Float64Histogram - DeploymentCount metric.Int64Counter - DeploymentDuration metric.Float64Histogram - AppCount metric.Int64Counter - AppDuration metric.Float64Histogram - EvaluationCount metric.Int64Counter - EvaluationDuration metric.Float64Histogram -} - -const ( - AppName attribute.Key = attribute.Key("keptn.deployment.app.name") - AppVersion attribute.Key = attribute.Key("keptn.deployment.app.version") - AppNamespace attribute.Key = attribute.Key("keptn.deployment.app.namespace") - AppStatus attribute.Key = attribute.Key("keptn.deployment.app.status") - AppPreviousVersion attribute.Key = attribute.Key("keptn.deployment.app.previousversion") - WorkloadName attribute.Key = attribute.Key("keptn.deployment.workload.name") - WorkloadVersion attribute.Key = attribute.Key("keptn.deployment.workload.version") - WorkloadPreviousVersion attribute.Key = attribute.Key("keptn.deployment.workload.previousversion") - WorkloadNamespace attribute.Key = attribute.Key("keptn.deployment.workload.namespace") - WorkloadStatus attribute.Key = attribute.Key("keptn.deployment.workload.status") - TaskStatus attribute.Key = attribute.Key("keptn.deployment.task.status") - TaskName attribute.Key = attribute.Key("keptn.deployment.task.name") - TaskType attribute.Key = attribute.Key("keptn.deployment.task.type") - EvaluationStatus attribute.Key = attribute.Key("keptn.deployment.evaluation.status") - EvaluationName attribute.Key = attribute.Key("keptn.deployment.evaluation.name") - EvaluationType attribute.Key = attribute.Key("keptn.deployment.evaluation.type") -) - -func GenerateTaskName(checkType CheckType, taskName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return fmt.Sprintf("%s-%s-%d", checkType, TruncateString(taskName, 32), randomId) -} - -func GenerateEvaluationName(checkType CheckType, evalName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return fmt.Sprintf("%s-%s-%d", checkType, TruncateString(evalName, 27), randomId) -} - -type GaugeValue struct { - Value int64 - Attributes []attribute.KeyValue -} - -type GaugeFloatValue struct { - Value float64 - Attributes []attribute.KeyValue -} +// PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases +type PhaseTraceID map[string]propagation.MapCarrier diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/common/common_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/common/common_test.go deleted file mode 100644 index 1bb60e0921..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/common/common_test.go +++ /dev/null @@ -1,304 +0,0 @@ -package common - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestKeptnState_IsCompleted(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - { - State: StateSucceeded, - Want: true, - }, - { - State: StateCancelled, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsCompleted(), tt.Want) - }) - } -} - -func TestKeptnState_IsSucceeded(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateSucceeded, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsSucceeded(), tt.Want) - }) - } -} - -func TestKeptnState_IsFailed(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsFailed(), tt.Want) - }) - } -} - -func TestKeptnState_IsCancelled(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateCancelled, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsCancelled(), tt.Want) - }) - } -} - -func TestKeptnKeptnState_IsPending(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StatePending, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPending(), tt.Want) - }) - } -} - -func Test_UpdateStatusSummary(t *testing.T) { - emmptySummary := StatusSummary{0, 0, 0, 0, 0, 0, 0} - tests := []struct { - State KeptnState - Want StatusSummary - }{ - { - State: StateProgressing, - Want: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - }, - { - State: StateFailed, - Want: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - }, - { - State: StateSucceeded, - Want: StatusSummary{0, 0, 0, 1, 0, 0, 0}, - }, - { - State: StatePending, - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: "", - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: StateUnknown, - Want: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - }, - { - State: StateCancelled, - Want: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, UpdateStatusSummary(tt.State, emmptySummary), tt.Want) - }) - } -} - -func Test_GetTotalCount(t *testing.T) { - summary := StatusSummary{2, 0, 2, 1, 0, 3, 5} - require.Equal(t, summary.GetTotalCount(), 11) -} - -func Test_GeOverallState(t *testing.T) { - tests := []struct { - Name string - Summary StatusSummary - Want KeptnState - }{ - { - Name: "failed", - Summary: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - Want: StateFailed, - }, - { - Name: "cancelled", - Summary: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - Want: StateFailed, - }, - { - Name: "progressing", - Summary: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - Want: StateProgressing, - }, - { - Name: "pending", - Summary: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - Want: StatePending, - }, - { - Name: "unknown", - Summary: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "unknown totalcount", - Summary: StatusSummary{5, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "succeeded", - Summary: StatusSummary{1, 0, 0, 1, 0, 0, 0}, - Want: StateSucceeded, - }, - } - for _, tt := range tests { - t.Run(tt.Name, func(t *testing.T) { - require.Equal(t, GetOverallState(tt.Summary), tt.Want) - }) - } -} - -func Test_TruncateString(t *testing.T) { - tests := []struct { - Input string - Max int - Want string - }{ - { - Input: "some_string", - Max: 20, - Want: "some_string", - }, - { - Input: "some_string", - Max: 5, - Want: "some_", - }, - { - Input: "", - Max: 5, - Want: "", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, TruncateString(tt.Input, tt.Max), tt.Want) - }) - } -} - -func Test_GenerateTaskName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentCheckType, - Name: "short-name", - Want: "pre-short-name-", - }, - { - Check: PreDeploymentCheckType, - Name: "", - Want: "pre--", - }, - { - Check: PreDeploymentCheckType, - Name: "loooooooooooooooooooooooooooooooooooooong_name", - Want: "pre-looooooooooooooooooooooooooooooo-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateTaskName(tt.Check, tt.Name), tt.Want)) - }) - } -} - -func Test_GenerateEvaluationName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentEvaluationCheckType, - Name: "short-name", - Want: "pre-eval-short-name-", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: "", - Want: "pre-eval--", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: "loooooooooooooooooooooooooooooooooooooong_name", - Want: "pre-eval-loooooooooooooooooooooooooo-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateEvaluationName(tt.Check, tt.Name), tt.Want)) - }) - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/common/phases.go b/lifecycle-operator/apis/lifecycle/v1alpha1/common/phases.go deleted file mode 100644 index b8c8fec5b0..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/common/phases.go +++ /dev/null @@ -1,98 +0,0 @@ -package common - -import ( - "strings" - - "go.opentelemetry.io/otel/propagation" -) - -type KeptnPhase KeptnPhaseType - -type KeptnPhaseType struct { - LongName string - ShortName string -} - -var phases = []KeptnPhaseType{ - PhaseWorkloadPreDeployment, - PhaseWorkloadPostDeployment, - PhaseWorkloadPreEvaluation, - PhaseWorkloadPostEvaluation, - PhaseWorkloadDeployment, - PhaseAppPreDeployment, - PhaseAppPostDeployment, - PhaseAppPreEvaluation, - PhaseAppPostEvaluation, - PhaseAppDeployment, - PhaseCompleted, - PhaseCancelled, -} - -func (p KeptnPhaseType) IsEvaluation() bool { - return strings.Contains(p.ShortName, "DeployEvaluations") -} - -func (p KeptnPhaseType) IsPreEvaluation() bool { - return strings.Contains(p.ShortName, "PreDeployEvaluations") -} - -func (p KeptnPhaseType) IsPostEvaluation() bool { - return strings.Contains(p.ShortName, "PostDeployEvaluations") -} - -func (p KeptnPhaseType) IsTask() bool { - return strings.Contains(p.ShortName, "DeployTasks") -} - -func (p KeptnPhaseType) IsPreTask() bool { - return strings.Contains(p.ShortName, "PreDeployTasks") -} - -func (p KeptnPhaseType) IsPostTask() bool { - return strings.Contains(p.ShortName, "PostDeployTasks") -} - -func GetShortPhaseName(phase string) string { - for _, p := range phases { - if phase == p.ShortName { - return p.ShortName - } - } - - for _, p := range phases { - if phase == p.LongName { - return p.ShortName - } - } - - return "" -} - -var ( - PhaseWorkloadPreDeployment = KeptnPhaseType{LongName: "Workload Pre-Deployment Tasks", ShortName: "WorkloadPreDeployTasks"} - PhaseWorkloadPostDeployment = KeptnPhaseType{LongName: "Workload Post-Deployment Tasks", ShortName: "WorkloadPostDeployTasks"} - PhaseWorkloadPreEvaluation = KeptnPhaseType{LongName: "Workload Pre-Deployment Evaluations", ShortName: "WorkloadPreDeployEvaluations"} - PhaseWorkloadPostEvaluation = KeptnPhaseType{LongName: "Workload Post-Deployment Evaluations", ShortName: "WorkloadPostDeployEvaluations"} - PhaseWorkloadDeployment = KeptnPhaseType{LongName: "Workload Deployment", ShortName: "WorkloadDeploy"} - PhaseAppPreDeployment = KeptnPhaseType{LongName: "App Pre-Deployment Tasks", ShortName: "AppPreDeployTasks"} - PhaseAppPostDeployment = KeptnPhaseType{LongName: "App Post-Deployment Tasks", ShortName: "AppPostDeployTasks"} - PhaseAppPreEvaluation = KeptnPhaseType{LongName: "App Pre-Deployment Evaluations", ShortName: "AppPreDeployEvaluations"} - PhaseAppPostEvaluation = KeptnPhaseType{LongName: "App Post-Deployment Evaluations", ShortName: "AppPostDeployEvaluations"} - PhaseAppDeployment = KeptnPhaseType{LongName: "App Deployment", ShortName: "AppDeploy"} - PhaseReconcileEvaluation = KeptnPhaseType{LongName: "Reconcile Evaluation", ShortName: "ReconcileEvaluation"} - PhaseCreateEvaluation = KeptnPhaseType{LongName: "Create Evaluation", ShortName: "Create Evaluation"} - PhaseCompleted = KeptnPhaseType{LongName: "Completed", ShortName: "Completed"} - PhaseCancelled = KeptnPhaseType{LongName: "Cancelled", ShortName: "Cancelled"} -) - -// PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases -type PhaseTraceID map[string]propagation.MapCarrier - -func (pid PhaseTraceID) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - pid[GetShortPhaseName(phase)] = carrier - -} - -func (pid PhaseTraceID) GetPhaseTraceID(phase string) propagation.MapCarrier { - return pid[GetShortPhaseName(phase)] -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/common/phases_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/common/phases_test.go deleted file mode 100644 index 271732c1f1..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/common/phases_test.go +++ /dev/null @@ -1,275 +0,0 @@ -package common - -import ( - "testing" - - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/propagation" -) - -func TestKeptnPhaseType_IsEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: false, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: false, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: false, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: false, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostTask(), tt.Want) - }) - } -} - -func TestPhaseTraceID(t *testing.T) { - trace := PhaseTraceID{} - - trace.SetPhaseTraceID(PhaseAppDeployment.LongName, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - }, trace) - - trace.SetPhaseTraceID(PhaseWorkloadDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, trace) - - require.Equal(t, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, trace.GetPhaseTraceID(PhaseAppDeployment.LongName)) - - require.Equal(t, propagation.MapCarrier{ - "name3": "trace3", - }, trace.GetPhaseTraceID(PhaseWorkloadDeployment.ShortName)) -} - -func TestGetShortPhaseName(t *testing.T) { - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("WorkloadPreDeployTasks")) - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("Workload Pre-Deployment Tasks")) - require.Equal(t, "", GetShortPhaseName("Workload Pre-Deploycdddment Tasks")) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnapp_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnapp_test.go deleted file mode 100644 index 9ce1fdcd23..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnapp_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package v1alpha1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnApp(t *testing.T) { - app := &KeptnApp{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - }, - Spec: KeptnAppSpec{ - Version: "version", - }, - } - - appVersionName := app.GetAppVersionName() - require.Equal(t, "app-version", appVersionName) - - appVersion := app.GenerateAppVersion("prev", map[string]string{}) - require.Equal(t, KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{}, - Name: "app-version", - Namespace: "namespace", - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - Version: "version", - }, - AppName: "app", - PreviousVersion: "prev", - }, - }, appVersion) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("version"), - }, app.GetSpanAttributes()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnapp_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnapp_types.go index 9aae545d40..cbcaae11c6 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnapp_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnapp_types.go @@ -17,17 +17,9 @@ limitations under the License. package v1alpha1 import ( - "strings" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppSpec defines the desired state of KeptnApp type KeptnAppSpec struct { Version string `json:"version"` @@ -82,33 +74,3 @@ type KeptnAppList struct { func init() { SchemeBuilder.Register(&KeptnApp{}, &KeptnAppList{}) } - -func (a KeptnApp) GetAppVersionName() string { - return strings.ToLower(a.Name + "-" + a.Spec.Version) -} - -func (a KeptnApp) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnApp) GenerateAppVersion(previousVersion string, traceContextCarrier map[string]string) KeptnAppVersion { - return KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: traceContextCarrier, - Name: a.GetAppVersionName(), - Namespace: a.Namespace, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: a.Spec, - AppName: a.Name, - PreviousVersion: previousVersion, - }, - } -} - -func (a KeptnApp) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Name), - common.AppVersion.String(a.Spec.Version), - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_conversion_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_conversion_test.go index 7e05f07f37..90953ba582 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_conversion_test.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_conversion_test.go @@ -13,6 +13,8 @@ import ( v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" ) +const StateFailed common.KeptnState = "Failed" + func TestKeptnAppVersion_ConvertFrom(t *testing.T) { tests := []struct { name string @@ -205,25 +207,25 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -231,48 +233,48 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { { TaskDefinitionName: "def1", TaskName: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { TaskDefinitionName: "def12", TaskName: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []TaskStatus{ { TaskDefinitionName: "def2", TaskName: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { TaskDefinitionName: "def22", TaskName: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ { EvaluationDefinitionName: "def3", EvaluationName: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { EvaluationDefinitionName: "def32", EvaluationName: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ { EvaluationDefinitionName: "def4", EvaluationName: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { EvaluationDefinitionName: "def42", EvaluationName: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -285,7 +287,7 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, }, @@ -366,25 +368,25 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -392,48 +394,48 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { { TaskDefinitionName: "def1", TaskName: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { TaskDefinitionName: "def12", TaskName: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []TaskStatus{ { TaskDefinitionName: "def2", TaskName: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { TaskDefinitionName: "def22", TaskName: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ { EvaluationDefinitionName: "def3", EvaluationName: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { EvaluationDefinitionName: "def32", EvaluationName: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ { EvaluationDefinitionName: "def4", EvaluationName: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { EvaluationDefinitionName: "def42", EvaluationName: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -446,7 +448,7 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, wantErr: false, diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_test.go deleted file mode 100644 index 95a6c0b4b4..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_test.go +++ /dev/null @@ -1,373 +0,0 @@ -package v1alpha1 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -//nolint:dupl -func TestKeptnAppVersion(t *testing.T) { - app := &KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - }, - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, - Status: common.StateFailed, - PreDeploymentTaskStatus: []TaskStatus{ - { - TaskDefinitionName: "defname", - Status: common.StateFailed, - TaskName: "taskname", - }, - }, - PostDeploymentTaskStatus: []TaskStatus{ - { - TaskDefinitionName: "defname2", - Status: common.StateFailed, - TaskName: "taskname2", - }, - }, - PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ - { - EvaluationDefinitionName: "defname3", - Status: common.StateFailed, - EvaluationName: "taskname3", - }, - }, - PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ - { - EvaluationDefinitionName: "defname4", - Status: common.StateFailed, - EvaluationName: "taskname4", - }, - }, - CurrentPhase: common.PhaseAppDeployment.ShortName, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - PreDeploymentTasks: []string{"task1", "task2"}, - PostDeploymentTasks: []string{"task3", "task4"}, - PreDeploymentEvaluations: []string{"task5", "task6"}, - PostDeploymentEvaluations: []string{"task7", "task8"}, - Version: "version", - }, - PreviousVersion: "prev", - AppName: "appname", - TraceId: map[string]string{"traceparent": "trace1"}, - }, - } - - require.True(t, app.IsPreDeploymentCompleted()) - require.False(t, app.IsPreDeploymentSucceeded()) - require.True(t, app.IsPreDeploymentFailed()) - - require.True(t, app.IsPreDeploymentEvaluationCompleted()) - require.False(t, app.IsPreDeploymentEvaluationSucceeded()) - require.True(t, app.IsPreDeploymentEvaluationFailed()) - - require.True(t, app.IsPostDeploymentCompleted()) - require.False(t, app.IsPostDeploymentSucceeded()) - require.True(t, app.IsPostDeploymentFailed()) - - require.True(t, app.IsPostDeploymentEvaluationCompleted()) - require.False(t, app.IsPostDeploymentEvaluationSucceeded()) - require.True(t, app.IsPostDeploymentEvaluationFailed()) - - require.True(t, app.AreWorkloadsCompleted()) - require.False(t, app.AreWorkloadsSucceeded()) - require.True(t, app.AreWorkloadsFailed()) - - require.False(t, app.IsEndTimeSet()) - require.False(t, app.IsStartTimeSet()) - - app.SetStartTime() - app.SetEndTime() - - require.True(t, app.IsEndTimeSet()) - require.True(t, app.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - common.AppStatus.String(string(common.StateFailed)), - }, app.GetMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppPreviousVersion.String("prev"), - }, app.GetDurationMetricsAttributes()) - - require.Equal(t, common.StateFailed, app.GetState()) - - require.Equal(t, []string{"task1", "task2"}, app.GetPreDeploymentTasks()) - require.Equal(t, []string{"task3", "task4"}, app.GetPostDeploymentTasks()) - require.Equal(t, []string{"task5", "task6"}, app.GetPreDeploymentEvaluations()) - require.Equal(t, []string{"task7", "task8"}, app.GetPostDeploymentEvaluations()) - - require.Equal(t, []TaskStatus{ - { - TaskDefinitionName: "defname", - Status: common.StateFailed, - TaskName: "taskname", - }, - }, app.GetPreDeploymentTaskStatus()) - - require.Equal(t, []TaskStatus{ - { - TaskDefinitionName: "defname2", - Status: common.StateFailed, - TaskName: "taskname2", - }, - }, app.GetPostDeploymentTaskStatus()) - - require.Equal(t, []EvaluationStatus{ - { - EvaluationDefinitionName: "defname3", - Status: common.StateFailed, - EvaluationName: "taskname3", - }, - }, app.GetPreDeploymentEvaluationTaskStatus()) - - require.Equal(t, []EvaluationStatus{ - { - EvaluationDefinitionName: "defname4", - Status: common.StateFailed, - EvaluationName: "taskname4", - }, - }, app.GetPostDeploymentEvaluationTaskStatus()) - - require.Equal(t, "appname", app.GetAppName()) - require.Equal(t, "prev", app.GetPreviousVersion()) - require.Equal(t, "appname", app.GetParentName()) - require.Equal(t, "namespace", app.GetNamespace()) - - app.SetState(common.StatePending) - require.Equal(t, common.StatePending, app.GetState()) - - require.True(t, !app.GetStartTime().IsZero()) - require.True(t, !app.GetEndTime().IsZero()) - - app.SetCurrentPhase(common.PhaseAppDeployment.LongName) - require.Equal(t, common.PhaseAppDeployment.LongName, app.GetCurrentPhase()) - - app.Status.EndTime = v1.Time{Time: time.Time{}} - app.Complete() - require.True(t, !app.GetEndTime().IsZero()) - - require.Equal(t, "version", app.GetVersion()) - - require.Equal(t, "trace1.appname.version.phase", app.GetSpanKey("phase")) - - task := app.GenerateTask("taskdef", common.PostDeploymentCheckType) - require.Equal(t, KeptnTaskSpec{ - AppVersion: app.GetVersion(), - AppName: app.GetParentName(), - TaskDefinition: "taskdef", - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: common.PostDeploymentCheckType, - }, task.Spec) - - evaluation := app.GenerateEvaluation("taskdef", common.PostDeploymentCheckType) - require.Equal(t, KeptnEvaluationSpec{ - AppVersion: app.GetVersion(), - AppName: app.GetParentName(), - EvaluationDefinition: "taskdef", - Type: common.PostDeploymentCheckType, - RetryInterval: metav1.Duration{ - Duration: 5 * time.Second, - }, - }, evaluation.Spec) - - require.Equal(t, "phase", app.GetSpanName("phase")) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetSpanAttributes()) -} - -func TestKeptnAppVersion_GetWorkloadNameOfApp(t *testing.T) { - type fields struct { - Spec KeptnAppVersionSpec - } - type args struct { - workloadName string - } - tests := []struct { - name string - fields fields - args args - want string - }{ - { - name: "", - fields: fields{ - Spec: KeptnAppVersionSpec{AppName: "my-app"}, - }, - args: args{ - workloadName: "my-workload", - }, - want: "my-app-my-workload", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - v := KeptnAppVersion{ - Spec: tt.fields.Spec, - } - if got := v.GetWorkloadNameOfApp(tt.args.workloadName); got != tt.want { - t.Errorf("GetWorkloadNameOfApp() = %v, want %v", got, tt.want) - } - }) - } -} - -//nolint:dupl -func TestKeptnAppVersion_CancelRemainingPhases(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - } - - tests := []struct { - app KeptnAppVersion - phase common.KeptnPhaseType - want KeptnAppVersion - }{ - { - app: app, - phase: common.PhaseAppPostEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPreEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateCancelled, - PostDeploymentEvaluationStatus: common.StateCancelled, - WorkloadOverallStatus: common.StateCancelled, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseWorkloadDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateCancelled, - PostDeploymentEvaluationStatus: common.StateCancelled, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - } - - for _, tt := range tests { - t.Run("", func(t *testing.T) { - tt.app.CancelRemainingPhases(tt.phase) - require.Equal(t, tt.want, tt.app) - }) - } -} - -func TestKeptnAppVersion_SetPhaseTraceID(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{}, - } - - app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, - }, - }, app) - - app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{ - "name2": "trace2", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name2": "trace2", - }, - }, - }, - }, app) -} - -func TestKeptnAppVersionList(t *testing.T) { - list := KeptnAppVersionList{ - Items: []KeptnAppVersion{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_types.go index f64b75ea8b..7618a388d5 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnappversion_types.go @@ -17,20 +17,11 @@ limitations under the License. package v1alpha1 import ( - "fmt" - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppVersionSpec defines the desired state of KeptnAppVersion type KeptnAppVersionSpec struct { KeptnAppSpec `json:",inline"` @@ -136,272 +127,3 @@ func (a KeptnAppVersionList) GetItems() []client.Object { func init() { SchemeBuilder.Register(&KeptnAppVersion{}, &KeptnAppVersionList{}) } - -func (a KeptnAppVersion) IsPreDeploymentCompleted() bool { - return a.Status.PreDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationCompleted() bool { - return a.Status.PreDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentSucceeded() bool { - return a.Status.PreDeploymentStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPreDeploymentFailed() bool { - return a.Status.PreDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationSucceeded() bool { - return a.Status.PreDeploymentEvaluationStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationFailed() bool { - return a.Status.PreDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentCompleted() bool { - return a.Status.PostDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationCompleted() bool { - return a.Status.PostDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentFailed() bool { - return a.Status.PostDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationSucceeded() bool { - return a.Status.PostDeploymentEvaluationStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationFailed() bool { - return a.Status.PostDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentSucceeded() bool { - return a.Status.PostDeploymentStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsCompleted() bool { - return a.Status.WorkloadOverallStatus.IsCompleted() -} - -func (a KeptnAppVersion) AreWorkloadsSucceeded() bool { - return a.Status.WorkloadOverallStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsFailed() bool { - return a.Status.WorkloadOverallStatus.IsFailed() -} - -func (a *KeptnAppVersion) SetStartTime() { - if a.Status.StartTime.IsZero() { - a.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a *KeptnAppVersion) SetEndTime() { - if a.Status.EndTime.IsZero() { - a.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a KeptnAppVersion) GetStartTime() time.Time { - return a.Status.StartTime.Time -} - -func (a KeptnAppVersion) GetEndTime() time.Time { - return a.Status.EndTime.Time -} - -func (a *KeptnAppVersion) IsStartTimeSet() bool { - return !a.Status.StartTime.IsZero() -} - -func (a *KeptnAppVersion) IsEndTimeSet() bool { - return !a.Status.EndTime.IsZero() -} - -func (a *KeptnAppVersion) Complete() { - a.SetEndTime() -} - -func (a KeptnAppVersion) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - common.AppStatus.String(string(a.Status.Status)), - } -} - -func (a KeptnAppVersion) GetDurationMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppPreviousVersion.String(a.Spec.PreviousVersion), - } -} - -func (a KeptnAppVersion) GetState() common.KeptnState { - return a.Status.Status -} - -func (a KeptnAppVersion) GetPreDeploymentTasks() []string { - return a.Spec.PreDeploymentTasks -} - -func (a KeptnAppVersion) GetPostDeploymentTasks() []string { - return a.Spec.PostDeploymentTasks -} - -func (a KeptnAppVersion) GetPreDeploymentTaskStatus() []TaskStatus { - return a.Status.PreDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentTaskStatus() []TaskStatus { - return a.Status.PostDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluations() []string { - return a.Spec.PreDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluations() []string { - return a.Spec.PostDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluationTaskStatus() []EvaluationStatus { - return a.Status.PreDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluationTaskStatus() []EvaluationStatus { - return a.Status.PostDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetAppName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetPreviousVersion() string { - return a.Spec.PreviousVersion -} - -func (a KeptnAppVersion) GetParentName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetNamespace() string { - return a.Namespace -} - -func (a *KeptnAppVersion) SetState(state common.KeptnState) { - a.Status.Status = state -} - -func (a KeptnAppVersion) GetCurrentPhase() string { - return a.Status.CurrentPhase -} - -func (a *KeptnAppVersion) SetCurrentPhase(phase string) { - a.Status.CurrentPhase = phase -} - -func (a KeptnAppVersion) GetVersion() string { - return a.Spec.Version -} - -func (a KeptnAppVersion) GenerateTask(taskDefinition string, checkType common.CheckType) KeptnTask { - return KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition), - Namespace: a.Namespace, - }, - Spec: KeptnTaskSpec{ - AppVersion: a.GetVersion(), - AppName: a.GetParentName(), - TaskDefinition: taskDefinition, - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: checkType, - }, - } -} - -func (a KeptnAppVersion) GenerateEvaluation(evaluationDefinition string, checkType common.CheckType) KeptnEvaluation { - return KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition), - Namespace: a.Namespace, - }, - Spec: KeptnEvaluationSpec{ - AppVersion: a.Spec.Version, - AppName: a.Spec.AppName, - EvaluationDefinition: evaluationDefinition, - Type: checkType, - RetryInterval: metav1.Duration{ - Duration: 5 * time.Second, - }, - }, - } -} - -func (a KeptnAppVersion) GetSpanName(phase string) string { - if phase == "" { - return a.Name - } - return phase -} - -func (a KeptnAppVersion) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnAppVersion) GetSpanKey(phase string) string { - return fmt.Sprintf("%s.%s.%s.%s", a.Spec.TraceId["traceparent"], a.Spec.AppName, a.Spec.Version, phase) -} - -func (v KeptnAppVersion) GetWorkloadNameOfApp(workloadName string) string { - return fmt.Sprintf("%s-%s", v.Spec.AppName, workloadName) -} - -func (a *KeptnAppVersion) CancelRemainingPhases(phase common.KeptnPhaseType) { - // no need to cancel anything when post-eval tasks fail - if phase == common.PhaseAppPostEvaluation { - return - } - // cancel workload deployment and post-deployment tasks if app pre-eval failed - if phase == common.PhaseAppPreEvaluation { - a.Status.WorkloadOverallStatus = common.StateCancelled - } - // cancel post-deployment tasks if workload deployment failed - a.Status.PostDeploymentStatus = common.StateCancelled - a.Status.PostDeploymentEvaluationStatus = common.StateCancelled - a.Status.Status = common.StateFailed -} - -func (a *KeptnAppVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - if a.Status.PhaseTraceIDs == nil { - a.Status.PhaseTraceIDs = common.PhaseTraceID{} - } - a.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluation_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluation_test.go deleted file mode 100644 index 6fa539a847..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluation_test.go +++ /dev/null @@ -1,107 +0,0 @@ -package v1alpha1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnEvaluation(t *testing.T) { - evaluation := &KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - } - - evaluation.SetPhaseTraceID("", nil) - require.Equal(t, KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - }, *evaluation) - - require.Equal(t, "evaluation", evaluation.GetSpanKey("")) - require.Equal(t, "evaluation", evaluation.GetSpanName("")) - - require.False(t, evaluation.IsEndTimeSet()) - require.False(t, evaluation.IsStartTimeSet()) - - evaluation.SetStartTime() - evaluation.SetEndTime() - - require.True(t, evaluation.IsEndTimeSet()) - require.True(t, evaluation.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - common.EvaluationStatus.String(string(common.StateFailed)), - }, evaluation.GetMetricsAttributes()) - - evaluation.AddEvaluationStatus(Objective{Name: "objName"}) - require.Equal(t, EvaluationStatusItem{ - Status: common.StatePending, - }, evaluation.Status.EvaluationStatus["objName"]) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetSpanAttributes()) - -} - -func TestKeptnEvaluationList(t *testing.T) { - list := KeptnEvaluationList{ - Items: []KeptnEvaluation{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluation_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluation_types.go index a71e37c6fa..87ee07cefc 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluation_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluation_types.go @@ -17,19 +17,10 @@ limitations under the License. package v1alpha1 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationSpec defines the desired state of KeptnEvaluation type KeptnEvaluationSpec struct { // +optional @@ -111,93 +102,3 @@ type KeptnEvaluationList struct { func init() { SchemeBuilder.Register(&KeptnEvaluation{}, &KeptnEvaluationList{}) } - -func (e KeptnEvaluationList) GetItems() []client.Object { - var b []client.Object - for _, i := range e.Items { - b = append(b, &i) - } - return b -} - -func (e *KeptnEvaluation) SetStartTime() { - if e.Status.StartTime.IsZero() { - e.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) SetEndTime() { - if e.Status.EndTime.IsZero() { - e.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) IsStartTimeSet() bool { - return !e.Status.StartTime.IsZero() -} - -func (e *KeptnEvaluation) IsEndTimeSet() bool { - return !e.Status.EndTime.IsZero() -} - -func (e KeptnEvaluation) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e KeptnEvaluation) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - common.EvaluationStatus.String(string(e.Status.OverallStatus)), - } -} - -func (e *KeptnEvaluation) AddEvaluationStatus(objective Objective) { - - evaluationStatusItem := EvaluationStatusItem{ - Status: common.StatePending, - } - if e.Status.EvaluationStatus == nil { - e.Status.EvaluationStatus = make(map[string]EvaluationStatusItem) - } - e.Status.EvaluationStatus[objective.Name] = evaluationStatusItem - -} - -func (e KeptnEvaluation) SetSpanAttributes(span trace.Span) { - span.SetAttributes(e.GetSpanAttributes()...) -} - -func (e KeptnEvaluation) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e *KeptnEvaluation) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (e KeptnEvaluation) GetSpanKey(phase string) string { - return e.Name -} - -func (e KeptnEvaluation) GetSpanName(phase string) string { - return e.Name -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationdefinition_types.go index 74f3a0c380..a31bad9e75 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationdefinition_types.go @@ -20,9 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationDefinitionSpec defines the desired state of KeptnEvaluationDefinition type KeptnEvaluationDefinitionSpec struct { Source string `json:"source"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_types.go index 8048cde5a9..1a8d7db6aa 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_types.go @@ -20,9 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationProviderSpec defines the desired state of KeptnEvaluationProvider type KeptnEvaluationProviderSpec struct { TargetServer string `json:"targetServer"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptntask_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptntask_test.go deleted file mode 100644 index 9cdae0f619..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptntask_test.go +++ /dev/null @@ -1,118 +0,0 @@ -package v1alpha1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnTask(t *testing.T) { - task := &KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - }, - Spec: KeptnTaskSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - } - - task.SetPhaseTraceID("", nil) - require.Equal(t, KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - }, - Spec: KeptnTaskSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - }, *task) - - require.Equal(t, "task", task.GetSpanKey("")) - require.Equal(t, "task", task.GetSpanName("")) - - require.False(t, task.IsEndTimeSet()) - require.False(t, task.IsStartTimeSet()) - - task.SetStartTime() - task.SetEndTime() - - require.True(t, task.IsEndTimeSet()) - require.True(t, task.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - common.TaskStatus.String(string(common.StateFailed)), - }, task.GetMetricsAttributes()) - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/task-name": "task", - "keptn.sh/version": "appversion", - }, task.CreateKeptnLabels()) - - task.Spec.Workload = "workload" - task.Spec.WorkloadVersion = "workloadversion" - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/workload": "workload", - "keptn.sh/task-name": "task", - "keptn.sh/version": "workloadversion", - }, task.CreateKeptnLabels()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("workloadversion"), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetSpanAttributes()) - -} - -func TestKeptnTaskList(t *testing.T) { - list := KeptnTaskList{ - Items: []KeptnTask{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptntask_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptntask_types.go index a57e30eea6..971a118048 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptntask_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptntask_types.go @@ -17,19 +17,10 @@ limitations under the License. package v1alpha1 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnTaskSpec defines the desired state of KeptnTask type KeptnTaskSpec struct { Workload string `json:"workload"` @@ -114,97 +105,3 @@ type KeptnTaskList struct { func init() { SchemeBuilder.Register(&KeptnTask{}, &KeptnTaskList{}) } - -func (t KeptnTaskList) GetItems() []client.Object { - var b []client.Object - for _, i := range t.Items { - b = append(b, &i) - } - return b -} - -func (t *KeptnTask) SetStartTime() { - if t.Status.StartTime.IsZero() { - t.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) SetEndTime() { - if t.Status.EndTime.IsZero() { - t.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) IsStartTimeSet() bool { - return !t.Status.StartTime.IsZero() -} - -func (t *KeptnTask) IsEndTimeSet() bool { - return !t.Status.EndTime.IsZero() -} - -func (t KeptnTask) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.AppName), - common.AppVersion.String(t.Spec.AppVersion), - common.WorkloadName.String(t.Spec.Workload), - common.WorkloadVersion.String(t.Spec.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t KeptnTask) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.AppName), - common.AppVersion.String(t.Spec.AppVersion), - common.WorkloadName.String(t.Spec.Workload), - common.WorkloadVersion.String(t.Spec.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - common.TaskStatus.String(string(t.Status.Status)), - } -} - -func (t KeptnTask) SetSpanAttributes(span trace.Span) { - span.SetAttributes(t.GetSpanAttributes()...) -} - -func (t KeptnTask) CreateKeptnLabels() map[string]string { - if t.Spec.Workload != "" { - return map[string]string{ - common.AppAnnotation: t.Spec.AppName, - common.WorkloadAnnotation: t.Spec.Workload, - common.VersionAnnotation: t.Spec.WorkloadVersion, - common.TaskNameAnnotation: t.Name, - } - } - return map[string]string{ - common.AppAnnotation: t.Spec.AppName, - common.VersionAnnotation: t.Spec.AppVersion, - common.TaskNameAnnotation: t.Name, - } -} - -func (t KeptnTask) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.AppName), - common.AppVersion.String(t.Spec.AppVersion), - common.WorkloadName.String(t.Spec.Workload), - common.WorkloadVersion.String(t.Spec.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t *KeptnTask) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (t KeptnTask) GetSpanKey(phase string) string { - return t.Name -} - -func (t KeptnTask) GetSpanName(phase string) string { - return t.Name -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptntaskdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptntaskdefinition_types.go index 6b7295c711..767f34330a 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptntaskdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptntaskdefinition_types.go @@ -20,9 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnTaskDefinitionSpec defines the desired state of KeptnTaskDefinition type KeptnTaskDefinitionSpec struct { // +optional diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkload_test.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkload_test.go deleted file mode 100644 index aeb8cd0025..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkload_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package v1alpha1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnWorkload(t *testing.T) { - workload := &KeptnWorkload{ - ObjectMeta: metav1.ObjectMeta{ - Name: "workload", - Namespace: "namespace", - }, - Spec: KeptnWorkloadSpec{ - Version: "version", - AppName: "app", - }, - } - - workloadInstanceName := workload.GetWorkloadInstanceName() - require.Equal(t, "workload-version", workloadInstanceName) - - workloadInstance := workload.GenerateWorkloadInstance("prev", map[string]string{}) - require.Equal(t, KeptnWorkloadInstance{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{}, - Name: "workload-version", - Namespace: "namespace", - }, - Spec: KeptnWorkloadInstanceSpec{ - KeptnWorkloadSpec: KeptnWorkloadSpec{ - Version: "version", - AppName: "app", - }, - WorkloadName: "workload", - PreviousVersion: "prev", - }, - }, workloadInstance) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("version"), - }, workload.GetSpanAttributes()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkload_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkload_types.go index 054a7ab6e6..fe1025abee 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkload_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkload_types.go @@ -17,18 +17,10 @@ limitations under the License. package v1alpha1 import ( - "strings" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnWorkloadSpec defines the desired state of KeptnWorkload type KeptnWorkloadSpec struct { AppName string `json:"app"` @@ -87,34 +79,3 @@ type ResourceReference struct { func init() { SchemeBuilder.Register(&KeptnWorkload{}, &KeptnWorkloadList{}) } - -func (w KeptnWorkload) GetWorkloadInstanceName() string { - return strings.ToLower(w.Name + "-" + w.Spec.Version) -} - -func (w KeptnWorkload) SetSpanAttributes(span trace.Span) { - span.SetAttributes(w.GetSpanAttributes()...) -} - -func (w KeptnWorkload) GenerateWorkloadInstance(previousVersion string, traceContextCarrier map[string]string) KeptnWorkloadInstance { - return KeptnWorkloadInstance{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: traceContextCarrier, - Name: w.GetWorkloadInstanceName(), - Namespace: w.Namespace, - }, - Spec: KeptnWorkloadInstanceSpec{ - KeptnWorkloadSpec: w.Spec, - WorkloadName: w.Name, - PreviousVersion: previousVersion, - }, - } -} - -func (i KeptnWorkload) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(i.Spec.AppName), - common.WorkloadName.String(i.Name), - common.WorkloadVersion.String(i.Spec.Version), - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_types.go b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_types.go index 45d3d18307..5a147ab2a9 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_types.go @@ -21,9 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnWorkloadInstanceSpec defines the desired state of KeptnWorkloadInstance type KeptnWorkloadInstanceSpec struct { KeptnWorkloadSpec `json:",inline"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha1/semconv/semconv.go b/lifecycle-operator/apis/lifecycle/v1alpha1/semconv/semconv.go deleted file mode 100644 index 90f68f6122..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha1/semconv/semconv.go +++ /dev/null @@ -1,12 +0,0 @@ -package semconv - -import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha1/common" - "go.opentelemetry.io/otel/trace" -) - -func AddAttributeFromAnnotations(s trace.Span, annotations map[string]string) { - s.SetAttributes(common.AppName.String(annotations[common.AppAnnotation])) - s.SetAttributes(common.WorkloadName.String(annotations[common.WorkloadAnnotation])) - s.SetAttributes(common.WorkloadVersion.String(annotations[common.VersionAnnotation])) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/common/common.go b/lifecycle-operator/apis/lifecycle/v1alpha2/common/common.go index 57c49d7889..1a462b331c 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/common/common.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/common/common.go @@ -5,171 +5,17 @@ package common import ( "errors" - "fmt" - "math/rand" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/propagation" ) -const WorkloadAnnotation = "keptn.sh/workload" -const VersionAnnotation = "keptn.sh/version" -const AppAnnotation = "keptn.sh/app" -const PreDeploymentTaskAnnotation = "keptn.sh/pre-deployment-tasks" -const PostDeploymentTaskAnnotation = "keptn.sh/post-deployment-tasks" -const K8sRecommendedWorkloadAnnotations = "app.kubernetes.io/name" -const K8sRecommendedVersionAnnotations = "app.kubernetes.io/version" -const K8sRecommendedAppAnnotations = "app.kubernetes.io/part-of" -const PreDeploymentEvaluationAnnotation = "keptn.sh/pre-deployment-evaluations" -const PostDeploymentEvaluationAnnotation = "keptn.sh/post-deployment-evaluations" -const TaskNameAnnotation = "keptn.sh/task-name" -const NamespaceEnabledAnnotation = "keptn.sh/lifecycle-toolkit" -const CreateAppTaskSpanName = "create_%s_app_task" -const CreateWorkloadTaskSpanName = "create_%s_deployment_task" -const CreateAppEvalSpanName = "create_%s_app_evaluation" -const CreateWorkloadEvalSpanName = "create_%s_deployment_evaluation" - -const MaxAppNameLength = 25 -const MaxWorkloadNameLength = 25 -const MaxTaskNameLength = 25 -const MaxVersionLength = 12 - var ErrCannotCastKeptnAppVersion = errors.New("cannot cast KeptnAppVersion to v1") var ErrCannotCastKeptnApp = errors.New("cannot cast KeptnApp to v1") // KeptnState is a string containing current Phase state (Progressing/Succeeded/Failed/Unknown/Pending/Cancelled) type KeptnState string -const ( - StateProgressing KeptnState = "Progressing" - StateSucceeded KeptnState = "Succeeded" - StateFailed KeptnState = "Failed" - StateUnknown KeptnState = "Unknown" - StatePending KeptnState = "Pending" - StateDeprecated KeptnState = "Deprecated" - // StateCancelled represents state that was cancelled due to a previous step having failed. - // Deprecated: Use StateDeprecated instead. Should only be used in checks for backwards compatibility reasons - StateCancelled KeptnState = "Cancelled" -) - -func (k KeptnState) IsCompleted() bool { - return k == StateSucceeded || k == StateFailed || k == StateDeprecated || k == StateCancelled -} - -func (k KeptnState) IsSucceeded() bool { - return k == StateSucceeded -} - -func (k KeptnState) IsFailed() bool { - return k == StateFailed -} - -func (k KeptnState) IsDeprecated() bool { - return k == StateDeprecated || k == StateCancelled -} - -func (k KeptnState) IsPending() bool { - return k == StatePending -} - -type StatusSummary struct { - Total int - Progressing int - Failed int - Succeeded int - Pending int - Unknown int - Deprecated int -} - -func UpdateStatusSummary(status KeptnState, summary StatusSummary) StatusSummary { - switch status { - case StateFailed: - summary.Failed++ - case StateDeprecated: - summary.Deprecated++ - case StateSucceeded: - summary.Succeeded++ - case StateProgressing: - summary.Progressing++ - case StatePending, "": - summary.Pending++ - case StateUnknown: - summary.Unknown++ - } - return summary -} - -func (s StatusSummary) GetTotalCount() int { - return s.Failed + s.Succeeded + s.Progressing + s.Pending + s.Unknown + s.Deprecated -} - -func GetOverallState(s StatusSummary) KeptnState { - if s.Failed > 0 || s.Deprecated > 0 { - return StateFailed - } - if s.Progressing > 0 { - return StateProgressing - } - if s.Pending > 0 { - return StatePending - } - if s.Unknown > 0 || s.GetTotalCount() != s.Total { - return StateUnknown - } - return StateSucceeded -} - -func TruncateString(s string, max int) string { - if len(s) > max { - return s[:max] - } - return s -} - type CheckType string -const PreDeploymentCheckType CheckType = "pre" -const PostDeploymentCheckType CheckType = "post" -const PreDeploymentEvaluationCheckType CheckType = "pre-eval" -const PostDeploymentEvaluationCheckType CheckType = "post-eval" - -type KeptnMeters struct { - TaskCount metric.Int64Counter - TaskDuration metric.Float64Histogram - DeploymentCount metric.Int64Counter - DeploymentDuration metric.Float64Histogram - AppCount metric.Int64Counter - AppDuration metric.Float64Histogram - EvaluationCount metric.Int64Counter - EvaluationDuration metric.Float64Histogram -} - -const ( - AppName attribute.Key = attribute.Key("keptn.deployment.app.name") - AppVersion attribute.Key = attribute.Key("keptn.deployment.app.version") - AppNamespace attribute.Key = attribute.Key("keptn.deployment.app.namespace") - AppStatus attribute.Key = attribute.Key("keptn.deployment.app.status") - AppPreviousVersion attribute.Key = attribute.Key("keptn.deployment.app.previousversion") - WorkloadName attribute.Key = attribute.Key("keptn.deployment.workload.name") - WorkloadVersion attribute.Key = attribute.Key("keptn.deployment.workload.version") - WorkloadPreviousVersion attribute.Key = attribute.Key("keptn.deployment.workload.previousversion") - WorkloadNamespace attribute.Key = attribute.Key("keptn.deployment.workload.namespace") - WorkloadStatus attribute.Key = attribute.Key("keptn.deployment.workload.status") - TaskStatus attribute.Key = attribute.Key("keptn.deployment.task.status") - TaskName attribute.Key = attribute.Key("keptn.deployment.task.name") - TaskType attribute.Key = attribute.Key("keptn.deployment.task.type") - EvaluationStatus attribute.Key = attribute.Key("keptn.deployment.evaluation.status") - EvaluationName attribute.Key = attribute.Key("keptn.deployment.evaluation.name") - EvaluationType attribute.Key = attribute.Key("keptn.deployment.evaluation.type") -) - -func GenerateTaskName(checkType CheckType, taskName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return fmt.Sprintf("%s-%s-%d", checkType, TruncateString(taskName, 32), randomId) -} - -func GenerateEvaluationName(checkType CheckType, evalName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return fmt.Sprintf("%s-%s-%d", checkType, TruncateString(evalName, 27), randomId) -} +// PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases +type PhaseTraceID map[string]propagation.MapCarrier diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/common/common_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/common/common_test.go deleted file mode 100644 index 707a7b2ad0..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/common/common_test.go +++ /dev/null @@ -1,312 +0,0 @@ -package common - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestKeptnState_IsCompleted(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - { - State: StateSucceeded, - Want: true, - }, - { - State: StateDeprecated, - Want: true, - }, - { - State: StateCancelled, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.Want, tt.State.IsCompleted()) - }) - } -} - -func TestKeptnState_IsSucceeded(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateSucceeded, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsSucceeded(), tt.Want) - }) - } -} - -func TestKeptnState_IsFailed(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsFailed(), tt.Want) - }) - } -} - -func TestKeptnState_IsDeprecated(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateDeprecated, - Want: true, - }, - { - State: StateCancelled, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsDeprecated(), tt.Want) - }) - } -} - -func TestKeptnKeptnState_IsPending(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StatePending, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPending(), tt.Want) - }) - } -} - -func Test_UpdateStatusSummary(t *testing.T) { - emmptySummary := StatusSummary{0, 0, 0, 0, 0, 0, 0} - tests := []struct { - State KeptnState - Want StatusSummary - }{ - { - State: StateProgressing, - Want: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - }, - { - State: StateFailed, - Want: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - }, - { - State: StateSucceeded, - Want: StatusSummary{0, 0, 0, 1, 0, 0, 0}, - }, - { - State: StatePending, - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: "", - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: StateUnknown, - Want: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - }, - { - State: StateDeprecated, - Want: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, UpdateStatusSummary(tt.State, emmptySummary), tt.Want) - }) - } -} - -func Test_GetTotalCount(t *testing.T) { - summary := StatusSummary{2, 0, 2, 1, 0, 3, 5} - require.Equal(t, summary.GetTotalCount(), 11) -} - -func Test_GeOverallState(t *testing.T) { - tests := []struct { - Name string - Summary StatusSummary - Want KeptnState - }{ - { - Name: "failed", - Summary: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - Want: StateFailed, - }, - { - Name: "deprecated", - Summary: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - Want: StateFailed, - }, - { - Name: "progressing", - Summary: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - Want: StateProgressing, - }, - { - Name: "pending", - Summary: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - Want: StatePending, - }, - { - Name: "unknown", - Summary: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "unknown totalcount", - Summary: StatusSummary{5, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "succeeded", - Summary: StatusSummary{1, 0, 0, 1, 0, 0, 0}, - Want: StateSucceeded, - }, - } - for _, tt := range tests { - t.Run(tt.Name, func(t *testing.T) { - require.Equal(t, GetOverallState(tt.Summary), tt.Want) - }) - } -} - -func Test_TruncateString(t *testing.T) { - tests := []struct { - Input string - Max int - Want string - }{ - { - Input: "some_string", - Max: 20, - Want: "some_string", - }, - { - Input: "some_string", - Max: 5, - Want: "some_", - }, - { - Input: "", - Max: 5, - Want: "", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, TruncateString(tt.Input, tt.Max), tt.Want) - }) - } -} - -func Test_GenerateTaskName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentCheckType, - Name: "short-name", - Want: "pre-short-name-", - }, - { - Check: PreDeploymentCheckType, - Name: "", - Want: "pre--", - }, - { - Check: PreDeploymentCheckType, - Name: "loooooooooooooooooooooooooooooooooooooong_name", - Want: "pre-looooooooooooooooooooooooooooooo-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateTaskName(tt.Check, tt.Name), tt.Want)) - }) - } -} - -func Test_GenerateEvaluationName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentEvaluationCheckType, - Name: "short-name", - Want: "pre-eval-short-name-", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: "", - Want: "pre-eval--", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: "loooooooooooooooooooooooooooooooooooooong_name", - Want: "pre-eval-loooooooooooooooooooooooooo-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateEvaluationName(tt.Check, tt.Name), tt.Want)) - }) - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/common/phases.go b/lifecycle-operator/apis/lifecycle/v1alpha2/common/phases.go deleted file mode 100644 index 9847322f3c..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/common/phases.go +++ /dev/null @@ -1,111 +0,0 @@ -package common - -import ( - "strings" - - "go.opentelemetry.io/otel/propagation" -) - -type KeptnPhase KeptnPhaseType - -type KeptnPhaseType struct { - LongName string - ShortName string -} - -var phases = []KeptnPhaseType{ - PhaseWorkloadPreDeployment, - PhaseWorkloadPostDeployment, - PhaseWorkloadPreEvaluation, - PhaseWorkloadPostEvaluation, - PhaseWorkloadDeployment, - PhaseAppPreDeployment, - PhaseAppPostDeployment, - PhaseAppPreEvaluation, - PhaseAppPostEvaluation, - PhaseAppDeployment, - PhaseReconcileEvaluation, - PhaseReconcileTask, - PhaseCreateEvaluation, - PhaseCreateTask, - PhaseCreateApp, - PhaseCreateWorkload, - PhaseCreateWorkloadInstance, - PhaseCreateAppVersion, - PhaseCompleted, - PhaseDeprecated, -} - -func (p KeptnPhaseType) IsEvaluation() bool { - return strings.Contains(p.ShortName, "DeployEvaluations") -} - -func (p KeptnPhaseType) IsPreEvaluation() bool { - return strings.Contains(p.ShortName, "PreDeployEvaluations") -} - -func (p KeptnPhaseType) IsPostEvaluation() bool { - return strings.Contains(p.ShortName, "PostDeployEvaluations") -} - -func (p KeptnPhaseType) IsTask() bool { - return strings.Contains(p.ShortName, "DeployTasks") -} - -func (p KeptnPhaseType) IsPreTask() bool { - return strings.Contains(p.ShortName, "PreDeployTasks") -} - -func (p KeptnPhaseType) IsPostTask() bool { - return strings.Contains(p.ShortName, "PostDeployTasks") -} - -func GetShortPhaseName(phase string) string { - for _, p := range phases { - if phase == p.ShortName { - return p.ShortName - } - } - - for _, p := range phases { - if phase == p.LongName { - return p.ShortName - } - } - - return "" -} - -var ( - PhaseWorkloadPreDeployment = KeptnPhaseType{LongName: "Workload Pre-Deployment Tasks", ShortName: "WorkloadPreDeployTasks"} - PhaseWorkloadPostDeployment = KeptnPhaseType{LongName: "Workload Post-Deployment Tasks", ShortName: "WorkloadPostDeployTasks"} - PhaseWorkloadPreEvaluation = KeptnPhaseType{LongName: "Workload Pre-Deployment Evaluations", ShortName: "WorkloadPreDeployEvaluations"} - PhaseWorkloadPostEvaluation = KeptnPhaseType{LongName: "Workload Post-Deployment Evaluations", ShortName: "WorkloadPostDeployEvaluations"} - PhaseWorkloadDeployment = KeptnPhaseType{LongName: "Workload Deployment", ShortName: "WorkloadDeploy"} - PhaseAppPreDeployment = KeptnPhaseType{LongName: "App Pre-Deployment Tasks", ShortName: "AppPreDeployTasks"} - PhaseAppPostDeployment = KeptnPhaseType{LongName: "App Post-Deployment Tasks", ShortName: "AppPostDeployTasks"} - PhaseAppPreEvaluation = KeptnPhaseType{LongName: "App Pre-Deployment Evaluations", ShortName: "AppPreDeployEvaluations"} - PhaseAppPostEvaluation = KeptnPhaseType{LongName: "App Post-Deployment Evaluations", ShortName: "AppPostDeployEvaluations"} - PhaseAppDeployment = KeptnPhaseType{LongName: "App Deployment", ShortName: "AppDeploy"} - PhaseReconcileEvaluation = KeptnPhaseType{LongName: "Reconcile Evaluation", ShortName: "ReconcileEvaluation"} - PhaseReconcileTask = KeptnPhaseType{LongName: "Reconcile Task", ShortName: "ReconcileTask"} - PhaseCreateEvaluation = KeptnPhaseType{LongName: "Create Evaluation", ShortName: "CreateEvaluation"} - PhaseCreateTask = KeptnPhaseType{LongName: "Create Task", ShortName: "CreateTask"} - PhaseCreateApp = KeptnPhaseType{LongName: "Create App", ShortName: "CreateApp"} - PhaseCreateWorkload = KeptnPhaseType{LongName: "Create Workload", ShortName: "CreateWorkload"} - PhaseCreateWorkloadInstance = KeptnPhaseType{LongName: "Create WorkloadInstance", ShortName: "CreateWorkloadInstance"} - PhaseCreateAppVersion = KeptnPhaseType{LongName: "Create AppVersion", ShortName: "CreateAppVersion"} - PhaseCompleted = KeptnPhaseType{LongName: "Completed", ShortName: "Completed"} - PhaseDeprecated = KeptnPhaseType{LongName: "Deprecated", ShortName: "Deprecated"} -) - -type PhaseTraceID map[string]propagation.MapCarrier - -func (pid PhaseTraceID) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - pid[GetShortPhaseName(phase)] = carrier - -} - -func (pid PhaseTraceID) GetPhaseTraceID(phase string) propagation.MapCarrier { - return pid[GetShortPhaseName(phase)] -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/common/phases_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/common/phases_test.go deleted file mode 100644 index 271732c1f1..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/common/phases_test.go +++ /dev/null @@ -1,275 +0,0 @@ -package common - -import ( - "testing" - - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/propagation" -) - -func TestKeptnPhaseType_IsEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: false, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: false, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: false, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: false, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostTask(), tt.Want) - }) - } -} - -func TestPhaseTraceID(t *testing.T) { - trace := PhaseTraceID{} - - trace.SetPhaseTraceID(PhaseAppDeployment.LongName, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - }, trace) - - trace.SetPhaseTraceID(PhaseWorkloadDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, trace) - - require.Equal(t, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, trace.GetPhaseTraceID(PhaseAppDeployment.LongName)) - - require.Equal(t, propagation.MapCarrier{ - "name3": "trace3", - }, trace.GetPhaseTraceID(PhaseWorkloadDeployment.ShortName)) -} - -func TestGetShortPhaseName(t *testing.T) { - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("WorkloadPreDeployTasks")) - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("Workload Pre-Deployment Tasks")) - require.Equal(t, "", GetShortPhaseName("Workload Pre-Deploycdddment Tasks")) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnapp_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnapp_test.go deleted file mode 100644 index 5c12c649c2..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnapp_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package v1alpha2 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnApp(t *testing.T) { - app := &KeptnApp{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - Generation: 1, - }, - Spec: KeptnAppSpec{ - Version: "version", - }, - } - - appVersionName := app.GetAppVersionName() - require.Equal(t, "app-version-1", appVersionName) - - appVersion := app.GenerateAppVersion("prev", map[string]string{}) - require.Equal(t, KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{}, - Name: "app-version-1", - Namespace: "namespace", - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - Version: "version", - }, - AppName: "app", - PreviousVersion: "prev", - }, - }, appVersion) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("version"), - }, app.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "version", - "appRevision": "1", - }, app.GetEventAnnotations()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnapp_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnapp_types.go index 374740f7a3..7ff9c10ed6 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnapp_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnapp_types.go @@ -17,19 +17,9 @@ limitations under the License. package v1alpha2 import ( - "fmt" - "strconv" - "strings" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppSpec defines the desired state of KeptnApp type KeptnAppSpec struct { Version string `json:"version"` @@ -87,41 +77,3 @@ type KeptnAppList struct { func init() { SchemeBuilder.Register(&KeptnApp{}, &KeptnAppList{}) } - -func (a KeptnApp) GetAppVersionName() string { - return strings.ToLower(fmt.Sprintf("%s-%s-%d", a.Name, a.Spec.Version, a.Generation)) -} - -func (a KeptnApp) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnApp) GenerateAppVersion(previousVersion string, traceContextCarrier map[string]string) KeptnAppVersion { - return KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: traceContextCarrier, - Name: a.GetAppVersionName(), - Namespace: a.Namespace, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: a.Spec, - AppName: a.Name, - PreviousVersion: previousVersion, - }, - } -} - -func (a KeptnApp) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Name), - common.AppVersion.String(a.Spec.Version), - } -} - -func (a KeptnApp) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": a.Name, - "appVersion": a.Spec.Version, - "appRevision": strconv.FormatInt(a.Generation, 10), - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_conversion_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_conversion_test.go index 3dd6d9a330..334d40bcb6 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_conversion_test.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_conversion_test.go @@ -13,6 +13,8 @@ import ( v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" ) +const StateFailed common.KeptnState = "Failed" + func TestKeptnAppVersion_ConvertFrom(t *testing.T) { tests := []struct { name string @@ -205,25 +207,25 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -231,48 +233,48 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { { DefinitionName: "def1", Name: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def12", Name: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []ItemStatus{ { DefinitionName: "def2", Name: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def22", Name: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def3", Name: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def32", Name: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def4", Name: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def42", Name: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -285,7 +287,7 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, }, @@ -366,25 +368,25 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -392,48 +394,48 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { { DefinitionName: "def1", Name: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def12", Name: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []ItemStatus{ { DefinitionName: "def2", Name: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def22", Name: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def3", Name: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def32", Name: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def4", Name: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def42", Name: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -446,7 +448,7 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, wantErr: false, diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_test.go deleted file mode 100644 index 69af203c59..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_test.go +++ /dev/null @@ -1,450 +0,0 @@ -package v1alpha2 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnAppVersion(t *testing.T) { - app := &KeptnAppVersion{ - ObjectMeta: v1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - }, - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, - Status: common.StateFailed, - PreDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, - PostDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, - PreDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, - PostDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, - CurrentPhase: common.PhaseAppDeployment.ShortName, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - PreDeploymentTasks: []string{"task1", "task2"}, - PostDeploymentTasks: []string{"task3", "task4"}, - PreDeploymentEvaluations: []string{"task5", "task6"}, - PostDeploymentEvaluations: []string{"task7", "task8"}, - Version: "version", - }, - PreviousVersion: "prev", - AppName: "appname", - TraceId: map[string]string{"traceparent": "trace1"}, - }, - } - - require.True(t, app.IsPreDeploymentCompleted()) - require.False(t, app.IsPreDeploymentSucceeded()) - require.True(t, app.IsPreDeploymentFailed()) - - require.True(t, app.IsPreDeploymentEvaluationCompleted()) - require.False(t, app.IsPreDeploymentEvaluationSucceeded()) - require.True(t, app.IsPreDeploymentEvaluationFailed()) - - require.True(t, app.IsPostDeploymentCompleted()) - require.False(t, app.IsPostDeploymentSucceeded()) - require.True(t, app.IsPostDeploymentFailed()) - - require.True(t, app.IsPostDeploymentEvaluationCompleted()) - require.False(t, app.IsPostDeploymentEvaluationSucceeded()) - require.True(t, app.IsPostDeploymentEvaluationFailed()) - - require.True(t, app.AreWorkloadsCompleted()) - require.False(t, app.AreWorkloadsSucceeded()) - require.True(t, app.AreWorkloadsFailed()) - - require.False(t, app.IsEndTimeSet()) - require.False(t, app.IsStartTimeSet()) - - app.SetStartTime() - app.SetEndTime() - - require.True(t, app.IsEndTimeSet()) - require.True(t, app.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - common.AppStatus.String(string(common.StateFailed)), - }, app.GetMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppPreviousVersion.String("prev"), - }, app.GetDurationMetricsAttributes()) - - require.Equal(t, common.StateFailed, app.GetState()) - - require.Equal(t, []string{"task1", "task2"}, app.GetPreDeploymentTasks()) - require.Equal(t, []string{"task3", "task4"}, app.GetPostDeploymentTasks()) - require.Equal(t, []string{"task5", "task6"}, app.GetPreDeploymentEvaluations()) - require.Equal(t, []string{"task7", "task8"}, app.GetPostDeploymentEvaluations()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, app.GetPreDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, app.GetPostDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, app.GetPreDeploymentEvaluationTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, app.GetPostDeploymentEvaluationTaskStatus()) - - require.Equal(t, "appname", app.GetAppName()) - require.Equal(t, "prev", app.GetPreviousVersion()) - require.Equal(t, "appname", app.GetParentName()) - require.Equal(t, "namespace", app.GetNamespace()) - - app.SetState(common.StatePending) - require.Equal(t, common.StatePending, app.GetState()) - - require.True(t, !app.GetStartTime().IsZero()) - require.True(t, !app.GetEndTime().IsZero()) - - app.SetCurrentPhase(common.PhaseAppDeployment.LongName) - require.Equal(t, common.PhaseAppDeployment.LongName, app.GetCurrentPhase()) - - app.Status.EndTime = v1.Time{Time: time.Time{}} - app.Complete() - require.True(t, !app.GetEndTime().IsZero()) - - require.Equal(t, "version", app.GetVersion()) - - require.Equal(t, "trace1.appname.version.phase", app.GetSpanKey("phase")) - - task := app.GenerateTask("taskdef", common.PostDeploymentCheckType) - require.Equal(t, KeptnTaskSpec{ - AppVersion: app.GetVersion(), - AppName: app.GetParentName(), - TaskDefinition: "taskdef", - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: common.PostDeploymentCheckType, - }, task.Spec) - - evaluation := app.GenerateEvaluation("taskdef", common.PostDeploymentCheckType) - require.Equal(t, KeptnEvaluationSpec{ - AppVersion: app.GetVersion(), - AppName: app.GetParentName(), - EvaluationDefinition: "taskdef", - Type: common.PostDeploymentCheckType, - RetryInterval: v1.Duration{ - Duration: 5 * time.Second, - }, - }, evaluation.Spec) - - require.Equal(t, "phase", app.GetSpanName("phase")) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "appname", - "appVersion": "version", - "appVersionName": "app", - }, app.GetEventAnnotations()) -} - -func TestKeptnAppVersion_GetWorkloadNameOfApp(t *testing.T) { - type fields struct { - Spec KeptnAppVersionSpec - } - type args struct { - workloadName string - } - tests := []struct { - name string - fields fields - args args - want string - }{ - { - name: "", - fields: fields{ - Spec: KeptnAppVersionSpec{AppName: "my-app"}, - }, - args: args{ - workloadName: "my-workload", - }, - want: "my-app-my-workload", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - v := KeptnAppVersion{ - Spec: tt.fields.Spec, - } - if got := v.GetWorkloadNameOfApp(tt.args.workloadName); got != tt.want { - t.Errorf("GetWorkloadNameOfApp() = %v, want %v", got, tt.want) - } - }) - } -} - -//nolint:dupl -func TestKeptnAppVersion_DeprecateRemainingPhases(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - } - - tests := []struct { - app KeptnAppVersion - phase common.KeptnPhaseType - want KeptnAppVersion - }{ - { - app: app, - phase: common.PhaseAppPostEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPostDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPreEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPreDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseDeprecated, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateDeprecated, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateDeprecated, - }, - }, - }, - { - app: app, - phase: common.PhaseWorkloadPreDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - } - - for _, tt := range tests { - t.Run("", func(t *testing.T) { - tt.app.DeprecateRemainingPhases(tt.phase) - require.Equal(t, tt.want, tt.app) - }) - } -} - -func TestKeptnAppVersion_SetPhaseTraceID(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{}, - } - - app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, - }, - }, app) - - app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{ - "name2": "trace2", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name2": "trace2", - }, - }, - }, - }, app) -} - -func TestKeptnAppVersionList(t *testing.T) { - list := KeptnAppVersionList{ - Items: []KeptnAppVersion{ - { - ObjectMeta: v1.ObjectMeta{ - Name: "obj1", - }, - Status: KeptnAppVersionStatus{ - Status: common.StateSucceeded, - }, - }, - { - ObjectMeta: v1.ObjectMeta{ - Name: "obj2", - }, - Status: KeptnAppVersionStatus{ - Status: common.StateDeprecated, - }, - }, - }, - } - - // fetch the list items - got := list.GetItems() - require.Len(t, got, 2) - - require.Equal(t, "obj1", list.Items[0].GetName()) - require.Equal(t, "obj2", list.Items[1].GetName()) - - // remove deprecated items from the list - list.RemoveDeprecated() - - // check that deprecated items are not present in the list anymore - got = list.GetItems() - require.Len(t, got, 1) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_types.go index 96cc4c1552..4c03891640 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnappversion_types.go @@ -17,20 +17,10 @@ limitations under the License. package v1alpha2 import ( - "fmt" - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppVersionSpec defines the desired state of KeptnAppVersion type KeptnAppVersionSpec struct { KeptnAppSpec `json:",inline"` @@ -125,327 +115,6 @@ type KeptnAppVersionList struct { Items []KeptnAppVersion `json:"items"` } -func (a KeptnAppVersionList) GetItems() []client.Object { - b := make([]client.Object, 0, len(a.Items)) - for i := 0; i < len(a.Items); i++ { - b = append(b, &a.Items[i]) - } - return b -} - -func (a *KeptnAppVersionList) RemoveDeprecated() { - b := make([]KeptnAppVersion, 0, len(a.Items)) - for i := 0; i < len(a.Items); i++ { - if a.Items[i].Status.Status != common.StateDeprecated { - b = append(b, a.Items[i]) - } - } - a.Items = b -} - func init() { SchemeBuilder.Register(&KeptnAppVersion{}, &KeptnAppVersionList{}) } - -func (a KeptnAppVersion) IsPreDeploymentCompleted() bool { - return a.Status.PreDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationCompleted() bool { - return a.Status.PreDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentSucceeded() bool { - return a.Status.PreDeploymentStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPreDeploymentFailed() bool { - return a.Status.PreDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationSucceeded() bool { - return a.Status.PreDeploymentEvaluationStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationFailed() bool { - return a.Status.PreDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentCompleted() bool { - return a.Status.PostDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationCompleted() bool { - return a.Status.PostDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentFailed() bool { - return a.Status.PostDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationSucceeded() bool { - return a.Status.PostDeploymentEvaluationStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationFailed() bool { - return a.Status.PostDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentSucceeded() bool { - return a.Status.PostDeploymentStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsCompleted() bool { - return a.Status.WorkloadOverallStatus.IsCompleted() -} - -func (a KeptnAppVersion) AreWorkloadsSucceeded() bool { - return a.Status.WorkloadOverallStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsFailed() bool { - return a.Status.WorkloadOverallStatus.IsFailed() -} - -func (a *KeptnAppVersion) SetStartTime() { - if a.Status.StartTime.IsZero() { - a.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a *KeptnAppVersion) SetEndTime() { - if a.Status.EndTime.IsZero() { - a.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a KeptnAppVersion) GetStartTime() time.Time { - return a.Status.StartTime.Time -} - -func (a KeptnAppVersion) GetEndTime() time.Time { - return a.Status.EndTime.Time -} - -func (a *KeptnAppVersion) IsStartTimeSet() bool { - return !a.Status.StartTime.IsZero() -} - -func (a *KeptnAppVersion) IsEndTimeSet() bool { - return !a.Status.EndTime.IsZero() -} - -func (a *KeptnAppVersion) Complete() { - a.SetEndTime() -} - -func (a KeptnAppVersion) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - common.AppStatus.String(string(a.Status.Status)), - } -} - -func (a KeptnAppVersion) GetDurationMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppPreviousVersion.String(a.Spec.PreviousVersion), - } -} - -func (a KeptnAppVersion) GetState() common.KeptnState { - return a.Status.Status -} - -func (a KeptnAppVersion) GetPreDeploymentTasks() []string { - return a.Spec.PreDeploymentTasks -} - -func (a KeptnAppVersion) GetPostDeploymentTasks() []string { - return a.Spec.PostDeploymentTasks -} - -func (a KeptnAppVersion) GetPreDeploymentTaskStatus() []ItemStatus { - return a.Status.PreDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentTaskStatus() []ItemStatus { - return a.Status.PostDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluations() []string { - return a.Spec.PreDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluations() []string { - return a.Spec.PostDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluationTaskStatus() []ItemStatus { - return a.Status.PreDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluationTaskStatus() []ItemStatus { - return a.Status.PostDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetAppName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetPreviousVersion() string { - return a.Spec.PreviousVersion -} - -func (a KeptnAppVersion) GetParentName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetNamespace() string { - return a.Namespace -} - -func (a *KeptnAppVersion) SetState(state common.KeptnState) { - a.Status.Status = state -} - -func (a KeptnAppVersion) GetCurrentPhase() string { - return a.Status.CurrentPhase -} - -func (a *KeptnAppVersion) SetCurrentPhase(phase string) { - a.Status.CurrentPhase = phase -} - -func (a KeptnAppVersion) GetVersion() string { - return a.Spec.Version -} - -func (a KeptnAppVersion) GenerateTask(taskDefinition string, checkType common.CheckType) KeptnTask { - return KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition), - Namespace: a.Namespace, - }, - Spec: KeptnTaskSpec{ - AppVersion: a.GetVersion(), - AppName: a.GetParentName(), - TaskDefinition: taskDefinition, - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: checkType, - }, - } -} - -func (a KeptnAppVersion) GenerateEvaluation(evaluationDefinition string, checkType common.CheckType) KeptnEvaluation { - return KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition), - Namespace: a.Namespace, - }, - Spec: KeptnEvaluationSpec{ - AppVersion: a.Spec.Version, - AppName: a.Spec.AppName, - EvaluationDefinition: evaluationDefinition, - Type: checkType, - RetryInterval: metav1.Duration{ - Duration: 5 * time.Second, - }, - }, - } -} - -func (a KeptnAppVersion) GetSpanName(phase string) string { - if phase == "" { - return a.Name - } - return phase -} - -func (a KeptnAppVersion) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnAppVersion) GetSpanKey(phase string) string { - return fmt.Sprintf("%s.%s.%s.%s", a.Spec.TraceId["traceparent"], a.Spec.AppName, a.Spec.Version, phase) -} - -func (v KeptnAppVersion) GetWorkloadNameOfApp(workloadName string) string { - return fmt.Sprintf("%s-%s", v.Spec.AppName, workloadName) -} - -//nolint:dupl -func (a *KeptnAppVersion) DeprecateRemainingPhases(phase common.KeptnPhaseType) { - // no need to deprecate anything when post-eval tasks fail - if phase == common.PhaseAppPostEvaluation { - return - } - // deprecate post evaluation when post tasks failed - if phase == common.PhaseAppPostDeployment { - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate post evaluation and tasks when app deployment failed - if phase == common.PhaseAppDeployment { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate app deployment, post tasks and evaluations if app pre-eval failed - if phase == common.PhaseAppPreEvaluation { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - } - // deprecate pre evaluations, app deployment and post tasks and evaluations when pre-tasks failed - if phase == common.PhaseAppPreDeployment { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate completely everything - if phase == common.PhaseDeprecated { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - a.Status.PreDeploymentStatus = common.StateDeprecated - a.Status.Status = common.StateDeprecated - return - } - a.Status.Status = common.StateFailed -} - -func (a *KeptnAppVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - if a.Status.PhaseTraceIDs == nil { - a.Status.PhaseTraceIDs = common.PhaseTraceID{} - } - a.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier -} - -func (a KeptnAppVersion) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": a.Spec.AppName, - "appVersion": a.Spec.Version, - "appVersionName": a.Name, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluation_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluation_test.go deleted file mode 100644 index f43628ace6..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluation_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package v1alpha2 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnEvaluation(t *testing.T) { - evaluation := &KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - EvaluationDefinition: "def", - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - } - - evaluation.SetPhaseTraceID("", nil) - require.Equal(t, KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - EvaluationDefinition: "def", - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - }, *evaluation) - - require.Equal(t, "evaluation", evaluation.GetSpanKey("")) - require.Equal(t, "evaluation", evaluation.GetSpanName("")) - - require.False(t, evaluation.IsEndTimeSet()) - require.False(t, evaluation.IsStartTimeSet()) - - evaluation.SetStartTime() - evaluation.SetEndTime() - - require.True(t, evaluation.IsEndTimeSet()) - require.True(t, evaluation.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - common.EvaluationStatus.String(string(common.StateFailed)), - }, evaluation.GetMetricsAttributes()) - - evaluation.AddEvaluationStatus(Objective{Name: "objName"}) - require.Equal(t, EvaluationStatusItem{ - Status: common.StatePending, - }, evaluation.Status.EvaluationStatus["objName"]) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "appversion", - "workloadName": "", - "workloadVersion": "", - "evaluationName": "evaluation", - "evaluationDefinitionName": "def", - }, evaluation.GetEventAnnotations()) -} - -func TestKeptnEvaluationList(t *testing.T) { - list := KeptnEvaluationList{ - Items: []KeptnEvaluation{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluation_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluation_types.go index fb041ab30e..98d41c4581 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluation_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluation_types.go @@ -17,19 +17,10 @@ limitations under the License. package v1alpha2 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationSpec defines the desired state of KeptnEvaluation type KeptnEvaluationSpec struct { // +optional @@ -111,104 +102,3 @@ type KeptnEvaluationList struct { func init() { SchemeBuilder.Register(&KeptnEvaluation{}, &KeptnEvaluationList{}) } - -func (e KeptnEvaluationList) GetItems() []client.Object { - var b []client.Object - for _, i := range e.Items { - b = append(b, &i) - } - return b -} - -func (e *KeptnEvaluation) SetStartTime() { - if e.Status.StartTime.IsZero() { - e.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) SetEndTime() { - if e.Status.EndTime.IsZero() { - e.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) IsStartTimeSet() bool { - return !e.Status.StartTime.IsZero() -} - -func (e *KeptnEvaluation) IsEndTimeSet() bool { - return !e.Status.EndTime.IsZero() -} - -func (e KeptnEvaluation) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e KeptnEvaluation) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - common.EvaluationStatus.String(string(e.Status.OverallStatus)), - } -} - -func (e *KeptnEvaluation) AddEvaluationStatus(objective Objective) { - - evaluationStatusItem := EvaluationStatusItem{ - Status: common.StatePending, - } - if e.Status.EvaluationStatus == nil { - e.Status.EvaluationStatus = make(map[string]EvaluationStatusItem) - } - e.Status.EvaluationStatus[objective.Name] = evaluationStatusItem - -} - -func (e KeptnEvaluation) SetSpanAttributes(span trace.Span) { - span.SetAttributes(e.GetSpanAttributes()...) -} - -func (e KeptnEvaluation) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e *KeptnEvaluation) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (e KeptnEvaluation) GetSpanKey(phase string) string { - return e.Name -} - -func (e KeptnEvaluation) GetSpanName(phase string) string { - return e.Name -} - -func (e KeptnEvaluation) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": e.Spec.AppName, - "appVersion": e.Spec.AppVersion, - "workloadName": e.Spec.Workload, - "workloadVersion": e.Spec.WorkloadVersion, - "evaluationName": e.Name, - "evaluationDefinitionName": e.Spec.EvaluationDefinition, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationdefinition_types.go index ffb98879dd..efb90dc3f6 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationdefinition_types.go @@ -20,9 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationDefinitionSpec defines the desired state of KeptnEvaluationDefinition type KeptnEvaluationDefinitionSpec struct { Source string `json:"source"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_types.go index e237cc7994..ba92e112d6 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_types.go @@ -21,9 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationProviderSpec defines the desired state of KeptnEvaluationProvider type KeptnEvaluationProviderSpec struct { TargetServer string `json:"targetServer"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptntask_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptntask_test.go deleted file mode 100644 index de4eb26b94..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptntask_test.go +++ /dev/null @@ -1,129 +0,0 @@ -package v1alpha2 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnTask(t *testing.T) { - task := &KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - }, - Spec: KeptnTaskSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - TaskDefinition: "def", - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - } - - task.SetPhaseTraceID("", nil) - require.Equal(t, KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - }, - Spec: KeptnTaskSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - TaskDefinition: "def", - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - }, *task) - - require.Equal(t, "task", task.GetSpanKey("")) - require.Equal(t, "task", task.GetSpanName("")) - - require.False(t, task.IsEndTimeSet()) - require.False(t, task.IsStartTimeSet()) - - task.SetStartTime() - task.SetEndTime() - - require.True(t, task.IsEndTimeSet()) - require.True(t, task.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - common.TaskStatus.String(string(common.StateFailed)), - }, task.GetMetricsAttributes()) - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/task-name": "task", - "keptn.sh/version": "appversion", - }, task.CreateKeptnLabels()) - - task.Spec.Workload = "workload" - task.Spec.WorkloadVersion = "workloadversion" - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/workload": "workload", - "keptn.sh/task-name": "task", - "keptn.sh/version": "workloadversion", - }, task.CreateKeptnLabels()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("workloadversion"), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "appversion", - "workloadName": "workload", - "workloadVersion": "workloadversion", - "taskName": "task", - "taskDefinitionName": "def", - }, task.GetEventAnnotations()) - -} - -func TestKeptnTaskList(t *testing.T) { - list := KeptnTaskList{ - Items: []KeptnTask{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptntask_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptntask_types.go index d9a8d933c7..d6e224c346 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptntask_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptntask_types.go @@ -17,19 +17,10 @@ limitations under the License. package v1alpha2 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnTaskSpec defines the desired state of KeptnTask type KeptnTaskSpec struct { Workload string `json:"workload"` @@ -114,108 +105,3 @@ type KeptnTaskList struct { func init() { SchemeBuilder.Register(&KeptnTask{}, &KeptnTaskList{}) } - -func (t KeptnTaskList) GetItems() []client.Object { - var b []client.Object - for _, i := range t.Items { - b = append(b, &i) - } - return b -} - -func (t *KeptnTask) SetStartTime() { - if t.Status.StartTime.IsZero() { - t.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) SetEndTime() { - if t.Status.EndTime.IsZero() { - t.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) IsStartTimeSet() bool { - return !t.Status.StartTime.IsZero() -} - -func (t *KeptnTask) IsEndTimeSet() bool { - return !t.Status.EndTime.IsZero() -} - -func (t KeptnTask) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.AppName), - common.AppVersion.String(t.Spec.AppVersion), - common.WorkloadName.String(t.Spec.Workload), - common.WorkloadVersion.String(t.Spec.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t KeptnTask) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.AppName), - common.AppVersion.String(t.Spec.AppVersion), - common.WorkloadName.String(t.Spec.Workload), - common.WorkloadVersion.String(t.Spec.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - common.TaskStatus.String(string(t.Status.Status)), - } -} - -func (t KeptnTask) SetSpanAttributes(span trace.Span) { - span.SetAttributes(t.GetSpanAttributes()...) -} - -func (t KeptnTask) CreateKeptnLabels() map[string]string { - if t.Spec.Workload != "" { - return map[string]string{ - common.AppAnnotation: t.Spec.AppName, - common.WorkloadAnnotation: t.Spec.Workload, - common.VersionAnnotation: t.Spec.WorkloadVersion, - common.TaskNameAnnotation: t.Name, - } - } - return map[string]string{ - common.AppAnnotation: t.Spec.AppName, - common.VersionAnnotation: t.Spec.AppVersion, - common.TaskNameAnnotation: t.Name, - } -} - -func (t KeptnTask) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.AppName), - common.AppVersion.String(t.Spec.AppVersion), - common.WorkloadName.String(t.Spec.Workload), - common.WorkloadVersion.String(t.Spec.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t *KeptnTask) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (t KeptnTask) GetSpanKey(phase string) string { - return t.Name -} - -func (t KeptnTask) GetSpanName(phase string) string { - return t.Name -} - -func (t KeptnTask) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": t.Spec.AppName, - "appVersion": t.Spec.AppVersion, - "workloadName": t.Spec.Workload, - "workloadVersion": t.Spec.WorkloadVersion, - "taskName": t.Name, - "taskDefinitionName": t.Spec.TaskDefinition, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptntaskdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptntaskdefinition_types.go index ab7c76844c..6c2f187b06 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptntaskdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptntaskdefinition_types.go @@ -20,9 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnTaskDefinitionSpec defines the desired state of KeptnTaskDefinition type KeptnTaskDefinitionSpec struct { // +optional diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkload_test.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkload_test.go deleted file mode 100644 index b57d5b079d..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkload_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package v1alpha2 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnWorkload(t *testing.T) { - workload := &KeptnWorkload{ - ObjectMeta: metav1.ObjectMeta{ - Name: "workload", - Namespace: "namespace", - }, - Spec: KeptnWorkloadSpec{ - Version: "version", - AppName: "app", - }, - } - - workloadInstanceName := workload.GetWorkloadInstanceName() - require.Equal(t, "workload-version", workloadInstanceName) - - workloadInstance := workload.GenerateWorkloadInstance("prev", map[string]string{}) - require.Equal(t, KeptnWorkloadInstance{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{}, - Name: "workload-version", - Namespace: "namespace", - }, - Spec: KeptnWorkloadInstanceSpec{ - KeptnWorkloadSpec: KeptnWorkloadSpec{ - Version: "version", - AppName: "app", - }, - WorkloadName: "workload", - PreviousVersion: "prev", - }, - }, workloadInstance) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("version"), - }, workload.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "workloadName": "workload", - "workloadVersion": "version", - }, workload.GetEventAnnotations()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkload_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkload_types.go index 91b084b885..86411bd585 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkload_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkload_types.go @@ -17,18 +17,10 @@ limitations under the License. package v1alpha2 import ( - "strings" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnWorkloadSpec defines the desired state of KeptnWorkload type KeptnWorkloadSpec struct { AppName string `json:"app"` @@ -87,42 +79,3 @@ type ResourceReference struct { func init() { SchemeBuilder.Register(&KeptnWorkload{}, &KeptnWorkloadList{}) } - -func (w KeptnWorkload) GetWorkloadInstanceName() string { - return strings.ToLower(w.Name + "-" + w.Spec.Version) -} - -func (w KeptnWorkload) SetSpanAttributes(span trace.Span) { - span.SetAttributes(w.GetSpanAttributes()...) -} - -func (w KeptnWorkload) GenerateWorkloadInstance(previousVersion string, traceContextCarrier map[string]string) KeptnWorkloadInstance { - return KeptnWorkloadInstance{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: traceContextCarrier, - Name: w.GetWorkloadInstanceName(), - Namespace: w.Namespace, - }, - Spec: KeptnWorkloadInstanceSpec{ - KeptnWorkloadSpec: w.Spec, - WorkloadName: w.Name, - PreviousVersion: previousVersion, - }, - } -} - -func (w KeptnWorkload) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Name), - common.WorkloadVersion.String(w.Spec.Version), - } -} - -func (w KeptnWorkload) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": w.Spec.AppName, - "workloadName": w.Name, - "workloadVersion": w.Spec.Version, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go index f9a97fdc8f..9c54107ac7 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go @@ -21,9 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnWorkloadInstanceSpec defines the desired state of KeptnWorkloadInstance type KeptnWorkloadInstanceSpec struct { KeptnWorkloadSpec `json:",inline"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha2/semconv/semconv.go b/lifecycle-operator/apis/lifecycle/v1alpha2/semconv/semconv.go deleted file mode 100644 index 474607e02e..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha2/semconv/semconv.go +++ /dev/null @@ -1,12 +0,0 @@ -package semconv - -import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha2/common" - "go.opentelemetry.io/otel/trace" -) - -func AddAttributeFromAnnotations(s trace.Span, annotations map[string]string) { - s.SetAttributes(common.AppName.String(annotations[common.AppAnnotation])) - s.SetAttributes(common.WorkloadName.String(annotations[common.WorkloadAnnotation])) - s.SetAttributes(common.WorkloadVersion.String(annotations[common.VersionAnnotation])) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/common/common.go b/lifecycle-operator/apis/lifecycle/v1alpha3/common/common.go index b503e8bef4..556bb6172f 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/common/common.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/common/common.go @@ -4,216 +4,18 @@ package common import ( - "crypto/sha256" - "encoding/hex" "errors" - "math/rand" - "strconv" - operatorcommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "go.opentelemetry.io/otel/propagation" ) -const WorkloadAnnotation = "keptn.sh/workload" -const VersionAnnotation = "keptn.sh/version" -const AppAnnotation = "keptn.sh/app" -const PreDeploymentTaskAnnotation = "keptn.sh/pre-deployment-tasks" -const PostDeploymentTaskAnnotation = "keptn.sh/post-deployment-tasks" -const K8sRecommendedWorkloadAnnotations = "app.kubernetes.io/name" -const K8sRecommendedVersionAnnotations = "app.kubernetes.io/version" -const K8sRecommendedAppAnnotations = "app.kubernetes.io/part-of" -const K8sRecommendedManagedByAnnotations = "app.kubernetes.io/managed-by" -const PreDeploymentEvaluationAnnotation = "keptn.sh/pre-deployment-evaluations" -const PostDeploymentEvaluationAnnotation = "keptn.sh/post-deployment-evaluations" -const SchedulingGateRemoved = "keptn.sh/scheduling-gate-removed" -const TaskNameAnnotation = "keptn.sh/task-name" -const NamespaceEnabledAnnotation = "keptn.sh/lifecycle-toolkit" -const CreateAppTaskSpanName = "create_%s_app_task" -const CreateWorkloadTaskSpanName = "create_%s_deployment_task" -const CreateAppEvalSpanName = "create_%s_app_evaluation" -const CreateWorkloadEvalSpanName = "create_%s_deployment_evaluation" -const AppTypeAnnotation = "keptn.sh/app-type" -const KeptnGate = "keptn-prechecks-gate" -const ContainerNameAnnotation = "keptn.sh/container" - -const MinKeptnNameLen = 80 -const MaxK8sObjectLength = 253 - -type AppType string - var ErrCannotCastKeptnAppVersion = errors.New("cannot cast KeptnAppVersion to v1") var ErrCannotCastKeptnApp = errors.New("cannot cast KeptnApp to v1") -const ( - AppTypeSingleService AppType = "single-service" - AppTypeMultiService AppType = "multi-service" -) - // KeptnState is a string containing current Phase state (Progressing/Succeeded/Failed/Unknown/Pending/Cancelled) type KeptnState string -const ( - StateProgressing KeptnState = "Progressing" - StateSucceeded KeptnState = "Succeeded" - StateFailed KeptnState = "Failed" - StateUnknown KeptnState = "Unknown" - StatePending KeptnState = "Pending" - StateDeprecated KeptnState = "Deprecated" - // StateCancelled represents state that was cancelled due to a previous step having failed. - // Deprecated: Use StateDeprecated instead. Should only be used in checks for backwards compatibility reasons - StateCancelled KeptnState = "Cancelled" -) - -func (k KeptnState) IsCompleted() bool { - return k == StateSucceeded || k == StateFailed || k == StateDeprecated || k == StateCancelled -} - -func (k KeptnState) IsSucceeded() bool { - return k == StateSucceeded -} - -func (k KeptnState) IsFailed() bool { - return k == StateFailed -} - -func (k KeptnState) IsDeprecated() bool { - return k == StateDeprecated || k == StateCancelled -} - -func (k KeptnState) IsPending() bool { - return k == StatePending -} - -type StatusSummary struct { - Total int - Progressing int - Failed int - Succeeded int - Pending int - Unknown int - Deprecated int -} - -func UpdateStatusSummary(status KeptnState, summary StatusSummary) StatusSummary { - switch status { - case StateFailed: - summary.Failed++ - case StateDeprecated: - summary.Deprecated++ - case StateSucceeded: - summary.Succeeded++ - case StateProgressing: - summary.Progressing++ - case StatePending, "": - summary.Pending++ - case StateUnknown: - summary.Unknown++ - } - return summary -} - -func (s StatusSummary) GetTotalCount() int { - return s.Failed + s.Succeeded + s.Progressing + s.Pending + s.Unknown + s.Deprecated -} - -func GetOverallState(s StatusSummary) KeptnState { - if s.Failed > 0 || s.Deprecated > 0 { - return StateFailed - } - if s.Progressing > 0 { - return StateProgressing - } - if s.Pending > 0 { - return StatePending - } - if s.Unknown > 0 || s.GetTotalCount() != s.Total { - return StateUnknown - } - return StateSucceeded -} - -func TruncateString(s string, max int) string { - if len(s) > max { - return s[:max] - } - return s -} - -func Hash(num int64) string { - // generate the SHA-256 hash of the bytes - hash := sha256.Sum256([]byte(strconv.FormatInt(num, 10))) - // take the first 4 bytes of the hash and convert to hex - return hex.EncodeToString(hash[:4]) -} - type CheckType string -const PreDeploymentCheckType CheckType = "pre" -const PostDeploymentCheckType CheckType = "post" -const PreDeploymentEvaluationCheckType CheckType = "pre-eval" -const PostDeploymentEvaluationCheckType CheckType = "post-eval" - -type KeptnMeters struct { - TaskCount metric.Int64Counter - TaskDuration metric.Float64Histogram - DeploymentCount metric.Int64Counter - DeploymentDuration metric.Float64Histogram - AppCount metric.Int64Counter - AppDuration metric.Float64Histogram - EvaluationCount metric.Int64Counter - EvaluationDuration metric.Float64Histogram -} - -const ( - AppName attribute.Key = attribute.Key("keptn.deployment.app.name") - AppVersion attribute.Key = attribute.Key("keptn.deployment.app.version") - AppNamespace attribute.Key = attribute.Key("keptn.deployment.app.namespace") - AppStatus attribute.Key = attribute.Key("keptn.deployment.app.status") - AppPreviousVersion attribute.Key = attribute.Key("keptn.deployment.app.previousversion") - WorkloadName attribute.Key = attribute.Key("keptn.deployment.workload.name") - WorkloadVersion attribute.Key = attribute.Key("keptn.deployment.workload.version") - WorkloadPreviousVersion attribute.Key = attribute.Key("keptn.deployment.workload.previousversion") - WorkloadNamespace attribute.Key = attribute.Key("keptn.deployment.workload.namespace") - WorkloadStatus attribute.Key = attribute.Key("keptn.deployment.workload.status") - TaskStatus attribute.Key = attribute.Key("keptn.deployment.task.status") - TaskName attribute.Key = attribute.Key("keptn.deployment.task.name") - TaskType attribute.Key = attribute.Key("keptn.deployment.task.type") - EvaluationStatus attribute.Key = attribute.Key("keptn.deployment.evaluation.status") - EvaluationName attribute.Key = attribute.Key("keptn.deployment.evaluation.name") - EvaluationType attribute.Key = attribute.Key("keptn.deployment.evaluation.type") -) - -func GenerateTaskName(checkType CheckType, taskName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return operatorcommon.CreateResourceName(MaxK8sObjectLength, MinKeptnNameLen, string(checkType), taskName, strconv.Itoa(randomId)) -} - -func GenerateJobName(taskName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return operatorcommon.CreateResourceName(MaxK8sObjectLength, MinKeptnNameLen, taskName, strconv.Itoa(randomId)) -} - -func GenerateEvaluationName(checkType CheckType, evalName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return operatorcommon.CreateResourceName(MaxK8sObjectLength, MinKeptnNameLen, string(checkType), evalName, strconv.Itoa(randomId)) -} - -// MergeMaps merges two maps into a new map. If a key exists in both maps, the -// value of the second map is picked. -func MergeMaps(m1 map[string]string, m2 map[string]string) map[string]string { - merged := make(map[string]string, len(m1)+len(m2)) - for key, value := range m1 { - merged[key] = value - } - for key, value := range m2 { - merged[key] = value - } - return merged -} - -// IsOwnerSupported returns whether the owner of the given object is supported to be considered a KeptnWorkload -func IsOwnerSupported(owner metav1.OwnerReference) bool { - return owner.Kind == "ReplicaSet" || owner.Kind == "Deployment" || owner.Kind == "StatefulSet" || owner.Kind == "DaemonSet" || owner.Kind == "Rollout" -} +// PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases +type PhaseTraceID map[string]propagation.MapCarrier diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/common/common_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/common/common_test.go deleted file mode 100644 index c39eaed9fe..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/common/common_test.go +++ /dev/null @@ -1,491 +0,0 @@ -package common - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/require" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -const ExtraLongName = "loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo01234567891234567890123456789" - -func TestKeptnState_IsCompleted(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - { - State: StateSucceeded, - Want: true, - }, - { - State: StateDeprecated, - Want: true, - }, - { - State: StateCancelled, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.Want, tt.State.IsCompleted()) - }) - } -} - -func TestKeptnState_IsSucceeded(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateSucceeded, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsSucceeded(), tt.Want) - }) - } -} - -func TestKeptnState_IsFailed(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsFailed(), tt.Want) - }) - } -} - -func TestHash(t *testing.T) { - tests := []struct { - in int64 - out string - }{ - { - in: int64(1), - out: "6b86b273", - }, - { - in: int64(2), - out: "d4735e3a", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.out, Hash(tt.in)) - }) - } -} - -func TestKeptnState_IsDeprecated(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateDeprecated, - Want: true, - }, - { - State: StateCancelled, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsDeprecated(), tt.Want) - }) - } -} - -func TestKeptnKeptnState_IsPending(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StatePending, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPending(), tt.Want) - }) - } -} - -func Test_UpdateStatusSummary(t *testing.T) { - emmptySummary := StatusSummary{0, 0, 0, 0, 0, 0, 0} - tests := []struct { - State KeptnState - Want StatusSummary - }{ - { - State: StateProgressing, - Want: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - }, - { - State: StateFailed, - Want: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - }, - { - State: StateSucceeded, - Want: StatusSummary{0, 0, 0, 1, 0, 0, 0}, - }, - { - State: StatePending, - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: "", - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: StateUnknown, - Want: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - }, - { - State: StateDeprecated, - Want: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, UpdateStatusSummary(tt.State, emmptySummary), tt.Want) - }) - } -} - -func Test_GetTotalCount(t *testing.T) { - summary := StatusSummary{2, 0, 2, 1, 0, 3, 5} - require.Equal(t, summary.GetTotalCount(), 11) -} - -func Test_GeOverallState(t *testing.T) { - tests := []struct { - Name string - Summary StatusSummary - Want KeptnState - }{ - { - Name: "failed", - Summary: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - Want: StateFailed, - }, - { - Name: "deprecated", - Summary: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - Want: StateFailed, - }, - { - Name: "progressing", - Summary: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - Want: StateProgressing, - }, - { - Name: "pending", - Summary: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - Want: StatePending, - }, - { - Name: "unknown", - Summary: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "unknown totalcount", - Summary: StatusSummary{5, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "succeeded", - Summary: StatusSummary{1, 0, 0, 1, 0, 0, 0}, - Want: StateSucceeded, - }, - } - for _, tt := range tests { - t.Run(tt.Name, func(t *testing.T) { - require.Equal(t, GetOverallState(tt.Summary), tt.Want) - }) - } -} - -func Test_TruncateString(t *testing.T) { - tests := []struct { - Input string - Max int - Want string - }{ - { - Input: "some_string", - Max: 20, - Want: "some_string", - }, - { - Input: "some_string", - Max: 5, - Want: "some_", - }, - { - Input: "", - Max: 5, - Want: "", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, TruncateString(tt.Input, tt.Max), tt.Want) - }) - } -} - -func Test_GenerateTaskName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentCheckType, - Name: "short-name", - Want: "pre-short-name-", - }, - { - Check: PreDeploymentCheckType, - Name: "", - Want: "pre--", - }, - { - Check: PreDeploymentCheckType, - Name: ExtraLongName, - Want: "pre-loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo0123456789123456-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateTaskName(tt.Check, tt.Name), tt.Want)) - }) - } -} - -func Test_GenerateEvaluationName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentEvaluationCheckType, - Name: "short-name", - Want: "pre-eval-short-name-", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: "", - Want: "pre-eval--", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: ExtraLongName, - Want: "pre-eval-loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo01234567891-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateEvaluationName(tt.Check, tt.Name), tt.Want)) - }) - } -} - -func Test_GenerateJobName(t *testing.T) { - tests := []struct { - Name string - Want string - }{ - { - Name: "short-name", - Want: "short-name-", - }, - { - Name: "", - Want: "-", - }, - { - Name: ExtraLongName, - Want: "loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo01234567891234567890-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateJobName(tt.Name), tt.Want)) - }) - } -} - -func Test_MergeMaps(t *testing.T) { - tests := []struct { - In1 map[string]string - In2 map[string]string - Want map[string]string - }{ - { - In1: nil, - In2: nil, - Want: map[string]string{}, - }, - { - In1: nil, - In2: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - Want: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - }, - { - In1: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - In2: nil, - Want: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - }, - { - In1: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - In2: map[string]string{ - "ll5": "ll6", - "ll7": "ll8", - }, - Want: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - "ll5": "ll6", - "ll7": "ll8", - }, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, MergeMaps(tt.In1, tt.In2), tt.Want) - }) - } -} - -func TestIsOwnerSupported(t *testing.T) { - type args struct { - owner v1.OwnerReference - } - tests := []struct { - name string - args args - want bool - }{ - { - name: "Deployment -> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "Deployment", - }, - }, - want: true, - }, - { - name: "DaemonSet-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "DaemonSet", - }, - }, - want: true, - }, - { - name: "ReplicaSet-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "ReplicaSet", - }, - }, - want: true, - }, - { - name: "StatefulSet-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "StatefulSet", - }, - }, - want: true, - }, - { - name: "Rollout-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "Rollout", - }, - }, - want: true, - }, - { - name: "Job-> false", - args: args{ - owner: v1.OwnerReference{ - Kind: "Job", - }, - }, - want: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := IsOwnerSupported(tt.args.owner); got != tt.want { - t.Errorf("IsOwnerSupported() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/common/phases.go b/lifecycle-operator/apis/lifecycle/v1alpha3/common/phases.go deleted file mode 100644 index 32b8951959..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/common/phases.go +++ /dev/null @@ -1,131 +0,0 @@ -package common - -import ( - "strings" - - "go.opentelemetry.io/otel/propagation" -) - -type KeptnPhase KeptnPhaseType - -type KeptnPhaseType struct { - LongName string - ShortName string -} - -var phases = []KeptnPhaseType{ - PhaseWorkloadPreDeployment, - PhaseWorkloadPostDeployment, - PhaseWorkloadPreEvaluation, - PhaseWorkloadPostEvaluation, - PhaseWorkloadDeployment, - PhaseAppPreDeployment, - PhaseAppPostDeployment, - PhaseAppPreEvaluation, - PhaseAppPostEvaluation, - PhaseAppDeployment, - PhaseReconcileEvaluation, - PhaseReconcileTask, - PhaseReconcileWorkload, - PhaseUpdateWorkload, - PhaseCreateEvaluation, - PhaseCreateTask, - PhaseCreateAppCreationRequest, - PhaseCreateWorkload, - PhaseCreateWorkloadVersion, - PhaseCreateAppVersion, - PhaseAppCompleted, - PhaseWorkloadCompleted, - PhaseDeprecateAppVersion, - PhaseDeprecated, - PhaseAppCompleted, -} - -func (p KeptnPhaseType) IsEvaluation() bool { - return strings.Contains(p.ShortName, "DeployEvaluations") -} - -func (p KeptnPhaseType) IsPreEvaluation() bool { - return strings.Contains(p.ShortName, "PreDeployEvaluations") -} - -func (p KeptnPhaseType) IsPostEvaluation() bool { - return strings.Contains(p.ShortName, "PostDeployEvaluations") -} - -func (p KeptnPhaseType) IsTask() bool { - return strings.Contains(p.ShortName, "DeployTasks") -} - -func (p KeptnPhaseType) IsPreTask() bool { - return strings.Contains(p.ShortName, "PreDeployTasks") -} - -func (p KeptnPhaseType) IsPostTask() bool { - return strings.Contains(p.ShortName, "PostDeployTasks") -} - -func GetShortPhaseName(phase string) string { - for _, p := range phases { - if phase == p.ShortName { - return p.ShortName - } - } - - for _, p := range phases { - if phase == p.LongName { - return p.ShortName - } - } - - return "" -} - -var ( - PhaseWorkloadPreDeployment = KeptnPhaseType{LongName: "Workload Pre-Deployment Tasks", ShortName: "WorkloadPreDeployTasks"} - PhaseWorkloadPostDeployment = KeptnPhaseType{LongName: "Workload Post-Deployment Tasks", ShortName: "WorkloadPostDeployTasks"} - PhaseWorkloadPreEvaluation = KeptnPhaseType{LongName: "Workload Pre-Deployment Evaluations", ShortName: "WorkloadPreDeployEvaluations"} - PhaseWorkloadPostEvaluation = KeptnPhaseType{LongName: "Workload Post-Deployment Evaluations", ShortName: "WorkloadPostDeployEvaluations"} - PhaseWorkloadDeployment = KeptnPhaseType{LongName: "Workload Deployment", ShortName: "WorkloadDeploy"} - PhaseAppPreDeployment = KeptnPhaseType{LongName: "App Pre-Deployment Tasks", ShortName: "AppPreDeployTasks"} - PhaseAppPostDeployment = KeptnPhaseType{LongName: "App Post-Deployment Tasks", ShortName: "AppPostDeployTasks"} - PhaseAppPreEvaluation = KeptnPhaseType{LongName: "App Pre-Deployment Evaluations", ShortName: "AppPreDeployEvaluations"} - PhaseAppPostEvaluation = KeptnPhaseType{LongName: "App Post-Deployment Evaluations", ShortName: "AppPostDeployEvaluations"} - PhaseAppDeployment = KeptnPhaseType{LongName: "App Deployment", ShortName: "AppDeploy"} - PhaseReconcileEvaluation = KeptnPhaseType{LongName: "Reconcile Evaluation", ShortName: "ReconcileEvaluation"} - PhaseReconcileTask = KeptnPhaseType{LongName: "Reconcile Task", ShortName: "ReconcileTask"} - PhaseReconcileWorkload = KeptnPhaseType{LongName: "Reconcile Workloads", ShortName: "ReconcileWorkload"} - PhaseCreateEvaluation = KeptnPhaseType{LongName: "Create Evaluation", ShortName: "CreateEvaluation"} - PhaseCreateTask = KeptnPhaseType{LongName: "Create Task", ShortName: "CreateTask"} - PhaseCreateAppCreationRequest = KeptnPhaseType{LongName: "Create AppCreationRequest", ShortName: "CreateAppCreationRequest"} - PhaseCreateWorkload = KeptnPhaseType{LongName: "Create Workload", ShortName: "CreateWorkload"} - PhaseUpdateWorkload = KeptnPhaseType{LongName: "Update Workload", ShortName: "UpdateWorkload"} - PhaseCreateWorkloadVersion = KeptnPhaseType{LongName: "Create WorkloadVersion", ShortName: "CreateWorkloadVersion"} - PhaseCreateAppVersion = KeptnPhaseType{LongName: "Create AppVersion", ShortName: "CreateAppVersion"} - PhaseDeprecateAppVersion = KeptnPhaseType{LongName: "Deprecate AppVersion", ShortName: "DeprecateAppVersion"} - PhaseAppCompleted = KeptnPhaseType{LongName: "App Completed", ShortName: "AppCompleted"} - PhaseWorkloadCompleted = KeptnPhaseType{LongName: "Workload Completed", ShortName: "WorkloadCompleted"} - PhaseCompleted = KeptnPhaseType{LongName: "Completed", ShortName: "Completed"} - PhaseDeprecated = KeptnPhaseType{LongName: "Deprecated", ShortName: "Deprecated"} -) - -type PhaseTraceID map[string]propagation.MapCarrier - -func (pid PhaseTraceID) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - pid[GetShortPhaseName(phase)] = carrier - -} - -func (pid PhaseTraceID) GetPhaseTraceID(phase string) propagation.MapCarrier { - return pid[GetShortPhaseName(phase)] -} - -var ( - PhaseStateFinished = "Finished" - PhaseStateStarted = "Started" - PhaseStateFailed = "Failed" - PhaseStateStatusChanged = "StatusChanged" - PhaseStateReconcileError = "ReconcileError" - PhaseStateReconcileTimeout = "ReconcileTimeout" - PhaseStateNotFound = "NotFound" -) diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/common/phases_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/common/phases_test.go deleted file mode 100644 index 271732c1f1..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/common/phases_test.go +++ /dev/null @@ -1,275 +0,0 @@ -package common - -import ( - "testing" - - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/propagation" -) - -func TestKeptnPhaseType_IsEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: false, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: false, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: false, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: false, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostTask(), tt.Want) - }) - } -} - -func TestPhaseTraceID(t *testing.T) { - trace := PhaseTraceID{} - - trace.SetPhaseTraceID(PhaseAppDeployment.LongName, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - }, trace) - - trace.SetPhaseTraceID(PhaseWorkloadDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, trace) - - require.Equal(t, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, trace.GetPhaseTraceID(PhaseAppDeployment.LongName)) - - require.Equal(t, propagation.MapCarrier{ - "name3": "trace3", - }, trace.GetPhaseTraceID(PhaseWorkloadDeployment.ShortName)) -} - -func TestGetShortPhaseName(t *testing.T) { - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("WorkloadPreDeployTasks")) - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("Workload Pre-Deployment Tasks")) - require.Equal(t, "", GetShortPhaseName("Workload Pre-Deploycdddment Tasks")) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnapp_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnapp_test.go deleted file mode 100644 index 2bdef82be9..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnapp_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnApp(t *testing.T) { - app := &KeptnApp{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - Generation: 1, - }, - Spec: KeptnAppSpec{ - Version: "version", - }, - } - - appVersionName := app.GetAppVersionName() - require.Equal(t, "app-version-6b86b273", appVersionName) - - appVersion := app.GenerateAppVersion("prev") - require.Equal(t, KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app-version-6b86b273", - Namespace: "namespace", - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - Version: "version", - }, - AppName: "app", - PreviousVersion: "prev", - }, - }, appVersion) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("version"), - }, app.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "version", - "appRevision": "6b86b273", - }, app.GetEventAnnotations()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnapp_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnapp_types.go index 490c2d413e..9a47f99d53 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnapp_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnapp_types.go @@ -17,16 +17,9 @@ limitations under the License. package v1alpha3 import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - operatorcommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppSpec defines the desired state of KeptnApp type KeptnAppSpec struct { // Version defines the version of the application. For automatically created KeptnApps, @@ -110,40 +103,3 @@ type KeptnAppList struct { func init() { SchemeBuilder.Register(&KeptnApp{}, &KeptnAppList{}) } - -func (a KeptnApp) GetAppVersionName() string { - return operatorcommon.CreateResourceName(common.MaxK8sObjectLength, common.MinKeptnNameLen, a.Name, a.Spec.Version, common.Hash(a.Generation)) -} - -func (a KeptnApp) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnApp) GenerateAppVersion(previousVersion string) KeptnAppVersion { - return KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: a.GetAppVersionName(), - Namespace: a.Namespace, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: a.Spec, - AppName: a.Name, - PreviousVersion: previousVersion, - }, - } -} - -func (a KeptnApp) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Name), - common.AppVersion.String(a.Spec.Version), - } -} - -func (a KeptnApp) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": a.Name, - "appVersion": a.Spec.Version, - "appRevision": common.Hash(a.Generation), - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappcreationrequest_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappcreationrequest_types.go index 61ffd26d89..38c44297a8 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappcreationrequest_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappcreationrequest_types.go @@ -17,15 +17,9 @@ limitations under the License. package v1alpha3 import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppCreationRequestSpec defines the desired state of KeptnAppCreationRequest type KeptnAppCreationRequestSpec struct { // AppName is the name of the KeptnApp the KeptnAppCreationRequest should create if no user-defined object with that name is found. @@ -62,17 +56,3 @@ type KeptnAppCreationRequestList struct { func init() { SchemeBuilder.Register(&KeptnAppCreationRequest{}, &KeptnAppCreationRequestList{}) } - -func (kacr KeptnAppCreationRequest) IsSingleService() bool { - return kacr.Annotations[common.AppTypeAnnotation] == string(common.AppTypeSingleService) -} - -func (kacr KeptnAppCreationRequest) SetSpanAttributes(span trace.Span) { - span.SetAttributes(kacr.GetSpanAttributes()...) -} - -func (kacr KeptnAppCreationRequest) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(kacr.Name), - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappcreationrequest_types_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappcreationrequest_types_test.go deleted file mode 100644 index b4243c762e..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappcreationrequest_types_test.go +++ /dev/null @@ -1,74 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnAppCreationRequest_IsSingleService(t *testing.T) { - type fields struct { - ObjectMeta v1.ObjectMeta - } - tests := []struct { - name string - fields fields - want bool - }{ - { - name: "single-service application", - fields: fields{ObjectMeta: v1.ObjectMeta{ - Annotations: map[string]string{ - common.AppTypeAnnotation: string(common.AppTypeSingleService), - }, - }}, - want: true, - }, - { - name: "multi-service application", - fields: fields{ObjectMeta: v1.ObjectMeta{ - Annotations: map[string]string{ - common.AppTypeAnnotation: string(common.AppTypeMultiService), - }, - }}, - want: false, - }, - { - name: "anything else", - fields: fields{ObjectMeta: v1.ObjectMeta{ - Annotations: map[string]string{ - common.AppTypeAnnotation: "", - }, - }}, - want: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - kacr := &KeptnAppCreationRequest{ - ObjectMeta: tt.fields.ObjectMeta, - } - if got := kacr.IsSingleService(); got != tt.want { - t.Errorf("IsSingleService() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestKeptnAppCreationRequest_GetSpanAttributes(t *testing.T) { - kacr := KeptnAppCreationRequest{ - ObjectMeta: v1.ObjectMeta{ - Name: "my-app", - }, - Spec: KeptnAppCreationRequestSpec{}, - } - - spanAttrs := kacr.GetSpanAttributes() - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String(kacr.Name), - }, spanAttrs) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_conversion_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_conversion_test.go index f5ad67d49a..e1eef7f2cf 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_conversion_test.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_conversion_test.go @@ -13,6 +13,8 @@ import ( v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" ) +const StateFailed common.KeptnState = "Failed" + func TestKeptnAppVersion_ConvertFrom(t *testing.T) { tests := []struct { name string @@ -206,25 +208,25 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -232,48 +234,48 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { { DefinitionName: "def1", Name: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def12", Name: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []ItemStatus{ { DefinitionName: "def2", Name: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def22", Name: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def3", Name: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def32", Name: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def4", Name: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def42", Name: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -286,7 +288,7 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, }, @@ -368,25 +370,25 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -394,48 +396,48 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { { DefinitionName: "def1", Name: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def12", Name: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []ItemStatus{ { DefinitionName: "def2", Name: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def22", Name: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def3", Name: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def32", Name: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def4", Name: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def42", Name: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -448,7 +450,7 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, wantErr: false, diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_test.go deleted file mode 100644 index 1f51ba625f..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_test.go +++ /dev/null @@ -1,488 +0,0 @@ -package v1alpha3 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnAppVersion(t *testing.T) { - app := &KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - }, - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, - Status: common.StateFailed, - PreDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, - PostDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, - PreDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, - PostDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, - CurrentPhase: common.PhaseAppDeployment.ShortName, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - PreDeploymentTasks: []string{"task1", "task2"}, - PostDeploymentTasks: []string{"task3", "task4"}, - PreDeploymentEvaluations: []string{"task5", "task6"}, - PostDeploymentEvaluations: []string{"task7", "task8"}, - Version: "version", - }, - PreviousVersion: "prev", - AppName: "appname", - TraceId: map[string]string{"traceparent": "trace1"}, - }, - } - - require.True(t, app.IsPreDeploymentCompleted()) - require.False(t, app.IsPreDeploymentSucceeded()) - require.True(t, app.IsPreDeploymentFailed()) - - require.True(t, app.IsPreDeploymentEvaluationCompleted()) - require.False(t, app.IsPreDeploymentEvaluationSucceeded()) - require.True(t, app.IsPreDeploymentEvaluationFailed()) - - require.True(t, app.IsPostDeploymentCompleted()) - require.False(t, app.IsPostDeploymentSucceeded()) - require.True(t, app.IsPostDeploymentFailed()) - - require.True(t, app.IsPostDeploymentEvaluationCompleted()) - require.False(t, app.IsPostDeploymentEvaluationSucceeded()) - require.True(t, app.IsPostDeploymentEvaluationFailed()) - - require.True(t, app.AreWorkloadsCompleted()) - require.False(t, app.AreWorkloadsSucceeded()) - require.True(t, app.AreWorkloadsFailed()) - - require.False(t, app.IsEndTimeSet()) - require.False(t, app.IsStartTimeSet()) - - app.SetStartTime() - app.SetEndTime() - - require.True(t, app.IsEndTimeSet()) - require.True(t, app.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - common.AppStatus.String(string(common.StateFailed)), - }, app.GetMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppPreviousVersion.String("prev"), - }, app.GetDurationMetricsAttributes()) - - require.Equal(t, common.StateFailed, app.GetState()) - - require.Equal(t, []string{"task1", "task2"}, app.GetPreDeploymentTasks()) - require.Equal(t, []string{"task3", "task4"}, app.GetPostDeploymentTasks()) - require.Equal(t, []string{"task5", "task6"}, app.GetPreDeploymentEvaluations()) - require.Equal(t, []string{"task7", "task8"}, app.GetPostDeploymentEvaluations()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, app.GetPreDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, app.GetPostDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, app.GetPreDeploymentEvaluationTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, app.GetPostDeploymentEvaluationTaskStatus()) - - require.Equal(t, "appname", app.GetAppName()) - require.Equal(t, "prev", app.GetPreviousVersion()) - require.Equal(t, "appname", app.GetParentName()) - require.Equal(t, "namespace", app.GetNamespace()) - - app.SetState(common.StatePending) - require.Equal(t, common.StatePending, app.GetState()) - - require.True(t, !app.GetStartTime().IsZero()) - require.True(t, !app.GetEndTime().IsZero()) - - app.SetCurrentPhase(common.PhaseAppDeployment.LongName) - require.Equal(t, common.PhaseAppDeployment.LongName, app.GetCurrentPhase()) - - app.Status.EndTime = v1.Time{Time: time.Time{}} - app.Complete() - require.True(t, !app.GetEndTime().IsZero()) - - require.Equal(t, "version", app.GetVersion()) - - require.Equal(t, "trace1.appname.version.phase", app.GetSpanKey("phase")) - - retries := int32(5) - task := app.GenerateTask(KeptnTaskDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "task-def", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: KeptnTaskDefinitionSpec{ - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, - }, common.PostDeploymentCheckType) - require.Equal(t, KeptnTaskSpec{ - Context: TaskContext{ - AppName: app.GetParentName(), - AppVersion: app.GetVersion(), - TaskType: string(common.PostDeploymentCheckType), - ObjectType: "App", - }, - TaskDefinition: "task-def", - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: common.PostDeploymentCheckType, - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, task.Spec) - - require.Equal(t, map[string]string{ - "label1": "label2", - }, task.Labels) - - require.Equal(t, map[string]string{ - "annotation1": "annotation2", - }, task.Annotations) - - evaluation := app.GenerateEvaluation(KeptnEvaluationDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "eval-def", - }, - }, common.PostDeploymentCheckType) - require.Equal(t, KeptnEvaluationSpec{ - AppVersion: app.GetVersion(), - AppName: app.GetParentName(), - EvaluationDefinition: "eval-def", - Type: common.PostDeploymentCheckType, - RetryInterval: metav1.Duration{ - Duration: 5 * time.Second, - }, - }, evaluation.Spec) - - require.Equal(t, "phase", app.GetSpanName("phase")) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "appname", - "appVersion": "version", - "appVersionName": "app", - }, app.GetEventAnnotations()) -} - -func TestKeptnAppVersion_GetWorkloadNameOfApp(t *testing.T) { - type fields struct { - Spec KeptnAppVersionSpec - } - type args struct { - workloadName string - } - tests := []struct { - name string - fields fields - args args - want string - }{ - { - name: "", - fields: fields{ - Spec: KeptnAppVersionSpec{AppName: "my-app"}, - }, - args: args{ - workloadName: "my-workload", - }, - want: "my-app-my-workload", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - v := KeptnAppVersion{ - Spec: tt.fields.Spec, - } - if got := v.GetWorkloadNameOfApp(tt.args.workloadName); got != tt.want { - t.Errorf("GetWorkloadNameOfApp() = %v, want %v", got, tt.want) - } - }) - } -} - -//nolint:dupl -func TestKeptnAppVersion_DeprecateRemainingPhases(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - } - - tests := []struct { - app KeptnAppVersion - phase common.KeptnPhaseType - want KeptnAppVersion - }{ - { - app: app, - phase: common.PhaseAppPostEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPostDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPreEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPreDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseDeprecated, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateDeprecated, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateDeprecated, - }, - }, - }, - { - app: app, - phase: common.PhaseWorkloadPreDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - } - - for _, tt := range tests { - t.Run("", func(t *testing.T) { - tt.app.DeprecateRemainingPhases(tt.phase) - require.Equal(t, tt.want, tt.app) - }) - } -} - -func TestKeptnAppVersion_SetPhaseTraceID(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{}, - } - - app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, - }, - }, app) - - app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{ - "name2": "trace2", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name2": "trace2", - }, - }, - }, - }, app) -} - -func TestKeptnAppVersionList(t *testing.T) { - list := KeptnAppVersionList{ - Items: []KeptnAppVersion{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - Status: KeptnAppVersionStatus{ - Status: common.StateSucceeded, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - Status: KeptnAppVersionStatus{ - Status: common.StateDeprecated, - }, - }, - }, - } - - // fetch the list items - got := list.GetItems() - require.Len(t, got, 2) - - require.Equal(t, "obj1", list.Items[0].GetName()) - require.Equal(t, "obj2", list.Items[1].GetName()) - - // remove deprecated items from the list - list.RemoveDeprecated() - - // check that deprecated items are not present in the list anymore - got = list.GetItems() - require.Len(t, got, 1) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_types.go index c8dd39bddf..e391910c41 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnappversion_types.go @@ -17,20 +17,10 @@ limitations under the License. package v1alpha3 import ( - "fmt" - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppVersionSpec defines the desired state of KeptnAppVersion type KeptnAppVersionSpec struct { KeptnAppSpec `json:",inline"` @@ -146,335 +136,6 @@ type KeptnAppVersionList struct { Items []KeptnAppVersion `json:"items"` } -func (a KeptnAppVersionList) GetItems() []client.Object { - b := make([]client.Object, 0, len(a.Items)) - for i := 0; i < len(a.Items); i++ { - b = append(b, &a.Items[i]) - } - return b -} - -func (a *KeptnAppVersionList) RemoveDeprecated() { - b := make([]KeptnAppVersion, 0, len(a.Items)) - for i := 0; i < len(a.Items); i++ { - if a.Items[i].Status.Status != common.StateDeprecated { - b = append(b, a.Items[i]) - } - } - a.Items = b -} - func init() { SchemeBuilder.Register(&KeptnAppVersion{}, &KeptnAppVersionList{}) } - -func (a KeptnAppVersion) IsPreDeploymentCompleted() bool { - return a.Status.PreDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationCompleted() bool { - return a.Status.PreDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentSucceeded() bool { - return a.Status.PreDeploymentStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPreDeploymentFailed() bool { - return a.Status.PreDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationSucceeded() bool { - return a.Status.PreDeploymentEvaluationStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationFailed() bool { - return a.Status.PreDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentCompleted() bool { - return a.Status.PostDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationCompleted() bool { - return a.Status.PostDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentFailed() bool { - return a.Status.PostDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationSucceeded() bool { - return a.Status.PostDeploymentEvaluationStatus.IsSucceeded() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationFailed() bool { - return a.Status.PostDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentSucceeded() bool { - return a.Status.PostDeploymentStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsCompleted() bool { - return a.Status.WorkloadOverallStatus.IsCompleted() -} - -func (a KeptnAppVersion) AreWorkloadsSucceeded() bool { - return a.Status.WorkloadOverallStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsFailed() bool { - return a.Status.WorkloadOverallStatus.IsFailed() -} - -func (a *KeptnAppVersion) SetStartTime() { - if a.Status.StartTime.IsZero() { - a.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a *KeptnAppVersion) SetEndTime() { - if a.Status.EndTime.IsZero() { - a.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a KeptnAppVersion) GetStartTime() time.Time { - return a.Status.StartTime.Time -} - -func (a KeptnAppVersion) GetEndTime() time.Time { - return a.Status.EndTime.Time -} - -func (a *KeptnAppVersion) IsStartTimeSet() bool { - return !a.Status.StartTime.IsZero() -} - -func (a *KeptnAppVersion) IsEndTimeSet() bool { - return !a.Status.EndTime.IsZero() -} - -func (a *KeptnAppVersion) Complete() { - a.SetEndTime() -} - -func (a KeptnAppVersion) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - common.AppStatus.String(string(a.Status.Status)), - } -} - -func (a KeptnAppVersion) GetDurationMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppPreviousVersion.String(a.Spec.PreviousVersion), - } -} - -func (a KeptnAppVersion) GetState() common.KeptnState { - return a.Status.Status -} - -func (a KeptnAppVersion) GetPreDeploymentTasks() []string { - return a.Spec.PreDeploymentTasks -} - -func (a KeptnAppVersion) GetPostDeploymentTasks() []string { - return a.Spec.PostDeploymentTasks -} - -func (a KeptnAppVersion) GetPreDeploymentTaskStatus() []ItemStatus { - return a.Status.PreDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentTaskStatus() []ItemStatus { - return a.Status.PostDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluations() []string { - return a.Spec.PreDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluations() []string { - return a.Spec.PostDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluationTaskStatus() []ItemStatus { - return a.Status.PreDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluationTaskStatus() []ItemStatus { - return a.Status.PostDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetAppName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetPreviousVersion() string { - return a.Spec.PreviousVersion -} - -func (a KeptnAppVersion) GetParentName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetNamespace() string { - return a.Namespace -} - -func (a *KeptnAppVersion) SetState(state common.KeptnState) { - a.Status.Status = state -} - -func (a KeptnAppVersion) GetCurrentPhase() string { - return a.Status.CurrentPhase -} - -func (a *KeptnAppVersion) SetCurrentPhase(phase string) { - a.Status.CurrentPhase = phase -} - -func (a KeptnAppVersion) GetVersion() string { - return a.Spec.Version -} - -func (a KeptnAppVersion) GenerateTask(taskDefinition KeptnTaskDefinition, checkType common.CheckType) KeptnTask { - return KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition.Name), - Namespace: a.Namespace, - Labels: taskDefinition.Labels, - Annotations: taskDefinition.Annotations, - }, - Spec: KeptnTaskSpec{ - Context: TaskContext{ - AppName: a.GetParentName(), - AppVersion: a.GetVersion(), - TaskType: string(checkType), - ObjectType: "App", - }, - TaskDefinition: taskDefinition.Name, - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: checkType, - Retries: taskDefinition.Spec.Retries, - Timeout: taskDefinition.Spec.Timeout, - }, - } -} - -func (a KeptnAppVersion) GenerateEvaluation(evaluationDefinition KeptnEvaluationDefinition, checkType common.CheckType) KeptnEvaluation { - return KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition.Name), - Namespace: a.Namespace, - }, - Spec: KeptnEvaluationSpec{ - AppVersion: a.Spec.Version, - AppName: a.Spec.AppName, - EvaluationDefinition: evaluationDefinition.Name, - Type: checkType, - RetryInterval: metav1.Duration{ - Duration: 5 * time.Second, - }, - }, - } -} - -func (a KeptnAppVersion) GetSpanName(phase string) string { - if phase == "" { - return a.Name - } - return phase -} - -func (a KeptnAppVersion) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnAppVersion) GetSpanKey(phase string) string { - return fmt.Sprintf("%s.%s.%s.%s", a.Spec.TraceId["traceparent"], a.Spec.AppName, a.Spec.Version, phase) -} - -func (v KeptnAppVersion) GetWorkloadNameOfApp(workloadName string) string { - return fmt.Sprintf("%s-%s", v.Spec.AppName, workloadName) -} - -//nolint:dupl -func (a *KeptnAppVersion) DeprecateRemainingPhases(phase common.KeptnPhaseType) { - // no need to deprecate anything when post-eval tasks fail - if phase == common.PhaseAppPostEvaluation { - return - } - // deprecate post evaluation when post tasks failed - if phase == common.PhaseAppPostDeployment { - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate post evaluation and tasks when app deployment failed - if phase == common.PhaseAppDeployment { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate app deployment, post tasks and evaluations if app pre-eval failed - if phase == common.PhaseAppPreEvaluation { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - } - // deprecate pre evaluations, app deployment and post tasks and evaluations when pre-tasks failed - if phase == common.PhaseAppPreDeployment { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate completely everything - if phase == common.PhaseDeprecated { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - a.Status.PreDeploymentStatus = common.StateDeprecated - a.Status.Status = common.StateDeprecated - return - } - a.Status.Status = common.StateFailed -} - -func (a *KeptnAppVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - if a.Status.PhaseTraceIDs == nil { - a.Status.PhaseTraceIDs = common.PhaseTraceID{} - } - a.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier -} - -func (a KeptnAppVersion) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": a.Spec.AppName, - "appVersion": a.Spec.Version, - "appVersionName": a.Name, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluation_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluation_test.go deleted file mode 100644 index 8ea9a916f8..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluation_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnEvaluation(t *testing.T) { - evaluation := &KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - EvaluationDefinition: "def", - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - } - - evaluation.SetPhaseTraceID("", nil) - require.Equal(t, KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - EvaluationDefinition: "def", - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - }, *evaluation) - - require.Equal(t, "evaluation", evaluation.GetSpanKey("")) - require.Equal(t, "evaluation", evaluation.GetSpanName("")) - - require.False(t, evaluation.IsEndTimeSet()) - require.False(t, evaluation.IsStartTimeSet()) - - evaluation.SetStartTime() - evaluation.SetEndTime() - - require.True(t, evaluation.IsEndTimeSet()) - require.True(t, evaluation.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - common.EvaluationStatus.String(string(common.StateFailed)), - }, evaluation.GetMetricsAttributes()) - - evaluation.AddEvaluationStatus(Objective{KeptnMetricRef: KeptnMetricReference{Name: "objName"}}) - require.Equal(t, EvaluationStatusItem{ - Status: common.StatePending, - }, evaluation.Status.EvaluationStatus["objName"]) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "appversion", - "workloadName": "", - "workloadVersion": "", - "evaluationName": "evaluation", - "evaluationDefinitionName": "def", - }, evaluation.GetEventAnnotations()) -} - -func TestKeptnEvaluationList(t *testing.T) { - list := KeptnEvaluationList{ - Items: []KeptnEvaluation{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluation_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluation_types.go index 088a5fc7eb..e3edeaa57f 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluation_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluation_types.go @@ -17,19 +17,10 @@ limitations under the License. package v1alpha3 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationSpec defines the desired state of KeptnEvaluation type KeptnEvaluationSpec struct { // Workload defines the KeptnWorkload for which the KeptnEvaluation is done. @@ -139,104 +130,3 @@ type KeptnEvaluationList struct { func init() { SchemeBuilder.Register(&KeptnEvaluation{}, &KeptnEvaluationList{}) } - -func (e KeptnEvaluationList) GetItems() []client.Object { - var b []client.Object - for _, i := range e.Items { - b = append(b, &i) - } - return b -} - -func (e *KeptnEvaluation) SetStartTime() { - if e.Status.StartTime.IsZero() { - e.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) SetEndTime() { - if e.Status.EndTime.IsZero() { - e.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) IsStartTimeSet() bool { - return !e.Status.StartTime.IsZero() -} - -func (e *KeptnEvaluation) IsEndTimeSet() bool { - return !e.Status.EndTime.IsZero() -} - -func (e KeptnEvaluation) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e KeptnEvaluation) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - common.EvaluationStatus.String(string(e.Status.OverallStatus)), - } -} - -func (e *KeptnEvaluation) AddEvaluationStatus(objective Objective) { - - evaluationStatusItem := EvaluationStatusItem{ - Status: common.StatePending, - } - if e.Status.EvaluationStatus == nil { - e.Status.EvaluationStatus = make(map[string]EvaluationStatusItem) - } - e.Status.EvaluationStatus[objective.KeptnMetricRef.Name] = evaluationStatusItem - -} - -func (e KeptnEvaluation) SetSpanAttributes(span trace.Span) { - span.SetAttributes(e.GetSpanAttributes()...) -} - -func (e KeptnEvaluation) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e *KeptnEvaluation) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (e KeptnEvaluation) GetSpanKey(phase string) string { - return e.Name -} - -func (e KeptnEvaluation) GetSpanName(phase string) string { - return e.Name -} - -func (e KeptnEvaluation) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": e.Spec.AppName, - "appVersion": e.Spec.AppVersion, - "workloadName": e.Spec.Workload, - "workloadVersion": e.Spec.WorkloadVersion, - "evaluationName": e.Name, - "evaluationDefinitionName": e.Spec.EvaluationDefinition, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationdefinition_types.go index 6bef568974..efae983f16 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationdefinition_types.go @@ -20,9 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationDefinitionSpec defines the desired state of KeptnEvaluationDefinition type KeptnEvaluationDefinitionSpec struct { // Objectives is a list of objectives that have to be met for a KeptnEvaluation referencing this diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationprovider_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationprovider_types.go index b8dde12a62..05ff216216 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationprovider_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnevaluationprovider_types.go @@ -21,9 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnEvaluationProviderSpec defines the desired state of KeptnEvaluationProvider type KeptnEvaluationProviderSpec struct { TargetServer string `json:"targetServer"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntask_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntask_test.go deleted file mode 100644 index 23e17c9d7b..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntask_test.go +++ /dev/null @@ -1,156 +0,0 @@ -package v1alpha3 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnTask(t *testing.T) { - task := &KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: KeptnTaskSpec{ - Context: TaskContext{ - AppName: "app", - AppVersion: "appversion", - }, - Type: common.PostDeploymentCheckType, - TaskDefinition: "def", - Timeout: metav1.Duration{ - Duration: time.Duration(5 * time.Minute), - }, - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - } - - task.SetPhaseTraceID("", nil) - require.Equal(t, KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: KeptnTaskSpec{ - Context: TaskContext{ - AppName: "app", - AppVersion: "appversion", - }, - Type: common.PostDeploymentCheckType, - TaskDefinition: "def", - Timeout: metav1.Duration{ - Duration: time.Duration(5 * time.Minute), - }, - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - }, *task) - - require.Equal(t, "task", task.GetSpanKey("")) - require.Equal(t, "task", task.GetSpanName("")) - - require.False(t, task.IsEndTimeSet()) - require.False(t, task.IsStartTimeSet()) - - task.SetStartTime() - task.SetEndTime() - - require.True(t, task.IsEndTimeSet()) - require.True(t, task.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - common.TaskStatus.String(string(common.StateFailed)), - }, task.GetMetricsAttributes()) - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/task-name": "task", - "keptn.sh/version": "appversion", - "annotation1": "annotation2", - }, task.CreateKeptnAnnotations()) - - task.Spec.Context.WorkloadName = "workload" - task.Spec.Context.WorkloadVersion = "workloadversion" - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/workload": "workload", - "keptn.sh/task-name": "task", - "keptn.sh/version": "workloadversion", - "annotation1": "annotation2", - }, task.CreateKeptnAnnotations()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("workloadversion"), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "appversion", - "workloadName": "workload", - "workloadVersion": "workloadversion", - "taskName": "task", - "taskDefinitionName": "def", - }, task.GetEventAnnotations()) - - require.Equal(t, int64(300), *task.GetActiveDeadlineSeconds()) - -} - -func TestKeptnTaskList(t *testing.T) { - list := KeptnTaskList{ - Items: []KeptnTask{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntask_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntask_types.go index 7c15952901..90f071c09b 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntask_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntask_types.go @@ -17,19 +17,10 @@ limitations under the License. package v1alpha3 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnTaskSpec defines the desired state of KeptnTask type KeptnTaskSpec struct { // TaskDefinition refers to the name of the KeptnTaskDefinition @@ -164,114 +155,3 @@ type KeptnTaskList struct { func init() { SchemeBuilder.Register(&KeptnTask{}, &KeptnTaskList{}) } - -func (t KeptnTaskList) GetItems() []client.Object { - var b []client.Object - for _, i := range t.Items { - b = append(b, &i) - } - return b -} - -func (t *KeptnTask) SetStartTime() { - if t.Status.StartTime.IsZero() { - t.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) SetEndTime() { - if t.Status.EndTime.IsZero() { - t.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) IsStartTimeSet() bool { - return !t.Status.StartTime.IsZero() -} - -func (t *KeptnTask) IsEndTimeSet() bool { - return !t.Status.EndTime.IsZero() -} - -func (t KeptnTask) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.Context.AppName), - common.AppVersion.String(t.Spec.Context.AppVersion), - common.WorkloadName.String(t.Spec.Context.WorkloadName), - common.WorkloadVersion.String(t.Spec.Context.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t KeptnTask) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.Context.AppName), - common.AppVersion.String(t.Spec.Context.AppVersion), - common.WorkloadName.String(t.Spec.Context.WorkloadName), - common.WorkloadVersion.String(t.Spec.Context.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - common.TaskStatus.String(string(t.Status.Status)), - } -} - -func (t KeptnTask) SetSpanAttributes(span trace.Span) { - span.SetAttributes(t.GetSpanAttributes()...) -} - -func (t KeptnTask) CreateKeptnAnnotations() map[string]string { - if t.Spec.Context.WorkloadName != "" { - return common.MergeMaps(t.Annotations, map[string]string{ - common.AppAnnotation: t.Spec.Context.AppName, - common.WorkloadAnnotation: t.Spec.Context.WorkloadName, - common.VersionAnnotation: t.Spec.Context.WorkloadVersion, - common.TaskNameAnnotation: t.Name, - }) - } - return common.MergeMaps(t.Annotations, map[string]string{ - common.AppAnnotation: t.Spec.Context.AppName, - common.VersionAnnotation: t.Spec.Context.AppVersion, - common.TaskNameAnnotation: t.Name, - }) -} - -func (t KeptnTask) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.Context.AppName), - common.AppVersion.String(t.Spec.Context.AppVersion), - common.WorkloadName.String(t.Spec.Context.WorkloadName), - common.WorkloadVersion.String(t.Spec.Context.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t *KeptnTask) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (t KeptnTask) GetSpanKey(phase string) string { - return t.Name -} - -func (t KeptnTask) GetSpanName(phase string) string { - return t.Name -} - -func (t KeptnTask) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": t.Spec.Context.AppName, - "appVersion": t.Spec.Context.AppVersion, - "workloadName": t.Spec.Context.WorkloadName, - "workloadVersion": t.Spec.Context.WorkloadVersion, - "taskName": t.Name, - "taskDefinitionName": t.Spec.TaskDefinition, - } -} - -func (t KeptnTask) GetActiveDeadlineSeconds() *int64 { - deadline, _ := time.ParseDuration(t.Spec.Timeout.Duration.String()) - seconds := int64(deadline.Seconds()) - return &seconds -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go index 42c3a97720..51bfd7acdc 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go @@ -21,9 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnTaskDefinitionSpec defines the desired state of KeptnTaskDefinition type KeptnTaskDefinitionSpec struct { // Deprecated @@ -177,17 +174,3 @@ type KeptnTaskDefinitionList struct { func init() { SchemeBuilder.Register(&KeptnTaskDefinition{}, &KeptnTaskDefinitionList{}) } - -func (d *KeptnTaskDefinition) GetServiceAccount() string { - if d.Spec.ServiceAccount == nil { - return "" - } - return d.Spec.ServiceAccount.Name -} - -func (d *KeptnTaskDefinition) GetAutomountServiceAccountToken() *bool { - if d.Spec.AutomountServiceAccountToken == nil { - return nil - } - return d.Spec.AutomountServiceAccountToken.Type -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types_test.go deleted file mode 100644 index 4d92cbad95..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestTaskDefinition_GetServiceAccountNoName(t *testing.T) { - d := &KeptnTaskDefinition{ - Spec: KeptnTaskDefinitionSpec{}, - } - svcAccname := d.GetServiceAccount() - require.Equal(t, svcAccname, "") -} - -func TestTaskDefinition_GetServiceAccountName(t *testing.T) { - sAName := "sva" - d := &KeptnTaskDefinition{ - Spec: KeptnTaskDefinitionSpec{ - ServiceAccount: &ServiceAccountSpec{ - Name: sAName, - }, - }, - } - svcAccname := d.GetServiceAccount() - require.Equal(t, svcAccname, sAName) -} - -func TestTaskDefinition_GetAutomountServiceAccountToken(t *testing.T) { - token := true - d := &KeptnTaskDefinition{ - Spec: KeptnTaskDefinitionSpec{ - AutomountServiceAccountToken: &AutomountServiceAccountTokenSpec{ - Type: &token, - }, - }, - } - require.True(t, *d.GetAutomountServiceAccountToken()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_test.go deleted file mode 100644 index 4214505c77..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnWorkload(t *testing.T) { - workload := &KeptnWorkload{ - ObjectMeta: metav1.ObjectMeta{ - Name: "workload", - Namespace: "namespace", - }, - Spec: KeptnWorkloadSpec{ - Version: "version", - AppName: "app", - }, - } - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("version"), - }, workload.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "workloadName": "workload", - "workloadVersion": "version", - }, workload.GetEventAnnotations()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_types.go index 9b96d0e6cd..bbefa62409 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_types.go @@ -17,19 +17,10 @@ limitations under the License. package v1alpha3 import ( - "fmt" - "strings" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnWorkloadSpec defines the desired state of KeptnWorkload type KeptnWorkloadSpec struct { // AppName is the name of the KeptnApp containing the KeptnWorkload. @@ -109,27 +100,3 @@ type ResourceReference struct { func init() { SchemeBuilder.Register(&KeptnWorkload{}, &KeptnWorkloadList{}) } - -func (w KeptnWorkload) SetSpanAttributes(span trace.Span) { - span.SetAttributes(w.GetSpanAttributes()...) -} - -func (w KeptnWorkload) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Name), - common.WorkloadVersion.String(w.Spec.Version), - } -} - -func (w KeptnWorkload) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": w.Spec.AppName, - "workloadName": w.Name, - "workloadVersion": w.Spec.Version, - } -} - -func (w KeptnWorkload) GetNameWithoutAppPrefix() string { - return strings.TrimPrefix(w.Name, fmt.Sprintf("%s-", w.Spec.AppName)) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_types_test.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_types_test.go deleted file mode 100644 index a114e026ef..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkload_types_test.go +++ /dev/null @@ -1,44 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/stretchr/testify/require" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnWorkload_GetNameWithoutAppPrefix(t *testing.T) { - type fields struct { - ObjectMeta v1.ObjectMeta - Spec KeptnWorkloadSpec - } - tests := []struct { - name string - fields fields - want string - }{ - { - name: "remove app prefix", - fields: fields{ - ObjectMeta: v1.ObjectMeta{ - Name: "my-app-my-workload", - }, - Spec: KeptnWorkloadSpec{ - AppName: "my-app", - }, - }, - want: "my-workload", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - w := KeptnWorkload{ - ObjectMeta: tt.fields.ObjectMeta, - Spec: tt.fields.Spec, - } - got := w.GetNameWithoutAppPrefix() - - require.Equal(t, tt.want, got) - }) - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go index 72e1005ec7..94c9662334 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha3/keptnworkloadinstance_types.go @@ -21,9 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnWorkloadInstanceSpec defines the desired state of KeptnWorkloadInstance type KeptnWorkloadInstanceSpec struct { KeptnWorkloadSpec `json:",inline"` diff --git a/lifecycle-operator/apis/lifecycle/v1alpha3/semconv/semconv.go b/lifecycle-operator/apis/lifecycle/v1alpha3/semconv/semconv.go deleted file mode 100644 index 547b3bf217..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha3/semconv/semconv.go +++ /dev/null @@ -1,12 +0,0 @@ -package semconv - -import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/trace" -) - -func AddAttributeFromAnnotations(s trace.Span, annotations map[string]string) { - s.SetAttributes(common.AppName.String(annotations[common.AppAnnotation])) - s.SetAttributes(common.WorkloadName.String(annotations[common.WorkloadAnnotation])) - s.SetAttributes(common.WorkloadVersion.String(annotations[common.VersionAnnotation])) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha4/keptnworkloadversion_types.go b/lifecycle-operator/apis/lifecycle/v1alpha4/keptnworkloadversion_types.go index 6da9506bd8..919e41c881 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha4/keptnworkloadversion_types.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha4/keptnworkloadversion_types.go @@ -17,16 +17,9 @@ limitations under the License. package v1alpha4 import ( - "fmt" - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) // KeptnWorkloadVersionSpec defines the desired state of KeptnWorkloadVersion @@ -139,326 +132,3 @@ type KeptnWorkloadVersionList struct { func init() { SchemeBuilder.Register(&KeptnWorkloadVersion{}, &KeptnWorkloadVersionList{}) } - -func (w KeptnWorkloadVersionList) GetItems() []client.Object { - var b []client.Object - for i := 0; i < len(w.Items); i++ { - b = append(b, &w.Items[i]) - } - return b -} - -func (w KeptnWorkloadVersion) IsPreDeploymentCompleted() bool { - return w.Status.PreDeploymentStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentEvaluationCompleted() bool { - return w.Status.PreDeploymentEvaluationStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentSucceeded() bool { - return w.Status.PreDeploymentStatus.IsSucceeded() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentFailed() bool { - return w.Status.PreDeploymentStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentEvaluationSucceeded() bool { - return w.Status.PreDeploymentEvaluationStatus.IsSucceeded() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentEvaluationFailed() bool { - return w.Status.PreDeploymentEvaluationStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentCompleted() bool { - return w.Status.PostDeploymentStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentEvaluationCompleted() bool { - return w.Status.PostDeploymentEvaluationStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentSucceeded() bool { - return w.Status.PostDeploymentStatus.IsSucceeded() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentFailed() bool { - return w.Status.PostDeploymentStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentEvaluationSucceeded() bool { - return w.Status.PostDeploymentEvaluationStatus.IsSucceeded() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentEvaluationFailed() bool { - return w.Status.PostDeploymentEvaluationStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsDeploymentCompleted() bool { - return w.Status.DeploymentStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsDeploymentSucceeded() bool { - return w.Status.DeploymentStatus.IsSucceeded() -} - -func (w KeptnWorkloadVersion) IsDeploymentFailed() bool { - return w.Status.DeploymentStatus.IsFailed() -} - -func (w *KeptnWorkloadVersion) SetStartTime() { - if w.Status.StartTime.IsZero() { - w.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (w *KeptnWorkloadVersion) SetEndTime() { - if w.Status.EndTime.IsZero() { - w.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (w *KeptnWorkloadVersion) IsStartTimeSet() bool { - return !w.Status.StartTime.IsZero() -} - -func (w *KeptnWorkloadVersion) IsEndTimeSet() bool { - return !w.Status.EndTime.IsZero() -} - -func (w KeptnWorkloadVersion) GetStartTime() time.Time { - return w.Status.StartTime.Time -} - -func (w KeptnWorkloadVersion) GetEndTime() time.Time { - return w.Status.EndTime.Time -} - -func (w *KeptnWorkloadVersion) Complete() { - w.SetEndTime() -} - -func (w KeptnWorkloadVersion) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadNamespace.String(w.Namespace), - } -} - -func (w KeptnWorkloadVersion) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadNamespace.String(w.Namespace), - common.WorkloadStatus.String(string(w.Status.Status)), - } -} - -func (w KeptnWorkloadVersion) GetDurationMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadPreviousVersion.String(w.Spec.PreviousVersion), - } -} - -func (w KeptnWorkloadVersion) GetState() common.KeptnState { - return w.Status.Status -} - -func (w KeptnWorkloadVersion) GetPreDeploymentTasks() []string { - return w.Spec.PreDeploymentTasks -} - -func (w KeptnWorkloadVersion) GetPostDeploymentTasks() []string { - return w.Spec.PostDeploymentTasks -} - -func (w KeptnWorkloadVersion) GetPreDeploymentTaskStatus() []v1alpha3.ItemStatus { - return w.Status.PreDeploymentTaskStatus -} - -func (w KeptnWorkloadVersion) GetPostDeploymentTaskStatus() []v1alpha3.ItemStatus { - return w.Status.PostDeploymentTaskStatus -} - -func (w KeptnWorkloadVersion) GetPreDeploymentEvaluations() []string { - return w.Spec.PreDeploymentEvaluations -} - -func (w KeptnWorkloadVersion) GetPostDeploymentEvaluations() []string { - return w.Spec.PostDeploymentEvaluations -} - -func (w KeptnWorkloadVersion) GetPreDeploymentEvaluationTaskStatus() []v1alpha3.ItemStatus { - return w.Status.PreDeploymentEvaluationTaskStatus -} - -func (w KeptnWorkloadVersion) GetPostDeploymentEvaluationTaskStatus() []v1alpha3.ItemStatus { - return w.Status.PostDeploymentEvaluationTaskStatus -} - -func (w KeptnWorkloadVersion) GetAppName() string { - return w.Spec.AppName -} - -func (w KeptnWorkloadVersion) GetPreviousVersion() string { - return w.Spec.PreviousVersion -} - -func (w KeptnWorkloadVersion) GetParentName() string { - return w.Spec.WorkloadName -} - -func (w KeptnWorkloadVersion) GetNamespace() string { - return w.Namespace -} - -func (w *KeptnWorkloadVersion) SetState(state common.KeptnState) { - w.Status.Status = state -} - -func (w KeptnWorkloadVersion) GetCurrentPhase() string { - return w.Status.CurrentPhase -} - -func (w *KeptnWorkloadVersion) SetCurrentPhase(phase string) { - w.Status.CurrentPhase = phase -} - -func (w KeptnWorkloadVersion) GetVersion() string { - return w.Spec.Version -} - -func (w KeptnWorkloadVersion) GenerateTask(taskDefinition v1alpha3.KeptnTaskDefinition, checkType common.CheckType) v1alpha3.KeptnTask { - return v1alpha3.KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition.Name), - Namespace: w.Namespace, - Labels: taskDefinition.Labels, - Annotations: taskDefinition.Annotations, - }, - Spec: v1alpha3.KeptnTaskSpec{ - Context: v1alpha3.TaskContext{ - WorkloadName: w.GetParentName(), - AppName: w.GetAppName(), - WorkloadVersion: w.GetVersion(), - TaskType: string(checkType), - ObjectType: "Workload", - }, - TaskDefinition: taskDefinition.Name, - Parameters: v1alpha3.TaskParameters{}, - SecureParameters: v1alpha3.SecureParameters{}, - Type: checkType, - Retries: taskDefinition.Spec.Retries, - Timeout: taskDefinition.Spec.Timeout, - }, - } -} - -func (w KeptnWorkloadVersion) GenerateEvaluation(evaluationDefinition v1alpha3.KeptnEvaluationDefinition, checkType common.CheckType) v1alpha3.KeptnEvaluation { - return v1alpha3.KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition.Name), - Namespace: w.Namespace, - }, - Spec: v1alpha3.KeptnEvaluationSpec{ - AppName: w.GetAppName(), - WorkloadVersion: w.GetVersion(), - Workload: w.GetParentName(), - EvaluationDefinition: evaluationDefinition.Name, - Type: checkType, - RetryInterval: metav1.Duration{ - Duration: 5 * time.Second, - }, - }, - } -} - -func (w KeptnWorkloadVersion) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadNamespace.String(w.Namespace), - } -} - -func (w KeptnWorkloadVersion) GetSpanKey(phase string) string { - return fmt.Sprintf("%s.%s.%s.%s", w.Spec.TraceId["traceparent"], w.Spec.WorkloadName, w.Spec.Version, phase) -} - -func (w KeptnWorkloadVersion) GetSpanName(phase string) string { - if phase == "" { - return w.Name - } - return fmt.Sprintf("%s/%s", w.Spec.WorkloadName, phase) -} - -func (w KeptnWorkloadVersion) SetSpanAttributes(span trace.Span) { - span.SetAttributes(w.GetSpanAttributes()...) -} - -//nolint:dupl -func (w *KeptnWorkloadVersion) DeprecateRemainingPhases(phase common.KeptnPhaseType) { - // no need to deprecate anything when post-eval tasks fail - if phase == common.PhaseWorkloadPostEvaluation { - return - } - // deprecate post evaluation when post tasks failed - if phase == common.PhaseWorkloadPostDeployment { - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate post evaluation and tasks when app deployment failed - if phase == common.PhaseWorkloadDeployment { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate app deployment, post tasks and evaluations if app pre-eval failed - if phase == common.PhaseWorkloadPreEvaluation { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - w.Status.DeploymentStatus = common.StateDeprecated - } - // deprecate pre evaluations, app deployment and post tasks and evaluations when pre-tasks failed - if phase == common.PhaseWorkloadPreDeployment { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - w.Status.DeploymentStatus = common.StateDeprecated - w.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate completely everything - if phase == common.PhaseDeprecated { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - w.Status.DeploymentStatus = common.StateDeprecated - w.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - w.Status.PreDeploymentStatus = common.StateDeprecated - w.Status.Status = common.StateDeprecated - return - } - - w.Status.Status = common.StateFailed -} - -func (w *KeptnWorkloadVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - if w.Status.PhaseTraceIDs == nil { - w.Status.PhaseTraceIDs = common.PhaseTraceID{} - } - w.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier -} - -func (w KeptnWorkloadVersion) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": w.Spec.AppName, - "workloadName": w.Spec.WorkloadName, - "workloadVersion": w.Spec.Version, - "workloadVersionName": w.Name, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha4/keptnworkloadversion_types_test.go b/lifecycle-operator/apis/lifecycle/v1alpha4/keptnworkloadversion_types_test.go deleted file mode 100644 index f7b8237cb3..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1alpha4/keptnworkloadversion_types_test.go +++ /dev/null @@ -1,448 +0,0 @@ -package v1alpha4 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnWorkloadVersion(t *testing.T) { - workload := &KeptnWorkloadVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: "workload", - Namespace: "namespace", - }, - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - DeploymentStatus: common.StateFailed, - Status: common.StateFailed, - PreDeploymentTaskStatus: []v1alpha3.ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, - PostDeploymentTaskStatus: []v1alpha3.ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, - PreDeploymentEvaluationTaskStatus: []v1alpha3.ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, - PostDeploymentEvaluationTaskStatus: []v1alpha3.ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, - CurrentPhase: common.PhaseAppDeployment.ShortName, - }, - Spec: KeptnWorkloadVersionSpec{ - KeptnWorkloadSpec: v1alpha3.KeptnWorkloadSpec{ - PreDeploymentTasks: []string{"task1", "task2"}, - PostDeploymentTasks: []string{"task3", "task4"}, - PreDeploymentEvaluations: []string{"task5", "task6"}, - PostDeploymentEvaluations: []string{"task7", "task8"}, - Version: "version", - AppName: "appname", - }, - PreviousVersion: "prev", - WorkloadName: "workloadname", - TraceId: map[string]string{"traceparent": "trace1"}, - }, - } - - require.True(t, workload.IsPreDeploymentCompleted()) - require.False(t, workload.IsPreDeploymentSucceeded()) - require.True(t, workload.IsPreDeploymentFailed()) - - require.True(t, workload.IsPreDeploymentEvaluationCompleted()) - require.False(t, workload.IsPreDeploymentEvaluationSucceeded()) - require.True(t, workload.IsPreDeploymentEvaluationFailed()) - - require.True(t, workload.IsPostDeploymentCompleted()) - require.False(t, workload.IsPostDeploymentSucceeded()) - require.True(t, workload.IsPostDeploymentFailed()) - - require.True(t, workload.IsPostDeploymentEvaluationCompleted()) - require.False(t, workload.IsPostDeploymentEvaluationSucceeded()) - require.True(t, workload.IsPostDeploymentEvaluationFailed()) - - require.True(t, workload.IsDeploymentCompleted()) - require.False(t, workload.IsDeploymentSucceeded()) - require.True(t, workload.IsDeploymentFailed()) - - require.False(t, workload.IsEndTimeSet()) - require.False(t, workload.IsStartTimeSet()) - - workload.SetStartTime() - workload.SetEndTime() - - require.True(t, workload.IsEndTimeSet()) - require.True(t, workload.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadVersion.String("version"), - common.WorkloadNamespace.String("namespace"), - }, workload.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadVersion.String("version"), - common.WorkloadNamespace.String("namespace"), - common.WorkloadStatus.String(string(common.StateFailed)), - }, workload.GetMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadVersion.String("version"), - common.WorkloadPreviousVersion.String("prev"), - }, workload.GetDurationMetricsAttributes()) - - require.Equal(t, common.StateFailed, workload.GetState()) - - require.Equal(t, []string{"task1", "task2"}, workload.GetPreDeploymentTasks()) - require.Equal(t, []string{"task3", "task4"}, workload.GetPostDeploymentTasks()) - require.Equal(t, []string{"task5", "task6"}, workload.GetPreDeploymentEvaluations()) - require.Equal(t, []string{"task7", "task8"}, workload.GetPostDeploymentEvaluations()) - - require.Equal(t, []v1alpha3.ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, workload.GetPreDeploymentTaskStatus()) - - require.Equal(t, []v1alpha3.ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, workload.GetPostDeploymentTaskStatus()) - - require.Equal(t, []v1alpha3.ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, workload.GetPreDeploymentEvaluationTaskStatus()) - - require.Equal(t, []v1alpha3.ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, workload.GetPostDeploymentEvaluationTaskStatus()) - - require.Equal(t, "appname", workload.GetAppName()) - require.Equal(t, "prev", workload.GetPreviousVersion()) - require.Equal(t, "workloadname", workload.GetParentName()) - require.Equal(t, "namespace", workload.GetNamespace()) - - workload.SetState(common.StatePending) - require.Equal(t, common.StatePending, workload.GetState()) - - require.True(t, !workload.GetStartTime().IsZero()) - require.True(t, !workload.GetEndTime().IsZero()) - - workload.SetCurrentPhase(common.PhaseAppDeployment.LongName) - require.Equal(t, common.PhaseAppDeployment.LongName, workload.GetCurrentPhase()) - - workload.Status.EndTime = v1.Time{Time: time.Time{}} - workload.Complete() - require.True(t, !workload.GetEndTime().IsZero()) - - require.Equal(t, "version", workload.GetVersion()) - - require.Equal(t, "trace1.workloadname.version.phase", workload.GetSpanKey("phase")) - - retries := int32(5) - task := workload.GenerateTask(v1alpha3.KeptnTaskDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "task-def", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: v1alpha3.KeptnTaskDefinitionSpec{ - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, - }, common.PostDeploymentCheckType) - require.Equal(t, v1alpha3.KeptnTaskSpec{ - Context: v1alpha3.TaskContext{ - AppName: workload.GetAppName(), - WorkloadVersion: workload.GetVersion(), - WorkloadName: workload.GetParentName(), - TaskType: string(common.PostDeploymentCheckType), - ObjectType: "Workload", - }, - TaskDefinition: "task-def", - Parameters: v1alpha3.TaskParameters{}, - SecureParameters: v1alpha3.SecureParameters{}, - Type: common.PostDeploymentCheckType, - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, task.Spec) - - require.Equal(t, map[string]string{ - "label1": "label2", - }, task.Labels) - - require.Equal(t, map[string]string{ - "annotation1": "annotation2", - }, task.Annotations) - - evaluation := workload.GenerateEvaluation(v1alpha3.KeptnEvaluationDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "eval-def", - }, - }, common.PostDeploymentCheckType) - require.Equal(t, v1alpha3.KeptnEvaluationSpec{ - AppName: workload.GetAppName(), - WorkloadVersion: workload.GetVersion(), - Workload: workload.GetParentName(), - EvaluationDefinition: "eval-def", - Type: common.PostDeploymentCheckType, - RetryInterval: metav1.Duration{ - Duration: 5 * time.Second, - }, - }, evaluation.Spec) - - require.Equal(t, "workload", workload.GetSpanName("")) - - require.Equal(t, "workloadname/phase", workload.GetSpanName("phase")) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadVersion.String("version"), - common.WorkloadNamespace.String("namespace"), - }, workload.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "appname", - "workloadName": "workloadname", - "workloadVersion": "version", - "workloadVersionName": "workload", - }, workload.GetEventAnnotations()) -} - -//nolint:dupl -func TestKeptnWorkloadVersion_DeprecateRemainingPhases(t *testing.T) { - workloadVersion := KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - DeploymentStatus: common.StatePending, - Status: common.StatePending, - }, - } - - tests := []struct { - workloadVersion KeptnWorkloadVersion - phase common.KeptnPhaseType - want KeptnWorkloadVersion - }{ - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPostEvaluation, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - DeploymentStatus: common.StatePending, - Status: common.StatePending, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPostDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPreEvaluation, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPreDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseDeprecated, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StateDeprecated, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StateDeprecated, - Status: common.StateDeprecated, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseAppPreDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - DeploymentStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - } - - for _, tt := range tests { - t.Run("", func(t *testing.T) { - tt.workloadVersion.DeprecateRemainingPhases(tt.phase) - require.Equal(t, tt.want, tt.workloadVersion) - }) - } -} - -func TestKeptnWorkloadVersion_SetPhaseTraceID(t *testing.T) { - app := KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{}, - } - - app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, - }, - }, app) - - app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{ - "name2": "trace2", - }) - - require.Equal(t, KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name2": "trace2", - }, - }, - }, - }, app) -} - -func TestKeptnWorkloadVersionList(t *testing.T) { - list := KeptnWorkloadVersionList{ - Items: []KeptnWorkloadVersion{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) - require.Equal(t, "obj1", got[0].GetName()) - require.Equal(t, "obj2", got[1].GetName()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1alpha4/zz_generated.deepcopy.go b/lifecycle-operator/apis/lifecycle/v1alpha4/zz_generated.deepcopy.go index 20ba8749ea..1532fc3230 100644 --- a/lifecycle-operator/apis/lifecycle/v1alpha4/zz_generated.deepcopy.go +++ b/lifecycle-operator/apis/lifecycle/v1alpha4/zz_generated.deepcopy.go @@ -23,7 +23,7 @@ package v1alpha4 import ( "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" - "go.opentelemetry.io/otel/propagation" + propagation "go.opentelemetry.io/otel/propagation" runtime "k8s.io/apimachinery/pkg/runtime" ) diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/common/common.go b/lifecycle-operator/apis/lifecycle/v1beta1/common/common.go index 5c91b6ca12..b7a6189925 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/common/common.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/common/common.go @@ -4,49 +4,9 @@ package common import ( - "crypto/sha256" - "encoding/hex" "errors" - "math/rand" - "strconv" - operatorcommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -const WorkloadAnnotation = "keptn.sh/workload" -const VersionAnnotation = "keptn.sh/version" -const AppAnnotation = "keptn.sh/app" -const PreDeploymentTaskAnnotation = "keptn.sh/pre-deployment-tasks" -const PostDeploymentTaskAnnotation = "keptn.sh/post-deployment-tasks" -const K8sRecommendedWorkloadAnnotations = "app.kubernetes.io/name" -const K8sRecommendedVersionAnnotations = "app.kubernetes.io/version" -const K8sRecommendedAppAnnotations = "app.kubernetes.io/part-of" -const K8sRecommendedManagedByAnnotations = "app.kubernetes.io/managed-by" -const PreDeploymentEvaluationAnnotation = "keptn.sh/pre-deployment-evaluations" -const PostDeploymentEvaluationAnnotation = "keptn.sh/post-deployment-evaluations" -const SchedulingGateRemoved = "keptn.sh/scheduling-gate-removed" -const TaskNameAnnotation = "keptn.sh/task-name" -const NamespaceEnabledAnnotation = "keptn.sh/lifecycle-toolkit" -const CreateAppTaskSpanName = "create_%s_app_task" -const CreateWorkloadTaskSpanName = "create_%s_deployment_task" -const CreateAppEvalSpanName = "create_%s_app_evaluation" -const CreateWorkloadEvalSpanName = "create_%s_deployment_evaluation" -const AppTypeAnnotation = "keptn.sh/app-type" -const KeptnGate = "keptn-prechecks-gate" -const ContainerNameAnnotation = "keptn.sh/container" -const MetadataAnnotation = "keptn.sh/metadata" - -const MinKeptnNameLen = 80 -const MaxK8sObjectLength = 253 - -type AppType string - -const ( - AppTypeSingleService AppType = "single-service" - AppTypeMultiService AppType = "multi-service" + "go.opentelemetry.io/otel/propagation" ) var ErrCannotCastKeptnAppVersion = errors.New("cannot cast KeptnAppVersion to v1") @@ -55,181 +15,7 @@ var ErrCannotCastKeptnApp = errors.New("cannot cast KeptnApp to v1") // KeptnState is a string containing current Phase state (Progressing/Succeeded/Failed/Unknown/Pending/Deprecated/Warning) type KeptnState string -const ( - StateProgressing KeptnState = "Progressing" - StateSucceeded KeptnState = "Succeeded" - StateFailed KeptnState = "Failed" - StateUnknown KeptnState = "Unknown" - StatePending KeptnState = "Pending" - StateDeprecated KeptnState = "Deprecated" - StateWarning KeptnState = "Warning" -) - -func (k KeptnState) IsCompleted() bool { - return k == StateSucceeded || k == StateFailed || k == StateDeprecated || k == StateWarning -} - -func (k KeptnState) IsSucceeded() bool { - return k == StateSucceeded -} - -func (k KeptnState) IsFailed() bool { - return k == StateFailed -} - -func (k KeptnState) IsDeprecated() bool { - return k == StateDeprecated -} - -func (k KeptnState) IsPending() bool { - return k == StatePending -} - -func (k KeptnState) IsWarning() bool { - return k == StateWarning -} - -type StatusSummary struct { - Total int - Progressing int - Failed int - Succeeded int - Pending int - Unknown int - Deprecated int -} - -func UpdateStatusSummary(status KeptnState, summary StatusSummary) StatusSummary { - switch status { - case StateFailed: - summary.Failed++ - case StateDeprecated: - summary.Deprecated++ - case StateSucceeded: - summary.Succeeded++ - case StateProgressing: - summary.Progressing++ - case StatePending, "": - summary.Pending++ - case StateUnknown: - summary.Unknown++ - } - return summary -} - -func (s StatusSummary) GetTotalCount() int { - return s.Failed + s.Succeeded + s.Progressing + s.Pending + s.Unknown + s.Deprecated -} - -func GetOverallState(s StatusSummary) KeptnState { - if s.Failed > 0 || s.Deprecated > 0 { - return StateFailed - } - if s.Progressing > 0 { - return StateProgressing - } - if s.Pending > 0 { - return StatePending - } - if s.Unknown > 0 { - return StateUnknown - } - if s.GetTotalCount() != s.Total { - return StatePending - } - return StateSucceeded -} - -func GetOverallStateBlockedDeployment(s StatusSummary, blockedDeployment bool) KeptnState { - state := GetOverallState(s) - if !blockedDeployment && state == StateFailed { - return StateWarning - } - return state -} - -func TruncateString(s string, max int) string { - if len(s) > max { - return s[:max] - } - return s -} - -func Hash(num int64) string { - // generate the SHA-256 hash of the bytes - hash := sha256.Sum256([]byte(strconv.FormatInt(num, 10))) - // take the first 4 bytes of the hash and convert to hex - return hex.EncodeToString(hash[:4]) -} - type CheckType string -const PreDeploymentCheckType CheckType = "pre" -const PostDeploymentCheckType CheckType = "post" -const PromotionCheckType CheckType = "promotion" -const PreDeploymentEvaluationCheckType CheckType = "pre-eval" -const PostDeploymentEvaluationCheckType CheckType = "post-eval" - -type KeptnMeters struct { - TaskCount metric.Int64Counter - TaskDuration metric.Float64Histogram - DeploymentCount metric.Int64Counter - DeploymentDuration metric.Float64Histogram - AppCount metric.Int64Counter - AppDuration metric.Float64Histogram - EvaluationCount metric.Int64Counter - EvaluationDuration metric.Float64Histogram - PromotionCount metric.Int64Counter -} - -const ( - AppName attribute.Key = attribute.Key("keptn.deployment.app.name") - AppVersion attribute.Key = attribute.Key("keptn.deployment.app.version") - AppNamespace attribute.Key = attribute.Key("keptn.deployment.app.namespace") - AppStatus attribute.Key = attribute.Key("keptn.deployment.app.status") - AppPreviousVersion attribute.Key = attribute.Key("keptn.deployment.app.previousversion") - WorkloadName attribute.Key = attribute.Key("keptn.deployment.workload.name") - WorkloadVersion attribute.Key = attribute.Key("keptn.deployment.workload.version") - WorkloadPreviousVersion attribute.Key = attribute.Key("keptn.deployment.workload.previousversion") - WorkloadNamespace attribute.Key = attribute.Key("keptn.deployment.workload.namespace") - WorkloadStatus attribute.Key = attribute.Key("keptn.deployment.workload.status") - TaskStatus attribute.Key = attribute.Key("keptn.deployment.task.status") - TaskName attribute.Key = attribute.Key("keptn.deployment.task.name") - TaskType attribute.Key = attribute.Key("keptn.deployment.task.type") - EvaluationStatus attribute.Key = attribute.Key("keptn.deployment.evaluation.status") - EvaluationName attribute.Key = attribute.Key("keptn.deployment.evaluation.name") - EvaluationType attribute.Key = attribute.Key("keptn.deployment.evaluation.type") -) - -func GenerateTaskName(checkType CheckType, taskName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return operatorcommon.CreateResourceName(MaxK8sObjectLength, MinKeptnNameLen, string(checkType), taskName, strconv.Itoa(randomId)) -} - -func GenerateJobName(taskName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return operatorcommon.CreateResourceName(MaxK8sObjectLength, MinKeptnNameLen, taskName, strconv.Itoa(randomId)) -} - -func GenerateEvaluationName(checkType CheckType, evalName string) string { - randomId := rand.Intn(99_999-10_000) + 10000 - return operatorcommon.CreateResourceName(MaxK8sObjectLength, MinKeptnNameLen, string(checkType), evalName, strconv.Itoa(randomId)) -} - -// MergeMaps merges two maps into a new map. If a key exists in both maps, the -// value of the second map is picked. -func MergeMaps(m1 map[string]string, m2 map[string]string) map[string]string { - merged := make(map[string]string, len(m1)+len(m2)) - for key, value := range m1 { - merged[key] = value - } - for key, value := range m2 { - merged[key] = value - } - return merged -} - -// IsOwnerSupported returns whether the owner of the given object is supported to be considered a KeptnWorkload -func IsOwnerSupported(owner metav1.OwnerReference) bool { - return owner.Kind == "ReplicaSet" || owner.Kind == "Deployment" || owner.Kind == "StatefulSet" || owner.Kind == "DaemonSet" || owner.Kind == "Rollout" -} +// PhaseTraceID is a map storing TraceIDs of OpenTelemetry spans in lifecycle phases +type PhaseTraceID map[string]propagation.MapCarrier diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/common/common_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/common/common_test.go deleted file mode 100644 index 49ef0a99a8..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/common/common_test.go +++ /dev/null @@ -1,552 +0,0 @@ -package common - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/require" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -const ExtraLongName = "loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo01234567891234567890123456789" - -func TestKeptnState_IsCompleted(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - { - State: StateSucceeded, - Want: true, - }, - { - State: StateDeprecated, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.Want, tt.State.IsCompleted()) - }) - } -} - -func TestKeptnState_IsWarning(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateFailed, - Want: false, - }, - { - State: StateWarning, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsWarning(), tt.Want) - }) - } -} - -func TestKeptnState_IsSucceeded(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateProgressing, - Want: false, - }, - { - State: StateSucceeded, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsSucceeded(), tt.Want) - }) - } -} - -func TestKeptnState_IsFailed(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateFailed, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsFailed(), tt.Want) - }) - } -} - -func TestHash(t *testing.T) { - tests := []struct { - in int64 - out string - }{ - { - in: int64(1), - out: "6b86b273", - }, - { - in: int64(2), - out: "d4735e3a", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.out, Hash(tt.in)) - }) - } -} - -func TestKeptnState_IsDeprecated(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StateDeprecated, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsDeprecated(), tt.Want) - }) - } -} - -func TestKeptnKeptnState_IsPending(t *testing.T) { - tests := []struct { - State KeptnState - Want bool - }{ - { - State: StateSucceeded, - Want: false, - }, - { - State: StatePending, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPending(), tt.Want) - }) - } -} - -func Test_UpdateStatusSummary(t *testing.T) { - emmptySummary := StatusSummary{0, 0, 0, 0, 0, 0, 0} - tests := []struct { - State KeptnState - Want StatusSummary - }{ - { - State: StateProgressing, - Want: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - }, - { - State: StateFailed, - Want: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - }, - { - State: StateSucceeded, - Want: StatusSummary{0, 0, 0, 1, 0, 0, 0}, - }, - { - State: StatePending, - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: "", - Want: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - }, - { - State: StateUnknown, - Want: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - }, - { - State: StateDeprecated, - Want: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, UpdateStatusSummary(tt.State, emmptySummary), tt.Want) - }) - } -} - -func Test_GetTotalCount(t *testing.T) { - summary := StatusSummary{2, 0, 2, 1, 0, 3, 5} - require.Equal(t, summary.GetTotalCount(), 11) -} - -func Test_GeOverallState(t *testing.T) { - tests := []struct { - Name string - Summary StatusSummary - Want KeptnState - }{ - { - Name: "failed", - Summary: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - Want: StateFailed, - }, - { - Name: "deprecated", - Summary: StatusSummary{0, 0, 0, 0, 0, 0, 1}, - Want: StateFailed, - }, - { - Name: "progressing", - Summary: StatusSummary{0, 1, 0, 0, 0, 0, 0}, - Want: StateProgressing, - }, - { - Name: "pending", - Summary: StatusSummary{0, 0, 0, 0, 1, 0, 0}, - Want: StatePending, - }, - { - Name: "unknown", - Summary: StatusSummary{0, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "unknown totalcount", - Summary: StatusSummary{5, 0, 0, 0, 0, 1, 0}, - Want: StateUnknown, - }, - { - Name: "succeeded", - Summary: StatusSummary{1, 0, 0, 1, 0, 0, 0}, - Want: StateSucceeded, - }, - { - Name: "pending total count", - Summary: StatusSummary{2, 0, 0, 1, 0, 0, 0}, - Want: StatePending, - }, - } - for _, tt := range tests { - t.Run(tt.Name, func(t *testing.T) { - require.Equal(t, GetOverallState(tt.Summary), tt.Want) - }) - } -} - -func Test_GeOverallStateBlockedDeployment(t *testing.T) { - tests := []struct { - Name string - Summary StatusSummary - Block bool - Want KeptnState - }{ - { - Name: "failed blocking", - Summary: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - Block: true, - Want: StateFailed, - }, - { - Name: "succeeded blocking", - Summary: StatusSummary{1, 0, 0, 1, 0, 0, 0}, - Block: true, - Want: StateSucceeded, - }, - { - Name: "failed non-blocking", - Summary: StatusSummary{0, 0, 1, 0, 0, 0, 0}, - Block: false, - Want: StateWarning, - }, - { - Name: "succeeded non-blocking", - Summary: StatusSummary{1, 0, 0, 1, 0, 0, 0}, - Block: false, - Want: StateSucceeded, - }, - } - for _, tt := range tests { - t.Run(tt.Name, func(t *testing.T) { - require.Equal(t, GetOverallStateBlockedDeployment(tt.Summary, tt.Block), tt.Want) - }) - } -} - -func Test_TruncateString(t *testing.T) { - tests := []struct { - Input string - Max int - Want string - }{ - { - Input: "some_string", - Max: 20, - Want: "some_string", - }, - { - Input: "some_string", - Max: 5, - Want: "some_", - }, - { - Input: "", - Max: 5, - Want: "", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, TruncateString(tt.Input, tt.Max), tt.Want) - }) - } -} - -func Test_GenerateTaskName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentCheckType, - Name: "short-name", - Want: "pre-short-name-", - }, - { - Check: PreDeploymentCheckType, - Name: "", - Want: "pre--", - }, - { - Check: PreDeploymentCheckType, - Name: ExtraLongName, - Want: "pre-loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo0123456789123456-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateTaskName(tt.Check, tt.Name), tt.Want)) - }) - } -} - -func Test_GenerateEvaluationName(t *testing.T) { - tests := []struct { - Check CheckType - Name string - Want string - }{ - { - Check: PreDeploymentEvaluationCheckType, - Name: "short-name", - Want: "pre-eval-short-name-", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: "", - Want: "pre-eval--", - }, - { - Check: PreDeploymentEvaluationCheckType, - Name: ExtraLongName, - Want: "pre-eval-loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo01234567891-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateEvaluationName(tt.Check, tt.Name), tt.Want)) - }) - } -} - -func Test_GenerateJobName(t *testing.T) { - tests := []struct { - Name string - Want string - }{ - { - Name: "short-name", - Want: "short-name-", - }, - { - Name: "", - Want: "-", - }, - { - Name: ExtraLongName, - Want: "loooooooooooooooooooooo00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ooooooo01234567891234567890-", - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.True(t, strings.HasPrefix(GenerateJobName(tt.Name), tt.Want)) - }) - } -} - -func Test_MergeMaps(t *testing.T) { - tests := []struct { - In1 map[string]string - In2 map[string]string - Want map[string]string - }{ - { - In1: nil, - In2: nil, - Want: map[string]string{}, - }, - { - In1: nil, - In2: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - Want: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - }, - { - In1: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - In2: nil, - Want: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - }, - { - In1: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - }, - In2: map[string]string{ - "ll5": "ll6", - "ll7": "ll8", - }, - Want: map[string]string{ - "ll1": "ll2", - "ll3": "ll4", - "ll5": "ll6", - "ll7": "ll8", - }, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, MergeMaps(tt.In1, tt.In2), tt.Want) - }) - } -} - -func TestIsOwnerSupported(t *testing.T) { - type args struct { - owner v1.OwnerReference - } - tests := []struct { - name string - args args - want bool - }{ - { - name: "Deployment -> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "Deployment", - }, - }, - want: true, - }, - { - name: "DaemonSet-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "DaemonSet", - }, - }, - want: true, - }, - { - name: "ReplicaSet-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "ReplicaSet", - }, - }, - want: true, - }, - { - name: "StatefulSet-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "StatefulSet", - }, - }, - want: true, - }, - { - name: "Rollout-> true", - args: args{ - owner: v1.OwnerReference{ - Kind: "Rollout", - }, - }, - want: true, - }, - { - name: "Job-> false", - args: args{ - owner: v1.OwnerReference{ - Kind: "Job", - }, - }, - want: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := IsOwnerSupported(tt.args.owner); got != tt.want { - t.Errorf("IsOwnerSupported() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/common/phases.go b/lifecycle-operator/apis/lifecycle/v1beta1/common/phases.go deleted file mode 100644 index 875e193a8f..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/common/phases.go +++ /dev/null @@ -1,137 +0,0 @@ -package common - -import ( - "strings" - - "go.opentelemetry.io/otel/propagation" -) - -type KeptnPhase KeptnPhaseType - -type KeptnPhaseType struct { - LongName string - ShortName string -} - -var phases = []KeptnPhaseType{ - PhaseWorkloadPreDeployment, - PhaseWorkloadPostDeployment, - PhaseWorkloadPreEvaluation, - PhaseWorkloadPostEvaluation, - PhaseWorkloadDeployment, - PhaseAppPreDeployment, - PhaseAppPostDeployment, - PhaseAppPreEvaluation, - PhaseAppPostEvaluation, - PhasePromotion, - PhaseAppDeployment, - PhaseReconcileEvaluation, - PhaseReconcileTask, - PhaseReconcileWorkload, - PhaseUpdateWorkload, - PhaseCreateEvaluation, - PhaseCreateTask, - PhaseCreateAppCreationRequest, - PhaseCreateWorkload, - PhaseCreateWorkloadVersion, - PhaseCreateAppVersion, - PhaseAppCompleted, - PhaseWorkloadCompleted, - PhaseDeprecateAppVersion, - PhaseDeprecated, - PhaseAppCompleted, -} - -func (p KeptnPhaseType) IsEvaluation() bool { - return strings.Contains(p.ShortName, "DeployEvaluations") -} - -func (p KeptnPhaseType) IsPreEvaluation() bool { - return strings.Contains(p.ShortName, "PreDeployEvaluations") -} - -func (p KeptnPhaseType) IsPostEvaluation() bool { - return strings.Contains(p.ShortName, "PostDeployEvaluations") -} - -func (p KeptnPhaseType) IsTask() bool { - return strings.Contains(p.ShortName, "DeployTasks") -} - -func (p KeptnPhaseType) IsPreTask() bool { - return strings.Contains(p.ShortName, "PreDeployTasks") -} - -func (p KeptnPhaseType) IsPostTask() bool { - return strings.Contains(p.ShortName, "PostDeployTasks") -} - -func (p KeptnPhaseType) IsPromotionTask() bool { - return strings.Contains(p.ShortName, "PromotionTasks") -} - -func GetShortPhaseName(phase string) string { - for _, p := range phases { - if phase == p.ShortName { - return p.ShortName - } - } - - for _, p := range phases { - if phase == p.LongName { - return p.ShortName - } - } - - return "" -} - -var ( - PhaseWorkloadPreDeployment = KeptnPhaseType{LongName: "Workload Pre-Deployment Tasks", ShortName: "WorkloadPreDeployTasks"} - PhaseWorkloadPostDeployment = KeptnPhaseType{LongName: "Workload Post-Deployment Tasks", ShortName: "WorkloadPostDeployTasks"} - PhaseWorkloadPreEvaluation = KeptnPhaseType{LongName: "Workload Pre-Deployment Evaluations", ShortName: "WorkloadPreDeployEvaluations"} - PhaseWorkloadPostEvaluation = KeptnPhaseType{LongName: "Workload Post-Deployment Evaluations", ShortName: "WorkloadPostDeployEvaluations"} - PhaseWorkloadDeployment = KeptnPhaseType{LongName: "Workload Deployment", ShortName: "WorkloadDeploy"} - PhaseAppPreDeployment = KeptnPhaseType{LongName: "App Pre-Deployment Tasks", ShortName: "AppPreDeployTasks"} - PhaseAppPostDeployment = KeptnPhaseType{LongName: "App Post-Deployment Tasks", ShortName: "AppPostDeployTasks"} - PhaseAppPreEvaluation = KeptnPhaseType{LongName: "App Pre-Deployment Evaluations", ShortName: "AppPreDeployEvaluations"} - PhaseAppPostEvaluation = KeptnPhaseType{LongName: "App Post-Deployment Evaluations", ShortName: "AppPostDeployEvaluations"} - PhasePromotion = KeptnPhaseType{LongName: "Promotion Tasks", ShortName: "PromotionTasks"} - PhaseAppDeployment = KeptnPhaseType{LongName: "App Deployment", ShortName: "AppDeploy"} - PhaseReconcileEvaluation = KeptnPhaseType{LongName: "Reconcile Evaluation", ShortName: "ReconcileEvaluation"} - PhaseReconcileTask = KeptnPhaseType{LongName: "Reconcile Task", ShortName: "ReconcileTask"} - PhaseReconcileWorkload = KeptnPhaseType{LongName: "Reconcile Workloads", ShortName: "ReconcileWorkload"} - PhaseCreateEvaluation = KeptnPhaseType{LongName: "Create Evaluation", ShortName: "CreateEvaluation"} - PhaseCreateTask = KeptnPhaseType{LongName: "Create Task", ShortName: "CreateTask"} - PhaseCreateAppCreationRequest = KeptnPhaseType{LongName: "Create AppCreationRequest", ShortName: "CreateAppCreationRequest"} - PhaseCreateWorkload = KeptnPhaseType{LongName: "Create Workload", ShortName: "CreateWorkload"} - PhaseUpdateWorkload = KeptnPhaseType{LongName: "Update Workload", ShortName: "UpdateWorkload"} - PhaseCreateWorkloadVersion = KeptnPhaseType{LongName: "Create WorkloadVersion", ShortName: "CreateWorkloadVersion"} - PhaseCreateAppVersion = KeptnPhaseType{LongName: "Create AppVersion", ShortName: "CreateAppVersion"} - PhaseDeprecateAppVersion = KeptnPhaseType{LongName: "Deprecate AppVersion", ShortName: "DeprecateAppVersion"} - PhaseAppCompleted = KeptnPhaseType{LongName: "App Completed", ShortName: "AppCompleted"} - PhaseWorkloadCompleted = KeptnPhaseType{LongName: "Workload Completed", ShortName: "WorkloadCompleted"} - PhaseCompleted = KeptnPhaseType{LongName: "Completed", ShortName: "Completed"} - PhaseDeprecated = KeptnPhaseType{LongName: "Deprecated", ShortName: "Deprecated"} -) - -type PhaseTraceID map[string]propagation.MapCarrier - -func (pid PhaseTraceID) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - pid[GetShortPhaseName(phase)] = carrier - -} - -func (pid PhaseTraceID) GetPhaseTraceID(phase string) propagation.MapCarrier { - return pid[GetShortPhaseName(phase)] -} - -var ( - PhaseStateFinished = "Finished" - PhaseStateStarted = "Started" - PhaseStateFailed = "Failed" - PhaseStateStatusChanged = "StatusChanged" - PhaseStateReconcileError = "ReconcileError" - PhaseStateReconcileTimeout = "ReconcileTimeout" - PhaseStateNotFound = "NotFound" -) diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/common/phases_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/common/phases_test.go deleted file mode 100644 index 6cd689e04a..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/common/phases_test.go +++ /dev/null @@ -1,336 +0,0 @@ -package common - -import ( - "testing" - - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/propagation" -) - -func TestKeptnPhaseType_IsEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - { - State: PhasePromotion, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: false, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: true, - }, - { - State: PhaseAppPostEvaluation, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostEvaluation(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostEvaluation, - Want: true, - }, - { - State: PhaseWorkloadPreEvaluation, - Want: false, - }, - { - State: PhaseAppPostEvaluation, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - { - State: PhasePromotion, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostEvaluation(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhasePromotion, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPreTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: false, - }, - { - State: PhaseWorkloadPreDeployment, - Want: true, - }, - { - State: PhaseAppPostDeployment, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: true, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhasePromotion, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPreTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPostTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: true, - }, - { - State: PhaseWorkloadPreDeployment, - Want: false, - }, - { - State: PhaseAppPostDeployment, - Want: true, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhasePromotion, - Want: false, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPostTask(), tt.Want) - }) - } -} - -func TestKeptnPhaseType_IsPromotionTask(t *testing.T) { - tests := []struct { - State KeptnPhaseType - Want bool - }{ - { - State: PhaseWorkloadDeployment, - Want: false, - }, - { - State: PhaseWorkloadPostDeployment, - Want: false, - }, - { - State: PhaseWorkloadPreDeployment, - Want: false, - }, - { - State: PhaseAppPostDeployment, - Want: false, - }, - { - State: PhaseAppPreDeployment, - Want: false, - }, - { - State: PhaseAppPreEvaluation, - Want: false, - }, - { - State: PhasePromotion, - Want: true, - }, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, tt.State.IsPromotionTask(), tt.Want) - }) - } -} - -func TestPhaseTraceID(t *testing.T) { - trace := PhaseTraceID{} - - trace.SetPhaseTraceID(PhaseAppDeployment.LongName, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - }, trace) - - trace.SetPhaseTraceID(PhaseWorkloadDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, PhaseTraceID{ - PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, - PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, trace) - - require.Equal(t, propagation.MapCarrier{ - "name": "trace", - "name2": "trace2", - }, trace.GetPhaseTraceID(PhaseAppDeployment.LongName)) - - require.Equal(t, propagation.MapCarrier{ - "name3": "trace3", - }, trace.GetPhaseTraceID(PhaseWorkloadDeployment.ShortName)) -} - -func TestGetShortPhaseName(t *testing.T) { - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("WorkloadPreDeployTasks")) - require.Equal(t, "WorkloadPreDeployTasks", GetShortPhaseName("Workload Pre-Deployment Tasks")) - require.Equal(t, "", GetShortPhaseName("Workload Pre-Deploycdddment Tasks")) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnapp_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnapp_types.go index a1dc8ab148..3e279551ed 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnapp_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptnapp_types.go @@ -17,16 +17,9 @@ limitations under the License. package v1beta1 import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - operatorcommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnAppSpec defines the desired state of KeptnApp type KeptnAppSpec struct { // Version defines the version of the application. For automatically created KeptnApps, @@ -88,40 +81,3 @@ type KeptnAppList struct { func init() { SchemeBuilder.Register(&KeptnApp{}, &KeptnAppList{}) } - -func (a KeptnApp) GetAppVersionName() string { - return operatorcommon.CreateResourceName(common.MaxK8sObjectLength, common.MinKeptnNameLen, a.Name, a.Spec.Version, common.Hash(a.Generation)) -} - -func (a KeptnApp) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnApp) GenerateAppVersion(previousVersion string) KeptnAppVersion { - return KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: a.GetAppVersionName(), - Namespace: a.Namespace, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: a.Spec, - AppName: a.Name, - PreviousVersion: previousVersion, - }, - } -} - -func (a KeptnApp) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Name), - common.AppVersion.String(a.Spec.Version), - } -} - -func (a KeptnApp) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": a.Name, - "appVersion": a.Spec.Version, - "appRevision": common.Hash(a.Generation), - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnapp_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnapp_types_test.go deleted file mode 100644 index 9dafc82e18..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnapp_types_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package v1beta1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnApp(t *testing.T) { - app := &KeptnApp{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - Generation: 1, - }, - Spec: KeptnAppSpec{ - Version: "version", - }, - } - - appVersionName := app.GetAppVersionName() - require.Equal(t, "app-version-6b86b273", appVersionName) - - appVersion := app.GenerateAppVersion("prev") - require.Equal(t, KeptnAppVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: "app-version-6b86b273", - Namespace: "namespace", - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - Version: "version", - }, - AppName: "app", - PreviousVersion: "prev", - }, - }, appVersion) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("version"), - }, app.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "version", - "appRevision": "6b86b273", - }, app.GetEventAnnotations()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappcreationrequest_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappcreationrequest_types.go index 76cce4aed1..7776a4398e 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappcreationrequest_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappcreationrequest_types.go @@ -17,9 +17,6 @@ limitations under the License. package v1beta1 import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -59,17 +56,3 @@ type KeptnAppCreationRequestList struct { func init() { SchemeBuilder.Register(&KeptnAppCreationRequest{}, &KeptnAppCreationRequestList{}) } - -func (kacr KeptnAppCreationRequest) IsSingleService() bool { - return kacr.Annotations[common.AppTypeAnnotation] == string(common.AppTypeSingleService) -} - -func (kacr KeptnAppCreationRequest) SetSpanAttributes(span trace.Span) { - span.SetAttributes(kacr.GetSpanAttributes()...) -} - -func (kacr KeptnAppCreationRequest) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(kacr.Name), - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappcreationrequest_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappcreationrequest_types_test.go deleted file mode 100644 index 65e29ce574..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappcreationrequest_types_test.go +++ /dev/null @@ -1,74 +0,0 @@ -package v1beta1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnAppCreationRequest_IsSingleService(t *testing.T) { - type fields struct { - ObjectMeta v1.ObjectMeta - } - tests := []struct { - name string - fields fields - want bool - }{ - { - name: "single-service application", - fields: fields{ObjectMeta: v1.ObjectMeta{ - Annotations: map[string]string{ - common.AppTypeAnnotation: string(common.AppTypeSingleService), - }, - }}, - want: true, - }, - { - name: "multi-service application", - fields: fields{ObjectMeta: v1.ObjectMeta{ - Annotations: map[string]string{ - common.AppTypeAnnotation: string(common.AppTypeMultiService), - }, - }}, - want: false, - }, - { - name: "anything else", - fields: fields{ObjectMeta: v1.ObjectMeta{ - Annotations: map[string]string{ - common.AppTypeAnnotation: "", - }, - }}, - want: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - kacr := &KeptnAppCreationRequest{ - ObjectMeta: tt.fields.ObjectMeta, - } - if got := kacr.IsSingleService(); got != tt.want { - t.Errorf("IsSingleService() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestKeptnAppCreationRequest_GetSpanAttributes(t *testing.T) { - kacr := KeptnAppCreationRequest{ - ObjectMeta: v1.ObjectMeta{ - Name: "my-app", - }, - Spec: KeptnAppCreationRequestSpec{}, - } - - spanAttrs := kacr.GetSpanAttributes() - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String(kacr.Name), - }, spanAttrs) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_conversion_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_conversion_test.go index a9d79035e1..c3b428966e 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_conversion_test.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_conversion_test.go @@ -13,6 +13,8 @@ import ( v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" ) +const StateFailed common.KeptnState = "Failed" + func TestKeptnAppVersion_ConvertFrom(t *testing.T) { tests := []struct { name string @@ -159,7 +161,7 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { { DefinitionName: "def52", Name: "name52", - Status: v1common.StateSucceeded, + Status: v1common.StateFailed, }, }, PhaseTraceIDs: v1common.PhaseTraceID{ @@ -229,26 +231,26 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, - PromotionStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, + PromotionStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -256,60 +258,60 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { { DefinitionName: "def1", Name: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def12", Name: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []ItemStatus{ { DefinitionName: "def2", Name: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def22", Name: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def3", Name: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def32", Name: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def4", Name: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def42", Name: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PromotionTaskStatus: []ItemStatus{ { DefinitionName: "def5", Name: "name5", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def52", Name: "name52", - Status: common.StateSucceeded, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -322,7 +324,7 @@ func TestKeptnAppVersion_ConvertFrom(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, }, @@ -411,26 +413,26 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { }, }, Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, - PromotionStatus: common.StateFailed, + PreDeploymentStatus: StateFailed, + PostDeploymentStatus: StateFailed, + PreDeploymentEvaluationStatus: StateFailed, + PostDeploymentEvaluationStatus: StateFailed, + WorkloadOverallStatus: StateFailed, + PromotionStatus: StateFailed, WorkloadStatus: []WorkloadStatus{ { Workload: KeptnWorkloadRef{ Name: "name1", Version: "1", }, - Status: common.StateFailed, + Status: StateFailed, }, { Workload: KeptnWorkloadRef{ Name: "name2", Version: "2", }, - Status: common.StateFailed, + Status: StateFailed, }, }, CurrentPhase: "phase", @@ -438,60 +440,60 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { { DefinitionName: "def1", Name: "name1", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def12", Name: "name12", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentTaskStatus: []ItemStatus{ { DefinitionName: "def2", Name: "name2", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def22", Name: "name22", - Status: common.StateFailed, + Status: StateFailed, }, }, PreDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def3", Name: "name3", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def32", Name: "name32", - Status: common.StateFailed, + Status: StateFailed, }, }, PostDeploymentEvaluationTaskStatus: []ItemStatus{ { DefinitionName: "def4", Name: "name4", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def42", Name: "name42", - Status: common.StateFailed, + Status: StateFailed, }, }, PromotionTaskStatus: []ItemStatus{ { DefinitionName: "def5", Name: "name5", - Status: common.StateFailed, + Status: StateFailed, }, { DefinitionName: "def52", Name: "name52", - Status: common.StateSucceeded, + Status: StateFailed, }, }, PhaseTraceIDs: common.PhaseTraceID{ @@ -504,7 +506,7 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { "key222": "value222", }, }, - Status: common.StateFailed, + Status: StateFailed, }, }, wantErr: false, @@ -641,7 +643,7 @@ func TestKeptnAppVersion_ConvertTo(t *testing.T) { { DefinitionName: "def52", Name: "name52", - Status: v1common.StateSucceeded, + Status: v1common.StateFailed, }, }, PhaseTraceIDs: v1common.PhaseTraceID{ diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_types.go index 312faf0a9a..9b2d0b4d95 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_types.go @@ -17,15 +17,8 @@ limitations under the License. package v1beta1 import ( - "fmt" - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) // KeptnAppVersionSpec defines the desired state of KeptnAppVersion @@ -152,378 +145,6 @@ type KeptnAppVersionList struct { Items []KeptnAppVersion `json:"items"` } -func (a KeptnAppVersionList) GetItems() []client.Object { - b := make([]client.Object, 0, len(a.Items)) - for i := 0; i < len(a.Items); i++ { - b = append(b, &a.Items[i]) - } - return b -} - -func (a *KeptnAppVersionList) RemoveDeprecated() { - b := make([]KeptnAppVersion, 0, len(a.Items)) - for i := 0; i < len(a.Items); i++ { - if a.Items[i].Status.Status != common.StateDeprecated { - b = append(b, a.Items[i]) - } - } - a.Items = b -} - func init() { SchemeBuilder.Register(&KeptnAppVersion{}, &KeptnAppVersionList{}) } - -func (a KeptnAppVersion) IsPreDeploymentCompleted() bool { - return a.Status.PreDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationCompleted() bool { - return a.Status.PreDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPreDeploymentSucceeded(isBlocking bool) bool { - if isBlocking { - return a.Status.PreDeploymentStatus.IsSucceeded() - } - return a.Status.PreDeploymentStatus.IsSucceeded() || a.Status.PreDeploymentStatus.IsWarning() -} - -func (a KeptnAppVersion) IsPreDeploymentFailed() bool { - return a.Status.PreDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationSucceeded(isBlocking bool) bool { - if isBlocking { - return a.Status.PreDeploymentEvaluationStatus.IsSucceeded() - } - return a.Status.PreDeploymentEvaluationStatus.IsSucceeded() || a.Status.PreDeploymentEvaluationStatus.IsWarning() -} - -func (a KeptnAppVersion) IsPreDeploymentEvaluationFailed() bool { - return a.Status.PreDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentCompleted() bool { - return a.Status.PostDeploymentStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPromotionCompleted() bool { - return a.Status.PromotionStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationCompleted() bool { - return a.Status.PostDeploymentEvaluationStatus.IsCompleted() -} - -func (a KeptnAppVersion) IsPostDeploymentFailed() bool { - return a.Status.PostDeploymentStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPromotionFailed() bool { - return a.Status.PromotionStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationSucceeded(isBlocking bool) bool { - if isBlocking { - return a.Status.PostDeploymentEvaluationStatus.IsSucceeded() - } - return a.Status.PostDeploymentEvaluationStatus.IsSucceeded() || a.Status.PostDeploymentEvaluationStatus.IsWarning() -} - -func (a KeptnAppVersion) IsPostDeploymentEvaluationFailed() bool { - return a.Status.PostDeploymentEvaluationStatus.IsFailed() -} - -func (a KeptnAppVersion) IsPostDeploymentSucceeded(isBlocking bool) bool { - if isBlocking { - return a.Status.PostDeploymentStatus.IsSucceeded() - } - return a.Status.PostDeploymentStatus.IsSucceeded() || a.Status.PostDeploymentStatus.IsWarning() -} - -func (a KeptnAppVersion) IsPromotionSucceeded() bool { - return a.Status.PromotionStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsCompleted() bool { - return a.Status.WorkloadOverallStatus.IsCompleted() -} - -func (a KeptnAppVersion) AreWorkloadsSucceeded() bool { - return a.Status.WorkloadOverallStatus.IsSucceeded() -} - -func (a KeptnAppVersion) AreWorkloadsFailed() bool { - return a.Status.WorkloadOverallStatus.IsFailed() -} - -func (a *KeptnAppVersion) SetStartTime() { - if a.Status.StartTime.IsZero() { - a.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a *KeptnAppVersion) SetEndTime() { - if a.Status.EndTime.IsZero() { - a.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (a KeptnAppVersion) GetStartTime() time.Time { - return a.Status.StartTime.Time -} - -func (a KeptnAppVersion) GetEndTime() time.Time { - return a.Status.EndTime.Time -} - -func (a *KeptnAppVersion) IsStartTimeSet() bool { - return !a.Status.StartTime.IsZero() -} - -func (a *KeptnAppVersion) IsEndTimeSet() bool { - return !a.Status.EndTime.IsZero() -} - -func (a *KeptnAppVersion) Complete() { - a.SetEndTime() -} - -func (a KeptnAppVersion) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - common.AppStatus.String(string(a.Status.Status)), - } -} - -func (a KeptnAppVersion) GetDurationMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppNamespace.String(a.Namespace), - common.AppVersion.String(a.Spec.Version), - common.AppPreviousVersion.String(a.Spec.PreviousVersion), - } -} - -func (a KeptnAppVersion) GetState() common.KeptnState { - return a.Status.Status -} - -func (a KeptnAppVersion) GetPreDeploymentTasks() []string { - return a.Spec.PreDeploymentTasks -} - -func (a KeptnAppVersion) GetPostDeploymentTasks() []string { - return a.Spec.PostDeploymentTasks -} - -func (a KeptnAppVersion) GetPromotionTasks() []string { - return a.Spec.PromotionTasks -} - -func (a KeptnAppVersion) GetPreDeploymentTaskStatus() []ItemStatus { - return a.Status.PreDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentTaskStatus() []ItemStatus { - return a.Status.PostDeploymentTaskStatus -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluations() []string { - return a.Spec.PreDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluations() []string { - return a.Spec.PostDeploymentEvaluations -} - -func (a KeptnAppVersion) GetPreDeploymentEvaluationTaskStatus() []ItemStatus { - return a.Status.PreDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetPostDeploymentEvaluationTaskStatus() []ItemStatus { - return a.Status.PostDeploymentEvaluationTaskStatus -} - -func (a KeptnAppVersion) GetPromotionTaskStatus() []ItemStatus { - return a.Status.PromotionTaskStatus -} - -func (a KeptnAppVersion) GetAppName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetPreviousVersion() string { - return a.Spec.PreviousVersion -} - -func (a KeptnAppVersion) GetParentName() string { - return a.Spec.AppName -} - -func (a KeptnAppVersion) GetNamespace() string { - return a.Namespace -} - -func (a *KeptnAppVersion) SetState(state common.KeptnState) { - a.Status.Status = state -} - -func (a KeptnAppVersion) GetCurrentPhase() string { - return a.Status.CurrentPhase -} - -func (a *KeptnAppVersion) SetCurrentPhase(phase string) { - a.Status.CurrentPhase = phase -} - -func (a KeptnAppVersion) GetVersion() string { - return a.Spec.Version -} - -func (a KeptnAppVersion) GenerateTask(taskDefinition KeptnTaskDefinition, checkType common.CheckType) KeptnTask { - return KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition.Name), - Namespace: a.Namespace, - Labels: taskDefinition.Labels, - Annotations: taskDefinition.Annotations, - }, - Spec: KeptnTaskSpec{ - Context: TaskContext{ - AppName: a.GetParentName(), - AppVersion: a.GetVersion(), - TaskType: string(checkType), - ObjectType: "App", - }, - TaskDefinition: taskDefinition.Name, - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: checkType, - Retries: taskDefinition.Spec.Retries, - Timeout: taskDefinition.Spec.Timeout, - }, - } -} - -func (a KeptnAppVersion) GenerateEvaluation(evaluationDefinition KeptnEvaluationDefinition, checkType common.CheckType) KeptnEvaluation { - return KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition.Name), - Namespace: a.Namespace, - }, - Spec: KeptnEvaluationSpec{ - AppVersion: a.Spec.Version, - AppName: a.Spec.AppName, - EvaluationDefinition: evaluationDefinition.Name, - Type: checkType, - FailureConditions: FailureConditions{ - RetryInterval: evaluationDefinition.Spec.FailureConditions.RetryInterval, - Retries: evaluationDefinition.Spec.FailureConditions.Retries, - }, - }, - } -} - -func (a KeptnAppVersion) GetSpanName(phase string) string { - if phase == "" { - return a.Name - } - return phase -} - -func (a KeptnAppVersion) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(a.Spec.AppName), - common.AppVersion.String(a.Spec.Version), - common.AppNamespace.String(a.Namespace), - } -} - -func (a KeptnAppVersion) SetSpanAttributes(span trace.Span) { - span.SetAttributes(a.GetSpanAttributes()...) -} - -func (a KeptnAppVersion) GetSpanKey(phase string) string { - return fmt.Sprintf("%s.%s.%s.%s.%s", a.Spec.TraceId["traceparent"], a.Spec.AppName, a.ObjectMeta.Namespace, a.Spec.Version, phase) -} - -func (v KeptnAppVersion) GetWorkloadNameOfApp(workloadName string) string { - return fmt.Sprintf("%s-%s", v.Spec.AppName, workloadName) -} - -//nolint:dupl -func (a *KeptnAppVersion) DeprecateRemainingPhases(phase common.KeptnPhaseType) { - // no need to deprecate anything when promotion tasks fail - if phase == common.PhasePromotion { - return - } - // deprecate promotion tasks when post evaluation failed - if phase == common.PhaseAppPostEvaluation { - a.Status.PromotionStatus = common.StateDeprecated - } - // deprecate post evaluation when post tasks failed - if phase == common.PhaseAppPostDeployment { - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.PromotionStatus = common.StateDeprecated - } - // deprecate post evaluation and tasks when app deployment failed - if phase == common.PhaseAppDeployment { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.PromotionStatus = common.StateDeprecated - } - // deprecate app deployment, post tasks and evaluations if app pre-eval failed - if phase == common.PhaseAppPreEvaluation { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - a.Status.PromotionStatus = common.StateDeprecated - } - // deprecate pre evaluations, app deployment and post tasks and evaluations when pre-tasks failed - if phase == common.PhaseAppPreDeployment { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - a.Status.PromotionStatus = common.StateDeprecated - } - // deprecate completely everything - if phase == common.PhaseDeprecated { - a.Status.PostDeploymentStatus = common.StateDeprecated - a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - a.Status.WorkloadOverallStatus = common.StateDeprecated - a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - a.Status.PreDeploymentStatus = common.StateDeprecated - a.Status.PromotionStatus = common.StateDeprecated - a.Status.Status = common.StateDeprecated - return - } - a.Status.Status = common.StateFailed -} - -func (a *KeptnAppVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - if a.Status.PhaseTraceIDs == nil { - a.Status.PhaseTraceIDs = common.PhaseTraceID{} - } - a.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier -} - -func (a KeptnAppVersion) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": a.Spec.AppName, - "appVersion": a.Spec.Version, - "appVersionName": a.Name, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_types_test.go deleted file mode 100644 index ed99a72c7c..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnappversion_types_test.go +++ /dev/null @@ -1,570 +0,0 @@ -package v1beta1 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnAppVersion(t *testing.T) { - app := &KeptnAppVersion{ - ObjectMeta: v1.ObjectMeta{ - Name: "app", - Namespace: "namespace", - }, - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - WorkloadOverallStatus: common.StateFailed, - PromotionStatus: common.StateFailed, - Status: common.StateFailed, - PreDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, - PostDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, - PreDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, - PostDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, - PromotionTaskStatus: []ItemStatus{ - { - DefinitionName: "defname5", - Status: common.StateFailed, - Name: "taskname5", - }, - }, - CurrentPhase: common.PhaseAppDeployment.ShortName, - }, - Spec: KeptnAppVersionSpec{ - KeptnAppSpec: KeptnAppSpec{ - Version: "version", - }, - KeptnAppContextSpec: KeptnAppContextSpec{ - DeploymentTaskSpec: DeploymentTaskSpec{ - PreDeploymentTasks: []string{"task1", "task2"}, - PostDeploymentTasks: []string{"task3", "task4"}, - PreDeploymentEvaluations: []string{"task5", "task6"}, - PostDeploymentEvaluations: []string{"task7", "task8"}, - PromotionTasks: []string{"task9", "task10"}, - }, - }, - PreviousVersion: "prev", - AppName: "appname", - TraceId: map[string]string{"traceparent": "trace1"}, - }, - } - - require.True(t, app.IsPreDeploymentCompleted()) - require.False(t, app.IsPreDeploymentSucceeded(true)) - require.True(t, app.IsPreDeploymentFailed()) - - require.True(t, app.IsPreDeploymentEvaluationCompleted()) - require.False(t, app.IsPreDeploymentEvaluationSucceeded(true)) - require.True(t, app.IsPreDeploymentEvaluationFailed()) - - require.True(t, app.IsPostDeploymentCompleted()) - require.False(t, app.IsPostDeploymentSucceeded(true)) - require.True(t, app.IsPostDeploymentFailed()) - - require.True(t, app.IsPostDeploymentEvaluationCompleted()) - require.False(t, app.IsPostDeploymentEvaluationSucceeded(true)) - require.True(t, app.IsPostDeploymentEvaluationFailed()) - - require.True(t, app.IsPromotionCompleted()) - require.False(t, app.IsPromotionSucceeded()) - require.True(t, app.IsPromotionFailed()) - - require.True(t, app.AreWorkloadsCompleted()) - require.False(t, app.AreWorkloadsSucceeded()) - require.True(t, app.AreWorkloadsFailed()) - - app.Status.PreDeploymentStatus = common.StateWarning - app.Status.PreDeploymentEvaluationStatus = common.StateWarning - app.Status.PostDeploymentStatus = common.StateWarning - app.Status.PostDeploymentEvaluationStatus = common.StateWarning - - require.False(t, app.IsPreDeploymentSucceeded(true)) - require.True(t, app.IsPreDeploymentSucceeded(false)) - - require.False(t, app.IsPreDeploymentEvaluationSucceeded(true)) - require.True(t, app.IsPreDeploymentEvaluationSucceeded(false)) - - require.False(t, app.IsPostDeploymentSucceeded(true)) - require.True(t, app.IsPostDeploymentSucceeded(false)) - - require.False(t, app.IsPostDeploymentEvaluationSucceeded(true)) - require.True(t, app.IsPostDeploymentEvaluationSucceeded(false)) - - app.Status.PreDeploymentStatus = common.StateFailed - app.Status.PreDeploymentEvaluationStatus = common.StateFailed - app.Status.PostDeploymentStatus = common.StateFailed - app.Status.PostDeploymentEvaluationStatus = common.StateFailed - - require.False(t, app.IsEndTimeSet()) - require.False(t, app.IsStartTimeSet()) - - app.SetStartTime() - app.SetEndTime() - - require.True(t, app.IsEndTimeSet()) - require.True(t, app.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - common.AppStatus.String(string(common.StateFailed)), - }, app.GetMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppNamespace.String("namespace"), - common.AppVersion.String("version"), - common.AppPreviousVersion.String("prev"), - }, app.GetDurationMetricsAttributes()) - - require.Equal(t, common.StateFailed, app.GetState()) - - require.Equal(t, []string{"task1", "task2"}, app.GetPreDeploymentTasks()) - require.Equal(t, []string{"task3", "task4"}, app.GetPostDeploymentTasks()) - require.Equal(t, []string{"task5", "task6"}, app.GetPreDeploymentEvaluations()) - require.Equal(t, []string{"task7", "task8"}, app.GetPostDeploymentEvaluations()) - require.Equal(t, []string{"task9", "task10"}, app.GetPromotionTasks()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, app.GetPreDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, app.GetPostDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, app.GetPreDeploymentEvaluationTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, app.GetPostDeploymentEvaluationTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname5", - Status: common.StateFailed, - Name: "taskname5", - }, - }, app.GetPromotionTaskStatus()) - - require.Equal(t, "appname", app.GetAppName()) - require.Equal(t, "prev", app.GetPreviousVersion()) - require.Equal(t, "appname", app.GetParentName()) - require.Equal(t, "namespace", app.GetNamespace()) - - app.SetState(common.StatePending) - require.Equal(t, common.StatePending, app.GetState()) - - require.True(t, !app.GetStartTime().IsZero()) - require.True(t, !app.GetEndTime().IsZero()) - - app.SetCurrentPhase(common.PhaseAppDeployment.LongName) - require.Equal(t, common.PhaseAppDeployment.LongName, app.GetCurrentPhase()) - - app.Status.EndTime = v1.Time{Time: time.Time{}} - app.Complete() - require.True(t, !app.GetEndTime().IsZero()) - - require.Equal(t, "version", app.GetVersion()) - - require.Equal(t, "trace1.appname.namespace.version.phase", app.GetSpanKey("phase")) - - retries := int32(5) - task := app.GenerateTask(KeptnTaskDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "task-def", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: KeptnTaskDefinitionSpec{ - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, - }, common.PostDeploymentCheckType) - require.Equal(t, KeptnTaskSpec{ - Context: TaskContext{ - AppName: app.GetParentName(), - AppVersion: app.GetVersion(), - TaskType: string(common.PostDeploymentCheckType), - ObjectType: "App", - }, - TaskDefinition: "task-def", - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: common.PostDeploymentCheckType, - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, task.Spec) - - require.Equal(t, map[string]string{ - "label1": "label2", - }, task.Labels) - - require.Equal(t, map[string]string{ - "annotation1": "annotation2", - }, task.Annotations) - - evaluation := app.GenerateEvaluation(KeptnEvaluationDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "eval-def", - }, - Spec: KeptnEvaluationDefinitionSpec{ - FailureConditions: FailureConditions{ - RetryInterval: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: 5, - }, - }, - }, common.PostDeploymentCheckType) - require.Equal(t, KeptnEvaluationSpec{ - AppVersion: app.GetVersion(), - AppName: app.GetParentName(), - EvaluationDefinition: "eval-def", - Type: common.PostDeploymentCheckType, - FailureConditions: FailureConditions{ - RetryInterval: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: 5, - }, - }, evaluation.Spec) - - require.Equal(t, "phase", app.GetSpanName("phase")) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.AppVersion.String("version"), - common.AppNamespace.String("namespace"), - }, app.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "appname", - "appVersion": "version", - "appVersionName": "app", - }, app.GetEventAnnotations()) -} - -func TestKeptnAppVersion_GetWorkloadNameOfApp(t *testing.T) { - type fields struct { - Spec KeptnAppVersionSpec - } - type args struct { - workloadName string - } - tests := []struct { - name string - fields fields - args args - want string - }{ - { - name: "", - fields: fields{ - Spec: KeptnAppVersionSpec{AppName: "my-app"}, - }, - args: args{ - workloadName: "my-workload", - }, - want: "my-app-my-workload", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - v := KeptnAppVersion{ - Spec: tt.fields.Spec, - } - if got := v.GetWorkloadNameOfApp(tt.args.workloadName); got != tt.want { - t.Errorf("GetWorkloadNameOfApp() = %v, want %v", got, tt.want) - } - }) - } -} - -//nolint:dupl -func TestKeptnAppVersion_DeprecateRemainingPhases(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - PromotionStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - } - - tests := []struct { - app KeptnAppVersion - phase common.KeptnPhaseType - want KeptnAppVersion - }{ - { - app: app, - phase: common.PhasePromotion, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - PromotionStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StatePending, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPostEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - PromotionStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPostDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StateDeprecated, - PromotionStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - PromotionStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPreEvaluation, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - PromotionStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseAppPreDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - PromotionStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - app: app, - phase: common.PhaseDeprecated, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StateDeprecated, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - PromotionStatus: common.StateDeprecated, - WorkloadOverallStatus: common.StateDeprecated, - Status: common.StateDeprecated, - }, - }, - }, - { - app: app, - phase: common.PhaseWorkloadPreDeployment, - want: KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - PromotionStatus: common.StatePending, - WorkloadOverallStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - } - - for _, tt := range tests { - t.Run("", func(t *testing.T) { - tt.app.DeprecateRemainingPhases(tt.phase) - require.Equal(t, tt.want, tt.app) - }) - } -} - -func TestKeptnAppVersion_SetPhaseTraceID(t *testing.T) { - app := KeptnAppVersion{ - Status: KeptnAppVersionStatus{}, - } - - app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, - }, - }, app) - - app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{ - "name2": "trace2", - }) - - require.Equal(t, KeptnAppVersion{ - Status: KeptnAppVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name2": "trace2", - }, - }, - }, - }, app) -} - -func TestKeptnAppVersionList(t *testing.T) { - list := KeptnAppVersionList{ - Items: []KeptnAppVersion{ - { - ObjectMeta: v1.ObjectMeta{ - Name: "obj1", - }, - Status: KeptnAppVersionStatus{ - Status: common.StateSucceeded, - }, - }, - { - ObjectMeta: v1.ObjectMeta{ - Name: "obj2", - }, - Status: KeptnAppVersionStatus{ - Status: common.StateDeprecated, - }, - }, - }, - } - - // fetch the list items - got := list.GetItems() - require.Len(t, got, 2) - - require.Equal(t, "obj1", list.Items[0].GetName()) - require.Equal(t, "obj2", list.Items[1].GetName()) - - // remove deprecated items from the list - list.RemoveDeprecated() - - // check that deprecated items are not present in the list anymore - got = list.GetItems() - require.Len(t, got, 1) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnevaluation_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnevaluation_types.go index 8caa70d146..aa0dfcb42a 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnevaluation_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptnevaluation_types.go @@ -17,14 +17,8 @@ limitations under the License. package v1beta1 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) // KeptnEvaluationSpec defines the desired state of KeptnEvaluation @@ -124,104 +118,3 @@ type KeptnEvaluationList struct { func init() { SchemeBuilder.Register(&KeptnEvaluation{}, &KeptnEvaluationList{}) } - -func (e KeptnEvaluationList) GetItems() []client.Object { - var b []client.Object - for _, i := range e.Items { - b = append(b, &i) - } - return b -} - -func (e *KeptnEvaluation) SetStartTime() { - if e.Status.StartTime.IsZero() { - e.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) SetEndTime() { - if e.Status.EndTime.IsZero() { - e.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *KeptnEvaluation) IsStartTimeSet() bool { - return !e.Status.StartTime.IsZero() -} - -func (e *KeptnEvaluation) IsEndTimeSet() bool { - return !e.Status.EndTime.IsZero() -} - -func (e KeptnEvaluation) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e KeptnEvaluation) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - common.EvaluationStatus.String(string(e.Status.OverallStatus)), - } -} - -func (e *KeptnEvaluation) AddEvaluationStatus(objective Objective) { - - evaluationStatusItem := EvaluationStatusItem{ - Status: common.StatePending, - } - if e.Status.EvaluationStatus == nil { - e.Status.EvaluationStatus = make(map[string]EvaluationStatusItem) - } - e.Status.EvaluationStatus[objective.KeptnMetricRef.Name] = evaluationStatusItem - -} - -func (e KeptnEvaluation) SetSpanAttributes(span trace.Span) { - span.SetAttributes(e.GetSpanAttributes()...) -} - -func (e KeptnEvaluation) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(e.Spec.AppName), - common.AppVersion.String(e.Spec.AppVersion), - common.WorkloadName.String(e.Spec.Workload), - common.WorkloadVersion.String(e.Spec.WorkloadVersion), - common.EvaluationName.String(e.Name), - common.EvaluationType.String(string(e.Spec.Type)), - } -} - -func (e *KeptnEvaluation) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (e KeptnEvaluation) GetSpanKey(phase string) string { - return e.Name -} - -func (e KeptnEvaluation) GetSpanName(phase string) string { - return e.Name -} - -func (e KeptnEvaluation) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": e.Spec.AppName, - "appVersion": e.Spec.AppVersion, - "workloadName": e.Spec.Workload, - "workloadVersion": e.Spec.WorkloadVersion, - "evaluationName": e.Name, - "evaluationDefinitionName": e.Spec.EvaluationDefinition, - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnevaluation_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnevaluation_types_test.go deleted file mode 100644 index 4f96d5dd9f..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnevaluation_types_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package v1beta1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnEvaluation(t *testing.T) { - evaluation := &KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - EvaluationDefinition: "def", - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - } - - evaluation.SetPhaseTraceID("", nil) - require.Equal(t, KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "evaluation", - }, - Spec: KeptnEvaluationSpec{ - AppName: "app", - AppVersion: "appversion", - Type: common.PostDeploymentCheckType, - EvaluationDefinition: "def", - }, - Status: KeptnEvaluationStatus{ - OverallStatus: common.StateFailed, - }, - }, *evaluation) - - require.Equal(t, "evaluation", evaluation.GetSpanKey("")) - require.Equal(t, "evaluation", evaluation.GetSpanName("")) - - require.False(t, evaluation.IsEndTimeSet()) - require.False(t, evaluation.IsStartTimeSet()) - - evaluation.SetStartTime() - evaluation.SetEndTime() - - require.True(t, evaluation.IsEndTimeSet()) - require.True(t, evaluation.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - common.EvaluationStatus.String(string(common.StateFailed)), - }, evaluation.GetMetricsAttributes()) - - evaluation.AddEvaluationStatus(Objective{KeptnMetricRef: KeptnMetricReference{Name: "objName"}}) - require.Equal(t, EvaluationStatusItem{ - Status: common.StatePending, - }, evaluation.Status.EvaluationStatus["objName"]) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.EvaluationName.String("evaluation"), - common.EvaluationType.String(string(common.PostDeploymentCheckType)), - }, evaluation.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "appversion", - "workloadName": "", - "workloadVersion": "", - "evaluationName": "evaluation", - "evaluationDefinitionName": "def", - }, evaluation.GetEventAnnotations()) -} - -func TestKeptnEvaluationList(t *testing.T) { - list := KeptnEvaluationList{ - Items: []KeptnEvaluation{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptntask_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptntask_types.go index 85c6a2ae77..e940070fb4 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptntask_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptntask_types.go @@ -17,14 +17,8 @@ limitations under the License. package v1beta1 import ( - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) // KeptnTaskSpec defines the desired state of KeptnTask @@ -163,114 +157,3 @@ type KeptnTaskList struct { func init() { SchemeBuilder.Register(&KeptnTask{}, &KeptnTaskList{}) } - -func (t KeptnTaskList) GetItems() []client.Object { - var b []client.Object - for _, i := range t.Items { - b = append(b, &i) - } - return b -} - -func (t *KeptnTask) SetStartTime() { - if t.Status.StartTime.IsZero() { - t.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) SetEndTime() { - if t.Status.EndTime.IsZero() { - t.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *KeptnTask) IsStartTimeSet() bool { - return !t.Status.StartTime.IsZero() -} - -func (t *KeptnTask) IsEndTimeSet() bool { - return !t.Status.EndTime.IsZero() -} - -func (t KeptnTask) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.Context.AppName), - common.AppVersion.String(t.Spec.Context.AppVersion), - common.WorkloadName.String(t.Spec.Context.WorkloadName), - common.WorkloadVersion.String(t.Spec.Context.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t KeptnTask) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.Context.AppName), - common.AppVersion.String(t.Spec.Context.AppVersion), - common.WorkloadName.String(t.Spec.Context.WorkloadName), - common.WorkloadVersion.String(t.Spec.Context.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - common.TaskStatus.String(string(t.Status.Status)), - } -} - -func (t KeptnTask) SetSpanAttributes(span trace.Span) { - span.SetAttributes(t.GetSpanAttributes()...) -} - -func (t KeptnTask) CreateKeptnAnnotations() map[string]string { - if t.Spec.Context.WorkloadName != "" { - return common.MergeMaps(t.Annotations, map[string]string{ - common.AppAnnotation: t.Spec.Context.AppName, - common.WorkloadAnnotation: t.Spec.Context.WorkloadName, - common.VersionAnnotation: t.Spec.Context.WorkloadVersion, - common.TaskNameAnnotation: t.Name, - }) - } - return common.MergeMaps(t.Annotations, map[string]string{ - common.AppAnnotation: t.Spec.Context.AppName, - common.VersionAnnotation: t.Spec.Context.AppVersion, - common.TaskNameAnnotation: t.Name, - }) -} - -func (t KeptnTask) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(t.Spec.Context.AppName), - common.AppVersion.String(t.Spec.Context.AppVersion), - common.WorkloadName.String(t.Spec.Context.WorkloadName), - common.WorkloadVersion.String(t.Spec.Context.WorkloadVersion), - common.TaskName.String(t.Name), - common.TaskType.String(string(t.Spec.Type)), - } -} - -func (t *KeptnTask) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - // present due to SpanItem interface -} - -func (t KeptnTask) GetSpanKey(phase string) string { - return t.Name -} - -func (t KeptnTask) GetSpanName(phase string) string { - return t.Name -} - -func (t KeptnTask) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": t.Spec.Context.AppName, - "appVersion": t.Spec.Context.AppVersion, - "workloadName": t.Spec.Context.WorkloadName, - "workloadVersion": t.Spec.Context.WorkloadVersion, - "taskName": t.Name, - "taskDefinitionName": t.Spec.TaskDefinition, - } -} - -func (t KeptnTask) GetActiveDeadlineSeconds() *int64 { - deadline, _ := time.ParseDuration(t.Spec.Timeout.Duration.String()) - seconds := int64(deadline.Seconds()) - return &seconds -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptntask_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptntask_types_test.go deleted file mode 100644 index a48c02e9ad..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptntask_types_test.go +++ /dev/null @@ -1,156 +0,0 @@ -package v1beta1 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnTask(t *testing.T) { - task := &KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: KeptnTaskSpec{ - Context: TaskContext{ - AppName: "app", - AppVersion: "appversion", - }, - Type: common.PostDeploymentCheckType, - TaskDefinition: "def", - Timeout: metav1.Duration{ - Duration: time.Duration(5 * time.Minute), - }, - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - } - - task.SetPhaseTraceID("", nil) - require.Equal(t, KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: "task", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: KeptnTaskSpec{ - Context: TaskContext{ - AppName: "app", - AppVersion: "appversion", - }, - Type: common.PostDeploymentCheckType, - TaskDefinition: "def", - Timeout: metav1.Duration{ - Duration: time.Duration(5 * time.Minute), - }, - }, - Status: KeptnTaskStatus{ - Status: common.StateFailed, - }, - }, *task) - - require.Equal(t, "task", task.GetSpanKey("")) - require.Equal(t, "task", task.GetSpanName("")) - - require.False(t, task.IsEndTimeSet()) - require.False(t, task.IsStartTimeSet()) - - task.SetStartTime() - task.SetEndTime() - - require.True(t, task.IsEndTimeSet()) - require.True(t, task.IsStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String(""), - common.WorkloadVersion.String(""), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - common.TaskStatus.String(string(common.StateFailed)), - }, task.GetMetricsAttributes()) - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/task-name": "task", - "keptn.sh/version": "appversion", - "annotation1": "annotation2", - }, task.CreateKeptnAnnotations()) - - task.Spec.Context.WorkloadName = "workload" - task.Spec.Context.WorkloadVersion = "workloadversion" - - require.Equal(t, map[string]string{ - "keptn.sh/app": "app", - "keptn.sh/workload": "workload", - "keptn.sh/task-name": "task", - "keptn.sh/version": "workloadversion", - "annotation1": "annotation2", - }, task.CreateKeptnAnnotations()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.AppVersion.String("appversion"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("workloadversion"), - common.TaskName.String("task"), - common.TaskType.String(string(common.PostDeploymentCheckType)), - }, task.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "appVersion": "appversion", - "workloadName": "workload", - "workloadVersion": "workloadversion", - "taskName": "task", - "taskDefinitionName": "def", - }, task.GetEventAnnotations()) - - require.Equal(t, int64(300), *task.GetActiveDeadlineSeconds()) - -} - -func TestKeptnTaskList(t *testing.T) { - list := KeptnTaskList{ - Items: []KeptnTask{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptntaskdefinition_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptntaskdefinition_types.go index c81c518d53..b06ac7290c 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptntaskdefinition_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptntaskdefinition_types.go @@ -174,17 +174,3 @@ type KeptnTaskDefinitionList struct { func init() { SchemeBuilder.Register(&KeptnTaskDefinition{}, &KeptnTaskDefinitionList{}) } - -func (d *KeptnTaskDefinition) GetServiceAccount() string { - if d.Spec.ServiceAccount == nil { - return "" - } - return d.Spec.ServiceAccount.Name -} - -func (d *KeptnTaskDefinition) GetAutomountServiceAccountToken() *bool { - if d.Spec.AutomountServiceAccountToken == nil { - return nil - } - return d.Spec.AutomountServiceAccountToken.Type -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptntaskdefinition_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptntaskdefinition_types_test.go deleted file mode 100644 index 635c59ec89..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptntaskdefinition_types_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package v1beta1 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestTaskDefinition_GetServiceAccountNoName(t *testing.T) { - d := &KeptnTaskDefinition{ - Spec: KeptnTaskDefinitionSpec{}, - } - svcAccname := d.GetServiceAccount() - require.Equal(t, svcAccname, "") -} - -func TestTaskDefinition_GetServiceAccountName(t *testing.T) { - sAName := "sva" - d := &KeptnTaskDefinition{ - Spec: KeptnTaskDefinitionSpec{ - ServiceAccount: &ServiceAccountSpec{ - Name: sAName, - }, - }, - } - svcAccname := d.GetServiceAccount() - require.Equal(t, svcAccname, sAName) -} - -func TestTaskDefinition_GetAutomountServiceAccountToken(t *testing.T) { - token := true - d := &KeptnTaskDefinition{ - Spec: KeptnTaskDefinitionSpec{ - AutomountServiceAccountToken: &AutomountServiceAccountTokenSpec{ - Type: &token, - }, - }, - } - require.True(t, *d.GetAutomountServiceAccountToken()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkload_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkload_types.go index 4036c974b5..fc32d79999 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkload_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkload_types.go @@ -17,12 +17,6 @@ limitations under the License. package v1beta1 import ( - "fmt" - "strings" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ) @@ -109,27 +103,3 @@ type ResourceReference struct { func init() { SchemeBuilder.Register(&KeptnWorkload{}, &KeptnWorkloadList{}) } - -func (w KeptnWorkload) SetSpanAttributes(span trace.Span) { - span.SetAttributes(w.GetSpanAttributes()...) -} - -func (w KeptnWorkload) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Name), - common.WorkloadVersion.String(w.Spec.Version), - } -} - -func (w KeptnWorkload) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": w.Spec.AppName, - "workloadName": w.Name, - "workloadVersion": w.Spec.Version, - } -} - -func (w KeptnWorkload) GetNameWithoutAppPrefix() string { - return strings.TrimPrefix(w.Name, fmt.Sprintf("%s-", w.Spec.AppName)) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkload_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkload_types_test.go deleted file mode 100644 index 6bb8a01262..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkload_types_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package v1beta1 - -import ( - "testing" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnWorkload(t *testing.T) { - workload := &KeptnWorkload{ - ObjectMeta: v1.ObjectMeta{ - Name: "workload", - Namespace: "namespace", - }, - Spec: KeptnWorkloadSpec{ - Version: "version", - AppName: "app", - }, - } - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("app"), - common.WorkloadName.String("workload"), - common.WorkloadVersion.String("version"), - }, workload.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "app", - "workloadName": "workload", - "workloadVersion": "version", - }, workload.GetEventAnnotations()) -} - -func TestKeptnWorkload_GetNameWithoutAppPrefix(t *testing.T) { - type fields struct { - ObjectMeta v1.ObjectMeta - Spec KeptnWorkloadSpec - } - tests := []struct { - name string - fields fields - want string - }{ - { - name: "remove app prefix", - fields: fields{ - ObjectMeta: v1.ObjectMeta{ - Name: "my-app-my-workload", - }, - Spec: KeptnWorkloadSpec{ - AppName: "my-app", - }, - }, - want: "my-workload", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - w := KeptnWorkload{ - ObjectMeta: tt.fields.ObjectMeta, - Spec: tt.fields.Spec, - } - got := w.GetNameWithoutAppPrefix() - - require.Equal(t, tt.want, got) - }) - } -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkloadversion_types.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkloadversion_types.go index 39711f8f0e..e17d969551 100644 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkloadversion_types.go +++ b/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkloadversion_types.go @@ -17,15 +17,8 @@ limitations under the License. package v1beta1 import ( - "fmt" - "time" - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) type ItemStatus struct { @@ -162,370 +155,3 @@ type KeptnWorkloadVersionList struct { func init() { SchemeBuilder.Register(&KeptnWorkloadVersion{}, &KeptnWorkloadVersionList{}) } - -func (w KeptnWorkloadVersionList) GetItems() []client.Object { - var b []client.Object - for i := 0; i < len(w.Items); i++ { - b = append(b, &w.Items[i]) - } - return b -} - -func (w KeptnWorkloadVersion) IsPreDeploymentCompleted() bool { - return w.Status.PreDeploymentStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentEvaluationCompleted() bool { - return w.Status.PreDeploymentEvaluationStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentSucceeded(isBlocking bool) bool { - if isBlocking { - return w.Status.PreDeploymentStatus.IsSucceeded() - } - return w.Status.PreDeploymentStatus.IsSucceeded() || w.Status.PreDeploymentStatus.IsWarning() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentFailed() bool { - return w.Status.PreDeploymentStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentEvaluationSucceeded(isBlocking bool) bool { - if isBlocking { - return w.Status.PreDeploymentEvaluationStatus.IsSucceeded() - } - return w.Status.PreDeploymentEvaluationStatus.IsSucceeded() || w.Status.PreDeploymentEvaluationStatus.IsWarning() -} - -func (w KeptnWorkloadVersion) IsPreDeploymentEvaluationFailed() bool { - return w.Status.PreDeploymentEvaluationStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentCompleted() bool { - return w.Status.PostDeploymentStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentEvaluationCompleted() bool { - return w.Status.PostDeploymentEvaluationStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentSucceeded(isBlocking bool) bool { - if isBlocking { - return w.Status.PostDeploymentStatus.IsSucceeded() - } - return w.Status.PostDeploymentStatus.IsSucceeded() || w.Status.PostDeploymentStatus.IsWarning() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentFailed() bool { - return w.Status.PostDeploymentStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentEvaluationSucceeded(isBlocking bool) bool { - if isBlocking { - return w.Status.PostDeploymentEvaluationStatus.IsSucceeded() - } - return w.Status.PostDeploymentEvaluationStatus.IsSucceeded() || w.Status.PostDeploymentEvaluationStatus.IsWarning() -} - -func (w KeptnWorkloadVersion) IsPostDeploymentEvaluationFailed() bool { - return w.Status.PostDeploymentEvaluationStatus.IsFailed() -} - -func (w KeptnWorkloadVersion) IsDeploymentCompleted() bool { - return w.Status.DeploymentStatus.IsCompleted() -} - -func (w KeptnWorkloadVersion) IsDeploymentSucceeded() bool { - return w.Status.DeploymentStatus.IsSucceeded() -} - -func (w KeptnWorkloadVersion) IsDeploymentFailed() bool { - return w.Status.DeploymentStatus.IsFailed() -} - -func (w *KeptnWorkloadVersion) SetStartTime() { - if w.Status.StartTime.IsZero() { - w.Status.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (w *KeptnWorkloadVersion) SetEndTime() { - if w.Status.EndTime.IsZero() { - w.Status.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *ItemStatus) SetStartTime() { - if e.StartTime.IsZero() { - e.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *ItemStatus) SetEndTime() { - if e.EndTime.IsZero() { - e.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (w *KeptnWorkloadVersion) IsStartTimeSet() bool { - return !w.Status.StartTime.IsZero() -} - -func (w *KeptnWorkloadVersion) IsEndTimeSet() bool { - return !w.Status.EndTime.IsZero() -} - -func (w KeptnWorkloadVersion) GetStartTime() time.Time { - return w.Status.StartTime.Time -} - -func (w KeptnWorkloadVersion) GetEndTime() time.Time { - return w.Status.EndTime.Time -} - -func (w *KeptnWorkloadVersion) Complete() { - w.SetEndTime() -} - -func (w KeptnWorkloadVersion) GetActiveMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadNamespace.String(w.Namespace), - } -} - -func (w KeptnWorkloadVersion) GetMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadNamespace.String(w.Namespace), - common.WorkloadStatus.String(string(w.Status.Status)), - } -} - -func (w KeptnWorkloadVersion) GetDurationMetricsAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadNamespace.String(w.Namespace), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadPreviousVersion.String(w.Spec.PreviousVersion), - } -} - -func (w KeptnWorkloadVersion) GetState() common.KeptnState { - return w.Status.Status -} - -func (w KeptnWorkloadVersion) GetPreDeploymentTasks() []string { - return w.Spec.PreDeploymentTasks -} - -func (w KeptnWorkloadVersion) GetPostDeploymentTasks() []string { - return w.Spec.PostDeploymentTasks -} - -func (w KeptnWorkloadVersion) GetPreDeploymentTaskStatus() []ItemStatus { - return w.Status.PreDeploymentTaskStatus -} - -func (w KeptnWorkloadVersion) GetPostDeploymentTaskStatus() []ItemStatus { - return w.Status.PostDeploymentTaskStatus -} - -func (w KeptnWorkloadVersion) GetPreDeploymentEvaluations() []string { - return w.Spec.PreDeploymentEvaluations -} - -func (w KeptnWorkloadVersion) GetPostDeploymentEvaluations() []string { - return w.Spec.PostDeploymentEvaluations -} - -func (w KeptnWorkloadVersion) GetPreDeploymentEvaluationTaskStatus() []ItemStatus { - return w.Status.PreDeploymentEvaluationTaskStatus -} - -func (w KeptnWorkloadVersion) GetPostDeploymentEvaluationTaskStatus() []ItemStatus { - return w.Status.PostDeploymentEvaluationTaskStatus -} - -func (w KeptnWorkloadVersion) GetPromotionTasks() []string { - // promotion tasks are not included in Workloads, but we need the implementation of this method to fulfil the PhaseItem interface - return []string{} -} - -func (w KeptnWorkloadVersion) GetPromotionTaskStatus() []ItemStatus { - // promotion tasks are not included in Workloads, but we need the implementation of this method to fulfil the PhaseItem interface - return []ItemStatus{} -} - -func (w KeptnWorkloadVersion) GetAppName() string { - return w.Spec.AppName -} - -func (w KeptnWorkloadVersion) GetPreviousVersion() string { - return w.Spec.PreviousVersion -} - -func (w KeptnWorkloadVersion) GetParentName() string { - return w.Spec.WorkloadName -} - -func (w KeptnWorkloadVersion) GetNamespace() string { - return w.Namespace -} - -func (w *KeptnWorkloadVersion) SetState(state common.KeptnState) { - w.Status.Status = state -} - -func (w KeptnWorkloadVersion) GetCurrentPhase() string { - return w.Status.CurrentPhase -} - -func (w *KeptnWorkloadVersion) SetCurrentPhase(phase string) { - w.Status.CurrentPhase = phase -} - -func (w KeptnWorkloadVersion) GetVersion() string { - return w.Spec.Version -} - -func (w KeptnWorkloadVersion) GenerateTask(taskDefinition KeptnTaskDefinition, checkType common.CheckType) KeptnTask { - return KeptnTask{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateTaskName(checkType, taskDefinition.Name), - Namespace: w.Namespace, - Labels: taskDefinition.Labels, - Annotations: taskDefinition.Annotations, - }, - Spec: KeptnTaskSpec{ - Context: TaskContext{ - WorkloadName: w.GetParentName(), - AppName: w.GetAppName(), - WorkloadVersion: w.GetVersion(), - TaskType: string(checkType), - ObjectType: "Workload", - }, - TaskDefinition: taskDefinition.Name, - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: checkType, - Retries: taskDefinition.Spec.Retries, - Timeout: taskDefinition.Spec.Timeout, - }, - } -} - -func (w KeptnWorkloadVersion) GenerateEvaluation(evaluationDefinition KeptnEvaluationDefinition, checkType common.CheckType) KeptnEvaluation { - return KeptnEvaluation{ - ObjectMeta: metav1.ObjectMeta{ - Name: common.GenerateEvaluationName(checkType, evaluationDefinition.Name), - Namespace: w.Namespace, - }, - Spec: KeptnEvaluationSpec{ - AppName: w.GetAppName(), - WorkloadVersion: w.GetVersion(), - Workload: w.GetParentName(), - EvaluationDefinition: evaluationDefinition.Name, - Type: checkType, - FailureConditions: FailureConditions{ - RetryInterval: evaluationDefinition.Spec.RetryInterval, - Retries: evaluationDefinition.Spec.Retries, - }, - }, - } -} - -func (w KeptnWorkloadVersion) GetSpanAttributes() []attribute.KeyValue { - return []attribute.KeyValue{ - common.AppName.String(w.Spec.AppName), - common.WorkloadName.String(w.Spec.WorkloadName), - common.WorkloadVersion.String(w.Spec.Version), - common.WorkloadNamespace.String(w.Namespace), - } -} - -func (w KeptnWorkloadVersion) GetSpanKey(phase string) string { - return fmt.Sprintf("%s.%s.%s.%s.%s", w.Spec.TraceId["traceparent"], w.Spec.WorkloadName, w.ObjectMeta.Namespace, w.Spec.Version, phase) -} - -func (w KeptnWorkloadVersion) GetSpanName(phase string) string { - if phase == "" { - return w.Name - } - return fmt.Sprintf("%s/%s", w.Spec.WorkloadName, phase) -} - -func (w KeptnWorkloadVersion) SetSpanAttributes(span trace.Span) { - span.SetAttributes(w.GetSpanAttributes()...) -} - -//nolint:dupl -func (w *KeptnWorkloadVersion) DeprecateRemainingPhases(phase common.KeptnPhaseType) { - // no need to deprecate anything when post-eval tasks fail - if phase == common.PhaseWorkloadPostEvaluation { - return - } - // deprecate post evaluation when post tasks failed - if phase == common.PhaseWorkloadPostDeployment { - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate post evaluation and tasks when app deployment failed - if phase == common.PhaseWorkloadDeployment { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate app deployment, post tasks and evaluations if app pre-eval failed - if phase == common.PhaseWorkloadPreEvaluation { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - w.Status.DeploymentStatus = common.StateDeprecated - } - // deprecate pre evaluations, app deployment and post tasks and evaluations when pre-tasks failed - if phase == common.PhaseWorkloadPreDeployment { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - w.Status.DeploymentStatus = common.StateDeprecated - w.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - } - // deprecate completely everything - if phase == common.PhaseDeprecated { - w.Status.PostDeploymentStatus = common.StateDeprecated - w.Status.PostDeploymentEvaluationStatus = common.StateDeprecated - w.Status.DeploymentStatus = common.StateDeprecated - w.Status.PreDeploymentEvaluationStatus = common.StateDeprecated - w.Status.PreDeploymentStatus = common.StateDeprecated - w.Status.Status = common.StateDeprecated - return - } - - w.Status.Status = common.StateFailed -} - -func (w *KeptnWorkloadVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - if w.Status.PhaseTraceIDs == nil { - w.Status.PhaseTraceIDs = common.PhaseTraceID{} - } - w.Status.PhaseTraceIDs[common.GetShortPhaseName(phase)] = carrier -} - -func (w KeptnWorkloadVersion) GetEventAnnotations() map[string]string { - return map[string]string{ - "appName": w.Spec.AppName, - "workloadName": w.Spec.WorkloadName, - "workloadVersion": w.Spec.Version, - "workloadVersionName": w.Name, - } -} - -func (w *KeptnWorkloadVersion) SetDeploymentStartTime() { - w.Status.DeploymentStartTime = metav1.NewTime(time.Now().UTC()) -} - -func (w *KeptnWorkloadVersion) IsDeploymentStartTimeSet() bool { - return !w.Status.DeploymentStartTime.IsZero() -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkloadversion_types_test.go b/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkloadversion_types_test.go deleted file mode 100644 index 3881b52dac..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/keptnworkloadversion_types_test.go +++ /dev/null @@ -1,493 +0,0 @@ -package v1beta1 - -import ( - "testing" - "time" - - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "github.com/stretchr/testify/require" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/propagation" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnWorkloadVersion(t *testing.T) { - workload := &KeptnWorkloadVersion{ - ObjectMeta: v1.ObjectMeta{ - Name: "workload", - Namespace: "namespace", - }, - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StateFailed, - PreDeploymentEvaluationStatus: common.StateFailed, - PostDeploymentStatus: common.StateFailed, - PostDeploymentEvaluationStatus: common.StateFailed, - DeploymentStatus: common.StateFailed, - Status: common.StateFailed, - PreDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, - PostDeploymentTaskStatus: []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, - PreDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, - PostDeploymentEvaluationTaskStatus: []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, - CurrentPhase: common.PhaseAppDeployment.ShortName, - }, - Spec: KeptnWorkloadVersionSpec{ - KeptnWorkloadSpec: KeptnWorkloadSpec{ - PreDeploymentTasks: []string{"task1", "task2"}, - PostDeploymentTasks: []string{"task3", "task4"}, - PreDeploymentEvaluations: []string{"task5", "task6"}, - PostDeploymentEvaluations: []string{"task7", "task8"}, - Version: "version", - AppName: "appname", - }, - PreviousVersion: "prev", - WorkloadName: "workloadname", - TraceId: map[string]string{"traceparent": "trace1"}, - }, - } - - require.True(t, workload.IsPreDeploymentCompleted()) - require.False(t, workload.IsPreDeploymentSucceeded(true)) - require.True(t, workload.IsPreDeploymentFailed()) - - require.True(t, workload.IsPreDeploymentEvaluationCompleted()) - require.False(t, workload.IsPreDeploymentEvaluationSucceeded(true)) - require.True(t, workload.IsPreDeploymentEvaluationFailed()) - - require.True(t, workload.IsPostDeploymentCompleted()) - require.False(t, workload.IsPostDeploymentSucceeded(true)) - require.True(t, workload.IsPostDeploymentFailed()) - - require.True(t, workload.IsPostDeploymentEvaluationCompleted()) - require.False(t, workload.IsPostDeploymentEvaluationSucceeded(true)) - require.True(t, workload.IsPostDeploymentEvaluationFailed()) - - require.True(t, workload.IsDeploymentCompleted()) - require.False(t, workload.IsDeploymentSucceeded()) - require.True(t, workload.IsDeploymentFailed()) - - workload.Status.PreDeploymentStatus = common.StateWarning - workload.Status.PreDeploymentEvaluationStatus = common.StateWarning - workload.Status.PostDeploymentStatus = common.StateWarning - workload.Status.PostDeploymentEvaluationStatus = common.StateWarning - - require.False(t, workload.IsPreDeploymentSucceeded(true)) - require.True(t, workload.IsPreDeploymentSucceeded(false)) - - require.False(t, workload.IsPreDeploymentEvaluationSucceeded(true)) - require.True(t, workload.IsPreDeploymentEvaluationSucceeded(false)) - - require.False(t, workload.IsPostDeploymentSucceeded(true)) - require.True(t, workload.IsPostDeploymentSucceeded(false)) - - require.False(t, workload.IsPostDeploymentEvaluationSucceeded(true)) - require.True(t, workload.IsPostDeploymentEvaluationSucceeded(false)) - - workload.Status.PreDeploymentStatus = common.StateFailed - workload.Status.PreDeploymentEvaluationStatus = common.StateFailed - workload.Status.PostDeploymentStatus = common.StateFailed - workload.Status.PostDeploymentEvaluationStatus = common.StateFailed - - require.False(t, workload.IsEndTimeSet()) - require.False(t, workload.IsStartTimeSet()) - require.False(t, workload.IsDeploymentStartTimeSet()) - - workload.SetStartTime() - workload.SetEndTime() - workload.SetDeploymentStartTime() - - require.True(t, workload.IsEndTimeSet()) - require.True(t, workload.IsStartTimeSet()) - require.True(t, workload.IsDeploymentStartTimeSet()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadVersion.String("version"), - common.WorkloadNamespace.String("namespace"), - }, workload.GetActiveMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadVersion.String("version"), - common.WorkloadNamespace.String("namespace"), - common.WorkloadStatus.String(string(common.StateFailed)), - }, workload.GetMetricsAttributes()) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadNamespace.String("namespace"), - common.WorkloadVersion.String("version"), - common.WorkloadPreviousVersion.String("prev"), - }, workload.GetDurationMetricsAttributes()) - - require.Equal(t, common.StateFailed, workload.GetState()) - - require.Equal(t, []string{"task1", "task2"}, workload.GetPreDeploymentTasks()) - require.Equal(t, []string{"task3", "task4"}, workload.GetPostDeploymentTasks()) - require.Equal(t, []string{"task5", "task6"}, workload.GetPreDeploymentEvaluations()) - require.Equal(t, []string{"task7", "task8"}, workload.GetPostDeploymentEvaluations()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname", - Status: common.StateFailed, - Name: "taskname", - }, - }, workload.GetPreDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname2", - Status: common.StateFailed, - Name: "taskname2", - }, - }, workload.GetPostDeploymentTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname3", - Status: common.StateFailed, - Name: "taskname3", - }, - }, workload.GetPreDeploymentEvaluationTaskStatus()) - - require.Equal(t, []ItemStatus{ - { - DefinitionName: "defname4", - Status: common.StateFailed, - Name: "taskname4", - }, - }, workload.GetPostDeploymentEvaluationTaskStatus()) - - require.Equal(t, "appname", workload.GetAppName()) - require.Equal(t, "prev", workload.GetPreviousVersion()) - require.Equal(t, "workloadname", workload.GetParentName()) - require.Equal(t, "namespace", workload.GetNamespace()) - - workload.SetState(common.StatePending) - require.Equal(t, common.StatePending, workload.GetState()) - - require.True(t, !workload.GetStartTime().IsZero()) - require.True(t, !workload.GetEndTime().IsZero()) - - workload.SetCurrentPhase(common.PhaseAppDeployment.LongName) - require.Equal(t, common.PhaseAppDeployment.LongName, workload.GetCurrentPhase()) - - workload.Status.EndTime = v1.Time{Time: time.Time{}} - workload.Complete() - require.True(t, !workload.GetEndTime().IsZero()) - - require.Equal(t, "version", workload.GetVersion()) - - require.Equal(t, "trace1.workloadname.namespace.version.phase", workload.GetSpanKey("phase")) - - retries := int32(5) - task := workload.GenerateTask(KeptnTaskDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "task-def", - Labels: map[string]string{ - "label1": "label2", - }, - Annotations: map[string]string{ - "annotation1": "annotation2", - }, - }, - Spec: KeptnTaskDefinitionSpec{ - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, - }, common.PostDeploymentCheckType) - require.Equal(t, KeptnTaskSpec{ - Context: TaskContext{ - AppName: workload.GetAppName(), - WorkloadVersion: workload.GetVersion(), - WorkloadName: workload.GetParentName(), - TaskType: string(common.PostDeploymentCheckType), - ObjectType: "Workload", - }, - TaskDefinition: "task-def", - Parameters: TaskParameters{}, - SecureParameters: SecureParameters{}, - Type: common.PostDeploymentCheckType, - Timeout: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: &retries, - }, task.Spec) - - require.Equal(t, map[string]string{ - "label1": "label2", - }, task.Labels) - - require.Equal(t, map[string]string{ - "annotation1": "annotation2", - }, task.Annotations) - - evaluation := workload.GenerateEvaluation(KeptnEvaluationDefinition{ - ObjectMeta: v1.ObjectMeta{ - Name: "eval-def", - }, - Spec: KeptnEvaluationDefinitionSpec{ - FailureConditions: FailureConditions{ - RetryInterval: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: 5, - }, - }, - }, common.PostDeploymentCheckType) - require.Equal(t, KeptnEvaluationSpec{ - AppName: workload.GetAppName(), - WorkloadVersion: workload.GetVersion(), - Workload: workload.GetParentName(), - EvaluationDefinition: "eval-def", - Type: common.PostDeploymentCheckType, - FailureConditions: FailureConditions{ - RetryInterval: v1.Duration{ - Duration: 5 * time.Second, - }, - Retries: 5, - }, - }, evaluation.Spec) - - require.Equal(t, "workload", workload.GetSpanName("")) - - require.Equal(t, "workloadname/phase", workload.GetSpanName("phase")) - - require.Equal(t, []attribute.KeyValue{ - common.AppName.String("appname"), - common.WorkloadName.String("workloadname"), - common.WorkloadVersion.String("version"), - common.WorkloadNamespace.String("namespace"), - }, workload.GetSpanAttributes()) - - require.Equal(t, map[string]string{ - "appName": "appname", - "workloadName": "workloadname", - "workloadVersion": "version", - "workloadVersionName": "workload", - }, workload.GetEventAnnotations()) - - require.Equal(t, - []string{}, - workload.GetPromotionTasks(), - ) - - require.Equal(t, - []ItemStatus{}, - workload.GetPromotionTaskStatus(), - ) -} - -//nolint:dupl -func TestKeptnWorkloadVersion_DeprecateRemainingPhases(t *testing.T) { - workloadVersion := KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - DeploymentStatus: common.StatePending, - Status: common.StatePending, - }, - } - - tests := []struct { - workloadVersion KeptnWorkloadVersion - phase common.KeptnPhaseType - want KeptnWorkloadVersion - }{ - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPostEvaluation, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - DeploymentStatus: common.StatePending, - Status: common.StatePending, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPostDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPreEvaluation, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseWorkloadPreDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StateDeprecated, - Status: common.StateFailed, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseDeprecated, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StateDeprecated, - PreDeploymentEvaluationStatus: common.StateDeprecated, - PostDeploymentStatus: common.StateDeprecated, - PostDeploymentEvaluationStatus: common.StateDeprecated, - DeploymentStatus: common.StateDeprecated, - Status: common.StateDeprecated, - }, - }, - }, - { - workloadVersion: workloadVersion, - phase: common.PhaseAppPreDeployment, - want: KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PreDeploymentStatus: common.StatePending, - PreDeploymentEvaluationStatus: common.StatePending, - PostDeploymentStatus: common.StatePending, - PostDeploymentEvaluationStatus: common.StatePending, - DeploymentStatus: common.StatePending, - Status: common.StateFailed, - }, - }, - }, - } - - for _, tt := range tests { - t.Run("", func(t *testing.T) { - tt.workloadVersion.DeprecateRemainingPhases(tt.phase) - require.Equal(t, tt.want, tt.workloadVersion) - }) - } -} - -func TestKeptnWorkloadVersion_SetPhaseTraceID(t *testing.T) { - app := KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{}, - } - - app.SetPhaseTraceID(common.PhaseAppDeployment.ShortName, propagation.MapCarrier{ - "name3": "trace3", - }) - - require.Equal(t, KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - }, - }, - }, app) - - app.SetPhaseTraceID(common.PhaseWorkloadDeployment.LongName, propagation.MapCarrier{ - "name2": "trace2", - }) - - require.Equal(t, KeptnWorkloadVersion{ - Status: KeptnWorkloadVersionStatus{ - PhaseTraceIDs: common.PhaseTraceID{ - common.PhaseAppDeployment.ShortName: propagation.MapCarrier{ - "name3": "trace3", - }, - common.PhaseWorkloadDeployment.ShortName: propagation.MapCarrier{ - "name2": "trace2", - }, - }, - }, - }, app) -} - -func TestKeptnWorkloadVersionList(t *testing.T) { - list := KeptnWorkloadVersionList{ - Items: []KeptnWorkloadVersion{ - { - ObjectMeta: v1.ObjectMeta{ - Name: "obj1", - }, - }, - { - ObjectMeta: v1.ObjectMeta{ - Name: "obj2", - }, - }, - }, - } - - got := list.GetItems() - require.Len(t, got, 2) - require.Equal(t, "obj1", got[0].GetName()) - require.Equal(t, "obj2", got[1].GetName()) -} diff --git a/lifecycle-operator/apis/lifecycle/v1beta1/semconv/semconv.go b/lifecycle-operator/apis/lifecycle/v1beta1/semconv/semconv.go deleted file mode 100644 index 8c3d352de2..0000000000 --- a/lifecycle-operator/apis/lifecycle/v1beta1/semconv/semconv.go +++ /dev/null @@ -1,12 +0,0 @@ -package semconv - -import ( - "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1/common" - "go.opentelemetry.io/otel/trace" -) - -func AddAttributeFromAnnotations(s trace.Span, annotations map[string]string) { - s.SetAttributes(common.AppName.String(annotations[common.AppAnnotation])) - s.SetAttributes(common.WorkloadName.String(annotations[common.WorkloadAnnotation])) - s.SetAttributes(common.WorkloadVersion.String(annotations[common.VersionAnnotation])) -} diff --git a/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go b/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go index 7c64ef4542..45967eb36a 100644 --- a/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go +++ b/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go @@ -20,14 +20,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - // KeptnConfigSpec defines the desired state of KeptnConfig type KeptnConfigSpec struct { - // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster - // Important: Run "make" to regenerate code after modifying this file - // OTelCollectorUrl can be used to set the Open Telemetry collector that the lifecycle operator should use // +optional OTelCollectorUrl string `json:"OTelCollectorUrl,omitempty"` diff --git a/lifecycle-operator/chart/templates/keptnappversion-crd.yaml b/lifecycle-operator/chart/templates/keptnappversion-crd.yaml index 6c09bf38ad..33ccf08a07 100644 --- a/lifecycle-operator/chart/templates/keptnappversion-crd.yaml +++ b/lifecycle-operator/chart/templates/keptnappversion-crd.yaml @@ -791,6 +791,8 @@ spec: MapCarrier is a TextMapCarrier that uses a map held in memory as a storage medium for propagated key-value pairs. type: object + description: PhaseTraceID is a map storing TraceIDs of OpenTelemetry + spans in lifecycle phases type: object postDeploymentEvaluationStatus: default: Pending diff --git a/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml b/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml index 7071f3ec8f..85b247b907 100644 --- a/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml +++ b/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml @@ -773,6 +773,8 @@ spec: MapCarrier is a TextMapCarrier that uses a map held in memory as a storage medium for propagated key-value pairs. type: object + description: PhaseTraceID is a map storing TraceIDs of OpenTelemetry + spans in lifecycle phases type: object postDeploymentEvaluationStatus: default: Pending diff --git a/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml b/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml index f10dc6649c..a945dedea8 100644 --- a/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml +++ b/lifecycle-operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml @@ -392,6 +392,8 @@ spec: MapCarrier is a TextMapCarrier that uses a map held in memory as a storage medium for propagated key-value pairs. type: object + description: PhaseTraceID is a map storing TraceIDs of OpenTelemetry + spans in lifecycle phases type: object postDeploymentEvaluationStatus: default: Pending