Skip to content

Commit

Permalink
chore(operator): refactor operator and scheduler statuses + add unit …
Browse files Browse the repository at this point in the history
…tests (#548)

Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed Dec 20, 2022
1 parent 3b316f9 commit c661dc0
Show file tree
Hide file tree
Showing 24 changed files with 750 additions and 287 deletions.
2 changes: 1 addition & 1 deletion operator/api/v1alpha1/keptnapp_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (src *KeptnApp) ConvertTo(dstRaw conversion.Hub) error {
dst.Status.CurrentVersion = src.Status.CurrentVersion

// Set sensible defaults for new fields
dst.Spec.Revision = "1"
dst.Spec.Revision = 1

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions operator/api/v1alpha1/keptnapp_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2"
)

Expand Down Expand Up @@ -35,7 +35,7 @@ func TestKeptnApp_ConvertFrom(t *testing.T) {
},
Spec: v1alpha2.KeptnAppSpec{
Version: "1.2.3",
Revision: "1",
Revision: 1,
Workloads: []v1alpha2.KeptnWorkloadRef{
{
Name: "workload-1",
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestKeptnApp_ConvertTo(t *testing.T) {
},
Spec: v1alpha2.KeptnAppSpec{
Version: "1.2.3",
Revision: "1",
Revision: 1,
Workloads: []v1alpha2.KeptnWorkloadRef{
{
Name: "workload-1",
Expand Down
18 changes: 9 additions & 9 deletions operator/api/v1alpha2/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ const (
StateFailed KeptnState = "Failed"
StateUnknown KeptnState = "Unknown"
StatePending KeptnState = "Pending"
StateCancelled KeptnState = "Cancelled"
StateDeprecated KeptnState = "Deprecated"
)

func (k KeptnState) IsCompleted() bool {
return k == StateSucceeded || k == StateFailed || k == StateCancelled
return k == StateSucceeded || k == StateFailed || k == StateDeprecated
}

func (k KeptnState) IsSucceeded() bool {
Expand All @@ -54,8 +54,8 @@ func (k KeptnState) IsFailed() bool {
return k == StateFailed
}

func (k KeptnState) IsCancelled() bool {
return k == StateCancelled
func (k KeptnState) IsDeprecated() bool {
return k == StateDeprecated
}

func (k KeptnState) IsPending() bool {
Expand All @@ -69,15 +69,15 @@ type StatusSummary struct {
Succeeded int
Pending int
Unknown int
Cancelled int
Deprecated int
}

func UpdateStatusSummary(status KeptnState, summary StatusSummary) StatusSummary {
switch status {
case StateFailed:
summary.Failed++
case StateCancelled:
summary.Cancelled++
case StateDeprecated:
summary.Deprecated++
case StateSucceeded:
summary.Succeeded++
case StateProgressing:
Expand All @@ -91,11 +91,11 @@ func UpdateStatusSummary(status KeptnState, summary StatusSummary) StatusSummary
}

func (s StatusSummary) GetTotalCount() int {
return s.Failed + s.Succeeded + s.Progressing + s.Pending + s.Unknown + s.Cancelled
return s.Failed + s.Succeeded + s.Progressing + s.Pending + s.Unknown + s.Deprecated
}

func GetOverallState(s StatusSummary) KeptnState {
if s.Failed > 0 || s.Cancelled > 0 {
if s.Failed > 0 || s.Deprecated > 0 {
return StateFailed
}
if s.Progressing > 0 {
Expand Down
12 changes: 6 additions & 6 deletions operator/api/v1alpha2/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestKeptnState_IsCompleted(t *testing.T) {
Want: true,
},
{
State: StateCancelled,
State: StateDeprecated,
Want: true,
},
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestKeptnState_IsFailed(t *testing.T) {
}
}

func TestKeptnState_IsCancelled(t *testing.T) {
func TestKeptnState_IsDeprecated(t *testing.T) {
tests := []struct {
State KeptnState
Want bool
Expand All @@ -88,13 +88,13 @@ func TestKeptnState_IsCancelled(t *testing.T) {
Want: false,
},
{
State: StateCancelled,
State: StateDeprecated,
Want: true,
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
require.Equal(t, tt.State.IsCancelled(), tt.Want)
require.Equal(t, tt.State.IsDeprecated(), tt.Want)
})
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func Test_UpdateStatusSummary(t *testing.T) {
Want: StatusSummary{0, 0, 0, 0, 0, 1, 0},
},
{
State: StateCancelled,
State: StateDeprecated,
Want: StatusSummary{0, 0, 0, 0, 0, 0, 1},
},
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func Test_GeOverallState(t *testing.T) {
Want: StateFailed,
},
{
Name: "cancelled",
Name: "Deprecated",
Summary: StatusSummary{0, 0, 0, 0, 0, 0, 1},
Want: StateFailed,
},
Expand Down
4 changes: 2 additions & 2 deletions operator/api/v1alpha2/common/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var phases = []KeptnPhaseType{
PhaseAppPostEvaluation,
PhaseAppDeployment,
PhaseCompleted,
PhaseCancelled,
PhaseDeprecated,
}

func (p KeptnPhaseType) IsEvaluation() bool {
Expand Down Expand Up @@ -82,7 +82,7 @@ var (
PhaseReconcileEvaluation = KeptnPhaseType{LongName: "Reconcile Evaluation", ShortName: "ReconcileEvaluation"}
PhaseCreateEvaluation = KeptnPhaseType{LongName: "Create Evaluation", ShortName: "Create Evaluation"}
PhaseCompleted = KeptnPhaseType{LongName: "Completed", ShortName: "Completed"}
PhaseCancelled = KeptnPhaseType{LongName: "Cancelled", ShortName: "Cancelled"}
PhaseDeprecated = KeptnPhaseType{LongName: "Deprecated", ShortName: "Deprecated"}
)

type PhaseTraceID map[string]propagation.MapCarrier
Expand Down
4 changes: 2 additions & 2 deletions operator/api/v1alpha2/keptnapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
// KeptnAppSpec defines the desired state of KeptnApp
type KeptnAppSpec struct {
Version string `json:"version"`
// +kubebuilder:default:="1"
Revision string `json:"revision,omitempty"`
// +kubebuilder:default:=1
Revision int `json:"revision,omitempty"`
Workloads []KeptnWorkloadRef `json:"workloads,omitempty"`
PreDeploymentTasks []string `json:"preDeploymentTasks,omitempty"`
PostDeploymentTasks []string `json:"postDeploymentTasks,omitempty"`
Expand Down
72 changes: 64 additions & 8 deletions operator/api/v1alpha2/keptnappversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestKeptnAppVersion_GetWorkloadNameOfApp(t *testing.T) {
}
}

func TestKeptnAppVersion_CancelRemainingPhases(t *testing.T) {
func TestKeptnAppVersion_DeprecateRemainingPhases(t *testing.T) {
app := KeptnAppVersion{
Status: KeptnAppVersionStatus{
PreDeploymentStatus: common.StatePending,
Expand Down Expand Up @@ -275,29 +275,85 @@ func TestKeptnAppVersion_CancelRemainingPhases(t *testing.T) {
},
},
},
{
app: app,
phase: common.PhaseAppPostDeployment,
want: KeptnAppVersion{
Status: KeptnAppVersionStatus{
PreDeploymentStatus: common.StatePending,
PreDeploymentEvaluationStatus: common.StatePending,
PostDeploymentStatus: common.StatePending,
PostDeploymentEvaluationStatus: common.StateDeprecated,
WorkloadOverallStatus: common.StatePending,
Status: common.StateFailed,
},
},
},
{
app: app,
phase: common.PhaseAppDeployment,
want: KeptnAppVersion{
Status: KeptnAppVersionStatus{
PreDeploymentStatus: common.StatePending,
PreDeploymentEvaluationStatus: common.StatePending,
PostDeploymentStatus: common.StateDeprecated,
PostDeploymentEvaluationStatus: common.StateDeprecated,
WorkloadOverallStatus: common.StatePending,
Status: common.StateFailed,
},
},
},
{
app: app,
phase: common.PhaseAppPreEvaluation,
want: KeptnAppVersion{
Status: KeptnAppVersionStatus{
PreDeploymentStatus: common.StatePending,
PreDeploymentEvaluationStatus: common.StatePending,
PostDeploymentStatus: common.StateCancelled,
PostDeploymentEvaluationStatus: common.StateCancelled,
WorkloadOverallStatus: common.StateCancelled,
PostDeploymentStatus: common.StateDeprecated,
PostDeploymentEvaluationStatus: common.StateDeprecated,
WorkloadOverallStatus: common.StateDeprecated,
Status: common.StateFailed,
},
},
},
{
app: app,
phase: common.PhaseAppPreDeployment,
want: KeptnAppVersion{
Status: KeptnAppVersionStatus{
PreDeploymentStatus: common.StatePending,
PreDeploymentEvaluationStatus: common.StateDeprecated,
PostDeploymentStatus: common.StateDeprecated,
PostDeploymentEvaluationStatus: common.StateDeprecated,
WorkloadOverallStatus: common.StateDeprecated,
Status: common.StateFailed,
},
},
},
{
app: app,
phase: common.PhaseWorkloadDeployment,
phase: common.PhaseDeprecated,
want: KeptnAppVersion{
Status: KeptnAppVersionStatus{
PreDeploymentStatus: common.StateDeprecated,
PreDeploymentEvaluationStatus: common.StateDeprecated,
PostDeploymentStatus: common.StateDeprecated,
PostDeploymentEvaluationStatus: common.StateDeprecated,
WorkloadOverallStatus: common.StateDeprecated,
Status: common.StateDeprecated,
},
},
},
{
app: app,
phase: common.PhaseWorkloadPreDeployment,
want: KeptnAppVersion{
Status: KeptnAppVersionStatus{
PreDeploymentStatus: common.StatePending,
PreDeploymentEvaluationStatus: common.StatePending,
PostDeploymentStatus: common.StateCancelled,
PostDeploymentEvaluationStatus: common.StateCancelled,
PostDeploymentStatus: common.StatePending,
PostDeploymentEvaluationStatus: common.StatePending,
WorkloadOverallStatus: common.StatePending,
Status: common.StateFailed,
},
Expand All @@ -307,7 +363,7 @@ func TestKeptnAppVersion_CancelRemainingPhases(t *testing.T) {

for _, tt := range tests {
t.Run("", func(t *testing.T) {
tt.app.CancelRemainingPhases(tt.phase)
tt.app.DeprecateRemainingPhases(tt.phase)
require.Equal(t, tt.want, tt.app)
})
}
Expand Down
39 changes: 32 additions & 7 deletions operator/api/v1alpha2/keptnappversion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,43 @@ func (v KeptnAppVersion) GetWorkloadNameOfApp(workloadName string) string {
return fmt.Sprintf("%s-%s", v.Spec.AppName, workloadName)
}

func (a *KeptnAppVersion) CancelRemainingPhases(phase common.KeptnPhaseType) {
// no need to cancel anything when post-eval tasks fail
func (a *KeptnAppVersion) DeprecateRemainingPhases(phase common.KeptnPhaseType) {
// no need to deprecate anything when post-eval tasks fail
if phase == common.PhaseAppPostEvaluation {
return
}
// cancel workload deployment and post-deployment tasks if app pre-eval failed
// deprecate post evaluation when post tasks failed
if phase == common.PhaseAppPostDeployment {
a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated
}
// deprecate post evaluation and tasks when app deployment failed
if phase == common.PhaseAppDeployment {
a.Status.PostDeploymentStatus = common.StateDeprecated
a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated
}
// deprecate app deployment, post tasks and evaluations if app pre-eval failed
if phase == common.PhaseAppPreEvaluation {
a.Status.WorkloadOverallStatus = common.StateCancelled
a.Status.PostDeploymentStatus = common.StateDeprecated
a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated
a.Status.WorkloadOverallStatus = common.StateDeprecated
}
// deprecate pre evaluations, app deployment and post tasks and evaluations when pre-tasks failed
if phase == common.PhaseAppPreDeployment {
a.Status.PostDeploymentStatus = common.StateDeprecated
a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated
a.Status.WorkloadOverallStatus = common.StateDeprecated
a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated
}
// deprecate completely everything
if phase == common.PhaseDeprecated {
a.Status.PostDeploymentStatus = common.StateDeprecated
a.Status.PostDeploymentEvaluationStatus = common.StateDeprecated
a.Status.WorkloadOverallStatus = common.StateDeprecated
a.Status.PreDeploymentEvaluationStatus = common.StateDeprecated
a.Status.PreDeploymentStatus = common.StateDeprecated
a.Status.Status = common.StateDeprecated
return
}
// cancel post-deployment tasks if workload deployment failed
a.Status.PostDeploymentStatus = common.StateCancelled
a.Status.PostDeploymentEvaluationStatus = common.StateCancelled
a.Status.Status = common.StateFailed
}

Expand Down

0 comments on commit c661dc0

Please sign in to comment.