diff --git a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go index 966fac5a63..8fe8127414 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go @@ -56,6 +56,7 @@ type KeptnAppVersionReconciler struct { Meters apicommon.KeptnMeters SpanHandler telemetry.ISpanHandler EvaluationHandler evaluation.IEvaluationHandler + PhaseHandler phase.IHandler } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnappversions,verbs=get;list;watch;create;update;patch;delete @@ -92,12 +93,6 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ defer completionFunc() currentPhase := apicommon.PhaseAppPreDeployment - phaseHandler := phase.Handler{ - Client: r.Client, - EventSender: r.EventSender, - Log: r.Log, - SpanHandler: r.SpanHandler, - } ctxAppTrace, spanAppTrace, err := r.SpanHandler.GetSpan(ctxAppTrace, r.getTracer(), appVersion, "") if err != nil { @@ -113,7 +108,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ reconcilePreDep := func(phaseCtx context.Context) (apicommon.KeptnState, error) { return r.reconcilePrePostDeployment(ctx, phaseCtx, appVersion, apicommon.PreDeploymentCheckType) } - result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePreDep) + result, err := r.PhaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePreDep) if !result.Continue { return result.Result, err } @@ -124,7 +119,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ reconcilePreEval := func(phaseCtx context.Context) (apicommon.KeptnState, error) { return r.reconcilePrePostEvaluation(ctx, phaseCtx, appVersion, apicommon.PreDeploymentEvaluationCheckType) } - result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePreEval) + result, err := r.PhaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePreEval) if !result.Continue { return result.Result, err } @@ -135,7 +130,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ reconcileAppDep := func(phaseCtx context.Context) (apicommon.KeptnState, error) { return r.reconcileWorkloads(ctx, appVersion) } - result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcileAppDep) + result, err := r.PhaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcileAppDep) if !result.Continue { return result.Result, err } @@ -146,7 +141,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ reconcilePostDep := func(phaseCtx context.Context) (apicommon.KeptnState, error) { return r.reconcilePrePostDeployment(ctx, phaseCtx, appVersion, apicommon.PostDeploymentCheckType) } - result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePostDep) + result, err := r.PhaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePostDep) if !result.Continue { return result.Result, err } @@ -157,7 +152,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ reconcilePostEval := func(phaseCtx context.Context) (apicommon.KeptnState, error) { return r.reconcilePrePostEvaluation(ctx, phaseCtx, appVersion, apicommon.PostDeploymentEvaluationCheckType) } - result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePostEval) + result, err := r.PhaseHandler.HandlePhase(ctx, ctxAppTrace, r.getTracer(), appVersion, currentPhase, reconcilePostEval) if !result.Continue { return result.Result, err } diff --git a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller_test.go b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller_test.go index cbd5528546..f41420d3ab 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller_test.go +++ b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller_test.go @@ -13,6 +13,8 @@ import ( "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/evaluation" evalfake "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/evaluation/fake" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/eventsender" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/phase" + phasefake "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/phase/fake" telemetryfake "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry/fake" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/testcommon" "github.com/stretchr/testify/require" @@ -50,6 +52,10 @@ func TestKeptnAppVersionReconciler_reconcile(t *testing.T) { r, eventChannel, _ := setupReconciler(app) + r.PhaseHandler = &phasefake.MockHandler{HandlePhaseFunc: func(ctx context.Context, ctxTrace context.Context, tracer trace.Tracer, reconcileObject client.Object, phaseMoqParam apicommon.KeptnPhaseType, reconcilePhase func(phaseCtx context.Context) (apicommon.KeptnState, error)) (phase.PhaseResult, error) { + return phase.PhaseResult{Continue: true, Result: ctrl.Result{Requeue: false}}, nil + }} + tests := []struct { name string req ctrl.Request @@ -66,16 +72,7 @@ func TestKeptnAppVersionReconciler_reconcile(t *testing.T) { }, wantErr: nil, events: []string{ - `AppPreDeployTasksStarted`, - `AppPreDeployTasksFinished`, - `AppPreDeployEvaluationsStarted`, - `AppPreDeployEvaluationsFinished`, - `AppDeployStarted`, - `AppDeployFinished`, - `AppPostDeployTasksStarted`, - `AppPostDeployTasksFinished`, - `AppPostDeployEvaluationsStarted`, - `AppPostDeployEvaluationsFinished`, + `AppCompletedFinished`, }, }, { @@ -154,7 +151,10 @@ func TestKeptnAppVersionReconciler_ReconcileFailed(t *testing.T) { }, Status: status, } - r, eventChannel, _ := setupReconciler(app) + r, _, _ := setupReconciler(app) + r.PhaseHandler = &phasefake.MockHandler{HandlePhaseFunc: func(ctx context.Context, ctxTrace context.Context, tracer trace.Tracer, reconcileObject client.Object, phaseMoqParam apicommon.KeptnPhaseType, reconcilePhase func(phaseCtx context.Context) (apicommon.KeptnState, error)) (phase.PhaseResult, error) { + return phase.PhaseResult{Continue: false, Result: ctrl.Result{}}, nil + }} req := ctrl.Request{ NamespacedName: types.NamespacedName{ @@ -166,19 +166,6 @@ func TestKeptnAppVersionReconciler_ReconcileFailed(t *testing.T) { result, err := r.Reconcile(context.WithValue(context.TODO(), CONTEXTID, req.Name), req) require.Nil(t, err) - expectedEvents := []string{ - "AppPreDeployTasksFailed", - } - - for _, e := range expectedEvents { - event := <-eventChannel - require.Equal(t, strings.Contains(event, req.Name), true, "wrong appversion") - require.Equal(t, strings.Contains(event, req.Namespace), true, "wrong namespace") - require.Equal(t, strings.Contains(event, e), true, fmt.Sprintf("no %s found in %s", e, event)) - } - - require.Nil(t, err) - // do not requeue since we reached completion require.False(t, result.Requeue) } diff --git a/lifecycle-operator/main.go b/lifecycle-operator/main.go index 7f193aa994..0deac281ba 100644 --- a/lifecycle-operator/main.go +++ b/lifecycle-operator/main.go @@ -318,6 +318,12 @@ func main() { mgr.GetScheme(), spanHandler, ) + appVersionPhaseHandler := phase.NewHandler( + mgr.GetClient(), + appVersionEventSender, + appVersionLogger, + spanHandler, + ) appVersionReconciler := &keptnappversion.KeptnAppVersionReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), @@ -327,6 +333,7 @@ func main() { Meters: keptnMeters, SpanHandler: spanHandler, EvaluationHandler: appVersionEvaluationHandler, + PhaseHandler: appVersionPhaseHandler, } if err = (appVersionReconciler).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeptnAppVersion") diff --git a/lifecycle-operator/test/component/appversion/appversion_suite_test.go b/lifecycle-operator/test/component/appversion/appversion_suite_test.go index 5c9331a583..084b78a753 100644 --- a/lifecycle-operator/test/component/appversion/appversion_suite_test.go +++ b/lifecycle-operator/test/component/appversion/appversion_suite_test.go @@ -9,6 +9,7 @@ import ( "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/evaluation" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/eventsender" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/phase" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/lifecycle/keptnappversion" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/test/component/common" @@ -18,8 +19,6 @@ import ( sdktest "go.opentelemetry.io/otel/sdk/trace/tracetest" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - // nolint:gci - // +kubebuilder:scaffold:imports ) func TestAppversion(t *testing.T) { @@ -45,7 +44,7 @@ var _ = BeforeSuite(func() { ctx, k8sManager, tracer, spanRecorder, k8sClient, readyToStart = common.InitSuite() tracerFactory := &common.TracerFactory{Tracer: tracer} - EvaluationHandler := evaluation.NewHandler( + evaluationHandler := evaluation.NewHandler( k8sManager.GetClient(), eventsender.NewK8sSender(k8sManager.GetEventRecorderFor("test-appversion-controller")), GinkgoLogr, @@ -53,6 +52,13 @@ var _ = BeforeSuite(func() { k8sManager.GetScheme(), &telemetry.Handler{}) + phaseHandler := phase.NewHandler( + k8sManager.GetClient(), + eventsender.NewK8sSender(k8sManager.GetEventRecorderFor("test-appversion-controller")), + GinkgoLogr, + &telemetry.Handler{}, + ) + config.Instance().SetDefaultNamespace(KeptnNamespace) // //setup controllers here @@ -64,7 +70,8 @@ var _ = BeforeSuite(func() { Meters: common.InitKeptnMeters(), SpanHandler: &telemetry.Handler{}, TracerFactory: tracerFactory, - EvaluationHandler: EvaluationHandler, + EvaluationHandler: evaluationHandler, + PhaseHandler: phaseHandler, } Eventually(controller.SetupWithManager(k8sManager)).WithTimeout(30 * time.Second).WithPolling(time.Second).Should(Succeed()) close(readyToStart)