From 8c1dee718556bc020395ddf01ea73ae7c76d4801 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 22 Nov 2022 11:21:54 +0100 Subject: [PATCH] polishing + phase tests Signed-off-by: odubajDT --- operator/api/v1alpha1/common/phases.go | 36 ++++++++++++++- operator/api/v1alpha1/common/phases_test.go | 46 +++++++++++++++++++ .../api/v1alpha1/keptnappversion_types.go | 18 ++++---- .../v1alpha1/keptnworkloadinstance_types.go | 20 ++++---- .../api/v1alpha1/zz_generated.deepcopy.go | 5 +- .../bases/lifecycle.keptn.sh_keptnapps.yaml | 2 +- .../lifecycle.keptn.sh_keptnappversions.yaml | 2 +- ...e.keptn.sh_keptnevaluationdefinitions.yaml | 2 +- ...cle.keptn.sh_keptnevaluationproviders.yaml | 2 +- .../lifecycle.keptn.sh_keptnevaluations.yaml | 2 +- ...fecycle.keptn.sh_keptntaskdefinitions.yaml | 2 +- .../bases/lifecycle.keptn.sh_keptntasks.yaml | 2 +- ...cycle.keptn.sh_keptnworkloadinstances.yaml | 2 +- .../lifecycle.keptn.sh_keptnworkloads.yaml | 2 +- 14 files changed, 111 insertions(+), 32 deletions(-) diff --git a/operator/api/v1alpha1/common/phases.go b/operator/api/v1alpha1/common/phases.go index 54f09ae5ebf..7ab718a51b8 100644 --- a/operator/api/v1alpha1/common/phases.go +++ b/operator/api/v1alpha1/common/phases.go @@ -37,6 +37,37 @@ func (p KeptnPhaseType) IsPostTask() bool { return strings.Contains(p.ShortName, "PostDeployTasks") } +func GetShortPhaseName(phase string) string { + var phases = []KeptnPhaseType{ + PhaseWorkloadPreDeployment, + PhaseWorkloadPostDeployment, + PhaseWorkloadPreEvaluation, + PhaseWorkloadPostEvaluation, + PhaseWorkloadDeployment, + PhaseAppPreDeployment, + PhaseAppPostDeployment, + PhaseAppPreEvaluation, + PhaseAppPostEvaluation, + PhaseAppDeployment, + PhaseCompleted, + PhaseCancelled, + } + + 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"} @@ -55,9 +86,10 @@ var ( type PhaseTraceID map[string]propagation.MapCarrier func (pid PhaseTraceID) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { - pid[phase] = carrier + pid[GetShortPhaseName(phase)] = carrier + } func (pid PhaseTraceID) GetPhaseTraceID(phase string) propagation.MapCarrier { - return pid[phase] + return pid[GetShortPhaseName(phase)] } diff --git a/operator/api/v1alpha1/common/phases_test.go b/operator/api/v1alpha1/common/phases_test.go index e200da4fbe4..271732c1f1f 100644 --- a/operator/api/v1alpha1/common/phases_test.go +++ b/operator/api/v1alpha1/common/phases_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/propagation" ) func TestKeptnPhaseType_IsEvaluation(t *testing.T) { @@ -227,3 +228,48 @@ func TestKeptnPhaseType_IsPostTask(t *testing.T) { }) } } + +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/operator/api/v1alpha1/keptnappversion_types.go b/operator/api/v1alpha1/keptnappversion_types.go index cf0d41a8f18..8b74716a18e 100644 --- a/operator/api/v1alpha1/keptnappversion_types.go +++ b/operator/api/v1alpha1/keptnappversion_types.go @@ -52,14 +52,14 @@ type KeptnAppVersionStatus struct { // +kubebuilder:default:=Pending PostDeploymentEvaluationStatus common.KeptnState `json:"postDeploymentEvaluationStatus,omitempty"` // +kubebuilder:default:=Pending - WorkloadOverallStatus common.KeptnState `json:"workloadOverallStatus,omitempty"` - WorkloadStatus []WorkloadStatus `json:"workloadStatus,omitempty"` - CurrentPhase string `json:"currentPhase,omitempty"` - PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"` - PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"` - PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"` - PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"` - PhaseTraceIDs map[string]propagation.MapCarrier `json:"phaseTraceIDs,omitempty"` + WorkloadOverallStatus common.KeptnState `json:"workloadOverallStatus,omitempty"` + WorkloadStatus []WorkloadStatus `json:"workloadStatus,omitempty"` + CurrentPhase string `json:"currentPhase,omitempty"` + PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"` + PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"` + PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"` + PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"` + PhaseTraceIDs common.PhaseTraceID `json:"phaseTraceIDs,omitempty"` // +kubebuilder:default:=Pending Status common.KeptnState `json:"status,omitempty"` @@ -378,7 +378,7 @@ func (a *KeptnAppVersion) CancelRemainingPhases(phase common.KeptnPhaseType) { func (a *KeptnAppVersion) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { if a.Status.PhaseTraceIDs == nil { - a.Status.PhaseTraceIDs = map[string]propagation.MapCarrier{} + a.Status.PhaseTraceIDs = common.PhaseTraceID{} } a.Status.PhaseTraceIDs[phase] = carrier } diff --git a/operator/api/v1alpha1/keptnworkloadinstance_types.go b/operator/api/v1alpha1/keptnworkloadinstance_types.go index ee9e223dc5a..4be91fdc553 100644 --- a/operator/api/v1alpha1/keptnworkloadinstance_types.go +++ b/operator/api/v1alpha1/keptnworkloadinstance_types.go @@ -50,15 +50,15 @@ type KeptnWorkloadInstanceStatus struct { // +kubebuilder:default:=Pending PostDeploymentEvaluationStatus common.KeptnState `json:"postDeploymentEvaluationStatus,omitempty"` // +kubebuilder:default:=Pending - PostDeploymentStatus common.KeptnState `json:"postDeploymentStatus,omitempty"` - PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"` - PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"` - PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"` - PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"` - StartTime metav1.Time `json:"startTime,omitempty"` - EndTime metav1.Time `json:"endTime,omitempty"` - CurrentPhase string `json:"currentPhase,omitempty"` - PhaseTraceIDs map[string]propagation.MapCarrier `json:"phaseTraceIDs,omitempty"` + PostDeploymentStatus common.KeptnState `json:"postDeploymentStatus,omitempty"` + PreDeploymentTaskStatus []TaskStatus `json:"preDeploymentTaskStatus,omitempty"` + PostDeploymentTaskStatus []TaskStatus `json:"postDeploymentTaskStatus,omitempty"` + PreDeploymentEvaluationTaskStatus []EvaluationStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"` + PostDeploymentEvaluationTaskStatus []EvaluationStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"` + StartTime metav1.Time `json:"startTime,omitempty"` + EndTime metav1.Time `json:"endTime,omitempty"` + CurrentPhase string `json:"currentPhase,omitempty"` + PhaseTraceIDs common.PhaseTraceID `json:"phaseTraceIDs,omitempty"` // +kubebuilder:default:=Pending Status common.KeptnState `json:"status,omitempty"` } @@ -418,7 +418,7 @@ func (w *KeptnWorkloadInstance) CancelRemainingPhases(phase common.KeptnPhaseTyp func (w *KeptnWorkloadInstance) SetPhaseTraceID(phase string, carrier propagation.MapCarrier) { if w.Status.PhaseTraceIDs == nil { - w.Status.PhaseTraceIDs = map[string]propagation.MapCarrier{} + w.Status.PhaseTraceIDs = common.PhaseTraceID{} } w.Status.PhaseTraceIDs[phase] = carrier } diff --git a/operator/api/v1alpha1/zz_generated.deepcopy.go b/operator/api/v1alpha1/zz_generated.deepcopy.go index 413cb65468c..83d9d704011 100644 --- a/operator/api/v1alpha1/zz_generated.deepcopy.go +++ b/operator/api/v1alpha1/zz_generated.deepcopy.go @@ -22,6 +22,7 @@ limitations under the License. package v1alpha1 import ( + "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1/common" "go.opentelemetry.io/otel/propagation" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -403,7 +404,7 @@ func (in *KeptnAppVersionStatus) DeepCopyInto(out *KeptnAppVersionStatus) { } if in.PhaseTraceIDs != nil { in, out := &in.PhaseTraceIDs, &out.PhaseTraceIDs - *out = make(map[string]propagation.MapCarrier, len(*in)) + *out = make(common.PhaseTraceID, len(*in)) for key, val := range *in { var outVal map[string]string if val == nil { @@ -1043,7 +1044,7 @@ func (in *KeptnWorkloadInstanceStatus) DeepCopyInto(out *KeptnWorkloadInstanceSt in.EndTime.DeepCopyInto(&out.EndTime) if in.PhaseTraceIDs != nil { in, out := &in.PhaseTraceIDs, &out.PhaseTraceIDs - *out = make(map[string]propagation.MapCarrier, len(*in)) + *out = make(common.PhaseTraceID, len(*in)) for key, val := range *in { var outVal map[string]string if val == nil { diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnapps.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnapps.yaml index 7b81667aa30..53475dd0bc8 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnapps.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnapps.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptnapps.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml index 0399839fe0e..236f169243f 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptnappversions.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationdefinitions.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationdefinitions.yaml index a5328e6cfc3..e1505d3d8f2 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationdefinitions.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationdefinitions.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptnevaluationdefinitions.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationproviders.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationproviders.yaml index eae86cd96b2..25d511b59c8 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationproviders.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluationproviders.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptnevaluationproviders.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluations.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluations.yaml index 41e507fa0ce..b8b04c75820 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluations.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnevaluations.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptnevaluations.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml index c06275ee72c..d4a54092953 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptntaskdefinitions.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptntaskdefinitions.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml index 8fee50eeb9c..d470769f768 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptntasks.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml index f9b30dc12cd..29d37bfe6a2 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptnworkloadinstances.lifecycle.keptn.sh spec: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloads.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloads.yaml index a41fe78ff67..461b60befe9 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloads.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloads.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.9.2 + controller-gen.kubebuilder.io/version: v0.10.0 creationTimestamp: null name: keptnworkloads.lifecycle.keptn.sh spec: