Skip to content

Commit

Permalink
chore(lifecycle-operator): make evaluationhandler injectable in Keptn…
Browse files Browse the repository at this point in the history
…AppVersionController (keptn#2402)
  • Loading branch information
geoffrey1330 authored Nov 6, 2023
1 parent 69817bd commit a060859
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common"
controllercommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/evaluation"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry"
controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors"
"go.opentelemetry.io/otel"
Expand All @@ -47,11 +48,12 @@ const traceComponentName = "keptn/lifecycle-operator/appversion"
type KeptnAppVersionReconciler struct {
Scheme *runtime.Scheme
client.Client
Log logr.Logger
EventSender controllercommon.IEvent
TracerFactory telemetry.TracerFactory
Meters apicommon.KeptnMeters
SpanHandler telemetry.ISpanHandler
Log logr.Logger
EventSender controllercommon.IEvent
TracerFactory telemetry.TracerFactory
Meters apicommon.KeptnMeters
SpanHandler telemetry.ISpanHandler
EvaluationHandler evaluation.IEvaluationHandler
}

// +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnappversions,verbs=get;list;watch;create;update;patch;delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common"
lfcv1alpha4 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha4"
controllercommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common"
"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/fake"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -296,6 +298,11 @@ func setupReconciler(objs ...client.Object) (*KeptnAppVersionReconciler, chan st
TracerFactory: tf,
SpanHandler: spanRecorder,
Meters: controllercommon.InitAppMeters(),
EvaluationHandler: &evalfake.MockEvaluationHandler{
ReconcileEvaluationsFunc: func(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, evaluationCreateAttributes evaluation.CreateEvaluationAttributes) ([]lfcv1alpha3.ItemStatus, apicommon.StatusSummary, error) {
return []lfcv1alpha3.ItemStatus{}, apicommon.StatusSummary{}, nil
},
},
}
return r, recorder.Events, spanRecorder
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@ import (
)

func (r *KeptnAppVersionReconciler) reconcilePrePostEvaluation(ctx context.Context, phaseCtx context.Context, appVersion *klcv1alpha3.KeptnAppVersion, checkType apicommon.CheckType) (apicommon.KeptnState, error) {
evaluationHandler := evaluation.EvaluationHandler{
Client: r.Client,
EventSender: r.EventSender,
Log: r.Log,
Tracer: r.getTracer(),
Scheme: r.Scheme,
SpanHandler: r.SpanHandler,
}

evaluationCreateAttributes := evaluation.CreateEvaluationAttributes{
SpanName: fmt.Sprintf(apicommon.CreateAppEvalSpanName, checkType),
CheckType: checkType,
}

newStatus, state, err := evaluationHandler.ReconcileEvaluations(ctx, phaseCtx, appVersion, evaluationCreateAttributes)
newStatus, state, err := r.EvaluationHandler.ReconcileEvaluations(ctx, phaseCtx, appVersion, evaluationCreateAttributes)
if err != nil {
return apicommon.StateUnknown, err
}
Expand Down
15 changes: 8 additions & 7 deletions lifecycle-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,14 @@ func main() {
appVersionLogger := ctrl.Log.WithName("KeptnAppVersion Controller").V(env.KeptnAppVersionControllerLogLevel)
appVersionRecorder := mgr.GetEventRecorderFor("keptnappversion-controller")
appVersionReconciler := &keptnappversion.KeptnAppVersionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: appVersionLogger,
EventSender: controllercommon.NewEventMultiplexer(appVersionLogger, appVersionRecorder, ceClient),
TracerFactory: telemetry.GetOtelInstance(),
Meters: keptnMeters,
SpanHandler: spanHandler,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: appVersionLogger,
EventSender: controllercommon.NewEventMultiplexer(appVersionLogger, appVersionRecorder, ceClient),
TracerFactory: telemetry.GetOtelInstance(),
Meters: keptnMeters,
SpanHandler: spanHandler,
EvaluationHandler: evaluationHandler,
}
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 @@ -8,6 +8,7 @@ import (

controllercommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common"
"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/telemetry"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/lifecycle/keptnappversion"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/test/component/common"
Expand All @@ -34,23 +35,36 @@ var (
spanRecorder *sdktest.SpanRecorder
)

const KeptnNamespace = "keptnlifecycle"
const (
KeptnNamespace = "keptnlifecycle"
traceComponentName = "keptn/lifecycle-operator/appversion"
)

var _ = BeforeSuite(func() {
var readyToStart chan struct{}
ctx, k8sManager, tracer, spanRecorder, k8sClient, readyToStart = common.InitSuite()

tracerFactory := &common.TracerFactory{Tracer: tracer}
EvaluationHandler := evaluation.NewEvaluationHandler(
k8sManager.GetClient(),
controllercommon.NewK8sSender(k8sManager.GetEventRecorderFor("test-appversion-controller")),
GinkgoLogr,
tracerFactory.GetTracer(traceComponentName),
k8sManager.GetScheme(),
&telemetry.SpanHandler{})

config.Instance().SetDefaultNamespace(KeptnNamespace)

// //setup controllers here
controller := &keptnappversion.KeptnAppVersionReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
EventSender: controllercommon.NewK8sSender(k8sManager.GetEventRecorderFor("test-appversion-controller")),
Log: GinkgoLogr,
Meters: common.InitKeptnMeters(),
SpanHandler: &telemetry.SpanHandler{},
TracerFactory: &common.TracerFactory{Tracer: tracer},
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
EventSender: controllercommon.NewK8sSender(k8sManager.GetEventRecorderFor("test-appversion-controller")),
Log: GinkgoLogr,
Meters: common.InitKeptnMeters(),
SpanHandler: &telemetry.SpanHandler{},
TracerFactory: tracerFactory,
EvaluationHandler: EvaluationHandler,
}
Eventually(controller.SetupWithManager(k8sManager)).WithTimeout(30 * time.Second).WithPolling(time.Second).Should(Succeed())
close(readyToStart)
Expand Down

0 comments on commit a060859

Please sign in to comment.