diff --git a/operator/PROJECT b/operator/PROJECT index ff0084804b..14743e4ddb 100644 --- a/operator/PROJECT +++ b/operator/PROJECT @@ -133,6 +133,9 @@ resources: kind: KeptnWorkloadInstance path: github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2 version: v1alpha2 + webhooks: + conversion: true + webhookVersion: v1 - api: crdVersion: v1 namespaced: true @@ -142,6 +145,9 @@ resources: kind: KeptnAppVersion path: github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2 version: v1alpha2 + webhooks: + conversion: true + webhookVersion: v1 - api: crdVersion: v1 namespaced: true diff --git a/operator/apis/lifecycle/v1alpha1/common/common.go b/operator/apis/lifecycle/v1alpha1/common/common.go index 5c2e69d21a..4984e6f6e4 100644 --- a/operator/apis/lifecycle/v1alpha1/common/common.go +++ b/operator/apis/lifecycle/v1alpha1/common/common.go @@ -33,6 +33,9 @@ const MaxTaskNameLength = 25 const MaxVersionLength = 12 var ErrCannotCastKeptnEvaluationProvider = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha2") +var ErrCannotCastKeptnAppVersion = errors.New("cannot cast KeptnAppVersion to v1alpha2") +var ErrCannotCastKeptnApp = errors.New("cannot cast KeptnApp to v1alpha2") +var ErrCannotCastKeptnWorkloadInstance = errors.New("cannot cast KeptnWorkloadInstance to v1alpha2") type KeptnState string diff --git a/operator/apis/lifecycle/v1alpha1/keptnapp_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnapp_conversion.go index 02727b4e60..e827bb62d3 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnapp_conversion.go +++ b/operator/apis/lifecycle/v1alpha1/keptnapp_conversion.go @@ -3,6 +3,7 @@ package v1alpha1 import ( "fmt" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" "sigs.k8s.io/controller-runtime/pkg/conversion" ) @@ -12,7 +13,7 @@ func (src *KeptnApp) ConvertTo(dstRaw conversion.Hub) error { dst, ok := dstRaw.(*v1alpha2.KeptnApp) if !ok { - return fmt.Errorf("cannot cast KeptnApp to v1alpha2. Got type %T", dstRaw) + return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastKeptnApp) } // Copy equal stuff to new object @@ -44,7 +45,7 @@ func (dst *KeptnApp) ConvertFrom(srcRaw conversion.Hub) error { src, ok := srcRaw.(*v1alpha2.KeptnApp) if !ok { - return fmt.Errorf("cannot cast KeptnApp to v1alpha1. Got type %T", srcRaw) + return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastKeptnApp) } // Copy equal stuff to new object diff --git a/operator/apis/lifecycle/v1alpha1/keptnapp_conversion_test.go b/operator/apis/lifecycle/v1alpha1/keptnapp_conversion_test.go index fba9721097..4c82b228df 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnapp_conversion_test.go +++ b/operator/apis/lifecycle/v1alpha1/keptnapp_conversion_test.go @@ -3,6 +3,7 @@ package v1alpha1 import ( "testing" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" "github.com/stretchr/testify/require" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -253,7 +254,7 @@ func TestKeptnApp_ConvertFrom_Errorcase(t *testing.T) { if err := dst.ConvertFrom(&testObj); err == nil { t.Errorf("ConvertFrom() error = %v", err) } else { - require.Contains(t, err.Error(), "cannot cast KeptnApp to v1alpha1") + require.ErrorIs(t, err, common.ErrCannotCastKeptnApp) } } @@ -266,6 +267,6 @@ func TestKeptnApp_ConvertTo_Errorcase(t *testing.T) { if err := testObj.ConvertTo(&dst); err == nil { t.Errorf("ConvertTo() error = %v", err) } else { - require.Contains(t, err.Error(), "cannot cast KeptnApp to v1alpha2") + require.ErrorIs(t, err, common.ErrCannotCastKeptnApp) } } diff --git a/operator/apis/lifecycle/v1alpha1/keptnappversion_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnappversion_conversion.go new file mode 100644 index 0000000000..333af32c6f --- /dev/null +++ b/operator/apis/lifecycle/v1alpha1/keptnappversion_conversion.go @@ -0,0 +1,228 @@ +package v1alpha1 + +import ( + "fmt" + + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" + v1alpha2common "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2/common" + "go.opentelemetry.io/otel/propagation" + "sigs.k8s.io/controller-runtime/pkg/conversion" +) + +// ConvertTo converts the src v1alpha1.KeptnAppVersion to the hub version (v1alpha2.KeptnAppVersion) +func (src *KeptnAppVersion) ConvertTo(dstRaw conversion.Hub) error { + dst, ok := dstRaw.(*v1alpha2.KeptnAppVersion) + + if !ok { + return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastKeptnAppVersion) + } + + // Copy equal stuff to new object + // DO NOT COPY TypeMeta + dst.ObjectMeta = src.ObjectMeta + + dst.Spec.Version = src.Spec.Version + for _, srcWl := range src.Spec.Workloads { + dst.Spec.Workloads = append(dst.Spec.Workloads, v1alpha2.KeptnWorkloadRef{ + Name: srcWl.Name, + Version: srcWl.Version, + }) + } + dst.Spec.PreDeploymentTasks = src.Spec.PreDeploymentTasks + dst.Spec.PostDeploymentTasks = src.Spec.PostDeploymentTasks + dst.Spec.PreDeploymentEvaluations = src.Spec.PreDeploymentEvaluations + dst.Spec.PostDeploymentEvaluations = src.Spec.PostDeploymentEvaluations + + dst.Spec.AppName = src.Spec.AppName + dst.Spec.PreviousVersion = src.Spec.PreviousVersion + + dst.Spec.TraceId = make(map[string]string, len(src.Spec.TraceId)) + for k, v := range src.Spec.TraceId { + dst.Spec.TraceId[k] = v + } + + dst.Status.PreDeploymentStatus = v1alpha2common.KeptnState(src.Status.PreDeploymentStatus) + dst.Status.PostDeploymentStatus = v1alpha2common.KeptnState(src.Status.PostDeploymentStatus) + dst.Status.PreDeploymentEvaluationStatus = v1alpha2common.KeptnState(src.Status.PreDeploymentEvaluationStatus) + dst.Status.PostDeploymentEvaluationStatus = v1alpha2common.KeptnState(src.Status.PostDeploymentEvaluationStatus) + dst.Status.WorkloadOverallStatus = v1alpha2common.KeptnState(src.Status.WorkloadOverallStatus) + dst.Status.Status = v1alpha2common.KeptnState(src.Status.Status) + + for _, srcWls := range src.Status.WorkloadStatus { + dst.Status.WorkloadStatus = append(dst.Status.WorkloadStatus, v1alpha2.WorkloadStatus{ + Workload: v1alpha2.KeptnWorkloadRef{ + Name: srcWls.Workload.Name, + Version: srcWls.Workload.Version, + }, + Status: v1alpha2common.KeptnState(srcWls.Status), + }) + } + + dst.Status.CurrentPhase = src.Status.CurrentPhase + + // Set sensible defaults for new fields + dst.Spec.Revision = 1 + + // Convert changed fields + for _, item := range src.Status.PreDeploymentTaskStatus { + dst.Status.PreDeploymentTaskStatus = append(dst.Status.PreDeploymentTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.TaskDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.TaskName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentTaskStatus { + dst.Status.PostDeploymentTaskStatus = append(dst.Status.PostDeploymentTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.TaskDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.TaskName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PreDeploymentEvaluationTaskStatus { + dst.Status.PreDeploymentEvaluationTaskStatus = append(dst.Status.PreDeploymentEvaluationTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.EvaluationDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.EvaluationName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentEvaluationTaskStatus { + dst.Status.PostDeploymentEvaluationTaskStatus = append(dst.Status.PostDeploymentEvaluationTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.EvaluationDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.EvaluationName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + dst.Status.PhaseTraceIDs = make(v1alpha2common.PhaseTraceID, len(src.Status.PhaseTraceIDs)) + for k, v := range src.Status.PhaseTraceIDs { + c := make(propagation.MapCarrier, len(v)) + for k1, v1 := range v { + c[k1] = v1 + } + dst.Status.PhaseTraceIDs[k] = c + } + + dst.Status.StartTime = src.Status.StartTime + dst.Status.EndTime = src.Status.EndTime + + return nil +} + +// ConvertFrom converts from the hub version (v1alpha2.KeptnAppVersion) to this version (v1alpha1.KeptnAppVersion) +func (dst *KeptnAppVersion) ConvertFrom(srcRaw conversion.Hub) error { + src, ok := srcRaw.(*v1alpha2.KeptnAppVersion) + + if !ok { + return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastKeptnAppVersion) + } + + // Copy equal stuff to new object + // DO NOT COPY TypeMeta + dst.ObjectMeta = src.ObjectMeta + + dst.Spec.Version = src.Spec.Version + for _, srcWl := range src.Spec.Workloads { + dst.Spec.Workloads = append(dst.Spec.Workloads, KeptnWorkloadRef{ + Name: srcWl.Name, + Version: srcWl.Version, + }) + } + dst.Spec.PreDeploymentTasks = src.Spec.PreDeploymentTasks + dst.Spec.PostDeploymentTasks = src.Spec.PostDeploymentTasks + dst.Spec.PreDeploymentEvaluations = src.Spec.PreDeploymentEvaluations + dst.Spec.PostDeploymentEvaluations = src.Spec.PostDeploymentEvaluations + + dst.Spec.AppName = src.Spec.AppName + dst.Spec.PreviousVersion = src.Spec.PreviousVersion + + dst.Spec.TraceId = make(map[string]string, len(src.Spec.TraceId)) + for k, v := range src.Spec.TraceId { + dst.Spec.TraceId[k] = v + } + + dst.Status.PreDeploymentStatus = common.KeptnState(src.Status.PreDeploymentStatus) + dst.Status.PostDeploymentStatus = common.KeptnState(src.Status.PostDeploymentStatus) + dst.Status.PreDeploymentEvaluationStatus = common.KeptnState(src.Status.PreDeploymentEvaluationStatus) + dst.Status.PostDeploymentEvaluationStatus = common.KeptnState(src.Status.PostDeploymentEvaluationStatus) + dst.Status.WorkloadOverallStatus = common.KeptnState(src.Status.WorkloadOverallStatus) + dst.Status.Status = common.KeptnState(src.Status.Status) + + for _, srcWls := range src.Status.WorkloadStatus { + dst.Status.WorkloadStatus = append(dst.Status.WorkloadStatus, WorkloadStatus{ + Workload: KeptnWorkloadRef{ + Name: srcWls.Workload.Name, + Version: srcWls.Workload.Version, + }, + Status: common.KeptnState(srcWls.Status), + }) + } + + dst.Status.CurrentPhase = src.Status.CurrentPhase + + // Convert changed fields + for _, item := range src.Status.PreDeploymentTaskStatus { + dst.Status.PreDeploymentTaskStatus = append(dst.Status.PreDeploymentTaskStatus, TaskStatus{ + TaskDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + TaskName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentTaskStatus { + dst.Status.PostDeploymentTaskStatus = append(dst.Status.PostDeploymentTaskStatus, TaskStatus{ + TaskDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + TaskName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PreDeploymentEvaluationTaskStatus { + dst.Status.PreDeploymentEvaluationTaskStatus = append(dst.Status.PreDeploymentEvaluationTaskStatus, EvaluationStatus{ + EvaluationDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + EvaluationName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentEvaluationTaskStatus { + dst.Status.PostDeploymentEvaluationTaskStatus = append(dst.Status.PostDeploymentEvaluationTaskStatus, EvaluationStatus{ + EvaluationDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + EvaluationName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + dst.Status.PhaseTraceIDs = make(common.PhaseTraceID, len(src.Status.PhaseTraceIDs)) + for k, v := range src.Status.PhaseTraceIDs { + c := make(propagation.MapCarrier, len(v)) + for k1, v1 := range v { + c[k1] = v1 + } + dst.Status.PhaseTraceIDs[k] = c + } + + dst.Status.StartTime = src.Status.StartTime + dst.Status.EndTime = src.Status.EndTime + + return nil +} diff --git a/operator/apis/lifecycle/v1alpha1/keptnappversion_conversion_test.go b/operator/apis/lifecycle/v1alpha1/keptnappversion_conversion_test.go new file mode 100644 index 0000000000..b25c6d9859 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha1/keptnappversion_conversion_test.go @@ -0,0 +1,626 @@ +package v1alpha1 + +import ( + "testing" + + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" + v1alpha2common "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2/common" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/propagation" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" +) + +func TestKeptnAppVersion_ConvertFrom(t *testing.T) { + tests := []struct { + name string + srcObj *v1alpha2.KeptnAppVersion + wantErr bool + wantObj *KeptnAppVersion + }{ + { + name: "Test that conversion from v1alpha2 to v1alpha1 works", + srcObj: &v1alpha2.KeptnAppVersion{ + TypeMeta: v1.TypeMeta{ + Kind: "KeptnAppVersion", + APIVersion: "lifecycle.keptn.sh/v1alpha2", + }, + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: v1alpha2.KeptnAppVersionSpec{ + KeptnAppSpec: v1alpha2.KeptnAppSpec{ + Version: "1.2.3", + Revision: 1, + Workloads: []v1alpha2.KeptnWorkloadRef{ + { + Name: "workload-1", + Version: "1.2.3", + }, + { + Name: "workload-2", + Version: "4.5.6", + }, + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + }, + AppName: "app", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: v1alpha2.KeptnAppVersionStatus{ + PreDeploymentStatus: v1alpha2common.StateFailed, + PostDeploymentStatus: v1alpha2common.StateFailed, + PreDeploymentEvaluationStatus: v1alpha2common.StateFailed, + PostDeploymentEvaluationStatus: v1alpha2common.StateFailed, + WorkloadOverallStatus: v1alpha2common.StateFailed, + WorkloadStatus: []v1alpha2.WorkloadStatus{ + { + Workload: v1alpha2.KeptnWorkloadRef{ + Name: "name1", + Version: "1", + }, + Status: v1alpha2common.StateFailed, + }, + { + Workload: v1alpha2.KeptnWorkloadRef{ + Name: "name2", + Version: "2", + }, + Status: v1alpha2common.StateFailed, + }, + }, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def1", + Name: "name1", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def12", + Name: "name12", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def2", + Name: "name2", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def22", + Name: "name22", + Status: v1alpha2common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def3", + Name: "name3", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def32", + Name: "name32", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def4", + Name: "name4", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def42", + Name: "name42", + Status: v1alpha2common.StateFailed, + }, + }, + PhaseTraceIDs: v1alpha2common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: v1alpha2common.StateFailed, + }, + }, + wantErr: false, + wantObj: &KeptnAppVersion{ + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: KeptnAppVersionSpec{ + KeptnAppSpec: KeptnAppSpec{ + Version: "1.2.3", + Workloads: []KeptnWorkloadRef{ + { + Name: "workload-1", + Version: "1.2.3", + }, + { + Name: "workload-2", + Version: "4.5.6", + }, + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + }, + AppName: "app", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: KeptnAppVersionStatus{ + PreDeploymentStatus: common.StateFailed, + PostDeploymentStatus: common.StateFailed, + PreDeploymentEvaluationStatus: common.StateFailed, + PostDeploymentEvaluationStatus: common.StateFailed, + WorkloadOverallStatus: common.StateFailed, + WorkloadStatus: []WorkloadStatus{ + { + Workload: KeptnWorkloadRef{ + Name: "name1", + Version: "1", + }, + Status: common.StateFailed, + }, + { + Workload: KeptnWorkloadRef{ + Name: "name2", + Version: "2", + }, + Status: common.StateFailed, + }, + }, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def1", + TaskName: "name1", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def12", + TaskName: "name12", + Status: common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def2", + TaskName: "name2", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def22", + TaskName: "name22", + Status: common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def3", + EvaluationName: "name3", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def32", + EvaluationName: "name32", + Status: common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def4", + EvaluationName: "name4", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def42", + EvaluationName: "name42", + Status: common.StateFailed, + }, + }, + PhaseTraceIDs: common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: common.StateFailed, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := &KeptnAppVersion{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: KeptnAppVersionSpec{}, + Status: KeptnAppVersionStatus{}, + } + if err := dst.ConvertFrom(tt.srcObj); (err != nil) != tt.wantErr { + t.Errorf("ConvertFrom() error = %v, wantErr %v", err, tt.wantErr) + } + if tt.wantObj != nil { + require.Equal(t, tt.wantObj, dst, "Object was not converted correctly") + } + }) + } +} + +func TestKeptnAppVersion_ConvertTo(t *testing.T) { + tests := []struct { + name string + src *KeptnAppVersion + wantErr bool + wantObj *v1alpha2.KeptnAppVersion + }{ + { + name: "Test that conversion from v1alpha1 to v1alpha2 works", + src: &KeptnAppVersion{ + TypeMeta: v1.TypeMeta{ + Kind: "KeptnAppVersion", + APIVersion: "lifecycle.keptn.sh/v1alpha1", + }, + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: KeptnAppVersionSpec{ + KeptnAppSpec: KeptnAppSpec{ + Version: "1.2.3", + Workloads: []KeptnWorkloadRef{ + { + Name: "workload-1", + Version: "1.2.3", + }, + { + Name: "workload-2", + Version: "4.5.6", + }, + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + }, + AppName: "app", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: KeptnAppVersionStatus{ + PreDeploymentStatus: common.StateFailed, + PostDeploymentStatus: common.StateFailed, + PreDeploymentEvaluationStatus: common.StateFailed, + PostDeploymentEvaluationStatus: common.StateFailed, + WorkloadOverallStatus: common.StateFailed, + WorkloadStatus: []WorkloadStatus{ + { + Workload: KeptnWorkloadRef{ + Name: "name1", + Version: "1", + }, + Status: common.StateFailed, + }, + { + Workload: KeptnWorkloadRef{ + Name: "name2", + Version: "2", + }, + Status: common.StateFailed, + }, + }, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def1", + TaskName: "name1", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def12", + TaskName: "name12", + Status: common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def2", + TaskName: "name2", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def22", + TaskName: "name22", + Status: common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def3", + EvaluationName: "name3", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def32", + EvaluationName: "name32", + Status: common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def4", + EvaluationName: "name4", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def42", + EvaluationName: "name42", + Status: common.StateFailed, + }, + }, + PhaseTraceIDs: common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: common.StateFailed, + }, + }, + wantErr: false, + wantObj: &v1alpha2.KeptnAppVersion{ + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: v1alpha2.KeptnAppVersionSpec{ + KeptnAppSpec: v1alpha2.KeptnAppSpec{ + Version: "1.2.3", + Revision: 1, + Workloads: []v1alpha2.KeptnWorkloadRef{ + { + Name: "workload-1", + Version: "1.2.3", + }, + { + Name: "workload-2", + Version: "4.5.6", + }, + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + }, + AppName: "app", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: v1alpha2.KeptnAppVersionStatus{ + PreDeploymentStatus: v1alpha2common.StateFailed, + PostDeploymentStatus: v1alpha2common.StateFailed, + PreDeploymentEvaluationStatus: v1alpha2common.StateFailed, + PostDeploymentEvaluationStatus: v1alpha2common.StateFailed, + WorkloadOverallStatus: v1alpha2common.StateFailed, + WorkloadStatus: []v1alpha2.WorkloadStatus{ + { + Workload: v1alpha2.KeptnWorkloadRef{ + Name: "name1", + Version: "1", + }, + Status: v1alpha2common.StateFailed, + }, + { + Workload: v1alpha2.KeptnWorkloadRef{ + Name: "name2", + Version: "2", + }, + Status: v1alpha2common.StateFailed, + }, + }, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def1", + Name: "name1", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def12", + Name: "name12", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def2", + Name: "name2", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def22", + Name: "name22", + Status: v1alpha2common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def3", + Name: "name3", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def32", + Name: "name32", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def4", + Name: "name4", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def42", + Name: "name42", + Status: v1alpha2common.StateFailed, + }, + }, + PhaseTraceIDs: v1alpha2common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: v1alpha2common.StateFailed, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := v1alpha2.KeptnAppVersion{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: v1alpha2.KeptnAppVersionSpec{}, + Status: v1alpha2.KeptnAppVersionStatus{}, + } + if err := tt.src.ConvertTo(&dst); (err != nil) != tt.wantErr { + t.Errorf("ConvertTo() error = %v, wantErr %v", err, tt.wantErr) + } + if tt.wantObj != nil { + require.Equal(t, tt.wantObj, &dst, "Object was not converted correctly") + } + }) + } +} + +func TestKeptnAppVersion_ConvertFrom_Errorcase(t *testing.T) { + // A random different object is used here to simulate a different API version + testObj := v2.ExternalJob{} + + dst := &KeptnAppVersion{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: KeptnAppVersionSpec{}, + Status: KeptnAppVersionStatus{}, + } + + if err := dst.ConvertFrom(&testObj); err == nil { + t.Errorf("ConvertFrom() error = %v", err) + } else { + require.ErrorIs(t, err, common.ErrCannotCastKeptnAppVersion) + } +} + +func TestKeptnAppVersion_ConvertTo_Errorcase(t *testing.T) { + testObj := KeptnAppVersion{} + + // A random different object is used here to simulate a different API version + dst := v2.ExternalJob{} + + if err := testObj.ConvertTo(&dst); err == nil { + t.Errorf("ConvertTo() error = %v", err) + } else { + require.ErrorIs(t, err, common.ErrCannotCastKeptnAppVersion) + } +} diff --git a/operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_conversion.go new file mode 100644 index 0000000000..cf197d708c --- /dev/null +++ b/operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_conversion.go @@ -0,0 +1,203 @@ +package v1alpha1 + +import ( + "fmt" + + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" + v1alpha2common "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2/common" + "go.opentelemetry.io/otel/propagation" + "sigs.k8s.io/controller-runtime/pkg/conversion" +) + +// ConvertTo converts the src v1alpha1.KeptnWorkloadInstance to the hub version (v1alpha2.KeptnWorkloadInstance) +func (src *KeptnWorkloadInstance) ConvertTo(dstRaw conversion.Hub) error { + dst, ok := dstRaw.(*v1alpha2.KeptnWorkloadInstance) + + if !ok { + return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastKeptnWorkloadInstance) + } + + // Copy equal stuff to new object + // DO NOT COPY TypeMeta + dst.ObjectMeta = src.ObjectMeta + + dst.Spec.AppName = src.Spec.AppName + dst.Spec.Version = src.Spec.Version + dst.Spec.PreDeploymentTasks = src.Spec.PreDeploymentTasks + dst.Spec.PostDeploymentTasks = src.Spec.PostDeploymentTasks + dst.Spec.PreDeploymentEvaluations = src.Spec.PreDeploymentEvaluations + dst.Spec.PostDeploymentEvaluations = src.Spec.PostDeploymentEvaluations + dst.Spec.ResourceReference = v1alpha2.ResourceReference{ + UID: src.Spec.ResourceReference.UID, + Kind: src.Spec.ResourceReference.Kind, + Name: src.Spec.ResourceReference.Name, + } + + dst.Spec.WorkloadName = src.Spec.WorkloadName + dst.Spec.PreviousVersion = src.Spec.PreviousVersion + dst.Spec.TraceId = make(map[string]string, len(src.Spec.TraceId)) + for k, v := range src.Spec.TraceId { + dst.Spec.TraceId[k] = v + } + + dst.Status.PreDeploymentStatus = v1alpha2common.KeptnState(src.Status.PreDeploymentStatus) + dst.Status.PostDeploymentStatus = v1alpha2common.KeptnState(src.Status.PostDeploymentStatus) + dst.Status.PreDeploymentEvaluationStatus = v1alpha2common.KeptnState(src.Status.PreDeploymentEvaluationStatus) + dst.Status.PostDeploymentEvaluationStatus = v1alpha2common.KeptnState(src.Status.PostDeploymentEvaluationStatus) + dst.Status.DeploymentStatus = v1alpha2common.KeptnState(src.Status.DeploymentStatus) + dst.Status.Status = v1alpha2common.KeptnState(src.Status.Status) + + dst.Status.CurrentPhase = src.Status.CurrentPhase + + // Convert changed fields + for _, item := range src.Status.PreDeploymentTaskStatus { + dst.Status.PreDeploymentTaskStatus = append(dst.Status.PreDeploymentTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.TaskDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.TaskName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentTaskStatus { + dst.Status.PostDeploymentTaskStatus = append(dst.Status.PostDeploymentTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.TaskDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.TaskName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PreDeploymentEvaluationTaskStatus { + dst.Status.PreDeploymentEvaluationTaskStatus = append(dst.Status.PreDeploymentEvaluationTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.EvaluationDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.EvaluationName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentEvaluationTaskStatus { + dst.Status.PostDeploymentEvaluationTaskStatus = append(dst.Status.PostDeploymentEvaluationTaskStatus, v1alpha2.ItemStatus{ + DefinitionName: item.EvaluationDefinitionName, + Status: v1alpha2common.KeptnState(item.Status), + Name: item.EvaluationName, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + dst.Status.PhaseTraceIDs = make(v1alpha2common.PhaseTraceID, len(src.Status.PhaseTraceIDs)) + for k, v := range src.Status.PhaseTraceIDs { + c := make(propagation.MapCarrier, len(v)) + for k1, v1 := range v { + c[k1] = v1 + } + dst.Status.PhaseTraceIDs[k] = c + } + + dst.Status.StartTime = src.Status.StartTime + dst.Status.EndTime = src.Status.EndTime + + return nil +} + +// ConvertFrom converts from the hub version (v1alpha2.KeptnWorkloadInstance) to this version (v1alpha1.KeptnWorkloadInstance) +func (dst *KeptnWorkloadInstance) ConvertFrom(srcRaw conversion.Hub) error { + src, ok := srcRaw.(*v1alpha2.KeptnWorkloadInstance) + + if !ok { + return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastKeptnWorkloadInstance) + } + + // Copy equal stuff to new object + // DO NOT COPY TypeMeta + dst.ObjectMeta = src.ObjectMeta + + dst.Spec.AppName = src.Spec.AppName + dst.Spec.Version = src.Spec.Version + dst.Spec.PreDeploymentTasks = src.Spec.PreDeploymentTasks + dst.Spec.PostDeploymentTasks = src.Spec.PostDeploymentTasks + dst.Spec.PreDeploymentEvaluations = src.Spec.PreDeploymentEvaluations + dst.Spec.PostDeploymentEvaluations = src.Spec.PostDeploymentEvaluations + dst.Spec.ResourceReference = ResourceReference{ + UID: src.Spec.ResourceReference.UID, + Kind: src.Spec.ResourceReference.Kind, + Name: src.Spec.ResourceReference.Name, + } + + dst.Spec.WorkloadName = src.Spec.WorkloadName + dst.Spec.PreviousVersion = src.Spec.PreviousVersion + dst.Spec.TraceId = make(map[string]string, len(src.Spec.TraceId)) + for k, v := range src.Spec.TraceId { + dst.Spec.TraceId[k] = v + } + + dst.Status.PreDeploymentStatus = common.KeptnState(src.Status.PreDeploymentStatus) + dst.Status.PostDeploymentStatus = common.KeptnState(src.Status.PostDeploymentStatus) + dst.Status.PreDeploymentEvaluationStatus = common.KeptnState(src.Status.PreDeploymentEvaluationStatus) + dst.Status.PostDeploymentEvaluationStatus = common.KeptnState(src.Status.PostDeploymentEvaluationStatus) + dst.Status.DeploymentStatus = common.KeptnState(src.Status.DeploymentStatus) + dst.Status.Status = common.KeptnState(src.Status.Status) + + dst.Status.CurrentPhase = src.Status.CurrentPhase + + // Convert changed fields + for _, item := range src.Status.PreDeploymentTaskStatus { + dst.Status.PreDeploymentTaskStatus = append(dst.Status.PreDeploymentTaskStatus, TaskStatus{ + TaskDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + TaskName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentTaskStatus { + dst.Status.PostDeploymentTaskStatus = append(dst.Status.PostDeploymentTaskStatus, TaskStatus{ + TaskDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + TaskName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PreDeploymentEvaluationTaskStatus { + dst.Status.PreDeploymentEvaluationTaskStatus = append(dst.Status.PreDeploymentEvaluationTaskStatus, EvaluationStatus{ + EvaluationDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + EvaluationName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + for _, item := range src.Status.PostDeploymentEvaluationTaskStatus { + dst.Status.PostDeploymentEvaluationTaskStatus = append(dst.Status.PostDeploymentEvaluationTaskStatus, EvaluationStatus{ + EvaluationDefinitionName: item.DefinitionName, + Status: common.KeptnState(item.Status), + EvaluationName: item.Name, + StartTime: item.StartTime, + EndTime: item.EndTime, + }) + } + + dst.Status.PhaseTraceIDs = make(common.PhaseTraceID, len(src.Status.PhaseTraceIDs)) + for k, v := range src.Status.PhaseTraceIDs { + c := make(propagation.MapCarrier, len(v)) + for k1, v1 := range v { + c[k1] = v1 + } + dst.Status.PhaseTraceIDs[k] = c + } + + dst.Status.StartTime = src.Status.StartTime + dst.Status.EndTime = src.Status.EndTime + + return nil +} diff --git a/operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_conversion_test.go b/operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_conversion_test.go new file mode 100644 index 0000000000..d2f2169ba6 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha1/keptnworkloadinstance_conversion_test.go @@ -0,0 +1,545 @@ +package v1alpha1 + +import ( + "testing" + + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" + v1alpha2common "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2/common" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/propagation" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" +) + +func TestKeptnWorkloadInstance_ConvertFrom(t *testing.T) { + tests := []struct { + name string + srcObj *v1alpha2.KeptnWorkloadInstance + wantErr bool + wantObj *KeptnWorkloadInstance + }{ + { + name: "Test that conversion from v1alpha2 to v1alpha1 works", + srcObj: &v1alpha2.KeptnWorkloadInstance{ + TypeMeta: v1.TypeMeta{ + Kind: "KeptnWorkloadInstance", + APIVersion: "lifecycle.keptn.sh/v1alpha2", + }, + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: v1alpha2.KeptnWorkloadInstanceSpec{ + KeptnWorkloadSpec: v1alpha2.KeptnWorkloadSpec{ + Version: "1.2.3", + ResourceReference: v1alpha2.ResourceReference{ + UID: types.UID("1"), + Kind: "Pod", + Name: "pod", + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + AppName: "app", + }, + WorkloadName: "workload", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: v1alpha2.KeptnWorkloadInstanceStatus{ + PreDeploymentStatus: v1alpha2common.StateFailed, + PostDeploymentStatus: v1alpha2common.StateFailed, + PreDeploymentEvaluationStatus: v1alpha2common.StateFailed, + PostDeploymentEvaluationStatus: v1alpha2common.StateFailed, + DeploymentStatus: v1alpha2common.StateFailed, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def1", + Name: "name1", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def12", + Name: "name12", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def2", + Name: "name2", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def22", + Name: "name22", + Status: v1alpha2common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def3", + Name: "name3", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def32", + Name: "name32", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def4", + Name: "name4", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def42", + Name: "name42", + Status: v1alpha2common.StateFailed, + }, + }, + PhaseTraceIDs: v1alpha2common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: v1alpha2common.StateFailed, + }, + }, + wantErr: false, + wantObj: &KeptnWorkloadInstance{ + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: KeptnWorkloadInstanceSpec{ + KeptnWorkloadSpec: KeptnWorkloadSpec{ + Version: "1.2.3", + ResourceReference: ResourceReference{ + UID: types.UID("1"), + Kind: "Pod", + Name: "pod", + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + AppName: "app", + }, + WorkloadName: "workload", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: KeptnWorkloadInstanceStatus{ + PreDeploymentStatus: common.StateFailed, + PostDeploymentStatus: common.StateFailed, + PreDeploymentEvaluationStatus: common.StateFailed, + PostDeploymentEvaluationStatus: common.StateFailed, + DeploymentStatus: common.StateFailed, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def1", + TaskName: "name1", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def12", + TaskName: "name12", + Status: common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def2", + TaskName: "name2", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def22", + TaskName: "name22", + Status: common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def3", + EvaluationName: "name3", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def32", + EvaluationName: "name32", + Status: common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def4", + EvaluationName: "name4", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def42", + EvaluationName: "name42", + Status: common.StateFailed, + }, + }, + PhaseTraceIDs: common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: common.StateFailed, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := &KeptnWorkloadInstance{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: KeptnWorkloadInstanceSpec{}, + Status: KeptnWorkloadInstanceStatus{}, + } + if err := dst.ConvertFrom(tt.srcObj); (err != nil) != tt.wantErr { + t.Errorf("ConvertFrom() error = %v, wantErr %v", err, tt.wantErr) + } + if tt.wantObj != nil { + require.Equal(t, tt.wantObj, dst, "Object was not converted correctly") + } + }) + } +} + +func TestKeptnWorkloadInstance_ConvertTo(t *testing.T) { + tests := []struct { + name string + src *KeptnWorkloadInstance + wantErr bool + wantObj *v1alpha2.KeptnWorkloadInstance + }{ + { + name: "Test that conversion from v1alpha1 to v1alpha2 works", + src: &KeptnWorkloadInstance{ + TypeMeta: v1.TypeMeta{ + Kind: "KeptnWorkloadInstance", + APIVersion: "lifecycle.keptn.sh/v1alpha1", + }, + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: KeptnWorkloadInstanceSpec{ + KeptnWorkloadSpec: KeptnWorkloadSpec{ + Version: "1.2.3", + ResourceReference: ResourceReference{ + UID: types.UID("1"), + Kind: "Pod", + Name: "pod", + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + AppName: "app", + }, + WorkloadName: "workload", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: KeptnWorkloadInstanceStatus{ + PreDeploymentStatus: common.StateFailed, + PostDeploymentStatus: common.StateFailed, + PreDeploymentEvaluationStatus: common.StateFailed, + PostDeploymentEvaluationStatus: common.StateFailed, + DeploymentStatus: common.StateFailed, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def1", + TaskName: "name1", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def12", + TaskName: "name12", + Status: common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []TaskStatus{ + { + TaskDefinitionName: "def2", + TaskName: "name2", + Status: common.StateFailed, + }, + { + TaskDefinitionName: "def22", + TaskName: "name22", + Status: common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def3", + EvaluationName: "name3", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def32", + EvaluationName: "name32", + Status: common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ + { + EvaluationDefinitionName: "def4", + EvaluationName: "name4", + Status: common.StateFailed, + }, + { + EvaluationDefinitionName: "def42", + EvaluationName: "name42", + Status: common.StateFailed, + }, + }, + PhaseTraceIDs: common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: common.StateFailed, + }, + }, + wantErr: false, + wantObj: &v1alpha2.KeptnWorkloadInstance{ + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: v1alpha2.KeptnWorkloadInstanceSpec{ + KeptnWorkloadSpec: v1alpha2.KeptnWorkloadSpec{ + Version: "1.2.3", + ResourceReference: v1alpha2.ResourceReference{ + UID: types.UID("1"), + Kind: "Pod", + Name: "pod", + }, + PreDeploymentTasks: []string{ + "some-pre-deployment-task1", + }, + PostDeploymentTasks: []string{ + "some-post-deployment-task2", + }, + PreDeploymentEvaluations: []string{ + "some-pre-evaluation-task1", + }, + PostDeploymentEvaluations: []string{ + "some-pre-evaluation-task2", + }, + AppName: "app", + }, + WorkloadName: "workload", + PreviousVersion: "1.0", + TraceId: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + }, + Status: v1alpha2.KeptnWorkloadInstanceStatus{ + PreDeploymentStatus: v1alpha2common.StateFailed, + PostDeploymentStatus: v1alpha2common.StateFailed, + PreDeploymentEvaluationStatus: v1alpha2common.StateFailed, + PostDeploymentEvaluationStatus: v1alpha2common.StateFailed, + DeploymentStatus: v1alpha2common.StateFailed, + CurrentPhase: "phase", + PreDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def1", + Name: "name1", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def12", + Name: "name12", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def2", + Name: "name2", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def22", + Name: "name22", + Status: v1alpha2common.StateFailed, + }, + }, + PreDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def3", + Name: "name3", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def32", + Name: "name32", + Status: v1alpha2common.StateFailed, + }, + }, + PostDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ + { + DefinitionName: "def4", + Name: "name4", + Status: v1alpha2common.StateFailed, + }, + { + DefinitionName: "def42", + Name: "name42", + Status: v1alpha2common.StateFailed, + }, + }, + PhaseTraceIDs: v1alpha2common.PhaseTraceID{ + "key": propagation.MapCarrier{ + "key1": "value1", + "key2": "value2", + }, + "key22": propagation.MapCarrier{ + "key122": "value122", + "key222": "value222", + }, + }, + Status: v1alpha2common.StateFailed, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := v1alpha2.KeptnWorkloadInstance{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: v1alpha2.KeptnWorkloadInstanceSpec{}, + Status: v1alpha2.KeptnWorkloadInstanceStatus{}, + } + if err := tt.src.ConvertTo(&dst); (err != nil) != tt.wantErr { + t.Errorf("ConvertTo() error = %v, wantErr %v", err, tt.wantErr) + } + if tt.wantObj != nil { + require.Equal(t, tt.wantObj, &dst, "Object was not converted correctly") + } + }) + } +} + +func TestKeptnWorkloadInstance_ConvertFrom_Errorcase(t *testing.T) { + // A random different object is used here to simulate a different API version + testObj := v2.ExternalJob{} + + dst := &KeptnWorkloadInstance{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: KeptnWorkloadInstanceSpec{}, + Status: KeptnWorkloadInstanceStatus{}, + } + + if err := dst.ConvertFrom(&testObj); err == nil { + t.Errorf("ConvertFrom() error = %v", err) + } else { + require.ErrorIs(t, err, common.ErrCannotCastKeptnWorkloadInstance) + } +} + +func TestKeptnWorkloadInstance_ConvertTo_Errorcase(t *testing.T) { + testObj := KeptnWorkloadInstance{} + + // A random different object is used here to simulate a different API version + dst := v2.ExternalJob{} + + if err := testObj.ConvertTo(&dst); err == nil { + t.Errorf("ConvertTo() error = %v", err) + } else { + require.ErrorIs(t, err, common.ErrCannotCastKeptnWorkloadInstance) + } +} diff --git a/operator/apis/lifecycle/v1alpha2/keptnappversion_conversion.go b/operator/apis/lifecycle/v1alpha2/keptnappversion_conversion.go new file mode 100644 index 0000000000..a36fb95494 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha2/keptnappversion_conversion.go @@ -0,0 +1,6 @@ +package v1alpha2 + +// Hub is the stub function to make the API conversion pattern with hub and spokes complete +func (*KeptnAppVersion) Hub() { + // Hub() needed to implement interface +} diff --git a/operator/apis/lifecycle/v1alpha2/keptnappversion_test.go b/operator/apis/lifecycle/v1alpha2/keptnappversion_test.go index 27d62e4529..73a42f3d30 100644 --- a/operator/apis/lifecycle/v1alpha2/keptnappversion_test.go +++ b/operator/apis/lifecycle/v1alpha2/keptnappversion_test.go @@ -25,32 +25,32 @@ func TestKeptnAppVersion(t *testing.T) { PostDeploymentEvaluationStatus: common.StateFailed, WorkloadOverallStatus: common.StateFailed, Status: common.StateFailed, - PreDeploymentTaskStatus: []TaskStatus{ + PreDeploymentTaskStatus: []ItemStatus{ { - TaskDefinitionName: "defname", - Status: common.StateFailed, - TaskName: "taskname", + DefinitionName: "defname", + Status: common.StateFailed, + Name: "taskname", }, }, - PostDeploymentTaskStatus: []TaskStatus{ + PostDeploymentTaskStatus: []ItemStatus{ { - TaskDefinitionName: "defname2", - Status: common.StateFailed, - TaskName: "taskname2", + DefinitionName: "defname2", + Status: common.StateFailed, + Name: "taskname2", }, }, - PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ + PreDeploymentEvaluationTaskStatus: []ItemStatus{ { - EvaluationDefinitionName: "defname3", - Status: common.StateFailed, - EvaluationName: "taskname3", + DefinitionName: "defname3", + Status: common.StateFailed, + Name: "taskname3", }, }, - PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ + PostDeploymentEvaluationTaskStatus: []ItemStatus{ { - EvaluationDefinitionName: "defname4", - Status: common.StateFailed, - EvaluationName: "taskname4", + DefinitionName: "defname4", + Status: common.StateFailed, + Name: "taskname4", }, }, CurrentPhase: common.PhaseAppDeployment.ShortName, @@ -124,35 +124,35 @@ func TestKeptnAppVersion(t *testing.T) { require.Equal(t, []string{"task5", "task6"}, app.GetPreDeploymentEvaluations()) require.Equal(t, []string{"task7", "task8"}, app.GetPostDeploymentEvaluations()) - require.Equal(t, []TaskStatus{ + require.Equal(t, []ItemStatus{ { - TaskDefinitionName: "defname", - Status: common.StateFailed, - TaskName: "taskname", + DefinitionName: "defname", + Status: common.StateFailed, + Name: "taskname", }, }, app.GetPreDeploymentTaskStatus()) - require.Equal(t, []TaskStatus{ + require.Equal(t, []ItemStatus{ { - TaskDefinitionName: "defname2", - Status: common.StateFailed, - TaskName: "taskname2", + DefinitionName: "defname2", + Status: common.StateFailed, + Name: "taskname2", }, }, app.GetPostDeploymentTaskStatus()) - require.Equal(t, []EvaluationStatus{ + require.Equal(t, []ItemStatus{ { - EvaluationDefinitionName: "defname3", - Status: common.StateFailed, - EvaluationName: "taskname3", + DefinitionName: "defname3", + Status: common.StateFailed, + Name: "taskname3", }, }, app.GetPreDeploymentEvaluationTaskStatus()) - require.Equal(t, []EvaluationStatus{ + require.Equal(t, []ItemStatus{ { - EvaluationDefinitionName: "defname4", - Status: common.StateFailed, - EvaluationName: "taskname4", + DefinitionName: "defname4", + Status: common.StateFailed, + Name: "taskname4", }, }, app.GetPostDeploymentEvaluationTaskStatus()) diff --git a/operator/apis/lifecycle/v1alpha2/keptnappversion_types.go b/operator/apis/lifecycle/v1alpha2/keptnappversion_types.go index e85dd3c17d..02821304c7 100644 --- a/operator/apis/lifecycle/v1alpha2/keptnappversion_types.go +++ b/operator/apis/lifecycle/v1alpha2/keptnappversion_types.go @@ -54,10 +54,10 @@ type KeptnAppVersionStatus struct { 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"` + PreDeploymentTaskStatus []ItemStatus `json:"preDeploymentTaskStatus,omitempty"` + PostDeploymentTaskStatus []ItemStatus `json:"postDeploymentTaskStatus,omitempty"` + PreDeploymentEvaluationTaskStatus []ItemStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"` + PostDeploymentEvaluationTaskStatus []ItemStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"` PhaseTraceIDs common.PhaseTraceID `json:"phaseTraceIDs,omitempty"` // +kubebuilder:default:=Pending Status common.KeptnState `json:"status,omitempty"` @@ -254,11 +254,11 @@ func (a KeptnAppVersion) GetPostDeploymentTasks() []string { return a.Spec.PostDeploymentTasks } -func (a KeptnAppVersion) GetPreDeploymentTaskStatus() []TaskStatus { +func (a KeptnAppVersion) GetPreDeploymentTaskStatus() []ItemStatus { return a.Status.PreDeploymentTaskStatus } -func (a KeptnAppVersion) GetPostDeploymentTaskStatus() []TaskStatus { +func (a KeptnAppVersion) GetPostDeploymentTaskStatus() []ItemStatus { return a.Status.PostDeploymentTaskStatus } @@ -270,11 +270,11 @@ func (a KeptnAppVersion) GetPostDeploymentEvaluations() []string { return a.Spec.PostDeploymentEvaluations } -func (a KeptnAppVersion) GetPreDeploymentEvaluationTaskStatus() []EvaluationStatus { +func (a KeptnAppVersion) GetPreDeploymentEvaluationTaskStatus() []ItemStatus { return a.Status.PreDeploymentEvaluationTaskStatus } -func (a KeptnAppVersion) GetPostDeploymentEvaluationTaskStatus() []EvaluationStatus { +func (a KeptnAppVersion) GetPostDeploymentEvaluationTaskStatus() []ItemStatus { return a.Status.PostDeploymentEvaluationTaskStatus } diff --git a/operator/apis/lifecycle/v1alpha2/keptnappversion_webhook.go b/operator/apis/lifecycle/v1alpha2/keptnappversion_webhook.go new file mode 100644 index 0000000000..b9f35f21e5 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha2/keptnappversion_webhook.go @@ -0,0 +1,11 @@ +package v1alpha2 + +import ( + ctrl "sigs.k8s.io/controller-runtime" +) + +func (r *KeptnAppVersion) SetupWebhookWithManager(mgr ctrl.Manager) error { + return ctrl.NewWebhookManagedBy(mgr). + For(r). + Complete() +} diff --git a/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_conversion.go b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_conversion.go new file mode 100644 index 0000000000..2dc66c1fc8 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_conversion.go @@ -0,0 +1,6 @@ +package v1alpha2 + +// Hub is the stub function to make the API conversion pattern with hub and spokes complete +func (*KeptnWorkloadInstance) Hub() { + // Hub() needed to implement interface +} diff --git a/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_test.go b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_test.go index 8f93ed3655..3a9305bab2 100644 --- a/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_test.go +++ b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_test.go @@ -25,32 +25,32 @@ func TestKeptnWorkloadInstance(t *testing.T) { PostDeploymentEvaluationStatus: common.StateFailed, DeploymentStatus: common.StateFailed, Status: common.StateFailed, - PreDeploymentTaskStatus: []TaskStatus{ + PreDeploymentTaskStatus: []ItemStatus{ { - TaskDefinitionName: "defname", - Status: common.StateFailed, - TaskName: "taskname", + DefinitionName: "defname", + Status: common.StateFailed, + Name: "taskname", }, }, - PostDeploymentTaskStatus: []TaskStatus{ + PostDeploymentTaskStatus: []ItemStatus{ { - TaskDefinitionName: "defname2", - Status: common.StateFailed, - TaskName: "taskname2", + DefinitionName: "defname2", + Status: common.StateFailed, + Name: "taskname2", }, }, - PreDeploymentEvaluationTaskStatus: []EvaluationStatus{ + PreDeploymentEvaluationTaskStatus: []ItemStatus{ { - EvaluationDefinitionName: "defname3", - Status: common.StateFailed, - EvaluationName: "taskname3", + DefinitionName: "defname3", + Status: common.StateFailed, + Name: "taskname3", }, }, - PostDeploymentEvaluationTaskStatus: []EvaluationStatus{ + PostDeploymentEvaluationTaskStatus: []ItemStatus{ { - EvaluationDefinitionName: "defname4", - Status: common.StateFailed, - EvaluationName: "taskname4", + DefinitionName: "defname4", + Status: common.StateFailed, + Name: "taskname4", }, }, CurrentPhase: common.PhaseAppDeployment.ShortName, @@ -128,35 +128,35 @@ func TestKeptnWorkloadInstance(t *testing.T) { require.Equal(t, []string{"task5", "task6"}, workload.GetPreDeploymentEvaluations()) require.Equal(t, []string{"task7", "task8"}, workload.GetPostDeploymentEvaluations()) - require.Equal(t, []TaskStatus{ + require.Equal(t, []ItemStatus{ { - TaskDefinitionName: "defname", - Status: common.StateFailed, - TaskName: "taskname", + DefinitionName: "defname", + Status: common.StateFailed, + Name: "taskname", }, }, workload.GetPreDeploymentTaskStatus()) - require.Equal(t, []TaskStatus{ + require.Equal(t, []ItemStatus{ { - TaskDefinitionName: "defname2", - Status: common.StateFailed, - TaskName: "taskname2", + DefinitionName: "defname2", + Status: common.StateFailed, + Name: "taskname2", }, }, workload.GetPostDeploymentTaskStatus()) - require.Equal(t, []EvaluationStatus{ + require.Equal(t, []ItemStatus{ { - EvaluationDefinitionName: "defname3", - Status: common.StateFailed, - EvaluationName: "taskname3", + DefinitionName: "defname3", + Status: common.StateFailed, + Name: "taskname3", }, }, workload.GetPreDeploymentEvaluationTaskStatus()) - require.Equal(t, []EvaluationStatus{ + require.Equal(t, []ItemStatus{ { - EvaluationDefinitionName: "defname4", - Status: common.StateFailed, - EvaluationName: "taskname4", + DefinitionName: "defname4", + Status: common.StateFailed, + Name: "taskname4", }, }, workload.GetPostDeploymentEvaluationTaskStatus()) diff --git a/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go index 09123879c0..7611378f33 100644 --- a/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go +++ b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_types.go @@ -51,10 +51,10 @@ type KeptnWorkloadInstanceStatus struct { 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"` + PreDeploymentTaskStatus []ItemStatus `json:"preDeploymentTaskStatus,omitempty"` + PostDeploymentTaskStatus []ItemStatus `json:"postDeploymentTaskStatus,omitempty"` + PreDeploymentEvaluationTaskStatus []ItemStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"` + PostDeploymentEvaluationTaskStatus []ItemStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"` StartTime metav1.Time `json:"startTime,omitempty"` EndTime metav1.Time `json:"endTime,omitempty"` CurrentPhase string `json:"currentPhase,omitempty"` @@ -63,22 +63,15 @@ type KeptnWorkloadInstanceStatus struct { Status common.KeptnState `json:"status,omitempty"` } -type TaskStatus struct { - TaskDefinitionName string `json:"taskDefinitionName,omitempty"` +type ItemStatus struct { + // DefinitionName is the name of the EvaluationDefinition/TaskDefiniton + DefinitionName string `json:"definitionName,omitempty"` // +kubebuilder:default:=Pending - Status common.KeptnState `json:"status,omitempty"` - TaskName string `json:"taskName,omitempty"` - StartTime metav1.Time `json:"startTime,omitempty"` - EndTime metav1.Time `json:"endTime,omitempty"` -} - -type EvaluationStatus struct { - EvaluationDefinitionName string `json:"evaluationDefinitionName,omitempty"` - // +kubebuilder:default:=Pending - Status common.KeptnState `json:"status,omitempty"` - EvaluationName string `json:"evaluationName,omitempty"` - StartTime metav1.Time `json:"startTime,omitempty"` - EndTime metav1.Time `json:"endTime,omitempty"` + Status common.KeptnState `json:"status,omitempty"` + // Name is the name of the Evaluation/Task + Name string `json:"name,omitempty"` + StartTime metav1.Time `json:"startTime,omitempty"` + EndTime metav1.Time `json:"endTime,omitempty"` } //+kubebuilder:object:root=true @@ -217,25 +210,13 @@ func (w *KeptnWorkloadInstance) Complete() { w.SetEndTime() } -func (t *TaskStatus) SetStartTime() { - if t.StartTime.IsZero() { - t.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (t *TaskStatus) SetEndTime() { - if t.EndTime.IsZero() { - t.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (e *EvaluationStatus) SetStartTime() { +func (e *ItemStatus) SetStartTime() { if e.StartTime.IsZero() { e.StartTime = metav1.NewTime(time.Now().UTC()) } } -func (e *EvaluationStatus) SetEndTime() { +func (e *ItemStatus) SetEndTime() { if e.EndTime.IsZero() { e.EndTime = metav1.NewTime(time.Now().UTC()) } @@ -281,11 +262,11 @@ func (w KeptnWorkloadInstance) GetPostDeploymentTasks() []string { return w.Spec.PostDeploymentTasks } -func (w KeptnWorkloadInstance) GetPreDeploymentTaskStatus() []TaskStatus { +func (w KeptnWorkloadInstance) GetPreDeploymentTaskStatus() []ItemStatus { return w.Status.PreDeploymentTaskStatus } -func (w KeptnWorkloadInstance) GetPostDeploymentTaskStatus() []TaskStatus { +func (w KeptnWorkloadInstance) GetPostDeploymentTaskStatus() []ItemStatus { return w.Status.PostDeploymentTaskStatus } @@ -297,11 +278,11 @@ func (w KeptnWorkloadInstance) GetPostDeploymentEvaluations() []string { return w.Spec.PostDeploymentEvaluations } -func (w KeptnWorkloadInstance) GetPreDeploymentEvaluationTaskStatus() []EvaluationStatus { +func (w KeptnWorkloadInstance) GetPreDeploymentEvaluationTaskStatus() []ItemStatus { return w.Status.PreDeploymentEvaluationTaskStatus } -func (w KeptnWorkloadInstance) GetPostDeploymentEvaluationTaskStatus() []EvaluationStatus { +func (w KeptnWorkloadInstance) GetPostDeploymentEvaluationTaskStatus() []ItemStatus { return w.Status.PostDeploymentEvaluationTaskStatus } diff --git a/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_webhook.go b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_webhook.go new file mode 100644 index 0000000000..20fd5653bd --- /dev/null +++ b/operator/apis/lifecycle/v1alpha2/keptnworkloadinstance_webhook.go @@ -0,0 +1,11 @@ +package v1alpha2 + +import ( + ctrl "sigs.k8s.io/controller-runtime" +) + +func (r *KeptnWorkloadInstance) SetupWebhookWithManager(mgr ctrl.Manager) error { + return ctrl.NewWebhookManagedBy(mgr). + For(r). + Complete() +} diff --git a/operator/apis/lifecycle/v1alpha2/zz_generated.deepcopy.go b/operator/apis/lifecycle/v1alpha2/zz_generated.deepcopy.go index d307399f8b..73534e2f2f 100644 --- a/operator/apis/lifecycle/v1alpha2/zz_generated.deepcopy.go +++ b/operator/apis/lifecycle/v1alpha2/zz_generated.deepcopy.go @@ -57,23 +57,6 @@ func (in *ContainerSpec) DeepCopy() *ContainerSpec { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *EvaluationStatus) DeepCopyInto(out *EvaluationStatus) { - *out = *in - in.StartTime.DeepCopyInto(&out.StartTime) - in.EndTime.DeepCopyInto(&out.EndTime) -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new EvaluationStatus. -func (in *EvaluationStatus) DeepCopy() *EvaluationStatus { - if in == nil { - return nil - } - out := new(EvaluationStatus) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *EvaluationStatusItem) DeepCopyInto(out *EvaluationStatusItem) { *out = *in @@ -170,6 +153,23 @@ func (in *Inline) DeepCopy() *Inline { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ItemStatus) DeepCopyInto(out *ItemStatus) { + *out = *in + in.StartTime.DeepCopyInto(&out.StartTime) + in.EndTime.DeepCopyInto(&out.EndTime) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ItemStatus. +func (in *ItemStatus) DeepCopy() *ItemStatus { + if in == nil { + return nil + } + out := new(ItemStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *KeptnApp) DeepCopyInto(out *KeptnApp) { *out = *in @@ -376,28 +376,28 @@ func (in *KeptnAppVersionStatus) DeepCopyInto(out *KeptnAppVersionStatus) { } if in.PreDeploymentTaskStatus != nil { in, out := &in.PreDeploymentTaskStatus, &out.PreDeploymentTaskStatus - *out = make([]TaskStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.PostDeploymentTaskStatus != nil { in, out := &in.PostDeploymentTaskStatus, &out.PostDeploymentTaskStatus - *out = make([]TaskStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.PreDeploymentEvaluationTaskStatus != nil { in, out := &in.PreDeploymentEvaluationTaskStatus, &out.PreDeploymentEvaluationTaskStatus - *out = make([]EvaluationStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.PostDeploymentEvaluationTaskStatus != nil { in, out := &in.PostDeploymentEvaluationTaskStatus, &out.PostDeploymentEvaluationTaskStatus - *out = make([]EvaluationStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -1015,28 +1015,28 @@ func (in *KeptnWorkloadInstanceStatus) DeepCopyInto(out *KeptnWorkloadInstanceSt *out = *in if in.PreDeploymentTaskStatus != nil { in, out := &in.PreDeploymentTaskStatus, &out.PreDeploymentTaskStatus - *out = make([]TaskStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.PostDeploymentTaskStatus != nil { in, out := &in.PostDeploymentTaskStatus, &out.PostDeploymentTaskStatus - *out = make([]TaskStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.PreDeploymentEvaluationTaskStatus != nil { in, out := &in.PreDeploymentEvaluationTaskStatus, &out.PreDeploymentEvaluationTaskStatus - *out = make([]EvaluationStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.PostDeploymentEvaluationTaskStatus != nil { in, out := &in.PostDeploymentEvaluationTaskStatus, &out.PostDeploymentEvaluationTaskStatus - *out = make([]EvaluationStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -1252,23 +1252,6 @@ func (in *TaskParameters) DeepCopy() *TaskParameters { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *TaskStatus) DeepCopyInto(out *TaskStatus) { - *out = *in - in.StartTime.DeepCopyInto(&out.StartTime) - in.EndTime.DeepCopyInto(&out.EndTime) -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TaskStatus. -func (in *TaskStatus) DeepCopy() *TaskStatus { - if in == nil { - return nil - } - out := new(TaskStatus) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *WorkloadStatus) DeepCopyInto(out *WorkloadStatus) { *out = *in diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml index 6b6335d5dd..2b1fb5a87b 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnappversions.yaml @@ -359,12 +359,14 @@ spec: postDeploymentEvaluationTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string - evaluationDefinitionName: - type: string - evaluationName: + name: + description: name of Evaluation/Task type: string startTime: format: date-time @@ -380,19 +382,21 @@ spec: postDeploymentTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string + name: + description: name of Evaluation/Task + type: string startTime: format: date-time type: string status: default: Pending type: string - taskDefinitionName: - type: string - taskName: - type: string type: object type: array preDeploymentEvaluationStatus: @@ -401,12 +405,14 @@ spec: preDeploymentEvaluationTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string - evaluationDefinitionName: - type: string - evaluationName: + name: + description: name of Evaluation/Task type: string startTime: format: date-time @@ -422,19 +428,21 @@ spec: preDeploymentTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string + name: + description: name of Evaluation/Task + type: string startTime: format: date-time type: string status: default: Pending type: string - taskDefinitionName: - type: string - taskName: - type: string type: object type: array startTime: diff --git a/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml b/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml index fb829a9e3a..bc0ff5f97a 100644 --- a/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml +++ b/operator/config/crd/bases/lifecycle.keptn.sh_keptnworkloadinstances.yaml @@ -369,12 +369,14 @@ spec: postDeploymentEvaluationTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string - evaluationDefinitionName: - type: string - evaluationName: + name: + description: name of Evaluation/Task type: string startTime: format: date-time @@ -390,19 +392,21 @@ spec: postDeploymentTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string + name: + description: name of Evaluation/Task + type: string startTime: format: date-time type: string status: default: Pending type: string - taskDefinitionName: - type: string - taskName: - type: string type: object type: array preDeploymentEvaluationStatus: @@ -411,12 +415,14 @@ spec: preDeploymentEvaluationTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string - evaluationDefinitionName: - type: string - evaluationName: + name: + description: name of Evaluation/Task type: string startTime: format: date-time @@ -432,19 +438,21 @@ spec: preDeploymentTaskStatus: items: properties: + definitionName: + description: name of EvaluationDefinition/TaskDefiniton + type: string endTime: format: date-time type: string + name: + description: name of Evaluation/Task + type: string startTime: format: date-time type: string status: default: Pending type: string - taskDefinitionName: - type: string - taskName: - type: string type: object type: array startTime: diff --git a/operator/config/crd/kustomization.yaml b/operator/config/crd/kustomization.yaml index f700168cc1..b69c53b2a1 100644 --- a/operator/config/crd/kustomization.yaml +++ b/operator/config/crd/kustomization.yaml @@ -20,8 +20,8 @@ patchesStrategicMerge: #- patches/webhook_in_keptntasks.yaml - patches/webhook_in_keptnapps.yaml #- patches/webhook_in_keptnworkloads.yaml -#- patches/webhook_in_keptnworkloadinstances.yaml -#- patches/webhook_in_keptnappversions.yaml +- patches/webhook_in_keptnworkloadinstances.yaml +- patches/webhook_in_keptnappversions.yaml #- patches/webhook_in_keptnevaluationdefinitions.yaml - patches/webhook_in_keptnevaluationproviders.yaml #- patches/webhook_in_keptnevaluations.yaml @@ -33,8 +33,8 @@ patchesStrategicMerge: #- patches/cainjection_in_keptntasks.yaml - patches/cainjection_in_keptnapps.yaml #- patches/cainjection_in_keptnworkloads.yaml -#- patches/cainjection_in_keptnworkloadinstances.yaml -#- patches/cainjection_in_keptnappversions.yaml +- patches/cainjection_in_keptnworkloadinstances.yaml +- patches/cainjection_in_keptnappversions.yaml #- patches/cainjection_in_keptnevaluationdefinitions.yaml - patches/cainjection_in_keptnevaluationproviders.yaml #- patches/cainjection_in_keptnevaluations.yaml diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index 9a6960598c..84192a0588 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -35,14 +35,14 @@ type EvaluationCreateAttributes struct { CheckType apicommon.CheckType } -func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, evaluationCreateAttributes EvaluationCreateAttributes) ([]klcv1alpha2.EvaluationStatus, apicommon.StatusSummary, error) { +func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, evaluationCreateAttributes EvaluationCreateAttributes) ([]klcv1alpha2.ItemStatus, apicommon.StatusSummary, error) { piWrapper, err := interfaces.NewPhaseItemWrapperFromClientObject(reconcileObject) if err != nil { return nil, apicommon.StatusSummary{}, err } var evaluations []string - var statuses []klcv1alpha2.EvaluationStatus + var statuses []klcv1alpha2.ItemStatus switch evaluationCreateAttributes.CheckType { case apicommon.PreDeploymentEvaluationCheckType: @@ -56,16 +56,16 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co var summary apicommon.StatusSummary summary.Total = len(evaluations) // Check current state of the PrePostEvaluationTasks - var newStatus []klcv1alpha2.EvaluationStatus + var newStatus []klcv1alpha2.ItemStatus for _, evaluationName := range evaluations { var oldstatus apicommon.KeptnState for _, ts := range statuses { - if ts.EvaluationDefinitionName == evaluationName { + if ts.DefinitionName == evaluationName { oldstatus = ts.Status } } - evaluationStatus := GetEvaluationStatus(evaluationName, statuses) + evaluationStatus := GetItemStatus(evaluationName, statuses) evaluation := &klcv1alpha2.KeptnEvaluation{} evaluationExists := false @@ -80,10 +80,10 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co } // Check if Evaluation is already created - if evaluationStatus.EvaluationName != "" { - err := r.Client.Get(ctx, types.NamespacedName{Name: evaluationStatus.EvaluationName, Namespace: piWrapper.GetNamespace()}, evaluation) + if evaluationStatus.Name != "" { + err := r.Client.Get(ctx, types.NamespacedName{Name: evaluationStatus.Name, Namespace: piWrapper.GetNamespace()}, evaluation) if err != nil && errors.IsNotFound(err) { - evaluationStatus.EvaluationName = "" + evaluationStatus.Name = "" } else if err != nil { return nil, summary, err } @@ -97,7 +97,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co if err != nil { return nil, summary, err } - evaluationStatus.EvaluationName = evaluationName + evaluationStatus.Name = evaluationName evaluationStatus.SetStartTime() _, _, err = r.SpanHandler.GetSpan(phaseCtx, r.Tracer, evaluation, "") if err != nil { diff --git a/operator/controllers/common/evaluationhandler_test.go b/operator/controllers/common/evaluationhandler_test.go index 46c08f934b..8855666005 100644 --- a/operator/controllers/common/evaluationhandler_test.go +++ b/operator/controllers/common/evaluationhandler_test.go @@ -25,7 +25,7 @@ func TestEvaluationHandler(t *testing.T) { name string object client.Object createAttr EvaluationCreateAttributes - wantStatus []v1alpha2.EvaluationStatus + wantStatus []v1alpha2.ItemStatus wantSummary apicommon.StatusSummary evalObj v1alpha2.KeptnEvaluation wantErr error @@ -53,7 +53,7 @@ func TestEvaluationHandler(t *testing.T) { EvaluationDefinition: "", CheckType: apicommon.PreDeploymentEvaluationCheckType, }, - wantStatus: []v1alpha2.EvaluationStatus(nil), + wantStatus: []v1alpha2.ItemStatus(nil), wantSummary: apicommon.StatusSummary{}, wantErr: nil, getSpanCalls: 0, @@ -74,11 +74,11 @@ func TestEvaluationHandler(t *testing.T) { EvaluationDefinition: "eval-def", CheckType: apicommon.PreDeploymentEvaluationCheckType, }, - wantStatus: []v1alpha2.EvaluationStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - EvaluationDefinitionName: "eval-def", - Status: apicommon.StatePending, - EvaluationName: "pre-eval-eval-def-", + DefinitionName: "eval-def", + Status: apicommon.StatePending, + Name: "pre-eval-eval-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Pending: 1}, @@ -96,11 +96,11 @@ func TestEvaluationHandler(t *testing.T) { }, Status: v1alpha2.KeptnAppVersionStatus{ PreDeploymentEvaluationStatus: apicommon.StateSucceeded, - PreDeploymentEvaluationTaskStatus: []v1alpha2.EvaluationStatus{ + PreDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ { - EvaluationDefinitionName: "eval-def", - Status: apicommon.StateSucceeded, - EvaluationName: "pre-eval-eval-def-", + DefinitionName: "eval-def", + Status: apicommon.StateSucceeded, + Name: "pre-eval-eval-def-", }, }, }, @@ -111,11 +111,11 @@ func TestEvaluationHandler(t *testing.T) { EvaluationDefinition: "eval-def", CheckType: apicommon.PreDeploymentEvaluationCheckType, }, - wantStatus: []v1alpha2.EvaluationStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - EvaluationDefinitionName: "eval-def", - Status: apicommon.StateSucceeded, - EvaluationName: "pre-eval-eval-def-", + DefinitionName: "eval-def", + Status: apicommon.StateSucceeded, + Name: "pre-eval-eval-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Succeeded: 1}, @@ -136,11 +136,11 @@ func TestEvaluationHandler(t *testing.T) { }, Status: v1alpha2.KeptnAppVersionStatus{ PreDeploymentEvaluationStatus: apicommon.StateSucceeded, - PreDeploymentEvaluationTaskStatus: []v1alpha2.EvaluationStatus{ + PreDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ { - EvaluationDefinitionName: "eval-def", - Status: apicommon.StateProgressing, - EvaluationName: "pre-eval-eval-def-", + DefinitionName: "eval-def", + Status: apicommon.StateProgressing, + Name: "pre-eval-eval-def-", }, }, }, @@ -166,11 +166,11 @@ func TestEvaluationHandler(t *testing.T) { EvaluationDefinition: "eval-def", CheckType: apicommon.PreDeploymentEvaluationCheckType, }, - wantStatus: []v1alpha2.EvaluationStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - EvaluationDefinitionName: "eval-def", - Status: apicommon.StateFailed, - EvaluationName: "pre-eval-eval-def-", + DefinitionName: "eval-def", + Status: apicommon.StateFailed, + Name: "pre-eval-eval-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Failed: 1}, @@ -194,11 +194,11 @@ func TestEvaluationHandler(t *testing.T) { }, Status: v1alpha2.KeptnAppVersionStatus{ PreDeploymentEvaluationStatus: apicommon.StateSucceeded, - PreDeploymentEvaluationTaskStatus: []v1alpha2.EvaluationStatus{ + PreDeploymentEvaluationTaskStatus: []v1alpha2.ItemStatus{ { - EvaluationDefinitionName: "eval-def", - Status: apicommon.StateProgressing, - EvaluationName: "pre-eval-eval-def-", + DefinitionName: "eval-def", + Status: apicommon.StateProgressing, + Name: "pre-eval-eval-def-", }, }, }, @@ -217,11 +217,11 @@ func TestEvaluationHandler(t *testing.T) { EvaluationDefinition: "eval-def", CheckType: apicommon.PreDeploymentEvaluationCheckType, }, - wantStatus: []v1alpha2.EvaluationStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - EvaluationDefinitionName: "eval-def", - Status: apicommon.StateSucceeded, - EvaluationName: "pre-eval-eval-def-", + DefinitionName: "eval-def", + Status: apicommon.StateSucceeded, + Name: "pre-eval-eval-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Succeeded: 1}, @@ -258,8 +258,8 @@ func TestEvaluationHandler(t *testing.T) { status, summary, err := handler.ReconcileEvaluations(context.TODO(), context.TODO(), tt.object, tt.createAttr) if len(tt.wantStatus) == len(status) { for j, item := range status { - require.Equal(t, tt.wantStatus[j].EvaluationDefinitionName, item.EvaluationDefinitionName) - require.True(t, strings.Contains(item.EvaluationName, tt.wantStatus[j].EvaluationName)) + require.Equal(t, tt.wantStatus[j].DefinitionName, item.DefinitionName) + require.True(t, strings.Contains(item.Name, tt.wantStatus[j].Name)) require.Equal(t, tt.wantStatus[j].Status, item.Status) } } else { diff --git a/operator/controllers/common/helperfunctions.go b/operator/controllers/common/helperfunctions.go index f246561dd7..96a6a37db0 100644 --- a/operator/controllers/common/helperfunctions.go +++ b/operator/controllers/common/helperfunctions.go @@ -6,29 +6,16 @@ import ( "k8s.io/apimachinery/pkg/types" ) -func GetTaskStatus(taskName string, instanceStatus []klcv1alpha2.TaskStatus) klcv1alpha2.TaskStatus { +func GetItemStatus(name string, instanceStatus []klcv1alpha2.ItemStatus) klcv1alpha2.ItemStatus { for _, status := range instanceStatus { - if status.TaskDefinitionName == taskName { + if status.DefinitionName == name { return status } } - return klcv1alpha2.TaskStatus{ - TaskDefinitionName: taskName, - Status: apicommon.StatePending, - TaskName: "", - } -} - -func GetEvaluationStatus(evaluationName string, instanceStatus []klcv1alpha2.EvaluationStatus) klcv1alpha2.EvaluationStatus { - for _, status := range instanceStatus { - if status.EvaluationDefinitionName == evaluationName { - return status - } - } - return klcv1alpha2.EvaluationStatus{ - EvaluationDefinitionName: evaluationName, - Status: apicommon.StatePending, - EvaluationName: "", + return klcv1alpha2.ItemStatus{ + DefinitionName: name, + Status: apicommon.StatePending, + Name: "", } } diff --git a/operator/controllers/common/helperfunctions_test.go b/operator/controllers/common/helperfunctions_test.go index 7f775a6536..c6ef8512fc 100644 --- a/operator/controllers/common/helperfunctions_test.go +++ b/operator/controllers/common/helperfunctions_test.go @@ -9,110 +9,56 @@ import ( "k8s.io/apimachinery/pkg/types" ) -func Test_GetTaskStatus(t *testing.T) { +func Test_GetItemStatus(t *testing.T) { tests := []struct { name string - inStatus []klcv1alpha2.TaskStatus - want klcv1alpha2.TaskStatus + inStatus []klcv1alpha2.ItemStatus + want klcv1alpha2.ItemStatus }{ { name: "non-existing", - inStatus: []klcv1alpha2.TaskStatus{ + inStatus: []klcv1alpha2.ItemStatus{ { - TaskDefinitionName: "def-name", - TaskName: "name", - Status: apicommon.StatePending, + DefinitionName: "def-name", + Name: "name", + Status: apicommon.StatePending, }, }, - want: klcv1alpha2.TaskStatus{ - TaskDefinitionName: "non-existing", - Status: apicommon.StatePending, - TaskName: "", + want: klcv1alpha2.ItemStatus{ + DefinitionName: "non-existing", + Status: apicommon.StatePending, + Name: "", }, }, { name: "def-name", - inStatus: []klcv1alpha2.TaskStatus{ + inStatus: []klcv1alpha2.ItemStatus{ { - TaskDefinitionName: "def-name", - TaskName: "name", - Status: apicommon.StateProgressing, + DefinitionName: "def-name", + Name: "name", + Status: apicommon.StateProgressing, }, }, - want: klcv1alpha2.TaskStatus{ - TaskDefinitionName: "def-name", - TaskName: "name", - Status: apicommon.StateProgressing, + want: klcv1alpha2.ItemStatus{ + DefinitionName: "def-name", + Name: "name", + Status: apicommon.StateProgressing, }, }, { name: "empty", - inStatus: []klcv1alpha2.TaskStatus{}, - want: klcv1alpha2.TaskStatus{ - TaskDefinitionName: "empty", - Status: apicommon.StatePending, - TaskName: "", + inStatus: []klcv1alpha2.ItemStatus{}, + want: klcv1alpha2.ItemStatus{ + DefinitionName: "empty", + Status: apicommon.StatePending, + Name: "", }, }, } for _, tt := range tests { t.Run("", func(t *testing.T) { - require.Equal(t, GetTaskStatus(tt.name, tt.inStatus), tt.want) - }) - } -} - -func Test_GetEvaluationStatus(t *testing.T) { - tests := []struct { - name string - inStatus []klcv1alpha2.EvaluationStatus - want klcv1alpha2.EvaluationStatus - }{ - { - name: "non-existing", - inStatus: []klcv1alpha2.EvaluationStatus{ - { - EvaluationDefinitionName: "def-name", - EvaluationName: "name", - Status: apicommon.StatePending, - }, - }, - want: klcv1alpha2.EvaluationStatus{ - EvaluationDefinitionName: "non-existing", - Status: apicommon.StatePending, - EvaluationName: "", - }, - }, - { - name: "def-name", - inStatus: []klcv1alpha2.EvaluationStatus{ - { - EvaluationDefinitionName: "def-name", - EvaluationName: "name", - Status: apicommon.StateProgressing, - }, - }, - want: klcv1alpha2.EvaluationStatus{ - EvaluationDefinitionName: "def-name", - EvaluationName: "name", - Status: apicommon.StateProgressing, - }, - }, - { - name: "empty", - inStatus: []klcv1alpha2.EvaluationStatus{}, - want: klcv1alpha2.EvaluationStatus{ - EvaluationDefinitionName: "empty", - Status: apicommon.StatePending, - EvaluationName: "", - }, - }, - } - - for _, tt := range tests { - t.Run("", func(t *testing.T) { - require.Equal(t, GetEvaluationStatus(tt.name, tt.inStatus), tt.want) + require.Equal(t, GetItemStatus(tt.name, tt.inStatus), tt.want) }) } } diff --git a/operator/controllers/common/taskhandler.go b/operator/controllers/common/taskhandler.go index 932a4bcea5..1b0584cadd 100644 --- a/operator/controllers/common/taskhandler.go +++ b/operator/controllers/common/taskhandler.go @@ -35,7 +35,7 @@ type TaskCreateAttributes struct { CheckType apicommon.CheckType } -func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, taskCreateAttributes TaskCreateAttributes) ([]klcv1alpha2.TaskStatus, apicommon.StatusSummary, error) { +func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, taskCreateAttributes TaskCreateAttributes) ([]klcv1alpha2.ItemStatus, apicommon.StatusSummary, error) { piWrapper, err := interfaces.NewPhaseItemWrapperFromClientObject(reconcileObject) if err != nil { return nil, apicommon.StatusSummary{}, err @@ -47,7 +47,7 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex } var tasks []string - var statuses []klcv1alpha2.TaskStatus + var statuses []klcv1alpha2.ItemStatus switch taskCreateAttributes.CheckType { case apicommon.PreDeploymentCheckType: @@ -61,16 +61,16 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex var summary apicommon.StatusSummary summary.Total = len(tasks) // Check current state of the PrePostDeploymentTasks - var newStatus []klcv1alpha2.TaskStatus + var newStatus []klcv1alpha2.ItemStatus for _, taskDefinitionName := range tasks { var oldstatus apicommon.KeptnState for _, ts := range statuses { - if ts.TaskDefinitionName == taskDefinitionName { + if ts.DefinitionName == taskDefinitionName { oldstatus = ts.Status } } - taskStatus := GetTaskStatus(taskDefinitionName, statuses) + taskStatus := GetItemStatus(taskDefinitionName, statuses) task := &klcv1alpha2.KeptnTask{} taskExists := false @@ -85,10 +85,10 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex } // Check if Task is already created - if taskStatus.TaskName != "" { - err := r.Client.Get(ctx, types.NamespacedName{Name: taskStatus.TaskName, Namespace: piWrapper.GetNamespace()}, task) + if taskStatus.Name != "" { + err := r.Client.Get(ctx, types.NamespacedName{Name: taskStatus.Name, Namespace: piWrapper.GetNamespace()}, task) if err != nil && errors.IsNotFound(err) { - taskStatus.TaskName = "" + taskStatus.Name = "" } else if err != nil { return nil, summary, err } @@ -102,7 +102,7 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex if err != nil { return nil, summary, err } - taskStatus.TaskName = taskName + taskStatus.Name = taskName taskStatus.SetStartTime() _, _, err = r.SpanHandler.GetSpan(phaseCtx, r.Tracer, task, "") if err != nil { diff --git a/operator/controllers/common/taskhandler_test.go b/operator/controllers/common/taskhandler_test.go index d152262c01..62f2792d1e 100644 --- a/operator/controllers/common/taskhandler_test.go +++ b/operator/controllers/common/taskhandler_test.go @@ -24,7 +24,7 @@ func TestTaskHandler(t *testing.T) { name string object client.Object createAttr TaskCreateAttributes - wantStatus []v1alpha2.TaskStatus + wantStatus []v1alpha2.ItemStatus wantSummary apicommon.StatusSummary taskObj v1alpha2.KeptnTask wantErr error @@ -51,7 +51,7 @@ func TestTaskHandler(t *testing.T) { TaskDefinition: "", CheckType: apicommon.PreDeploymentCheckType, }, - wantStatus: []v1alpha2.TaskStatus(nil), + wantStatus: []v1alpha2.ItemStatus(nil), wantSummary: apicommon.StatusSummary{}, wantErr: nil, getSpanCalls: 0, @@ -72,11 +72,11 @@ func TestTaskHandler(t *testing.T) { TaskDefinition: "task-def", CheckType: apicommon.PreDeploymentCheckType, }, - wantStatus: []v1alpha2.TaskStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - TaskDefinitionName: "task-def", - Status: apicommon.StatePending, - TaskName: "pre-task-def-", + DefinitionName: "task-def", + Status: apicommon.StatePending, + Name: "pre-task-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Pending: 1}, @@ -94,11 +94,11 @@ func TestTaskHandler(t *testing.T) { }, Status: v1alpha2.KeptnAppVersionStatus{ PreDeploymentStatus: apicommon.StateSucceeded, - PreDeploymentTaskStatus: []v1alpha2.TaskStatus{ + PreDeploymentTaskStatus: []v1alpha2.ItemStatus{ { - TaskDefinitionName: "task-def", - Status: apicommon.StateSucceeded, - TaskName: "pre-task-def-", + DefinitionName: "task-def", + Status: apicommon.StateSucceeded, + Name: "pre-task-def-", }, }, }, @@ -109,11 +109,11 @@ func TestTaskHandler(t *testing.T) { TaskDefinition: "task-def", CheckType: apicommon.PreDeploymentCheckType, }, - wantStatus: []v1alpha2.TaskStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - TaskDefinitionName: "task-def", - Status: apicommon.StateSucceeded, - TaskName: "pre-task-def-", + DefinitionName: "task-def", + Status: apicommon.StateSucceeded, + Name: "pre-task-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Succeeded: 1}, @@ -134,11 +134,11 @@ func TestTaskHandler(t *testing.T) { }, Status: v1alpha2.KeptnAppVersionStatus{ PreDeploymentStatus: apicommon.StateSucceeded, - PreDeploymentTaskStatus: []v1alpha2.TaskStatus{ + PreDeploymentTaskStatus: []v1alpha2.ItemStatus{ { - TaskDefinitionName: "task-def", - Status: apicommon.StateProgressing, - TaskName: "pre-task-def-", + DefinitionName: "task-def", + Status: apicommon.StateProgressing, + Name: "pre-task-def-", }, }, }, @@ -157,11 +157,11 @@ func TestTaskHandler(t *testing.T) { TaskDefinition: "task-def", CheckType: apicommon.PreDeploymentCheckType, }, - wantStatus: []v1alpha2.TaskStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - TaskDefinitionName: "task-def", - Status: apicommon.StateFailed, - TaskName: "pre-task-def-", + DefinitionName: "task-def", + Status: apicommon.StateFailed, + Name: "pre-task-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Failed: 1}, @@ -182,11 +182,11 @@ func TestTaskHandler(t *testing.T) { }, Status: v1alpha2.KeptnAppVersionStatus{ PreDeploymentStatus: apicommon.StateSucceeded, - PreDeploymentTaskStatus: []v1alpha2.TaskStatus{ + PreDeploymentTaskStatus: []v1alpha2.ItemStatus{ { - TaskDefinitionName: "task-def", - Status: apicommon.StateProgressing, - TaskName: "pre-task-def-", + DefinitionName: "task-def", + Status: apicommon.StateProgressing, + Name: "pre-task-def-", }, }, }, @@ -205,11 +205,11 @@ func TestTaskHandler(t *testing.T) { TaskDefinition: "task-def", CheckType: apicommon.PreDeploymentCheckType, }, - wantStatus: []v1alpha2.TaskStatus{ + wantStatus: []v1alpha2.ItemStatus{ { - TaskDefinitionName: "task-def", - Status: apicommon.StateSucceeded, - TaskName: "pre-task-def-", + DefinitionName: "task-def", + Status: apicommon.StateSucceeded, + Name: "pre-task-def-", }, }, wantSummary: apicommon.StatusSummary{Total: 1, Succeeded: 1}, @@ -242,8 +242,8 @@ func TestTaskHandler(t *testing.T) { status, summary, err := handler.ReconcileTasks(context.TODO(), context.TODO(), tt.object, tt.createAttr) if len(tt.wantStatus) == len(status) { for j, item := range status { - require.Equal(t, tt.wantStatus[j].TaskDefinitionName, item.TaskDefinitionName) - require.True(t, strings.Contains(item.TaskName, tt.wantStatus[j].TaskName)) + require.Equal(t, tt.wantStatus[j].DefinitionName, item.DefinitionName) + require.True(t, strings.Contains(item.Name, tt.wantStatus[j].Name)) require.Equal(t, tt.wantStatus[j].Status, item.Status) } } else { diff --git a/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go b/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go index e8f1029c02..4ba3863d1f 100644 --- a/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go +++ b/operator/controllers/lifecycle/interfaces/fake/phaseitem_mock.go @@ -14,94 +14,94 @@ import ( // PhaseItemMock is a mock implementation of interfaces.PhaseItem. // -// func TestSomethingThatUsesPhaseItem(t *testing.T) { +// func TestSomethingThatUsesPhaseItem(t *testing.T) { // -// // make and configure a mocked interfaces.PhaseItem -// mockedPhaseItem := &PhaseItemMock{ -// CompleteFunc: func() { -// panic("mock out the Complete method") -// }, -// DeprecateRemainingPhasesFunc: func(phase apicommon.KeptnPhaseType) { -// panic("mock out the DeprecateRemainingPhases method") -// }, -// GenerateEvaluationFunc: func(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha2.KeptnEvaluation { -// panic("mock out the GenerateEvaluation method") -// }, -// GenerateTaskFunc: func(taskDefinition string, checkType apicommon.CheckType) klcv1alpha2.KeptnTask { -// panic("mock out the GenerateTask method") -// }, -// GetAppNameFunc: func() string { -// panic("mock out the GetAppName method") -// }, -// GetCurrentPhaseFunc: func() string { -// panic("mock out the GetCurrentPhase method") -// }, -// GetEndTimeFunc: func() time.Time { -// panic("mock out the GetEndTime method") -// }, -// GetNamespaceFunc: func() string { -// panic("mock out the GetNamespace method") -// }, -// GetParentNameFunc: func() string { -// panic("mock out the GetParentName method") -// }, -// GetPostDeploymentEvaluationTaskStatusFunc: func() []klcv1alpha2.EvaluationStatus { -// panic("mock out the GetPostDeploymentEvaluationTaskStatus method") -// }, -// GetPostDeploymentEvaluationsFunc: func() []string { -// panic("mock out the GetPostDeploymentEvaluations method") -// }, -// GetPostDeploymentTaskStatusFunc: func() []klcv1alpha2.TaskStatus { -// panic("mock out the GetPostDeploymentTaskStatus method") -// }, -// GetPostDeploymentTasksFunc: func() []string { -// panic("mock out the GetPostDeploymentTasks method") -// }, -// GetPreDeploymentEvaluationTaskStatusFunc: func() []klcv1alpha2.EvaluationStatus { -// panic("mock out the GetPreDeploymentEvaluationTaskStatus method") -// }, -// GetPreDeploymentEvaluationsFunc: func() []string { -// panic("mock out the GetPreDeploymentEvaluations method") -// }, -// GetPreDeploymentTaskStatusFunc: func() []klcv1alpha2.TaskStatus { -// panic("mock out the GetPreDeploymentTaskStatus method") -// }, -// GetPreDeploymentTasksFunc: func() []string { -// panic("mock out the GetPreDeploymentTasks method") -// }, -// GetPreviousVersionFunc: func() string { -// panic("mock out the GetPreviousVersion method") -// }, -// GetSpanAttributesFunc: func() []attribute.KeyValue { -// panic("mock out the GetSpanAttributes method") -// }, -// GetStartTimeFunc: func() time.Time { -// panic("mock out the GetStartTime method") -// }, -// GetStateFunc: func() apicommon.KeptnState { -// panic("mock out the GetState method") -// }, -// GetVersionFunc: func() string { -// panic("mock out the GetVersion method") -// }, -// IsEndTimeSetFunc: func() bool { -// panic("mock out the IsEndTimeSet method") -// }, -// SetCurrentPhaseFunc: func(s string) { -// panic("mock out the SetCurrentPhase method") -// }, -// SetSpanAttributesFunc: func(span trace.Span) { -// panic("mock out the SetSpanAttributes method") -// }, -// SetStateFunc: func(keptnState apicommon.KeptnState) { -// panic("mock out the SetState method") -// }, -// } +// // make and configure a mocked interfaces.PhaseItem +// mockedPhaseItem := &PhaseItemMock{ +// CompleteFunc: func() { +// panic("mock out the Complete method") +// }, +// DeprecateRemainingPhasesFunc: func(phase apicommon.KeptnPhaseType) { +// panic("mock out the DeprecateRemainingPhases method") +// }, +// GenerateEvaluationFunc: func(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha2.KeptnEvaluation { +// panic("mock out the GenerateEvaluation method") +// }, +// GenerateTaskFunc: func(taskDefinition string, checkType apicommon.CheckType) klcv1alpha2.KeptnTask { +// panic("mock out the GenerateTask method") +// }, +// GetAppNameFunc: func() string { +// panic("mock out the GetAppName method") +// }, +// GetCurrentPhaseFunc: func() string { +// panic("mock out the GetCurrentPhase method") +// }, +// GetEndTimeFunc: func() time.Time { +// panic("mock out the GetEndTime method") +// }, +// GetNamespaceFunc: func() string { +// panic("mock out the GetNamespace method") +// }, +// GetParentNameFunc: func() string { +// panic("mock out the GetParentName method") +// }, +// GetPostDeploymentEvaluationTaskStatusFunc: func() []klcv1alpha2.ItemStatus { +// panic("mock out the GetPostDeploymentEvaluationTaskStatus method") +// }, +// GetPostDeploymentEvaluationsFunc: func() []string { +// panic("mock out the GetPostDeploymentEvaluations method") +// }, +// GetPostDeploymentTaskStatusFunc: func() []klcv1alpha2.ItemStatus { +// panic("mock out the GetPostDeploymentTaskStatus method") +// }, +// GetPostDeploymentTasksFunc: func() []string { +// panic("mock out the GetPostDeploymentTasks method") +// }, +// GetPreDeploymentEvaluationTaskStatusFunc: func() []klcv1alpha2.ItemStatus { +// panic("mock out the GetPreDeploymentEvaluationTaskStatus method") +// }, +// GetPreDeploymentEvaluationsFunc: func() []string { +// panic("mock out the GetPreDeploymentEvaluations method") +// }, +// GetPreDeploymentTaskStatusFunc: func() []klcv1alpha2.ItemStatus { +// panic("mock out the GetPreDeploymentTaskStatus method") +// }, +// GetPreDeploymentTasksFunc: func() []string { +// panic("mock out the GetPreDeploymentTasks method") +// }, +// GetPreviousVersionFunc: func() string { +// panic("mock out the GetPreviousVersion method") +// }, +// GetSpanAttributesFunc: func() []attribute.KeyValue { +// panic("mock out the GetSpanAttributes method") +// }, +// GetStartTimeFunc: func() time.Time { +// panic("mock out the GetStartTime method") +// }, +// GetStateFunc: func() apicommon.KeptnState { +// panic("mock out the GetState method") +// }, +// GetVersionFunc: func() string { +// panic("mock out the GetVersion method") +// }, +// IsEndTimeSetFunc: func() bool { +// panic("mock out the IsEndTimeSet method") +// }, +// SetCurrentPhaseFunc: func(s string) { +// panic("mock out the SetCurrentPhase method") +// }, +// SetSpanAttributesFunc: func(span trace.Span) { +// panic("mock out the SetSpanAttributes method") +// }, +// SetStateFunc: func(keptnState apicommon.KeptnState) { +// panic("mock out the SetState method") +// }, +// } // -// // use mockedPhaseItem in code that requires interfaces.PhaseItem -// // and then make assertions. +// // use mockedPhaseItem in code that requires interfaces.PhaseItem +// // and then make assertions. // -// } +// } type PhaseItemMock struct { // CompleteFunc mocks the Complete method. CompleteFunc func() @@ -131,25 +131,25 @@ type PhaseItemMock struct { GetParentNameFunc func() string // GetPostDeploymentEvaluationTaskStatusFunc mocks the GetPostDeploymentEvaluationTaskStatus method. - GetPostDeploymentEvaluationTaskStatusFunc func() []klcv1alpha2.EvaluationStatus + GetPostDeploymentEvaluationTaskStatusFunc func() []klcv1alpha2.ItemStatus // GetPostDeploymentEvaluationsFunc mocks the GetPostDeploymentEvaluations method. GetPostDeploymentEvaluationsFunc func() []string // GetPostDeploymentTaskStatusFunc mocks the GetPostDeploymentTaskStatus method. - GetPostDeploymentTaskStatusFunc func() []klcv1alpha2.TaskStatus + GetPostDeploymentTaskStatusFunc func() []klcv1alpha2.ItemStatus // GetPostDeploymentTasksFunc mocks the GetPostDeploymentTasks method. GetPostDeploymentTasksFunc func() []string // GetPreDeploymentEvaluationTaskStatusFunc mocks the GetPreDeploymentEvaluationTaskStatus method. - GetPreDeploymentEvaluationTaskStatusFunc func() []klcv1alpha2.EvaluationStatus + GetPreDeploymentEvaluationTaskStatusFunc func() []klcv1alpha2.ItemStatus // GetPreDeploymentEvaluationsFunc mocks the GetPreDeploymentEvaluations method. GetPreDeploymentEvaluationsFunc func() []string // GetPreDeploymentTaskStatusFunc mocks the GetPreDeploymentTaskStatus method. - GetPreDeploymentTaskStatusFunc func() []klcv1alpha2.TaskStatus + GetPreDeploymentTaskStatusFunc func() []klcv1alpha2.ItemStatus // GetPreDeploymentTasksFunc mocks the GetPreDeploymentTasks method. GetPreDeploymentTasksFunc func() []string @@ -321,8 +321,7 @@ func (mock *PhaseItemMock) Complete() { // CompleteCalls gets all the calls that were made to Complete. // Check the length with: -// -// len(mockedPhaseItem.CompleteCalls()) +// len(mockedPhaseItem.CompleteCalls()) func (mock *PhaseItemMock) CompleteCalls() []struct { } { var calls []struct { @@ -351,8 +350,7 @@ func (mock *PhaseItemMock) DeprecateRemainingPhases(phase apicommon.KeptnPhaseTy // DeprecateRemainingPhasesCalls gets all the calls that were made to DeprecateRemainingPhases. // Check the length with: -// -// len(mockedPhaseItem.DeprecateRemainingPhasesCalls()) +// len(mockedPhaseItem.DeprecateRemainingPhasesCalls()) func (mock *PhaseItemMock) DeprecateRemainingPhasesCalls() []struct { Phase apicommon.KeptnPhaseType } { @@ -385,8 +383,7 @@ func (mock *PhaseItemMock) GenerateEvaluation(evaluationDefinition string, check // GenerateEvaluationCalls gets all the calls that were made to GenerateEvaluation. // Check the length with: -// -// len(mockedPhaseItem.GenerateEvaluationCalls()) +// len(mockedPhaseItem.GenerateEvaluationCalls()) func (mock *PhaseItemMock) GenerateEvaluationCalls() []struct { EvaluationDefinition string CheckType apicommon.CheckType @@ -421,8 +418,7 @@ func (mock *PhaseItemMock) GenerateTask(taskDefinition string, checkType apicomm // GenerateTaskCalls gets all the calls that were made to GenerateTask. // Check the length with: -// -// len(mockedPhaseItem.GenerateTaskCalls()) +// len(mockedPhaseItem.GenerateTaskCalls()) func (mock *PhaseItemMock) GenerateTaskCalls() []struct { TaskDefinition string CheckType apicommon.CheckType @@ -452,8 +448,7 @@ func (mock *PhaseItemMock) GetAppName() string { // GetAppNameCalls gets all the calls that were made to GetAppName. // Check the length with: -// -// len(mockedPhaseItem.GetAppNameCalls()) +// len(mockedPhaseItem.GetAppNameCalls()) func (mock *PhaseItemMock) GetAppNameCalls() []struct { } { var calls []struct { @@ -479,8 +474,7 @@ func (mock *PhaseItemMock) GetCurrentPhase() string { // GetCurrentPhaseCalls gets all the calls that were made to GetCurrentPhase. // Check the length with: -// -// len(mockedPhaseItem.GetCurrentPhaseCalls()) +// len(mockedPhaseItem.GetCurrentPhaseCalls()) func (mock *PhaseItemMock) GetCurrentPhaseCalls() []struct { } { var calls []struct { @@ -506,8 +500,7 @@ func (mock *PhaseItemMock) GetEndTime() time.Time { // GetEndTimeCalls gets all the calls that were made to GetEndTime. // Check the length with: -// -// len(mockedPhaseItem.GetEndTimeCalls()) +// len(mockedPhaseItem.GetEndTimeCalls()) func (mock *PhaseItemMock) GetEndTimeCalls() []struct { } { var calls []struct { @@ -533,8 +526,7 @@ func (mock *PhaseItemMock) GetNamespace() string { // GetNamespaceCalls gets all the calls that were made to GetNamespace. // Check the length with: -// -// len(mockedPhaseItem.GetNamespaceCalls()) +// len(mockedPhaseItem.GetNamespaceCalls()) func (mock *PhaseItemMock) GetNamespaceCalls() []struct { } { var calls []struct { @@ -560,8 +552,7 @@ func (mock *PhaseItemMock) GetParentName() string { // GetParentNameCalls gets all the calls that were made to GetParentName. // Check the length with: -// -// len(mockedPhaseItem.GetParentNameCalls()) +// len(mockedPhaseItem.GetParentNameCalls()) func (mock *PhaseItemMock) GetParentNameCalls() []struct { } { var calls []struct { @@ -573,7 +564,7 @@ func (mock *PhaseItemMock) GetParentNameCalls() []struct { } // GetPostDeploymentEvaluationTaskStatus calls GetPostDeploymentEvaluationTaskStatusFunc. -func (mock *PhaseItemMock) GetPostDeploymentEvaluationTaskStatus() []klcv1alpha2.EvaluationStatus { +func (mock *PhaseItemMock) GetPostDeploymentEvaluationTaskStatus() []klcv1alpha2.ItemStatus { if mock.GetPostDeploymentEvaluationTaskStatusFunc == nil { panic("PhaseItemMock.GetPostDeploymentEvaluationTaskStatusFunc: method is nil but PhaseItem.GetPostDeploymentEvaluationTaskStatus was just called") } @@ -587,8 +578,7 @@ func (mock *PhaseItemMock) GetPostDeploymentEvaluationTaskStatus() []klcv1alpha2 // GetPostDeploymentEvaluationTaskStatusCalls gets all the calls that were made to GetPostDeploymentEvaluationTaskStatus. // Check the length with: -// -// len(mockedPhaseItem.GetPostDeploymentEvaluationTaskStatusCalls()) +// len(mockedPhaseItem.GetPostDeploymentEvaluationTaskStatusCalls()) func (mock *PhaseItemMock) GetPostDeploymentEvaluationTaskStatusCalls() []struct { } { var calls []struct { @@ -614,8 +604,7 @@ func (mock *PhaseItemMock) GetPostDeploymentEvaluations() []string { // GetPostDeploymentEvaluationsCalls gets all the calls that were made to GetPostDeploymentEvaluations. // Check the length with: -// -// len(mockedPhaseItem.GetPostDeploymentEvaluationsCalls()) +// len(mockedPhaseItem.GetPostDeploymentEvaluationsCalls()) func (mock *PhaseItemMock) GetPostDeploymentEvaluationsCalls() []struct { } { var calls []struct { @@ -627,7 +616,7 @@ func (mock *PhaseItemMock) GetPostDeploymentEvaluationsCalls() []struct { } // GetPostDeploymentTaskStatus calls GetPostDeploymentTaskStatusFunc. -func (mock *PhaseItemMock) GetPostDeploymentTaskStatus() []klcv1alpha2.TaskStatus { +func (mock *PhaseItemMock) GetPostDeploymentTaskStatus() []klcv1alpha2.ItemStatus { if mock.GetPostDeploymentTaskStatusFunc == nil { panic("PhaseItemMock.GetPostDeploymentTaskStatusFunc: method is nil but PhaseItem.GetPostDeploymentTaskStatus was just called") } @@ -641,8 +630,7 @@ func (mock *PhaseItemMock) GetPostDeploymentTaskStatus() []klcv1alpha2.TaskStatu // GetPostDeploymentTaskStatusCalls gets all the calls that were made to GetPostDeploymentTaskStatus. // Check the length with: -// -// len(mockedPhaseItem.GetPostDeploymentTaskStatusCalls()) +// len(mockedPhaseItem.GetPostDeploymentTaskStatusCalls()) func (mock *PhaseItemMock) GetPostDeploymentTaskStatusCalls() []struct { } { var calls []struct { @@ -668,8 +656,7 @@ func (mock *PhaseItemMock) GetPostDeploymentTasks() []string { // GetPostDeploymentTasksCalls gets all the calls that were made to GetPostDeploymentTasks. // Check the length with: -// -// len(mockedPhaseItem.GetPostDeploymentTasksCalls()) +// len(mockedPhaseItem.GetPostDeploymentTasksCalls()) func (mock *PhaseItemMock) GetPostDeploymentTasksCalls() []struct { } { var calls []struct { @@ -681,7 +668,7 @@ func (mock *PhaseItemMock) GetPostDeploymentTasksCalls() []struct { } // GetPreDeploymentEvaluationTaskStatus calls GetPreDeploymentEvaluationTaskStatusFunc. -func (mock *PhaseItemMock) GetPreDeploymentEvaluationTaskStatus() []klcv1alpha2.EvaluationStatus { +func (mock *PhaseItemMock) GetPreDeploymentEvaluationTaskStatus() []klcv1alpha2.ItemStatus { if mock.GetPreDeploymentEvaluationTaskStatusFunc == nil { panic("PhaseItemMock.GetPreDeploymentEvaluationTaskStatusFunc: method is nil but PhaseItem.GetPreDeploymentEvaluationTaskStatus was just called") } @@ -695,8 +682,7 @@ func (mock *PhaseItemMock) GetPreDeploymentEvaluationTaskStatus() []klcv1alpha2. // GetPreDeploymentEvaluationTaskStatusCalls gets all the calls that were made to GetPreDeploymentEvaluationTaskStatus. // Check the length with: -// -// len(mockedPhaseItem.GetPreDeploymentEvaluationTaskStatusCalls()) +// len(mockedPhaseItem.GetPreDeploymentEvaluationTaskStatusCalls()) func (mock *PhaseItemMock) GetPreDeploymentEvaluationTaskStatusCalls() []struct { } { var calls []struct { @@ -722,8 +708,7 @@ func (mock *PhaseItemMock) GetPreDeploymentEvaluations() []string { // GetPreDeploymentEvaluationsCalls gets all the calls that were made to GetPreDeploymentEvaluations. // Check the length with: -// -// len(mockedPhaseItem.GetPreDeploymentEvaluationsCalls()) +// len(mockedPhaseItem.GetPreDeploymentEvaluationsCalls()) func (mock *PhaseItemMock) GetPreDeploymentEvaluationsCalls() []struct { } { var calls []struct { @@ -735,7 +720,7 @@ func (mock *PhaseItemMock) GetPreDeploymentEvaluationsCalls() []struct { } // GetPreDeploymentTaskStatus calls GetPreDeploymentTaskStatusFunc. -func (mock *PhaseItemMock) GetPreDeploymentTaskStatus() []klcv1alpha2.TaskStatus { +func (mock *PhaseItemMock) GetPreDeploymentTaskStatus() []klcv1alpha2.ItemStatus { if mock.GetPreDeploymentTaskStatusFunc == nil { panic("PhaseItemMock.GetPreDeploymentTaskStatusFunc: method is nil but PhaseItem.GetPreDeploymentTaskStatus was just called") } @@ -749,8 +734,7 @@ func (mock *PhaseItemMock) GetPreDeploymentTaskStatus() []klcv1alpha2.TaskStatus // GetPreDeploymentTaskStatusCalls gets all the calls that were made to GetPreDeploymentTaskStatus. // Check the length with: -// -// len(mockedPhaseItem.GetPreDeploymentTaskStatusCalls()) +// len(mockedPhaseItem.GetPreDeploymentTaskStatusCalls()) func (mock *PhaseItemMock) GetPreDeploymentTaskStatusCalls() []struct { } { var calls []struct { @@ -776,8 +760,7 @@ func (mock *PhaseItemMock) GetPreDeploymentTasks() []string { // GetPreDeploymentTasksCalls gets all the calls that were made to GetPreDeploymentTasks. // Check the length with: -// -// len(mockedPhaseItem.GetPreDeploymentTasksCalls()) +// len(mockedPhaseItem.GetPreDeploymentTasksCalls()) func (mock *PhaseItemMock) GetPreDeploymentTasksCalls() []struct { } { var calls []struct { @@ -803,8 +786,7 @@ func (mock *PhaseItemMock) GetPreviousVersion() string { // GetPreviousVersionCalls gets all the calls that were made to GetPreviousVersion. // Check the length with: -// -// len(mockedPhaseItem.GetPreviousVersionCalls()) +// len(mockedPhaseItem.GetPreviousVersionCalls()) func (mock *PhaseItemMock) GetPreviousVersionCalls() []struct { } { var calls []struct { @@ -830,8 +812,7 @@ func (mock *PhaseItemMock) GetSpanAttributes() []attribute.KeyValue { // GetSpanAttributesCalls gets all the calls that were made to GetSpanAttributes. // Check the length with: -// -// len(mockedPhaseItem.GetSpanAttributesCalls()) +// len(mockedPhaseItem.GetSpanAttributesCalls()) func (mock *PhaseItemMock) GetSpanAttributesCalls() []struct { } { var calls []struct { @@ -857,8 +838,7 @@ func (mock *PhaseItemMock) GetStartTime() time.Time { // GetStartTimeCalls gets all the calls that were made to GetStartTime. // Check the length with: -// -// len(mockedPhaseItem.GetStartTimeCalls()) +// len(mockedPhaseItem.GetStartTimeCalls()) func (mock *PhaseItemMock) GetStartTimeCalls() []struct { } { var calls []struct { @@ -884,8 +864,7 @@ func (mock *PhaseItemMock) GetState() apicommon.KeptnState { // GetStateCalls gets all the calls that were made to GetState. // Check the length with: -// -// len(mockedPhaseItem.GetStateCalls()) +// len(mockedPhaseItem.GetStateCalls()) func (mock *PhaseItemMock) GetStateCalls() []struct { } { var calls []struct { @@ -911,8 +890,7 @@ func (mock *PhaseItemMock) GetVersion() string { // GetVersionCalls gets all the calls that were made to GetVersion. // Check the length with: -// -// len(mockedPhaseItem.GetVersionCalls()) +// len(mockedPhaseItem.GetVersionCalls()) func (mock *PhaseItemMock) GetVersionCalls() []struct { } { var calls []struct { @@ -938,8 +916,7 @@ func (mock *PhaseItemMock) IsEndTimeSet() bool { // IsEndTimeSetCalls gets all the calls that were made to IsEndTimeSet. // Check the length with: -// -// len(mockedPhaseItem.IsEndTimeSetCalls()) +// len(mockedPhaseItem.IsEndTimeSetCalls()) func (mock *PhaseItemMock) IsEndTimeSetCalls() []struct { } { var calls []struct { @@ -968,8 +945,7 @@ func (mock *PhaseItemMock) SetCurrentPhase(s string) { // SetCurrentPhaseCalls gets all the calls that were made to SetCurrentPhase. // Check the length with: -// -// len(mockedPhaseItem.SetCurrentPhaseCalls()) +// len(mockedPhaseItem.SetCurrentPhaseCalls()) func (mock *PhaseItemMock) SetCurrentPhaseCalls() []struct { S string } { @@ -1000,8 +976,7 @@ func (mock *PhaseItemMock) SetSpanAttributes(span trace.Span) { // SetSpanAttributesCalls gets all the calls that were made to SetSpanAttributes. // Check the length with: -// -// len(mockedPhaseItem.SetSpanAttributesCalls()) +// len(mockedPhaseItem.SetSpanAttributesCalls()) func (mock *PhaseItemMock) SetSpanAttributesCalls() []struct { Span trace.Span } { @@ -1032,8 +1007,7 @@ func (mock *PhaseItemMock) SetState(keptnState apicommon.KeptnState) { // SetStateCalls gets all the calls that were made to SetState. // Check the length with: -// -// len(mockedPhaseItem.SetStateCalls()) +// len(mockedPhaseItem.SetStateCalls()) func (mock *PhaseItemMock) SetStateCalls() []struct { KeptnState apicommon.KeptnState } { diff --git a/operator/controllers/lifecycle/interfaces/phaseitem.go b/operator/controllers/lifecycle/interfaces/phaseitem.go index 74202f49cb..f023a1b3c3 100644 --- a/operator/controllers/lifecycle/interfaces/phaseitem.go +++ b/operator/controllers/lifecycle/interfaces/phaseitem.go @@ -30,12 +30,12 @@ type PhaseItem interface { GetAppName() string GetPreDeploymentTasks() []string GetPostDeploymentTasks() []string - GetPreDeploymentTaskStatus() []klcv1alpha2.TaskStatus - GetPostDeploymentTaskStatus() []klcv1alpha2.TaskStatus + GetPreDeploymentTaskStatus() []klcv1alpha2.ItemStatus + GetPostDeploymentTaskStatus() []klcv1alpha2.ItemStatus GetPreDeploymentEvaluations() []string GetPostDeploymentEvaluations() []string - GetPreDeploymentEvaluationTaskStatus() []klcv1alpha2.EvaluationStatus - GetPostDeploymentEvaluationTaskStatus() []klcv1alpha2.EvaluationStatus + GetPreDeploymentEvaluationTaskStatus() []klcv1alpha2.ItemStatus + GetPostDeploymentEvaluationTaskStatus() []klcv1alpha2.ItemStatus GenerateTask(taskDefinition string, checkType apicommon.CheckType) klcv1alpha2.KeptnTask GenerateEvaluation(evaluationDefinition string, checkType apicommon.CheckType) klcv1alpha2.KeptnEvaluation GetSpanAttributes() []attribute.KeyValue @@ -115,11 +115,11 @@ func (pw PhaseItemWrapper) GetPostDeploymentTasks() []string { return pw.Obj.GetPostDeploymentTasks() } -func (pw PhaseItemWrapper) GetPreDeploymentTaskStatus() []klcv1alpha2.TaskStatus { +func (pw PhaseItemWrapper) GetPreDeploymentTaskStatus() []klcv1alpha2.ItemStatus { return pw.Obj.GetPreDeploymentTaskStatus() } -func (pw PhaseItemWrapper) GetPostDeploymentTaskStatus() []klcv1alpha2.TaskStatus { +func (pw PhaseItemWrapper) GetPostDeploymentTaskStatus() []klcv1alpha2.ItemStatus { return pw.Obj.GetPostDeploymentTaskStatus() } @@ -131,11 +131,11 @@ func (pw PhaseItemWrapper) GetPostDeploymentEvaluations() []string { return pw.Obj.GetPostDeploymentEvaluations() } -func (pw PhaseItemWrapper) GetPreDeploymentEvaluationTaskStatus() []klcv1alpha2.EvaluationStatus { +func (pw PhaseItemWrapper) GetPreDeploymentEvaluationTaskStatus() []klcv1alpha2.ItemStatus { return pw.Obj.GetPreDeploymentEvaluationTaskStatus() } -func (pw PhaseItemWrapper) GetPostDeploymentEvaluationTaskStatus() []klcv1alpha2.EvaluationStatus { +func (pw PhaseItemWrapper) GetPostDeploymentEvaluationTaskStatus() []klcv1alpha2.ItemStatus { return pw.Obj.GetPostDeploymentEvaluationTaskStatus() } diff --git a/operator/controllers/lifecycle/interfaces/phaseitem_test.go b/operator/controllers/lifecycle/interfaces/phaseitem_test.go index 4f204f0b40..d8951f3873 100644 --- a/operator/controllers/lifecycle/interfaces/phaseitem_test.go +++ b/operator/controllers/lifecycle/interfaces/phaseitem_test.go @@ -77,10 +77,10 @@ func TestPhaseItem(t *testing.T) { GetPostDeploymentTasksFunc: func() []string { return nil }, - GetPreDeploymentTaskStatusFunc: func() []v1alpha2.TaskStatus { + GetPreDeploymentTaskStatusFunc: func() []v1alpha2.ItemStatus { return nil }, - GetPostDeploymentTaskStatusFunc: func() []v1alpha2.TaskStatus { + GetPostDeploymentTaskStatusFunc: func() []v1alpha2.ItemStatus { return nil }, GetPreDeploymentEvaluationsFunc: func() []string { @@ -89,10 +89,10 @@ func TestPhaseItem(t *testing.T) { GetPostDeploymentEvaluationsFunc: func() []string { return nil }, - GetPreDeploymentEvaluationTaskStatusFunc: func() []v1alpha2.EvaluationStatus { + GetPreDeploymentEvaluationTaskStatusFunc: func() []v1alpha2.ItemStatus { return nil }, - GetPostDeploymentEvaluationTaskStatusFunc: func() []v1alpha2.EvaluationStatus { + GetPostDeploymentEvaluationTaskStatusFunc: func() []v1alpha2.ItemStatus { return nil }, GenerateTaskFunc: func(taskDefinition string, checkType apicommon.CheckType) v1alpha2.KeptnTask { diff --git a/operator/controllers/lifecycle/keptnappversion/controller_test.go b/operator/controllers/lifecycle/keptnappversion/controller_test.go index 29da763ff5..10477ff127 100644 --- a/operator/controllers/lifecycle/keptnappversion/controller_test.go +++ b/operator/controllers/lifecycle/keptnappversion/controller_test.go @@ -127,10 +127,10 @@ func createFinishedAppVersionStatus() lfcv1alpha2.KeptnAppVersionStatus { PostDeploymentStatus: apicommon.StateSucceeded, PreDeploymentEvaluationStatus: apicommon.StateSucceeded, PostDeploymentEvaluationStatus: apicommon.StateSucceeded, - PreDeploymentTaskStatus: []lfcv1alpha2.TaskStatus{{Status: apicommon.StateSucceeded}}, - PostDeploymentTaskStatus: []lfcv1alpha2.TaskStatus{{Status: apicommon.StateSucceeded}}, - PreDeploymentEvaluationTaskStatus: []lfcv1alpha2.EvaluationStatus{{Status: apicommon.StateSucceeded}}, - PostDeploymentEvaluationTaskStatus: []lfcv1alpha2.EvaluationStatus{{Status: apicommon.StateSucceeded}}, + PreDeploymentTaskStatus: []lfcv1alpha2.ItemStatus{{Status: apicommon.StateSucceeded}}, + PostDeploymentTaskStatus: []lfcv1alpha2.ItemStatus{{Status: apicommon.StateSucceeded}}, + PreDeploymentEvaluationTaskStatus: []lfcv1alpha2.ItemStatus{{Status: apicommon.StateSucceeded}}, + PostDeploymentEvaluationTaskStatus: []lfcv1alpha2.ItemStatus{{Status: apicommon.StateSucceeded}}, WorkloadOverallStatus: apicommon.StateSucceeded, WorkloadStatus: []lfcv1alpha2.WorkloadStatus{{Status: apicommon.StateSucceeded}}, Status: apicommon.StateSucceeded, diff --git a/operator/main.go b/operator/main.go index 118a515942..b39661863d 100644 --- a/operator/main.go +++ b/operator/main.go @@ -353,6 +353,14 @@ func main() { setupLog.Error(err, "unable to create webhook", "webhook", "KeptnEvaluationProvider") os.Exit(1) } + if err = (&lifecyclev1alpha2.KeptnAppVersion{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "KeptnAppVersion") + os.Exit(1) + } + if err = (&lifecyclev1alpha2.KeptnWorkloadInstance{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "KeptnWorkloadInstance") + os.Exit(1) + } //+kubebuilder:scaffold:builder err = meter.RegisterCallback( diff --git a/operator/test/component/workloadinstancecontroller_test.go b/operator/test/component/workloadinstancecontroller_test.go index 7dc01fc337..d3ac28426f 100644 --- a/operator/test/component/workloadinstancecontroller_test.go +++ b/operator/test/component/workloadinstancecontroller_test.go @@ -360,11 +360,11 @@ var _ = Describe("KeptnWorkloadInstanceController", Ordered, func() { PostDeploymentEvaluationStatus: apicommon.StatePending, CurrentPhase: apicommon.PhaseWorkloadPreEvaluation.ShortName, Status: apicommon.StateProgressing, - PreDeploymentEvaluationTaskStatus: []klcv1alpha2.EvaluationStatus{ + PreDeploymentEvaluationTaskStatus: []klcv1alpha2.ItemStatus{ { - EvaluationName: "pre-eval-eval-def", - Status: apicommon.StateProgressing, - EvaluationDefinitionName: "eval-def", + Name: "pre-eval-eval-def", + Status: apicommon.StateProgressing, + DefinitionName: "eval-def", }, }, } diff --git a/scheduler/test/e2e/fake/v1alpha2/keptnworkloadinstance_types.go b/scheduler/test/e2e/fake/v1alpha2/keptnworkloadinstance_types.go index 6fda277f83..325752a563 100644 --- a/scheduler/test/e2e/fake/v1alpha2/keptnworkloadinstance_types.go +++ b/scheduler/test/e2e/fake/v1alpha2/keptnworkloadinstance_types.go @@ -45,34 +45,27 @@ 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"` + PostDeploymentStatus common.KeptnState `json:"postDeploymentStatus,omitempty"` + PreDeploymentTaskStatus []ItemStatus `json:"preDeploymentTaskStatus,omitempty"` + PostDeploymentTaskStatus []ItemStatus `json:"postDeploymentTaskStatus,omitempty"` + PreDeploymentEvaluationTaskStatus []ItemStatus `json:"preDeploymentEvaluationTaskStatus,omitempty"` + PostDeploymentEvaluationTaskStatus []ItemStatus `json:"postDeploymentEvaluationTaskStatus,omitempty"` + StartTime metav1.Time `json:"startTime,omitempty"` + EndTime metav1.Time `json:"endTime,omitempty"` + CurrentPhase string `json:"currentPhase,omitempty"` // +kubebuilder:default:=Pending Status common.KeptnState `json:"status,omitempty"` } -type TaskStatus struct { - TaskDefinitionName string `json:"taskDefinitionName,omitempty"` +type ItemStatus struct { + // name of EvaluationDefinition/TaskDefiniton + DefinitionName string `json:"definitionName,omitempty"` // +kubebuilder:default:=Pending - Status common.KeptnState `json:"status,omitempty"` - TaskName string `json:"taskName,omitempty"` - StartTime metav1.Time `json:"startTime,omitempty"` - EndTime metav1.Time `json:"endTime,omitempty"` -} - -type EvaluationStatus struct { - EvaluationDefinitionName string `json:"evaluationDefinitionName,omitempty"` - // +kubebuilder:default:=Pending - Status common.KeptnState `json:"status,omitempty"` - EvaluationName string `json:"evaluationName,omitempty"` - StartTime metav1.Time `json:"startTime,omitempty"` - EndTime metav1.Time `json:"endTime,omitempty"` + Status common.KeptnState `json:"status,omitempty"` + // name of Evaluation/Task + Name string `json:"name,omitempty"` + StartTime metav1.Time `json:"startTime,omitempty"` + EndTime metav1.Time `json:"endTime,omitempty"` } //+kubebuilder:object:root=true @@ -190,25 +183,13 @@ func (i *KeptnWorkloadInstance) IsEndTimeSet() bool { return !i.Status.EndTime.IsZero() } -func (i *TaskStatus) SetStartTime() { - if i.StartTime.IsZero() { - i.StartTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (i *TaskStatus) SetEndTime() { - if i.EndTime.IsZero() { - i.EndTime = metav1.NewTime(time.Now().UTC()) - } -} - -func (i *EvaluationStatus) SetStartTime() { +func (i *ItemStatus) SetStartTime() { if i.StartTime.IsZero() { i.StartTime = metav1.NewTime(time.Now().UTC()) } } -func (i *EvaluationStatus) SetEndTime() { +func (i *ItemStatus) SetEndTime() { if i.EndTime.IsZero() { i.EndTime = metav1.NewTime(time.Now().UTC()) } diff --git a/scheduler/test/e2e/fake/v1alpha2/zz_generated.deepcopy.go b/scheduler/test/e2e/fake/v1alpha2/zz_generated.deepcopy.go index e0b91a9982..e619aa20e8 100644 --- a/scheduler/test/e2e/fake/v1alpha2/zz_generated.deepcopy.go +++ b/scheduler/test/e2e/fake/v1alpha2/zz_generated.deepcopy.go @@ -139,14 +139,14 @@ func (in *KeptnWorkloadInstanceStatus) DeepCopyInto(out *KeptnWorkloadInstanceSt *out = *in if in.PreDeploymentTaskStatus != nil { in, out := &in.PreDeploymentTaskStatus, &out.PreDeploymentTaskStatus - *out = make([]TaskStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.PostDeploymentTaskStatus != nil { in, out := &in.PostDeploymentTaskStatus, &out.PostDeploymentTaskStatus - *out = make([]TaskStatus, len(*in)) + *out = make([]ItemStatus, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -267,18 +267,18 @@ func (in *ResourceReference) DeepCopy() *ResourceReference { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *TaskStatus) DeepCopyInto(out *TaskStatus) { +func (in *ItemStatus) DeepCopyInto(out *ItemStatus) { *out = *in in.StartTime.DeepCopyInto(&out.StartTime) in.EndTime.DeepCopyInto(&out.EndTime) } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TaskStatus. -func (in *TaskStatus) DeepCopy() *TaskStatus { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ItemStatus. +func (in *ItemStatus) DeepCopy() *ItemStatus { if in == nil { return nil } - out := new(TaskStatus) + out := new(ItemStatus) in.DeepCopyInto(out) return out } diff --git a/test/integration/podtato-head-application-v1alpha1/00-assert.yaml b/test/integration/podtato-head-application-v1alpha1/00-assert.yaml new file mode 100644 index 0000000000..3de4d33185 --- /dev/null +++ b/test/integration/podtato-head-application-v1alpha1/00-assert.yaml @@ -0,0 +1,28 @@ +apiVersion: lifecycle.keptn.sh/v1alpha1 +kind: KeptnAppVersion +metadata: + name: podtato-head-1.3-1 +status: + currentPhase: Completed + postDeploymentEvaluationStatus: Succeeded + postDeploymentStatus: Succeeded + preDeploymentEvaluationStatus: Succeeded + preDeploymentStatus: Succeeded + status: Succeeded + workloadOverallStatus: Succeeded +--- +apiVersion: lifecycle.keptn.sh/v1alpha1 +kind: KeptnWorkloadInstance +metadata: + name: podtato-head-podtato-head-entry-0.1.0 +status: + currentPhase: Completed + deploymentStatus: Succeeded + postDeploymentEvaluationStatus: Succeeded + postDeploymentStatus: Succeeded + postDeploymentTaskStatus: + - status: Succeeded + taskDefinitionName: post-deployment-hello + preDeploymentEvaluationStatus: Succeeded + preDeploymentStatus: Succeeded + status: Succeeded diff --git a/test/integration/podtato-head-application-v1alpha1/00-install.yaml b/test/integration/podtato-head-application-v1alpha1/00-install.yaml new file mode 100644 index 0000000000..5e79df7fb3 --- /dev/null +++ b/test/integration/podtato-head-application-v1alpha1/00-install.yaml @@ -0,0 +1,75 @@ +apiVersion: lifecycle.keptn.sh/v1alpha1 +kind: KeptnApp +metadata: + name: podtato-head +spec: + version: "1.3" + workloads: + - name: podtato-head-entry + version: 0.1.0 + postDeploymentTasks: + - post-deployment-hello +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: podtato-head-entry + labels: + app: podtato-head +spec: + selector: + matchLabels: + component: podtato-head-entry + template: + metadata: + labels: + component: podtato-head-entry + annotations: + keptn.sh/app: podtato-head + keptn.sh/workload: podtato-head-entry + keptn.sh/version: 0.1.0 + keptn.sh/post-deployment-tasks: post-deployment-hello + spec: + terminationGracePeriodSeconds: 5 + initContainers: + - name: init-myservice + image: busybox:1.28 + command: ['sh', '-c', 'sleep 30'] + containers: + - name: server + image: ghcr.io/podtato-head/entry:latest + imagePullPolicy: Always + ports: + - containerPort: 9000 + env: + - name: PODTATO_PORT + value: "9000" +--- +apiVersion: v1 +kind: Service +metadata: + name: podtato-head-entry + labels: + app: podtato-head +spec: + selector: + component: podtato-head-entry + ports: + - name: http + port: 9000 + protocol: TCP + targetPort: 9000 + type: LoadBalancer + # change to NodePort if no LoadBalancer controller is available + # type: NodePort +--- +apiVersion: lifecycle.keptn.sh/v1alpha1 +kind: KeptnTaskDefinition +metadata: + name: post-deployment-hello +spec: + function: + inline: + code: | + console.log("Post-Deployment Task has been executed"); + diff --git a/test/integration/podtato-head-application-v1alpha1/00-teststep.yaml b/test/integration/podtato-head-application-v1alpha1/00-teststep.yaml new file mode 100644 index 0000000000..ad4f1d95d5 --- /dev/null +++ b/test/integration/podtato-head-application-v1alpha1/00-teststep.yaml @@ -0,0 +1,4 @@ +apiVersion: kuttl.dev/v1 +kind: TestStep +commands: + - script: kubectl annotate ns $NAMESPACE keptn.sh/lifecycle-toolkit='enabled' diff --git a/test/integration/podtato-head-application/00-assert.yaml b/test/integration/podtato-head-application/00-assert.yaml index 348d344122..87f33d32ad 100644 --- a/test/integration/podtato-head-application/00-assert.yaml +++ b/test/integration/podtato-head-application/00-assert.yaml @@ -1,4 +1,4 @@ -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnAppVersion metadata: name: podtato-head-1.3-1 @@ -11,7 +11,7 @@ status: status: Succeeded workloadOverallStatus: Succeeded --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-entry-0.1.0 @@ -22,12 +22,12 @@ status: postDeploymentStatus: Succeeded postDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: post-deployment-hello + definitionName: post-deployment-hello preDeploymentEvaluationStatus: Succeeded preDeploymentStatus: Succeeded status: Succeeded --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-hat-0.1.0 @@ -40,10 +40,10 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-left-arm-0.1.0 @@ -56,10 +56,10 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-left-leg-0.1.0 @@ -72,10 +72,10 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-right-arm-0.1.0 @@ -88,10 +88,10 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-right-leg-0.1.0 @@ -104,5 +104,5 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded diff --git a/test/integration/podtato-head-application/00-install.yaml b/test/integration/podtato-head-application/00-install.yaml index db10265a84..0693b77816 100644 --- a/test/integration/podtato-head-application/00-install.yaml +++ b/test/integration/podtato-head-application/00-install.yaml @@ -1,4 +1,4 @@ -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnApp metadata: name: podtato-head @@ -308,7 +308,7 @@ spec: targetPort: 9000 type: ClusterIP --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnTaskDefinition metadata: name: post-deployment-hello @@ -318,7 +318,7 @@ spec: code: | console.log("Post-Deployment Task has been executed"); --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnTaskDefinition metadata: name: check-entry-service diff --git a/test/integration/restartable-app/01-assert.yaml b/test/integration/restartable-app/01-assert.yaml index 2cd963a0a5..6927fffc4e 100644 --- a/test/integration/restartable-app/01-assert.yaml +++ b/test/integration/restartable-app/01-assert.yaml @@ -35,7 +35,7 @@ status: postDeploymentStatus: Succeeded postDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: post-deployment-hello + definitionName: post-deployment-hello preDeploymentEvaluationStatus: Succeeded preDeploymentStatus: Succeeded status: Succeeded @@ -53,7 +53,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -69,7 +69,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -85,7 +85,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -101,7 +101,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -117,6 +117,6 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded diff --git a/test/integration/restartable-app/02-assert.yaml b/test/integration/restartable-app/02-assert.yaml index 7818342438..eb6149c050 100644 --- a/test/integration/restartable-app/02-assert.yaml +++ b/test/integration/restartable-app/02-assert.yaml @@ -48,7 +48,7 @@ status: postDeploymentStatus: Succeeded postDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: post-deployment-hello + definitionName: post-deployment-hello preDeploymentEvaluationStatus: Succeeded preDeploymentStatus: Succeeded status: Succeeded @@ -66,7 +66,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -82,7 +82,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -98,7 +98,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -114,7 +114,7 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded --- apiVersion: lifecycle.keptn.sh/v1alpha2 @@ -130,6 +130,6 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: check-entry-service + definitionName: check-entry-service status: Succeeded diff --git a/test/integration/simple-daemonset-annotated/01-assert.yaml b/test/integration/simple-daemonset-annotated/01-assert.yaml index cfd8397c01..bf5ede42e3 100644 --- a/test/integration/simple-daemonset-annotated/01-assert.yaml +++ b/test/integration/simple-daemonset-annotated/01-assert.yaml @@ -18,5 +18,5 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello status: Succeeded diff --git a/test/integration/simple-daemonset-annotated/02-assert.yaml b/test/integration/simple-daemonset-annotated/02-assert.yaml index de23577b7a..9a315b3c6e 100644 --- a/test/integration/simple-daemonset-annotated/02-assert.yaml +++ b/test/integration/simple-daemonset-annotated/02-assert.yaml @@ -12,5 +12,5 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello status: Succeeded diff --git a/test/integration/simple-deployment-annotated/01-assert.yaml b/test/integration/simple-deployment-annotated/01-assert.yaml index bb89c56064..acfdb60df8 100644 --- a/test/integration/simple-deployment-annotated/01-assert.yaml +++ b/test/integration/simple-deployment-annotated/01-assert.yaml @@ -15,9 +15,9 @@ status: postDeploymentStatus: Succeeded postDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello preDeploymentEvaluationStatus: Succeeded preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello diff --git a/test/integration/simple-deployment/01-assert.yaml b/test/integration/simple-deployment/01-assert.yaml index 7995109e21..0a25fd6563 100644 --- a/test/integration/simple-deployment/01-assert.yaml +++ b/test/integration/simple-deployment/01-assert.yaml @@ -16,9 +16,9 @@ status: postDeploymentStatus: Succeeded postDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello preDeploymentEvaluationStatus: Succeeded preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello diff --git a/test/integration/simple-statefulset-annotated/01-assert.yaml b/test/integration/simple-statefulset-annotated/01-assert.yaml index cfd8397c01..bf5ede42e3 100644 --- a/test/integration/simple-statefulset-annotated/01-assert.yaml +++ b/test/integration/simple-statefulset-annotated/01-assert.yaml @@ -18,5 +18,5 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello status: Succeeded diff --git a/test/integration/simple-statefulset-annotated/02-assert.yaml b/test/integration/simple-statefulset-annotated/02-assert.yaml index de23577b7a..9a315b3c6e 100644 --- a/test/integration/simple-statefulset-annotated/02-assert.yaml +++ b/test/integration/simple-statefulset-annotated/02-assert.yaml @@ -12,5 +12,5 @@ status: preDeploymentStatus: Succeeded preDeploymentTaskStatus: - status: Succeeded - taskDefinitionName: pre-deployment-hello + definitionName: pre-deployment-hello status: Succeeded diff --git a/test/integration/workload-instance-failing-pre-task/00-assert.yaml b/test/integration/workload-instance-failing-pre-task/00-assert.yaml index d32b36d776..512950212b 100644 --- a/test/integration/workload-instance-failing-pre-task/00-assert.yaml +++ b/test/integration/workload-instance-failing-pre-task/00-assert.yaml @@ -1,4 +1,4 @@ -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnAppVersion metadata: name: podtato-head-1.3-1 @@ -11,7 +11,7 @@ status: status: Progressing workloadOverallStatus: Progressing --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-entry-0.1.0 diff --git a/test/integration/workload-instance-failing-pre-task/00-install.yaml b/test/integration/workload-instance-failing-pre-task/00-install.yaml index 089c931125..d2c5f3d6f4 100644 --- a/test/integration/workload-instance-failing-pre-task/00-install.yaml +++ b/test/integration/workload-instance-failing-pre-task/00-install.yaml @@ -1,4 +1,4 @@ -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnApp metadata: name: podtato-head @@ -10,7 +10,7 @@ spec: postDeploymentTasks: - post-deployment-hello --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnTaskDefinition metadata: name: check-entry-service diff --git a/test/integration/workload-instance-missing-evaluation/00-assert.yaml b/test/integration/workload-instance-missing-evaluation/00-assert.yaml index 47d716f02f..4e043c5eb8 100644 --- a/test/integration/workload-instance-missing-evaluation/00-assert.yaml +++ b/test/integration/workload-instance-missing-evaluation/00-assert.yaml @@ -1,4 +1,4 @@ -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnAppVersion metadata: name: podtato-head-1.3-1 @@ -11,7 +11,7 @@ status: status: Progressing workloadOverallStatus: Progressing --- -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnWorkloadInstance metadata: name: podtato-head-podtato-head-entry-0.1.0 diff --git a/test/integration/workload-instance-missing-evaluation/00-install.yaml b/test/integration/workload-instance-missing-evaluation/00-install.yaml index cd3bcc8e5b..e89acf65b2 100644 --- a/test/integration/workload-instance-missing-evaluation/00-install.yaml +++ b/test/integration/workload-instance-missing-evaluation/00-install.yaml @@ -1,4 +1,4 @@ -apiVersion: lifecycle.keptn.sh/v1alpha1 +apiVersion: lifecycle.keptn.sh/v1alpha2 kind: KeptnApp metadata: name: podtato-head