From f72140578a78aa7caacb058d77333f513cf15efa Mon Sep 17 00:00:00 2001 From: odubajDT <93584209+odubajDT@users.noreply.github.com> Date: Thu, 6 Jul 2023 15:17:37 +0200 Subject: [PATCH] chore(operator): refactor k8s Event sending mechanism (#1687) Signed-off-by: odubajDT Signed-off-by: Nitishupkr --- .../controllers/common/evaluationhandler.go | 15 +- .../common/evaluationhandler_test.go | 4 +- operator/controllers/common/eventsender.go | 26 +++ .../controllers/common/eventsender_test.go | 28 +++ .../controllers/common/helperfunctions.go | 8 - operator/controllers/common/phasehandler.go | 11 +- .../controllers/common/phasehandler_test.go | 12 +- operator/controllers/common/taskhandler.go | 11 +- .../controllers/common/taskhandler_test.go | 4 +- .../lifecycle/keptnapp/controller.go | 11 +- .../lifecycle/keptnapp/controller_test.go | 2 +- .../lifecycle/keptnappversion/controller.go | 9 +- .../keptnappversion/controller_test.go | 2 +- .../reconcile_prepostdeployment.go | 2 +- .../reconcile_prepostevaluation.go | 2 +- .../reconcile_workloadsstate.go | 3 +- .../lifecycle/keptnevaluation/controller.go | 11 +- .../keptnevaluation/controller_test.go | 4 +- .../lifecycle/keptntask/controller.go | 3 +- .../lifecycle/keptntask/job_runner_builder.go | 4 +- .../lifecycle/keptntask/job_utils.go | 8 +- .../lifecycle/keptntask/job_utils_test.go | 31 ++- .../lifecycle/keptntask/runtime_builder.go | 6 +- .../keptntask/runtime_builder_test.go | 25 +-- .../keptntaskdefinition/controller.go | 7 +- .../keptntaskdefinition/reconcile_function.go | 9 +- .../lifecycle/keptnworkload/controller.go | 7 +- .../keptnworkloadinstance/controller.go | 19 +- .../keptnworkloadinstance/controller_test.go | 2 +- .../reconcile_prepostdeployment.go | 2 +- .../reconcile_prepostevaluation.go | 2 +- operator/main.go | 30 +-- operator/test/component/app/app_suite_test.go | 3 +- .../appversion/appversion_suite_test.go | 2 +- .../evaluation/evaluation_suite_test.go | 3 +- .../test/component/load/load_suite_test.go | 3 +- .../test/component/task/task_suite_test.go | 2 +- .../taskdefinition_suite_test.go | 9 +- .../component/workload/workload_suite_test.go | 3 +- .../workloadinstance_suite_test.go | 2 +- .../pod_mutator/pod_mutating_webhook.go | 23 +-- .../pod_mutator/pod_mutating_webhook_test.go | 189 +++++++++--------- 42 files changed, 292 insertions(+), 267 deletions(-) create mode 100644 operator/controllers/common/eventsender.go create mode 100644 operator/controllers/common/eventsender_test.go diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index e2829390ef..19fe33559c 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -15,14 +15,13 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) type EvaluationHandler struct { client.Client - Recorder record.EventRecorder + EventSender EventSender Log logr.Logger Tracer trace.Tracer Scheme *runtime.Scheme @@ -56,7 +55,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co evaluationExists := false if oldstatus != evaluationStatus.Status { - RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Normal", reconcileObject, "EvaluationStatusChanged", fmt.Sprintf("evaluation status changed from %s to %s", oldstatus, evaluationStatus.Status), piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Normal", reconcileObject, "EvaluationStatusChanged", fmt.Sprintf("evaluation status changed from %s to %s", oldstatus, evaluationStatus.Status), piWrapper.GetVersion()) } // Check if evaluation has already succeeded or failed @@ -107,7 +106,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co summary = apicommon.UpdateStatusSummary(ns.Status, summary) } if apicommon.GetOverallState(summary) != apicommon.StateSucceeded { - RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) } return newStatus, summary, nil } @@ -129,10 +128,10 @@ func (r EvaluationHandler) CreateKeptnEvaluation(ctx context.Context, namespace err = r.Client.Create(ctx, &newEvaluation) if err != nil { r.Log.Error(err, "could not create KeptnEvaluation") - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "CreateFailed", "could not create KeptnEvaluation", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, "CreateFailed", "could not create KeptnEvaluation", piWrapper.GetVersion()) return "", err } - RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "Created", "created", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", reconcileObject, "Created", "created", piWrapper.GetVersion()) return newEvaluation.Name, nil } @@ -146,7 +145,7 @@ func (r EvaluationHandler) emitEvaluationFailureEvents(evaluation *klcv1alpha3.K k8sEventMessage = fmt.Sprintf("%s\n%s", k8sEventMessage, msg) } } - RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "Failed", k8sEventMessage, piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "Failed", k8sEventMessage, piWrapper.GetVersion()) } func (r EvaluationHandler) setupEvaluations(evaluationCreateAttributes CreateEvaluationAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha3.ItemStatus) { @@ -191,7 +190,7 @@ func (r EvaluationHandler) handleEvaluationExists(phaseCtx context.Context, piWr if evaluationStatus.Status.IsSucceeded() { spanEvaluationTrace.AddEvent(evaluation.Name + " has finished") spanEvaluationTrace.SetStatus(codes.Ok, "Finished") - RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Normal", evaluation, "Succeeded", "evaluation succeeded", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Normal", evaluation, "Succeeded", "evaluation succeeded", piWrapper.GetVersion()) } else { spanEvaluationTrace.AddEvent(evaluation.Name + " has failed") r.emitEvaluationFailureEvents(evaluation, spanEvaluationTrace, piWrapper) diff --git a/operator/controllers/common/evaluationhandler_test.go b/operator/controllers/common/evaluationhandler_test.go index 95a878f638..53b812a4a7 100644 --- a/operator/controllers/common/evaluationhandler_test.go +++ b/operator/controllers/common/evaluationhandler_test.go @@ -270,7 +270,7 @@ func TestEvaluationHandler(t *testing.T) { handler := EvaluationHandler{ SpanHandler: &spanHandlerMock, Log: ctrl.Log.WithName("controller"), - Recorder: fakeRecorder, + EventSender: NewEventSender(fakeRecorder), Client: fake.NewClientBuilder().WithObjects(&tt.evalObj).Build(), Tracer: trace.NewNoopTracerProvider().Tracer("tracer"), Scheme: scheme.Scheme, @@ -351,7 +351,7 @@ func TestEvaluationHandler_createEvaluation(t *testing.T) { handler := EvaluationHandler{ SpanHandler: &kltfake.ISpanHandlerMock{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().Build(), Tracer: trace.NewNoopTracerProvider().Tracer("tracer"), Scheme: scheme.Scheme, diff --git a/operator/controllers/common/eventsender.go b/operator/controllers/common/eventsender.go new file mode 100644 index 0000000000..efbcf3131f --- /dev/null +++ b/operator/controllers/common/eventsender.go @@ -0,0 +1,26 @@ +package common + +import ( + "fmt" + + apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type EventSender struct { + recorder record.EventRecorder +} + +func NewEventSender(recorder record.EventRecorder) EventSender { + return EventSender{ + recorder: recorder, + } +} + +// SendK8sEvent creates k8s Event and adds it to Eventqueue +func (s *EventSender) SendK8sEvent(phase apicommon.KeptnPhaseType, eventType string, reconcileObject client.Object, shortReason string, longReason string, version string) { + msg := setEventMessage(phase, reconcileObject, longReason, version) + annotations := setAnnotations(reconcileObject, phase) + s.recorder.AnnotatedEventf(reconcileObject, annotations, eventType, fmt.Sprintf("%s%s", phase.ShortName, shortReason), msg) +} diff --git a/operator/controllers/common/eventsender_test.go b/operator/controllers/common/eventsender_test.go new file mode 100644 index 0000000000..9f2138690d --- /dev/null +++ b/operator/controllers/common/eventsender_test.go @@ -0,0 +1,28 @@ +package common + +import ( + "fmt" + "testing" + + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" + "github.com/stretchr/testify/require" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/record" +) + +func TestEventSender_SendK8sEvent(t *testing.T) { + fakeRecorder := record.NewFakeRecorder(100) + eventSender := NewEventSender(fakeRecorder) + + eventSender.SendK8sEvent(common.PhaseAppDeployment, "pre-event", &v1alpha3.KeptnAppVersion{ + ObjectMeta: v1.ObjectMeta{ + Name: "app", + Namespace: "ns", + }, + }, "reason-short", "reason-long", "ver1") + + event := <-fakeRecorder.Events + + require.Contains(t, event, fmt.Sprintf("%s: reason-long / Namespace: ns, Name: app, Version: ver1", common.PhaseAppDeployment.LongName)) +} diff --git a/operator/controllers/common/helperfunctions.go b/operator/controllers/common/helperfunctions.go index 3419fa6274..cc9002c24c 100644 --- a/operator/controllers/common/helperfunctions.go +++ b/operator/controllers/common/helperfunctions.go @@ -10,7 +10,6 @@ import ( "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/interfaces" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -42,13 +41,6 @@ func GetOldStatus(name string, statuses []klcv1alpha3.ItemStatus) apicommon.Kept return oldstatus } -// RecordEvent creates k8s Event and adds it to Eventqueue -func RecordEvent(recorder record.EventRecorder, phase apicommon.KeptnPhaseType, eventType string, reconcileObject client.Object, shortReason string, longReason string, version string) { - msg := setEventMessage(phase, reconcileObject, longReason, version) - annotations := setAnnotations(reconcileObject, phase) - recorder.AnnotatedEventf(reconcileObject, annotations, eventType, fmt.Sprintf("%s%s", phase.ShortName, shortReason), msg) -} - func setEventMessage(phase apicommon.KeptnPhaseType, reconcileObject client.Object, longReason string, version string) string { if version == "" { return fmt.Sprintf("%s: %s / Namespace: %s, Name: %s", phase.LongName, longReason, reconcileObject.GetNamespace(), reconcileObject.GetName()) diff --git a/operator/controllers/common/phasehandler.go b/operator/controllers/common/phasehandler.go index a3c517d8ad..0417261cfc 100644 --- a/operator/controllers/common/phasehandler.go +++ b/operator/controllers/common/phasehandler.go @@ -10,14 +10,13 @@ import ( "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/interfaces" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) type PhaseHandler struct { client.Client - Recorder record.EventRecorder + EventSender EventSender Log logr.Logger SpanHandler ISpanHandler } @@ -50,7 +49,7 @@ func (r PhaseHandler) HandlePhase(ctx context.Context, ctxTrace context.Context, state, err := reconcilePhase(spanPhaseCtx) if err != nil { spanPhaseTrace.AddEvent(phase.LongName + " could not get reconciled") - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "ReconcileErrored", "could not get reconciled", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, "ReconcileErrored", "could not get reconciled", piWrapper.GetVersion()) span.SetStatus(codes.Error, err.Error()) return &PhaseResult{Continue: false, Result: requeueResult}, err } @@ -73,7 +72,7 @@ func (r PhaseHandler) HandlePhase(ctx context.Context, ctxTrace context.Context, } piWrapper.SetState(apicommon.StateProgressing) - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) return &PhaseResult{Continue: false, Result: requeueResult}, nil } @@ -92,7 +91,7 @@ func (r PhaseHandler) handleCompletedPhase(state apicommon.KeptnState, piWrapper if err := r.SpanHandler.UnbindSpan(reconcileObject, phase.ShortName); err != nil { r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, reconcileObject.GetName()) } - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "Failed", "has failed", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, "Failed", "has failed", piWrapper.GetVersion()) piWrapper.DeprecateRemainingPhases(phase) return &PhaseResult{Continue: false, Result: ctrl.Result{}}, nil } @@ -104,7 +103,7 @@ func (r PhaseHandler) handleCompletedPhase(state apicommon.KeptnState, piWrapper if err := r.SpanHandler.UnbindSpan(reconcileObject, phase.ShortName); err != nil { r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, reconcileObject.GetName()) } - RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "Succeeded", "has succeeded", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", reconcileObject, "Succeeded", "has succeeded", piWrapper.GetVersion()) return &PhaseResult{Continue: true, Result: ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}}, nil } diff --git a/operator/controllers/common/phasehandler_test.go b/operator/controllers/common/phasehandler_test.go index 53d1097203..3ab592b814 100644 --- a/operator/controllers/common/phasehandler_test.go +++ b/operator/controllers/common/phasehandler_test.go @@ -53,7 +53,7 @@ func TestPhaseHandler(t *testing.T) { handler: PhaseHandler{ SpanHandler: &SpanHandler{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), }, object: &v1alpha3.KeptnAppVersion{ @@ -80,7 +80,7 @@ func TestPhaseHandler(t *testing.T) { handler: PhaseHandler{ SpanHandler: &SpanHandler{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), }, object: &v1alpha3.KeptnAppVersion{ @@ -107,7 +107,7 @@ func TestPhaseHandler(t *testing.T) { handler: PhaseHandler{ SpanHandler: &SpanHandler{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), }, object: &v1alpha3.KeptnAppVersion{ @@ -134,7 +134,7 @@ func TestPhaseHandler(t *testing.T) { handler: PhaseHandler{ SpanHandler: &SpanHandler{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), }, object: &v1alpha3.KeptnAppVersion{ @@ -161,7 +161,7 @@ func TestPhaseHandler(t *testing.T) { handler: PhaseHandler{ SpanHandler: &SpanHandler{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), }, object: &v1alpha3.KeptnAppVersion{ @@ -189,7 +189,7 @@ func TestPhaseHandler(t *testing.T) { handler: PhaseHandler{ SpanHandler: &SpanHandler{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), }, object: &v1alpha3.KeptnAppVersion{ diff --git a/operator/controllers/common/taskhandler.go b/operator/controllers/common/taskhandler.go index eca635a89c..41ad2aa797 100644 --- a/operator/controllers/common/taskhandler.go +++ b/operator/controllers/common/taskhandler.go @@ -15,14 +15,13 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) type TaskHandler struct { client.Client - Recorder record.EventRecorder + EventSender EventSender Log logr.Logger Tracer trace.Tracer Scheme *runtime.Scheme @@ -58,7 +57,7 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex taskExists := false if oldstatus != taskStatus.Status { - RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "TaskStatusChanged", fmt.Sprintf("task status changed from %s to %s", oldstatus, taskStatus.Status), piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", reconcileObject, "TaskStatusChanged", fmt.Sprintf("task status changed from %s to %s", oldstatus, taskStatus.Status), piWrapper.GetVersion()) } // Check if task has already succeeded or failed @@ -111,7 +110,7 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex summary = apicommon.UpdateStatusSummary(ns.Status, summary) } if apicommon.GetOverallState(summary) != apicommon.StateSucceeded { - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) } return newStatus, summary, nil } @@ -133,10 +132,10 @@ func (r TaskHandler) CreateKeptnTask(ctx context.Context, namespace string, reco err = r.Client.Create(ctx, &newTask) if err != nil { r.Log.Error(err, "could not create KeptnTask") - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "CreateFailed", "could not create KeptnTask", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, "CreateFailed", "could not create KeptnTask", piWrapper.GetVersion()) return "", err } - RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "Created", "created", piWrapper.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", reconcileObject, "Created", "created", piWrapper.GetVersion()) return newTask.Name, nil } diff --git a/operator/controllers/common/taskhandler_test.go b/operator/controllers/common/taskhandler_test.go index 39193c53f1..285865fdfc 100644 --- a/operator/controllers/common/taskhandler_test.go +++ b/operator/controllers/common/taskhandler_test.go @@ -373,7 +373,7 @@ func TestTaskHandler(t *testing.T) { handler := TaskHandler{ SpanHandler: &spanHandlerMock, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithObjects(initObjs...).Build(), Tracer: trace.NewNoopTracerProvider().Tracer("tracer"), Scheme: scheme.Scheme, @@ -444,7 +444,7 @@ func TestTaskHandler_createTask(t *testing.T) { handler := TaskHandler{ SpanHandler: &kltfake.ISpanHandlerMock{}, Log: ctrl.Log.WithName("controller"), - Recorder: record.NewFakeRecorder(100), + EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().Build(), Tracer: trace.NewNoopTracerProvider().Tracer("tracer"), Scheme: scheme.Scheme, diff --git a/operator/controllers/lifecycle/keptnapp/controller.go b/operator/controllers/lifecycle/keptnapp/controller.go index 4f85e9a75c..e9b90d4fe0 100644 --- a/operator/controllers/lifecycle/keptnapp/controller.go +++ b/operator/controllers/lifecycle/keptnapp/controller.go @@ -33,7 +33,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -48,7 +47,7 @@ const traceComponentName = "keptn/operator/app" type KeptnAppReconciler struct { client.Client Scheme *runtime.Scheme - Recorder record.EventRecorder + EventSender controllercommon.EventSender Log logr.Logger TracerFactory controllercommon.TracerFactory } @@ -106,10 +105,10 @@ func (r *KeptnAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c if err != nil { r.Log.Error(err, "could not create AppVersion") span.SetStatus(codes.Error, err.Error()) - controllercommon.RecordEvent(r.Recorder, common.PhaseCreateAppVersion, "Warning", appVersion, "AppVersionNotCreated", "Could not create KeptnAppVersion", appVersion.Spec.Version) + r.EventSender.SendK8sEvent(common.PhaseCreateAppVersion, "Warning", appVersion, "AppVersionNotCreated", "Could not create KeptnAppVersion", appVersion.Spec.Version) return ctrl.Result{}, err } - controllercommon.RecordEvent(r.Recorder, common.PhaseCreateAppVersion, "Normal", appVersion, "AppVersionCreated", "created KeptnAppVersion", appVersion.Spec.Version) + r.EventSender.SendK8sEvent(common.PhaseCreateAppVersion, "Normal", appVersion, "AppVersionCreated", "created KeptnAppVersion", appVersion.Spec.Version) app.Status.CurrentVersion = app.Spec.Version if err := r.Client.Status().Update(ctx, app); err != nil { @@ -173,10 +172,10 @@ func (r *KeptnAppReconciler) handleGenerationBump(ctx context.Context, app *klcv if app.Generation != 1 { if err := r.deprecateAppVersions(ctx, app); err != nil { r.Log.Error(err, "could not deprecate appVersions for appVersion %s", app.GetAppVersionName()) - controllercommon.RecordEvent(r.Recorder, common.PhaseCreateAppVersion, "Warning", app, "AppVersionNotDeprecated", fmt.Sprintf("could not deprecate KeptnAppVersions for KeptnAppVersion: %s", app.GetAppVersionName()), app.Spec.Version) + r.EventSender.SendK8sEvent(common.PhaseCreateAppVersion, "Warning", app, "AppVersionNotDeprecated", fmt.Sprintf("could not deprecate KeptnAppVersions for KeptnAppVersion: %s", app.GetAppVersionName()), app.Spec.Version) return err } - controllercommon.RecordEvent(r.Recorder, common.PhaseCreateAppVersion, "Normal", app, "AppVersionDeprecated", fmt.Sprintf("deprecated KeptnAppVersions for KeptnAppVersion: %s", app.GetAppVersionName()), app.Spec.Version) + r.EventSender.SendK8sEvent(common.PhaseCreateAppVersion, "Normal", app, "AppVersionDeprecated", fmt.Sprintf("deprecated KeptnAppVersions for KeptnAppVersion: %s", app.GetAppVersionName()), app.Spec.Version) } return nil } diff --git a/operator/controllers/lifecycle/keptnapp/controller_test.go b/operator/controllers/lifecycle/keptnapp/controller_test.go index 250ea0cdac..857e101fbb 100644 --- a/operator/controllers/lifecycle/keptnapp/controller_test.go +++ b/operator/controllers/lifecycle/keptnapp/controller_test.go @@ -210,7 +210,7 @@ func setupReconciler() (*KeptnAppReconciler, chan string, *fake.ITracerMock) { r := &KeptnAppReconciler{ Client: fakeClient, Scheme: scheme.Scheme, - Recorder: recorder, + EventSender: controllercommon.NewEventSender(recorder), Log: ctrl.Log.WithName("test-appController"), TracerFactory: tf, } diff --git a/operator/controllers/lifecycle/keptnappversion/controller.go b/operator/controllers/lifecycle/keptnappversion/controller.go index d6e3d845cf..3866b229e9 100644 --- a/operator/controllers/lifecycle/keptnappversion/controller.go +++ b/operator/controllers/lifecycle/keptnappversion/controller.go @@ -33,7 +33,6 @@ import ( "go.opentelemetry.io/otel/trace" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -48,7 +47,7 @@ type KeptnAppVersionReconciler struct { Scheme *runtime.Scheme client.Client Log logr.Logger - Recorder record.EventRecorder + EventSender controllercommon.EventSender TracerFactory controllercommon.TracerFactory Meters apicommon.KeptnMeters SpanHandler controllercommon.ISpanHandler @@ -91,7 +90,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ phase := apicommon.PhaseAppPreDeployment phaseHandler := controllercommon.PhaseHandler{ Client: r.Client, - Recorder: r.Recorder, + EventSender: r.EventSender, Log: r.Log, SpanHandler: r.SpanHandler, } @@ -104,7 +103,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ if appVersion.Status.CurrentPhase == "" { appVersion.SetSpanAttributes(spanAppTrace) spanAppTrace.AddEvent("App Version Pre-Deployment Tasks started", trace.WithTimestamp(time.Now())) - controllercommon.RecordEvent(r.Recorder, phase, "Normal", appVersion, "Started", "have started", appVersion.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", appVersion, "Started", "have started", appVersion.GetVersion()) } if !appVersion.IsPreDeploymentSucceeded() { @@ -161,7 +160,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ } } - controllercommon.RecordEvent(r.Recorder, phase, "Normal", appVersion, "Finished", "is finished", appVersion.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", appVersion, "Finished", "is finished", appVersion.GetVersion()) err = r.Client.Status().Update(ctx, appVersion) if err != nil { span.SetStatus(codes.Error, err.Error()) diff --git a/operator/controllers/lifecycle/keptnappversion/controller_test.go b/operator/controllers/lifecycle/keptnappversion/controller_test.go index 85cb565d05..4ed3dd19a8 100644 --- a/operator/controllers/lifecycle/keptnappversion/controller_test.go +++ b/operator/controllers/lifecycle/keptnappversion/controller_test.go @@ -198,7 +198,7 @@ func setupReconciler(objs ...client.Object) (*KeptnAppVersionReconciler, chan st r := &KeptnAppVersionReconciler{ Client: fakeClient, Scheme: scheme.Scheme, - Recorder: recorder, + EventSender: controllercommon.NewEventSender(recorder), Log: ctrl.Log.WithName("test-appVersionController"), TracerFactory: tf, SpanHandler: spanRecorder, diff --git a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go index c66d8f003a..6367e857d7 100644 --- a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go +++ b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostdeployment.go @@ -13,7 +13,7 @@ import ( func (r *KeptnAppVersionReconciler) reconcilePrePostDeployment(ctx context.Context, phaseCtx context.Context, appVersion *klcv1alpha3.KeptnAppVersion, checkType apicommon.CheckType) (apicommon.KeptnState, error) { taskHandler := controllercommon.TaskHandler{ Client: r.Client, - Recorder: r.Recorder, + EventSender: r.EventSender, Log: r.Log, Tracer: r.getTracer(), Scheme: r.Scheme, diff --git a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go index cc9531efd2..d33d8f505f 100644 --- a/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go +++ b/operator/controllers/lifecycle/keptnappversion/reconcile_prepostevaluation.go @@ -13,7 +13,7 @@ import ( func (r *KeptnAppVersionReconciler) reconcilePrePostEvaluation(ctx context.Context, phaseCtx context.Context, appVersion *klcv1alpha3.KeptnAppVersion, checkType apicommon.CheckType) (apicommon.KeptnState, error) { evaluationHandler := controllercommon.EvaluationHandler{ Client: r.Client, - Recorder: r.Recorder, + EventSender: r.EventSender, Log: r.Log, Tracer: r.getTracer(), Scheme: r.Scheme, diff --git a/operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go b/operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go index 367dcbed3b..d4d43d0f05 100644 --- a/operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go +++ b/operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go @@ -6,7 +6,6 @@ import ( klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" operatorcommon "github.com/keptn/lifecycle-toolkit/operator/common" - controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -44,7 +43,7 @@ func (r *KeptnAppVersionReconciler) reconcileWorkloads(ctx context.Context, appV } if !found { - controllercommon.RecordEvent(r.Recorder, phase, "Warning", appVersion, "NotFound", "workloadInstance not found", appVersion.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", appVersion, "NotFound", "workloadInstance not found", appVersion.GetVersion()) } newStatus = append(newStatus, klcv1alpha3.WorkloadStatus{ diff --git a/operator/controllers/lifecycle/keptnevaluation/controller.go b/operator/controllers/lifecycle/keptnevaluation/controller.go index 7f126e182d..f76fa9eda0 100644 --- a/operator/controllers/lifecycle/keptnevaluation/controller.go +++ b/operator/controllers/lifecycle/keptnevaluation/controller.go @@ -34,7 +34,6 @@ import ( "go.opentelemetry.io/otel/trace" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -47,7 +46,7 @@ const traceComponentName = "keptn/operator/evaluation" type KeptnEvaluationReconciler struct { client.Client Scheme *runtime.Scheme - Recorder record.EventRecorder + EventSender controllercommon.EventSender Log logr.Logger Meters apicommon.KeptnMeters TracerFactory controllercommon.TracerFactory @@ -139,19 +138,19 @@ func (r *KeptnEvaluationReconciler) handleEvaluationIncomplete(ctx context.Conte // Evaluation is uncompleted, update status anyway this avoids updating twice in case of completion err := r.Client.Status().Update(ctx, evaluation) if err != nil { - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "ReconcileErrored", "could not update status", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "ReconcileErrored", "could not update status", "") span.SetStatus(codes.Error, err.Error()) return err } - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Normal", evaluation, "NotFinished", "has not finished", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Normal", evaluation, "NotFinished", "has not finished", "") return nil } func (r *KeptnEvaluationReconciler) handleEvaluationExceededRetries(ctx context.Context, evaluation *klcv1alpha3.KeptnEvaluation, span trace.Span) { - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "ReconcileTimeOut", "retryCount exceeded", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "ReconcileTimeOut", "retryCount exceeded", "") err := controllererrors.ErrRetryCountExceeded span.SetStatus(codes.Error, err.Error()) evaluation.Status.OverallStatus = apicommon.StateFailed @@ -240,7 +239,7 @@ func (r *KeptnEvaluationReconciler) updateFinishedEvaluationMetrics(ctx context. err := r.Client.Status().Update(ctx, evaluation) if err != nil { span.SetStatus(codes.Error, err.Error()) - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "ReconcileErrored", "could not update status", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, "ReconcileErrored", "could not update status", "") return err } diff --git a/operator/controllers/lifecycle/keptnevaluation/controller_test.go b/operator/controllers/lifecycle/keptnevaluation/controller_test.go index f63900019f..94c35a4d10 100644 --- a/operator/controllers/lifecycle/keptnevaluation/controller_test.go +++ b/operator/controllers/lifecycle/keptnevaluation/controller_test.go @@ -247,8 +247,6 @@ func setupReconcilerAndClient(t *testing.T, objects ...client.Object) (*KeptnEva return tr }} - recorder := record.NewFakeRecorder(100) - fakeClient := k8sfake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build() provider := metric.NewMeterProvider() @@ -258,7 +256,7 @@ func setupReconcilerAndClient(t *testing.T, objects ...client.Object) (*KeptnEva Client: fakeClient, Scheme: fakeClient.Scheme(), Log: logr.Logger{}, - Recorder: recorder, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), Meters: controllercommon.SetUpKeptnTaskMeters(meter), TracerFactory: tf, } diff --git a/operator/controllers/lifecycle/keptntask/controller.go b/operator/controllers/lifecycle/keptntask/controller.go index 25f5ee4cd3..d3c7ee3286 100644 --- a/operator/controllers/lifecycle/keptntask/controller.go +++ b/operator/controllers/lifecycle/keptntask/controller.go @@ -31,7 +31,6 @@ import ( "go.opentelemetry.io/otel/trace" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -44,7 +43,7 @@ const traceComponentName = "keptn/operator/task" type KeptnTaskReconciler struct { client.Client Scheme *runtime.Scheme - Recorder record.EventRecorder + EventSender controllercommon.EventSender Log logr.Logger Meters apicommon.KeptnMeters TracerFactory controllercommon.TracerFactory diff --git a/operator/controllers/lifecycle/keptntask/job_runner_builder.go b/operator/controllers/lifecycle/keptntask/job_runner_builder.go index a7ee0025b6..a5d16c3594 100644 --- a/operator/controllers/lifecycle/keptntask/job_runner_builder.go +++ b/operator/controllers/lifecycle/keptntask/job_runner_builder.go @@ -3,9 +3,9 @@ package keptntask import ( "github.com/go-logr/logr" klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "golang.org/x/net/context" corev1 "k8s.io/api/core/v1" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -20,7 +20,7 @@ type JobRunnerBuilder interface { // BuilderOptions contains everything needed to build the current job type BuilderOptions struct { client.Client - recorder record.EventRecorder + EventSender controllercommon.EventSender req ctrl.Request Log logr.Logger task *klcv1alpha3.KeptnTask diff --git a/operator/controllers/lifecycle/keptntask/job_utils.go b/operator/controllers/lifecycle/keptntask/job_utils.go index 76cf0f016e..9e6b91067a 100644 --- a/operator/controllers/lifecycle/keptntask/job_utils.go +++ b/operator/controllers/lifecycle/keptntask/job_utils.go @@ -20,7 +20,7 @@ func (r *KeptnTaskReconciler) createJob(ctx context.Context, req ctrl.Request, t jobName := "" definition, err := controllercommon.GetTaskDefinition(r.Client, r.Log, ctx, task.Spec.TaskDefinition, req.Namespace) if err != nil { - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseCreateTask, "Warning", task, "TaskDefinitionNotFound", fmt.Sprintf("could not find KeptnTaskDefinition: %s ", task.Spec.TaskDefinition), "") + r.EventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", task, "TaskDefinitionNotFound", fmt.Sprintf("could not find KeptnTaskDefinition: %s ", task.Spec.TaskDefinition), "") return err } @@ -46,11 +46,11 @@ func (r *KeptnTaskReconciler) createFunctionJob(ctx context.Context, req ctrl.Re err = r.Client.Create(ctx, job) if err != nil { r.Log.Error(err, "could not create job") - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseCreateTask, "Warning", task, "JobNotCreated", fmt.Sprintf("could not create Job: %s ", task.Name), "") + r.EventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", task, "JobNotCreated", fmt.Sprintf("could not create Job: %s ", task.Name), "") return job.Name, err } - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileTask, "Normal", task, "JobCreated", fmt.Sprintf("created Job: %s ", task.Name), "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileTask, "Normal", task, "JobCreated", fmt.Sprintf("created Job: %s ", task.Name), "") return job.Name, nil } @@ -127,7 +127,7 @@ func (r *KeptnTaskReconciler) generateJob(ctx context.Context, task *klcv1alpha3 task: task, containerSpec: definition.Spec.Container, funcSpec: controllercommon.GetRuntimeSpec(definition), - recorder: r.Recorder, + EventSender: r.EventSender, Image: controllercommon.GetRuntimeImage(definition), MountPath: controllercommon.GetRuntimeMountPath(definition), ConfigMap: definition.Status.Function.ConfigMap, diff --git a/operator/controllers/lifecycle/keptntask/job_utils_test.go b/operator/controllers/lifecycle/keptntask/job_utils_test.go index 1e5010b98b..92bdc14d69 100644 --- a/operator/controllers/lifecycle/keptntask/job_utils_test.go +++ b/operator/controllers/lifecycle/keptntask/job_utils_test.go @@ -7,6 +7,7 @@ import ( klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/common" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "github.com/stretchr/testify/require" batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" @@ -26,8 +27,6 @@ func TestKeptnTaskReconciler_createJob(t *testing.T) { fakeClient := fake.NewClientBuilder().WithObjects(cm).Build() - fakeRecorder := &record.FakeRecorder{} - err := klcv1alpha3.AddToScheme(fakeClient.Scheme()) require.Nil(t, err) @@ -41,10 +40,10 @@ func TestKeptnTaskReconciler_createJob(t *testing.T) { require.Nil(t, err) r := &KeptnTaskReconciler{ - Client: fakeClient, - Recorder: fakeRecorder, - Log: ctrl.Log.WithName("task-controller"), - Scheme: fakeClient.Scheme(), + Client: fakeClient, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), + Log: ctrl.Log.WithName("task-controller"), + Scheme: fakeClient.Scheme(), } task := makeTask("my-task", namespace, taskDefinitionName) @@ -100,8 +99,6 @@ func TestKeptnTaskReconciler_createJob_withTaskDefInDefaultNamespace(t *testing. fakeClient := fake.NewClientBuilder().WithObjects(cm).Build() - fakeRecorder := &record.FakeRecorder{} - err := klcv1alpha3.AddToScheme(fakeClient.Scheme()) require.Nil(t, err) @@ -115,10 +112,10 @@ func TestKeptnTaskReconciler_createJob_withTaskDefInDefaultNamespace(t *testing. require.Nil(t, err) r := &KeptnTaskReconciler{ - Client: fakeClient, - Recorder: fakeRecorder, - Log: ctrl.Log.WithName("task-controller"), - Scheme: fakeClient.Scheme(), + Client: fakeClient, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), + Log: ctrl.Log.WithName("task-controller"), + Scheme: fakeClient.Scheme(), } task := makeTask("my-task", namespace, taskDefinitionName) @@ -173,8 +170,6 @@ func TestKeptnTaskReconciler_updateTaskStatus(t *testing.T) { fakeClient := fake.NewClientBuilder().WithObjects(job).Build() - fakeRecorder := &record.FakeRecorder{} - err := klcv1alpha3.AddToScheme(fakeClient.Scheme()) require.Nil(t, err) @@ -188,10 +183,10 @@ func TestKeptnTaskReconciler_updateTaskStatus(t *testing.T) { require.Nil(t, err) r := &KeptnTaskReconciler{ - Client: fakeClient, - Recorder: fakeRecorder, - Log: ctrl.Log.WithName("task-controller"), - Scheme: fakeClient.Scheme(), + Client: fakeClient, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), + Log: ctrl.Log.WithName("task-controller"), + Scheme: fakeClient.Scheme(), } task := makeTask("my-task", namespace, taskDefinitionName) diff --git a/operator/controllers/lifecycle/keptntask/runtime_builder.go b/operator/controllers/lifecycle/keptntask/runtime_builder.go index df562573c6..e6d4fc743c 100644 --- a/operator/controllers/lifecycle/keptntask/runtime_builder.go +++ b/operator/controllers/lifecycle/keptntask/runtime_builder.go @@ -157,7 +157,7 @@ func (fb *RuntimeBuilder) getParams(ctx context.Context) (*RuntimeExecutionParam if len(fb.options.task.Spec.Parameters.Inline) > 0 { err = mergo.Merge(¶ms.Parameters, fb.options.task.Spec.Parameters.Inline) if err != nil { - controllercommon.RecordEvent(fb.options.recorder, apicommon.PhaseCreateTask, "Warning", fb.options.task, "TaskDefinitionMergeFailure", fmt.Sprintf("could not merge KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") + fb.options.EventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, "TaskDefinitionMergeFailure", fmt.Sprintf("could not merge KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") return nil, err } } @@ -211,7 +211,7 @@ func (fb *RuntimeBuilder) handleParent(ctx context.Context, params *RuntimeExecu var parentJobParams RuntimeExecutionParams parentDefinition, err := controllercommon.GetTaskDefinition(fb.options.Client, fb.options.Log, ctx, fb.options.funcSpec.FunctionReference.Name, fb.options.req.Namespace) if err != nil { - controllercommon.RecordEvent(fb.options.recorder, apicommon.PhaseCreateTask, "Warning", fb.options.task, "TaskDefinitionNotFound", fmt.Sprintf("could not find KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") + fb.options.EventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, "TaskDefinitionNotFound", fmt.Sprintf("could not find KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") return err } parSpec := controllercommon.GetRuntimeSpec(parentDefinition) @@ -223,7 +223,7 @@ func (fb *RuntimeBuilder) handleParent(ctx context.Context, params *RuntimeExecu // merge parameter to make sure we use child task data for env var and secrets err = mergo.Merge(params, parentJobParams) if err != nil { - controllercommon.RecordEvent(fb.options.recorder, apicommon.PhaseCreateTask, "Warning", fb.options.task, "TaskDefinitionMergeFailure", fmt.Sprintf("could not merge KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") + fb.options.EventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, "TaskDefinitionMergeFailure", fmt.Sprintf("could not merge KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") return err } diff --git a/operator/controllers/lifecycle/keptntask/runtime_builder_test.go b/operator/controllers/lifecycle/keptntask/runtime_builder_test.go index 055c21939d..1137c0f4f6 100644 --- a/operator/controllers/lifecycle/keptntask/runtime_builder_test.go +++ b/operator/controllers/lifecycle/keptntask/runtime_builder_test.go @@ -7,6 +7,7 @@ import ( klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/common" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/common/fake" "github.com/stretchr/testify/require" "golang.org/x/net/context" @@ -58,8 +59,8 @@ func TestJSBuilder_handleParent(t *testing.T) { { name: "no definition", options: BuilderOptions{ - Client: fake.NewClient(), - recorder: &record.FakeRecorder{}, + Client: fake.NewClient(), + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), req: ctrl.Request{ NamespacedName: types.NamespacedName{Namespace: "default"}, }, @@ -74,8 +75,8 @@ func TestJSBuilder_handleParent(t *testing.T) { { name: "definition exists, recursive", options: BuilderOptions{ - Client: fake.NewClient(def), - recorder: &record.FakeRecorder{}, + Client: fake.NewClient(def), + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), req: ctrl.Request{ NamespacedName: types.NamespacedName{Namespace: "default"}, }, @@ -89,8 +90,8 @@ func TestJSBuilder_handleParent(t *testing.T) { { name: "definition exists, with parameters and secrets", options: BuilderOptions{ - Client: fake.NewClient(paramDef, def), - recorder: &record.FakeRecorder{}, + Client: fake.NewClient(paramDef, def), + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), req: ctrl.Request{ NamespacedName: types.NamespacedName{Namespace: "default"}, }, @@ -205,8 +206,8 @@ func TestJSBuilder_getParams(t *testing.T) { { name: "definition exists, no parent", options: BuilderOptions{ - Client: fake.NewClient(def), - recorder: &record.FakeRecorder{}, + Client: fake.NewClient(def), + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), req: ctrl.Request{ NamespacedName: types.NamespacedName{Namespace: "default"}, }, @@ -236,8 +237,8 @@ func TestJSBuilder_getParams(t *testing.T) { { name: "definition exists, parent with parameters and secrets", options: BuilderOptions{ - Client: fake.NewClient(paramDef, def), - recorder: &record.FakeRecorder{}, + Client: fake.NewClient(paramDef, def), + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), req: ctrl.Request{ NamespacedName: types.NamespacedName{Namespace: "default"}, }, @@ -268,8 +269,8 @@ func TestJSBuilder_getParams(t *testing.T) { { name: "definition exists, parent is of a different runtime", options: BuilderOptions{ - Client: fake.NewClient(parentPy, defJS), - recorder: &record.FakeRecorder{}, + Client: fake.NewClient(parentPy, defJS), + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), req: ctrl.Request{ NamespacedName: types.NamespacedName{Namespace: "default"}, }, diff --git a/operator/controllers/lifecycle/keptntaskdefinition/controller.go b/operator/controllers/lifecycle/keptntaskdefinition/controller.go index f9a3f249cb..4f75105ae5 100644 --- a/operator/controllers/lifecycle/keptntaskdefinition/controller.go +++ b/operator/controllers/lifecycle/keptntaskdefinition/controller.go @@ -27,7 +27,6 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -35,9 +34,9 @@ import ( // KeptnTaskDefinitionReconciler reconciles a KeptnTaskDefinition object type KeptnTaskDefinitionReconciler struct { client.Client - Scheme *runtime.Scheme - Log logr.Logger - Recorder record.EventRecorder + Scheme *runtime.Scheme + Log logr.Logger + EventSender controllercommon.EventSender } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptntaskdefinitions,verbs=get;list;watch;create;update;patch;delete diff --git a/operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go b/operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go index 916bbbb9df..ffd57ee9ae 100644 --- a/operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go +++ b/operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go @@ -6,7 +6,6 @@ import ( klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" - controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -32,18 +31,18 @@ func (r *KeptnTaskDefinitionReconciler) reconcileConfigMap(ctx context.Context, if (cm == nil || reflect.DeepEqual(cm, &corev1.ConfigMap{})) && functionCm != nil { //cm does not exist or new taskdef with inline func err := r.Client.Create(ctx, functionCm) if err != nil { - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileTask, "Warning", functionCm, "ConfigMapNotCreated", "could not create configmap", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileTask, "Warning", functionCm, "ConfigMapNotCreated", "could not create configmap", "") return } - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileTask, "Normal", functionCm, "ConfigMapCreated", "created configmap", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileTask, "Normal", functionCm, "ConfigMapCreated", "created configmap", "") } else if !reflect.DeepEqual(cm, functionCm) && functionCm != nil { //cm and inline func exists but differ err := r.Client.Update(ctx, functionCm) if err != nil { - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileTask, "Warning", functionCm, "ConfigMapNotUpdated", "uould not update configmap", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileTask, "Warning", functionCm, "ConfigMapNotUpdated", "uould not update configmap", "") return } - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseReconcileTask, "Normal", functionCm, "ConfigMapUpdated", "updated configmap", "") + r.EventSender.SendK8sEvent(apicommon.PhaseReconcileTask, "Normal", functionCm, "ConfigMapUpdated", "updated configmap", "") } //nothing changed } diff --git a/operator/controllers/lifecycle/keptnworkload/controller.go b/operator/controllers/lifecycle/keptnworkload/controller.go index 1ed4645996..682bc006f2 100644 --- a/operator/controllers/lifecycle/keptnworkload/controller.go +++ b/operator/controllers/lifecycle/keptnworkload/controller.go @@ -32,7 +32,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -47,7 +46,7 @@ const traceComponentName = "keptn/operator/workload" type KeptnWorkloadReconciler struct { client.Client Scheme *runtime.Scheme - Recorder record.EventRecorder + EventSender controllercommon.EventSender Log logr.Logger TracerFactory controllercommon.TracerFactory } @@ -105,10 +104,10 @@ func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Reques if err != nil { r.Log.Error(err, "could not create Workload Instance") span.SetStatus(codes.Error, err.Error()) - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseCreateWorklodInstance, "Warning", workloadInstance, "WorkloadInstanceNotCreated", "could not create KeptnWorkloadInstance ", workloadInstance.Spec.Version) + r.EventSender.SendK8sEvent(apicommon.PhaseCreateWorklodInstance, "Warning", workloadInstance, "WorkloadInstanceNotCreated", "could not create KeptnWorkloadInstance ", workloadInstance.Spec.Version) return ctrl.Result{}, err } - controllercommon.RecordEvent(r.Recorder, apicommon.PhaseCreateWorklodInstance, "Normal", workloadInstance, "WorkloadInstanceCreated", "created KeptnWorkloadInstance ", workloadInstance.Spec.Version) + r.EventSender.SendK8sEvent(apicommon.PhaseCreateWorklodInstance, "Normal", workloadInstance, "WorkloadInstanceCreated", "created KeptnWorkloadInstance ", workloadInstance.Spec.Version) workload.Status.CurrentVersion = workload.Spec.Version if err := r.Client.Status().Update(ctx, workload); err != nil { r.Log.Error(err, "could not update Current Version of Workload") diff --git a/operator/controllers/lifecycle/keptnworkloadinstance/controller.go b/operator/controllers/lifecycle/keptnworkloadinstance/controller.go index 4548caf6fa..bc433d6fa7 100644 --- a/operator/controllers/lifecycle/keptnworkloadinstance/controller.go +++ b/operator/controllers/lifecycle/keptnworkloadinstance/controller.go @@ -33,7 +33,6 @@ import ( "go.opentelemetry.io/otel/trace" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -47,7 +46,7 @@ const traceComponentName = "keptn/operator/workloadinstance" type KeptnWorkloadInstanceReconciler struct { client.Client Scheme *runtime.Scheme - Recorder record.EventRecorder + EventSender controllercommon.EventSender Log logr.Logger Meters apicommon.KeptnMeters SpanHandler *controllercommon.SpanHandler @@ -101,7 +100,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr phase := apicommon.PhaseWorkloadPreDeployment phaseHandler := controllercommon.PhaseHandler{ Client: r.Client, - Recorder: r.Recorder, + EventSender: r.EventSender, Log: r.Log, SpanHandler: r.SpanHandler, } @@ -114,7 +113,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr if workloadInstance.Status.CurrentPhase == "" { spanWorkloadTrace.AddEvent("WorkloadInstance Pre-Deployment Tasks started", trace.WithTimestamp(time.Now())) - controllercommon.RecordEvent(r.Recorder, phase, "Normal", workloadInstance, "Started", "have started", workloadInstance.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", workloadInstance, "Started", "have started", workloadInstance.GetVersion()) } if !workloadInstance.IsPreDeploymentSucceeded() { @@ -205,7 +204,7 @@ func (r *KeptnWorkloadInstanceReconciler) finishKeptnWorkloadInstanceReconcile(c r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, workloadInstance.Name) } - controllercommon.RecordEvent(r.Recorder, phase, "Normal", workloadInstance, "Finished", "is finished", workloadInstance.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", workloadInstance, "Finished", "is finished", workloadInstance.GetVersion()) return ctrl.Result{}, nil } @@ -226,9 +225,9 @@ func (r *KeptnWorkloadInstanceReconciler) SetupWithManager(mgr ctrl.Manager) err func (r *KeptnWorkloadInstanceReconciler) sendUnfinishedPreEvaluationEvents(appPreEvalStatus apicommon.KeptnState, phase apicommon.KeptnPhaseType, workloadInstance *klcv1alpha3.KeptnWorkloadInstance) { if appPreEvalStatus.IsFailed() { - controllercommon.RecordEvent(r.Recorder, phase, "Warning", workloadInstance, "Failed", "has failed since app has failed", workloadInstance.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", workloadInstance, "Failed", "has failed since app has failed", workloadInstance.GetVersion()) } - controllercommon.RecordEvent(r.Recorder, phase, "Normal", workloadInstance, "NotFinished", "Pre evaluations tasks for app not finished", workloadInstance.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", workloadInstance, "NotFinished", "Pre evaluations tasks for app not finished", workloadInstance.GetVersion()) } func (r *KeptnWorkloadInstanceReconciler) setupSpansContexts(ctx context.Context, workloadInstance *klcv1alpha3.KeptnWorkloadInstance) (context.Context, trace.Span, func(span trace.Span, workloadInstance *klcv1alpha3.KeptnWorkloadInstance)) { @@ -263,11 +262,11 @@ func (r *KeptnWorkloadInstanceReconciler) checkPreEvaluationStatusOfApp(ctx cont found, appVersion, err := r.getAppVersionForWorkloadInstance(ctx, workloadInstance) if err != nil { span.SetStatus(codes.Error, err.Error()) - controllercommon.RecordEvent(r.Recorder, phase, "Warning", workloadInstance, "GetAppVersionFailed", "has failed since app could not be retrieved", workloadInstance.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", workloadInstance, "GetAppVersionFailed", "has failed since app could not be retrieved", workloadInstance.GetVersion()) return true, fmt.Errorf(controllererrors.ErrCannotFetchAppVersionForWorkloadInstanceMsg + err.Error()) } else if !found { span.SetStatus(codes.Error, "app could not be found") - controllercommon.RecordEvent(r.Recorder, phase, "Warning", workloadInstance, "AppVersionNotFound", "has failed since app could not be found", workloadInstance.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Warning", workloadInstance, "AppVersionNotFound", "has failed since app could not be found", workloadInstance.GetVersion()) return true, fmt.Errorf(controllererrors.ErrCannotFetchAppVersionForWorkloadInstanceMsg) } @@ -277,7 +276,7 @@ func (r *KeptnWorkloadInstanceReconciler) checkPreEvaluationStatusOfApp(ctx cont return true, nil } - controllercommon.RecordEvent(r.Recorder, phase, "Normal", workloadInstance, "FinishedSuccess", "Pre evaluations tasks for app have finished successfully", workloadInstance.GetVersion()) + r.EventSender.SendK8sEvent(phase, "Normal", workloadInstance, "FinishedSuccess", "Pre evaluations tasks for app have finished successfully", workloadInstance.GetVersion()) // set the App trace id if not already set if len(workloadInstance.Spec.TraceId) < 1 { diff --git a/operator/controllers/lifecycle/keptnworkloadinstance/controller_test.go b/operator/controllers/lifecycle/keptnworkloadinstance/controller_test.go index d91b6a5efa..de3f5165f3 100644 --- a/operator/controllers/lifecycle/keptnworkloadinstance/controller_test.go +++ b/operator/controllers/lifecycle/keptnworkloadinstance/controller_test.go @@ -1039,7 +1039,7 @@ func setupReconciler() (*KeptnWorkloadInstanceReconciler, chan string, *fake.ITr r := &KeptnWorkloadInstanceReconciler{ Client: fakeClient, Scheme: scheme.Scheme, - Recorder: recorder, + EventSender: controllercommon.NewEventSender(recorder), Log: ctrl.Log.WithName("test-appController"), TracerFactory: tf, Meters: controllercommon.InitAppMeters(), diff --git a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go index 7e4d29c77f..237657a2da 100644 --- a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go +++ b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostdeployment.go @@ -13,7 +13,7 @@ import ( func (r *KeptnWorkloadInstanceReconciler) reconcilePrePostDeployment(ctx context.Context, phaseCtx context.Context, workloadInstance *klcv1alpha3.KeptnWorkloadInstance, checkType apicommon.CheckType) (apicommon.KeptnState, error) { taskHandler := controllercommon.TaskHandler{ Client: r.Client, - Recorder: r.Recorder, + EventSender: r.EventSender, Log: r.Log, Tracer: r.getTracer(), Scheme: r.Scheme, diff --git a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go index ac63e4aaf7..b854d271c5 100644 --- a/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go +++ b/operator/controllers/lifecycle/keptnworkloadinstance/reconcile_prepostevaluation.go @@ -13,7 +13,7 @@ import ( func (r *KeptnWorkloadInstanceReconciler) reconcilePrePostEvaluation(ctx context.Context, phaseCtx context.Context, workloadInstance *klcv1alpha3.KeptnWorkloadInstance, checkType apicommon.CheckType) (apicommon.KeptnState, error) { evaluationHandler := controllercommon.EvaluationHandler{ Client: r.Client, - Recorder: r.Recorder, + EventSender: r.EventSender, Log: r.Log, Tracer: r.getTracer(), Scheme: r.Scheme, diff --git a/operator/main.go b/operator/main.go index 2d7f965b0e..8e21c5047f 100644 --- a/operator/main.go +++ b/operator/main.go @@ -178,7 +178,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Log: taskLogger.V(env.KeptnTaskControllerLogLevel), - Recorder: mgr.GetEventRecorderFor("keptntask-controller"), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptntask-controller")), Meters: keptnMeters, TracerFactory: controllercommon.GetOtelInstance(), } @@ -189,10 +189,10 @@ func main() { taskDefinitionLogger := ctrl.Log.WithName("KeptnTaskDefinition Controller") taskDefinitionReconciler := &keptntaskdefinition.KeptnTaskDefinitionReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: taskDefinitionLogger.V(env.KeptnTaskDefinitionControllerLogLevel), - Recorder: mgr.GetEventRecorderFor("keptntaskdefinition-controller"), + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: taskDefinitionLogger.V(env.KeptnTaskDefinitionControllerLogLevel), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptntaskdefinition-controller")), } if err = (taskDefinitionReconciler).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeptnTaskDefinition") @@ -204,7 +204,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Log: appLogger.V(env.KeptnAppControllerLogLevel), - Recorder: mgr.GetEventRecorderFor("keptnapp-controller"), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnapp-controller")), TracerFactory: controllercommon.GetOtelInstance(), } if err = (appReconciler).SetupWithManager(mgr); err != nil { @@ -228,7 +228,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Log: workloadLogger.V(env.KeptnWorkloadControllerLogLevel), - Recorder: mgr.GetEventRecorderFor("keptnworkload-controller"), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnworkload-controller")), TracerFactory: controllercommon.GetOtelInstance(), } if err = (workloadReconciler).SetupWithManager(mgr); err != nil { @@ -241,7 +241,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Log: workloadInstanceLogger.V(env.KeptnWorkloadInstanceControllerLogLevel), - Recorder: mgr.GetEventRecorderFor("keptnworkloadinstance-controller"), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnworkloadinstance-controller")), Meters: keptnMeters, TracerFactory: controllercommon.GetOtelInstance(), SpanHandler: spanHandler, @@ -256,7 +256,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Log: appVersionLogger.V(env.KeptnAppVersionControllerLogLevel), - Recorder: mgr.GetEventRecorderFor("keptnappversion-controller"), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnappversion-controller")), TracerFactory: controllercommon.GetOtelInstance(), Meters: keptnMeters, SpanHandler: spanHandler, @@ -271,7 +271,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Log: evaluationLogger.V(env.KeptnEvaluationControllerLogLevel), - Recorder: mgr.GetEventRecorderFor("keptnevaluation-controller"), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnevaluation-controller")), TracerFactory: controllercommon.GetOtelInstance(), Meters: keptnMeters, Namespace: env.PodNamespace, @@ -354,11 +354,11 @@ func main() { if err := webhookBuilder.Run(mgr, map[string]*ctrlWebhook.Admission{ "/mutate-v1-pod": { Handler: &pod_mutator.PodMutatingWebhook{ - Client: mgr.GetClient(), - Tracer: otel.Tracer("keptn/webhook"), - Recorder: mgr.GetEventRecorderFor("keptn/webhook"), - Decoder: decoder, - Log: ctrl.Log.WithName("Mutating Webhook"), + Client: mgr.GetClient(), + Tracer: otel.Tracer("keptn/webhook"), + EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptn/webhook")), + Decoder: decoder, + Log: ctrl.Log.WithName("Mutating Webhook"), }, }, }); err != nil { diff --git a/operator/test/component/app/app_suite_test.go b/operator/test/component/app/app_suite_test.go index 0eb971ab38..58028bda63 100644 --- a/operator/test/component/app/app_suite_test.go +++ b/operator/test/component/app/app_suite_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnapp" "github.com/keptn/lifecycle-toolkit/operator/test/component/common" . "github.com/onsi/ginkgo/v2" @@ -39,7 +40,7 @@ var _ = BeforeSuite(func() { controller := &keptnapp.KeptnAppReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("test-app-controller"), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("test-app-controller")), Log: GinkgoLogr, TracerFactory: &common.TracerFactory{Tracer: tracer}, } diff --git a/operator/test/component/appversion/appversion_suite_test.go b/operator/test/component/appversion/appversion_suite_test.go index 7ab162f0e7..03fde57f76 100644 --- a/operator/test/component/appversion/appversion_suite_test.go +++ b/operator/test/component/appversion/appversion_suite_test.go @@ -40,7 +40,7 @@ var _ = BeforeSuite(func() { controller := &keptnappversion.KeptnAppVersionReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("test-appversion-controller"), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("test-appversion-controller")), Log: GinkgoLogr, Meters: common.InitKeptnMeters(), SpanHandler: &controllercommon.SpanHandler{}, diff --git a/operator/test/component/evaluation/evaluation_suite_test.go b/operator/test/component/evaluation/evaluation_suite_test.go index c5750e2add..5275362a48 100644 --- a/operator/test/component/evaluation/evaluation_suite_test.go +++ b/operator/test/component/evaluation/evaluation_suite_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnevaluation" "github.com/keptn/lifecycle-toolkit/operator/test/component/common" . "github.com/onsi/ginkgo/v2" @@ -43,7 +44,7 @@ var _ = BeforeSuite(func() { controller := &keptnevaluation.KeptnEvaluationReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("test-evaluation-controller"), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("test-evaluation-controller")), Log: GinkgoLogr, Meters: common.InitKeptnMeters(), TracerFactory: &common.TracerFactory{Tracer: tracer}, diff --git a/operator/test/component/load/load_suite_test.go b/operator/test/component/load/load_suite_test.go index 37dc3245f8..1b4812bfbd 100644 --- a/operator/test/component/load/load_suite_test.go +++ b/operator/test/component/load/load_suite_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnapp" "github.com/keptn/lifecycle-toolkit/operator/test/component/common" . "github.com/onsi/ginkgo/v2" @@ -39,7 +40,7 @@ var _ = BeforeSuite(func() { controller := &keptnapp.KeptnAppReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("load-app-controller"), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("load-app-controller")), Log: GinkgoLogr, TracerFactory: &common.TracerFactory{Tracer: tracer}, } diff --git a/operator/test/component/task/task_suite_test.go b/operator/test/component/task/task_suite_test.go index d76903fa65..b2c82a3098 100644 --- a/operator/test/component/task/task_suite_test.go +++ b/operator/test/component/task/task_suite_test.go @@ -41,7 +41,7 @@ var _ = BeforeSuite(func() { controller := &keptntask.KeptnTaskReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("test-task-controller"), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("test-task-controller")), Log: GinkgoLogr, Meters: common.InitKeptnMeters(), TracerFactory: &common.TracerFactory{Tracer: tracer}, diff --git a/operator/test/component/taskdefinition/taskdefinition_suite_test.go b/operator/test/component/taskdefinition/taskdefinition_suite_test.go index 077c62882a..3f91a8e071 100644 --- a/operator/test/component/taskdefinition/taskdefinition_suite_test.go +++ b/operator/test/component/taskdefinition/taskdefinition_suite_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptntaskdefinition" "github.com/keptn/lifecycle-toolkit/operator/test/component/common" . "github.com/onsi/ginkgo/v2" @@ -31,10 +32,10 @@ var _ = BeforeSuite(func() { ////setup controllers here controller := &keptntaskdefinition.KeptnTaskDefinitionReconciler{ - Client: k8sManager.GetClient(), - Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("test-taskdefinition-controller"), - Log: GinkgoLogr, + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("test-taskdefinition-controller")), + Log: GinkgoLogr, } Eventually(controller.SetupWithManager(k8sManager)).WithTimeout(30 * time.Second).WithPolling(time.Second).Should(Succeed()) close(readyToStart) diff --git a/operator/test/component/workload/workload_suite_test.go b/operator/test/component/workload/workload_suite_test.go index 950c88d5ae..0cf832cd46 100644 --- a/operator/test/component/workload/workload_suite_test.go +++ b/operator/test/component/workload/workload_suite_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnworkload" "github.com/keptn/lifecycle-toolkit/operator/test/component/common" . "github.com/onsi/ginkgo/v2" @@ -39,7 +40,7 @@ var _ = BeforeSuite(func() { controller := &keptnworkload.KeptnWorkloadReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("test-workload-controller"), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("test-workload-controller")), Log: GinkgoLogr, TracerFactory: &common.TracerFactory{Tracer: tracer}, } diff --git a/operator/test/component/workloadinstance/workloadinstance_suite_test.go b/operator/test/component/workloadinstance/workloadinstance_suite_test.go index 12f99934f2..86774f19cd 100644 --- a/operator/test/component/workloadinstance/workloadinstance_suite_test.go +++ b/operator/test/component/workloadinstance/workloadinstance_suite_test.go @@ -40,7 +40,7 @@ var _ = BeforeSuite(func() { controller := &keptnworkloadinstance.KeptnWorkloadInstanceReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), - Recorder: k8sManager.GetEventRecorderFor("test-workloadinstance-controller"), + EventSender: controllercommon.NewEventSender(k8sManager.GetEventRecorderFor("test-workloadinstance-controller")), Log: GinkgoLogr, Meters: common.InitKeptnMeters(), SpanHandler: &controllercommon.SpanHandler{}, diff --git a/operator/webhooks/pod_mutator/pod_mutating_webhook.go b/operator/webhooks/pod_mutator/pod_mutating_webhook.go index 2ba7a6cb88..2a9fb444ad 100644 --- a/operator/webhooks/pod_mutator/pod_mutating_webhook.go +++ b/operator/webhooks/pod_mutator/pod_mutating_webhook.go @@ -25,7 +25,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" @@ -37,11 +36,11 @@ import ( // PodMutatingWebhook annotates Pods type PodMutatingWebhook struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } const InvalidAnnotationMessage = "Invalid annotations" @@ -279,12 +278,12 @@ func (a *PodMutatingWebhook) handleWorkload(ctx context.Context, logger logr.Log err = a.Client.Create(ctx, workload) if err != nil { logger.Error(err, "Could not create Workload") - controllercommon.RecordEvent(a.Recorder, apicommon.PhaseCreateWorkload, "Warning", workload, "WorkloadNotCreated", "could not create KeptnWorkload", workload.Spec.Version) + a.EventSender.SendK8sEvent(apicommon.PhaseCreateWorkload, "Warning", workload, "WorkloadNotCreated", "could not create KeptnWorkload", workload.Spec.Version) span.SetStatus(codes.Error, err.Error()) return err } - controllercommon.RecordEvent(a.Recorder, apicommon.PhaseCreateWorkload, "Normal", workload, "WorkloadCreated", "created KeptnWorkload", workload.Spec.Version) + a.EventSender.SendK8sEvent(apicommon.PhaseCreateWorkload, "Normal", workload, "WorkloadCreated", "created KeptnWorkload", workload.Spec.Version) return nil } @@ -304,12 +303,12 @@ func (a *PodMutatingWebhook) handleWorkload(ctx context.Context, logger logr.Log err = a.Client.Update(ctx, workload) if err != nil { logger.Error(err, "Could not update Workload") - controllercommon.RecordEvent(a.Recorder, apicommon.PhaseCreateWorkload, "Warning", workload, "WorkloadNotUpdated", "could not update KeptnWorkload", workload.Spec.Version) + a.EventSender.SendK8sEvent(apicommon.PhaseCreateWorkload, "Warning", workload, "WorkloadNotUpdated", "could not update KeptnWorkload", workload.Spec.Version) span.SetStatus(codes.Error, err.Error()) return err } - controllercommon.RecordEvent(a.Recorder, apicommon.PhaseCreateWorkload, "Normal", workload, "WorkloadUpdated", "updated KeptnWorkload", workload.Spec.Version) + a.EventSender.SendK8sEvent(apicommon.PhaseCreateWorkload, "Normal", workload, "WorkloadUpdated", "updated KeptnWorkload", workload.Spec.Version) return nil } @@ -334,12 +333,12 @@ func (a *PodMutatingWebhook) handleApp(ctx context.Context, logger logr.Logger, err = a.Client.Create(ctx, appCreationRequest) if err != nil { logger.Error(err, "Could not create App") - controllercommon.RecordEvent(a.Recorder, apicommon.PhaseCreateApp, "Warning", appCreationRequest, "AppCreationRequestNotCreated", "could not create KeptnAppCreationRequest", appCreationRequest.Spec.AppName) + a.EventSender.SendK8sEvent(apicommon.PhaseCreateApp, "Warning", appCreationRequest, "AppCreationRequestNotCreated", "could not create KeptnAppCreationRequest", appCreationRequest.Spec.AppName) span.SetStatus(codes.Error, err.Error()) return err } - controllercommon.RecordEvent(a.Recorder, apicommon.PhaseCreateApp, "Normal", appCreationRequest, "AppCreationRequestCreated", "created KeptnAppCreationRequest", appCreationRequest.Spec.AppName) + a.EventSender.SendK8sEvent(apicommon.PhaseCreateApp, "Normal", appCreationRequest, "AppCreationRequestCreated", "created KeptnAppCreationRequest", appCreationRequest.Spec.AppName) return nil } diff --git a/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go b/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go index ab6493b247..9f6add05e9 100644 --- a/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go +++ b/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go @@ -10,6 +10,7 @@ import ( "github.com/go-logr/logr/testr" klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" + controllercommon "github.com/keptn/lifecycle-toolkit/operator/controllers/common" fakeclient "github.com/keptn/lifecycle-toolkit/operator/controllers/common/fake" "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/trace" @@ -26,11 +27,11 @@ import ( func TestPodMutatingWebhook_getOwnerReference(t *testing.T) { type fields struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } type args struct { resource *metav1.ObjectMeta @@ -84,11 +85,11 @@ func TestPodMutatingWebhook_getOwnerReference(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &PodMutatingWebhook{ - Client: tt.fields.Client, - Tracer: tt.fields.Tracer, - Decoder: tt.fields.Decoder, - Recorder: tt.fields.Recorder, - Log: tt.fields.Log, + Client: tt.fields.Client, + Tracer: tt.fields.Tracer, + Decoder: tt.fields.Decoder, + EventSender: tt.fields.EventSender, + Log: tt.fields.Log, } if got := a.getOwnerReference(tt.args.resource); !reflect.DeepEqual(got, tt.want) { t.Errorf("getOwnerReference() = %v, want %v", got, tt.want) @@ -99,11 +100,11 @@ func TestPodMutatingWebhook_getOwnerReference(t *testing.T) { func TestPodMutatingWebhook_getAppName(t *testing.T) { type fields struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } type args struct { pod *corev1.Pod @@ -160,11 +161,11 @@ func TestPodMutatingWebhook_getAppName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &PodMutatingWebhook{ - Client: tt.fields.Client, - Tracer: tt.fields.Tracer, - Decoder: tt.fields.Decoder, - Recorder: tt.fields.Recorder, - Log: tt.fields.Log, + Client: tt.fields.Client, + Tracer: tt.fields.Tracer, + Decoder: tt.fields.Decoder, + EventSender: tt.fields.EventSender, + Log: tt.fields.Log, } if got := a.getAppName(tt.args.pod); got != tt.want { t.Errorf("getAppName() = %v, want %v", got, tt.want) @@ -175,11 +176,11 @@ func TestPodMutatingWebhook_getAppName(t *testing.T) { func TestPodMutatingWebhook_getWorkloadName(t *testing.T) { type fields struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } type args struct { pod *corev1.Pod @@ -240,11 +241,11 @@ func TestPodMutatingWebhook_getWorkloadName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &PodMutatingWebhook{ - Client: tt.fields.Client, - Tracer: tt.fields.Tracer, - Decoder: tt.fields.Decoder, - Recorder: tt.fields.Recorder, - Log: tt.fields.Log, + Client: tt.fields.Client, + Tracer: tt.fields.Tracer, + Decoder: tt.fields.Decoder, + EventSender: tt.fields.EventSender, + Log: tt.fields.Log, } if got := a.getWorkloadName(tt.args.pod); got != tt.want { t.Errorf("getWorkloadName() = %v, want %v", got, tt.want) @@ -365,11 +366,11 @@ func Test_getLabelOrAnnotation(t *testing.T) { func TestPodMutatingWebhook_isPodAnnotated(t *testing.T) { type fields struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } type args struct { pod *corev1.Pod @@ -448,11 +449,11 @@ func TestPodMutatingWebhook_isPodAnnotated(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &PodMutatingWebhook{ - Client: tt.fields.Client, - Tracer: tt.fields.Tracer, - Decoder: tt.fields.Decoder, - Recorder: tt.fields.Recorder, - Log: tt.fields.Log, + Client: tt.fields.Client, + Tracer: tt.fields.Tracer, + Decoder: tt.fields.Decoder, + EventSender: tt.fields.EventSender, + Log: tt.fields.Log, } got := a.isPodAnnotated(tt.args.pod) if got != tt.want { @@ -536,11 +537,11 @@ func TestPodMutatingWebhook_copyAnnotationsIfParentAnnotated(t *testing.T) { fakeClient := fakeclient.NewClient(rsWithDpOwner, rsWithNoOwner, testDp, testSts, testDs) type fields struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } type args struct { ctx context.Context @@ -690,11 +691,11 @@ func TestPodMutatingWebhook_copyAnnotationsIfParentAnnotated(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &PodMutatingWebhook{ - Client: tt.fields.Client, - Tracer: tt.fields.Tracer, - Decoder: tt.fields.Decoder, - Recorder: tt.fields.Recorder, - Log: tt.fields.Log, + Client: tt.fields.Client, + Tracer: tt.fields.Tracer, + Decoder: tt.fields.Decoder, + EventSender: tt.fields.EventSender, + Log: tt.fields.Log, } got := a.copyAnnotationsIfParentAnnotated(tt.args.ctx, tt.args.req, tt.args.pod) if got != tt.want { @@ -706,11 +707,11 @@ func TestPodMutatingWebhook_copyAnnotationsIfParentAnnotated(t *testing.T) { func TestPodMutatingWebhook_copyResourceLabelsIfPresent(t *testing.T) { type fields struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } type args struct { sourceResource *metav1.ObjectMeta @@ -849,11 +850,11 @@ func TestPodMutatingWebhook_copyResourceLabelsIfPresent(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &PodMutatingWebhook{ - Client: tt.fields.Client, - Tracer: tt.fields.Tracer, - Decoder: tt.fields.Decoder, - Recorder: tt.fields.Recorder, - Log: tt.fields.Log, + Client: tt.fields.Client, + Tracer: tt.fields.Tracer, + Decoder: tt.fields.Decoder, + EventSender: tt.fields.EventSender, + Log: tt.fields.Log, } got := a.copyResourceLabelsIfPresent(tt.args.sourceResource, tt.args.targetPod) if got != tt.want { @@ -868,11 +869,11 @@ func TestPodMutatingWebhook_copyResourceLabelsIfPresent(t *testing.T) { func TestPodMutatingWebhook_isAppAnnotationPresent(t *testing.T) { type fields struct { - Client client.Client - Tracer trace.Tracer - Decoder *admission.Decoder - Recorder record.EventRecorder - Log logr.Logger + Client client.Client + Tracer trace.Tracer + Decoder *admission.Decoder + EventSender controllercommon.EventSender + Log logr.Logger } type args struct { pod *corev1.Pod @@ -929,11 +930,11 @@ func TestPodMutatingWebhook_isAppAnnotationPresent(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &PodMutatingWebhook{ - Client: tt.fields.Client, - Tracer: tt.fields.Tracer, - Decoder: tt.fields.Decoder, - Recorder: tt.fields.Recorder, - Log: tt.fields.Log, + Client: tt.fields.Client, + Tracer: tt.fields.Tracer, + Decoder: tt.fields.Decoder, + EventSender: tt.fields.EventSender, + Log: tt.fields.Log, } got := a.isAppAnnotationPresent(tt.args.pod) if got != tt.want { @@ -957,17 +958,15 @@ func TestPodMutatingWebhook_Handle_DisabledNamespace(t *testing.T) { return ctx, trace.SpanFromContext(ctx) }} - recorder := record.NewFakeRecorder(100) - decoder, err := admission.NewDecoder(runtime.NewScheme()) require.Nil(t, err) wh := &PodMutatingWebhook{ - Client: fakeClient, - Tracer: tr, - Decoder: decoder, - Recorder: recorder, - Log: testr.New(t), + Client: fakeClient, + Tracer: tr, + Decoder: decoder, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), + Log: testr.New(t), } pod := &corev1.Pod{ @@ -1022,17 +1021,15 @@ func TestPodMutatingWebhook_Handle_SingleService(t *testing.T) { return ctx, trace.SpanFromContext(ctx) }} - recorder := record.NewFakeRecorder(100) - decoder, err := admission.NewDecoder(runtime.NewScheme()) require.Nil(t, err) wh := &PodMutatingWebhook{ - Client: fakeClient, - Tracer: tr, - Decoder: decoder, - Recorder: recorder, - Log: testr.New(t), + Client: fakeClient, + Tracer: tr, + Decoder: decoder, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), + Log: testr.New(t), } pod := &corev1.Pod{ @@ -1145,17 +1142,15 @@ func TestPodMutatingWebhook_Handle_SingleService_AppCreationRequestAlreadyPresen return ctx, trace.SpanFromContext(ctx) }} - recorder := record.NewFakeRecorder(100) - decoder, err := admission.NewDecoder(runtime.NewScheme()) require.Nil(t, err) wh := &PodMutatingWebhook{ - Client: fakeClient, - Tracer: tr, - Decoder: decoder, - Recorder: recorder, - Log: testr.New(t), + Client: fakeClient, + Tracer: tr, + Decoder: decoder, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), + Log: testr.New(t), } pod := &corev1.Pod{ @@ -1255,17 +1250,15 @@ func TestPodMutatingWebhook_Handle_MultiService(t *testing.T) { return ctx, trace.SpanFromContext(ctx) }} - recorder := record.NewFakeRecorder(100) - decoder, err := admission.NewDecoder(runtime.NewScheme()) require.Nil(t, err) wh := &PodMutatingWebhook{ - Client: fakeClient, - Tracer: tr, - Decoder: decoder, - Recorder: recorder, - Log: testr.New(t), + Client: fakeClient, + Tracer: tr, + Decoder: decoder, + EventSender: controllercommon.NewEventSender(record.NewFakeRecorder(100)), + Log: testr.New(t), } pod := &corev1.Pod{