Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lifecycle-operator): adapt KeptnAppVersionReconciler to make use of PhaseHandler interface #2463

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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`,
},
},
{
Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions lifecycle-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -45,14 +44,21 @@ 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,
tracerFactory.GetTracer(traceComponentName),
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
Expand All @@ -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)
Expand Down
Loading