Skip to content

Commit

Permalink
chore: unify EvaluationStatus and TaskStatus to single structure (#569)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Evaluation and Task statuses in KeptnWorkloadInstance/KeptnAppVersion use the same structure
  • Loading branch information
odubajDT authored Jan 10, 2023
1 parent f1e9fe1 commit 9b31b04
Show file tree
Hide file tree
Showing 51 changed files with 2,248 additions and 618 deletions.
6 changes: 6 additions & 0 deletions operator/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions operator/apis/lifecycle/v1alpha1/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions operator/apis/lifecycle/v1alpha1/keptnapp_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions operator/apis/lifecycle/v1alpha1/keptnapp_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}
228 changes: 228 additions & 0 deletions operator/apis/lifecycle/v1alpha1/keptnappversion_conversion.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 9b31b04

Please sign in to comment.