From e0faf35f350218984437973f014297b5f133e243 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Sun, 6 Aug 2023 17:11:16 +0200 Subject: [PATCH] chore: refactoring eventing and telemetry Signed-off-by: Giovanni Liva --- .../options/v1alpha1/keptnconfig_types.go | 5 --- .../controllers/common/config/config.go | 11 ------- .../controllers/common/evaluationhandler.go | 11 ++++--- .../controllers/common/eventsender.go | 31 ++++++++++++++----- .../controllers/common/eventsender_test.go | 4 +-- .../controllers/common/phasehandler.go | 13 ++++---- .../controllers/common/phasehandler_test.go | 15 ++++----- .../controllers/common/taskhandler.go | 9 +++--- .../common/{ => telemetry}/metrics.go | 2 +- .../common/{ => telemetry}/metrics_test.go | 2 +- .../common/{ => telemetry}/otel_utils.go | 2 +- .../common/{ => telemetry}/otel_utils_test.go | 2 +- .../common/{ => telemetry}/spanhandler.go | 2 +- .../{ => telemetry}/spanhandler_test.go | 2 +- .../common/{ => telemetry}/tracer.go | 2 +- .../lifecycle/keptnapp/controller.go | 11 ++++--- .../lifecycle/keptnappversion/controller.go | 9 +++--- .../reconcile_workloadsstate.go | 2 +- .../lifecycle/keptnevaluation/controller.go | 13 ++++---- .../lifecycle/keptntask/controller.go | 7 +++-- .../lifecycle/keptntask/job_runner_builder.go | 2 +- .../lifecycle/keptntask/job_utils.go | 4 +-- .../lifecycle/keptntask/runtime_builder.go | 6 ++-- .../keptntaskdefinition/controller.go | 2 +- .../keptntaskdefinition/reconcile_function.go | 4 +-- .../lifecycle/keptnworkload/controller.go | 9 +++--- .../keptnworkloadinstance/controller.go | 17 +++++----- .../options/keptnconfig_controller.go | 4 +-- lifecycle-operator/main.go | 21 +++++++------ .../pod_mutator/pod_mutating_webhook.go | 8 ++--- 30 files changed, 121 insertions(+), 111 deletions(-) rename lifecycle-operator/controllers/common/{ => telemetry}/metrics.go (99%) rename lifecycle-operator/controllers/common/{ => telemetry}/metrics_test.go (99%) rename lifecycle-operator/controllers/common/{ => telemetry}/otel_utils.go (99%) rename lifecycle-operator/controllers/common/{ => telemetry}/otel_utils_test.go (99%) rename lifecycle-operator/controllers/common/{ => telemetry}/spanhandler.go (99%) rename lifecycle-operator/controllers/common/{ => telemetry}/spanhandler_test.go (99%) rename lifecycle-operator/controllers/common/{ => telemetry}/tracer.go (94%) diff --git a/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go b/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go index 4428c70cfe..50d72d5eae 100644 --- a/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go +++ b/lifecycle-operator/apis/options/v1alpha1/keptnconfig_types.go @@ -31,16 +31,11 @@ type KeptnConfigSpec struct { // OTelCollectorUrl can be used to set the Open Telemetry collector that the lifecycle operator should use // +optional OTelCollectorUrl string `json:"OTelCollectorUrl,omitempty"` - // KeptnAppCreationRequestTimeoutSeconds is used to set the interval in which automatic app discovery // searches for workload to put into the same auto-generated KeptnApp // +kubebuilder:default:=30 // +optional KeptnAppCreationRequestTimeoutSeconds uint `json:"keptnAppCreationRequestTimeoutSeconds,omitempty"` - - // CloudEventsEndpoint can be used to set the endpoint where Cloud Events should be posted by the lifecycle operator - // +optional - CloudEventsEndpoint string `json:"cloudEventsEndpoint,omitempty"` } // +kubebuilder:object:root=true diff --git a/lifecycle-operator/controllers/common/config/config.go b/lifecycle-operator/controllers/common/config/config.go index 043edb4348..c2df8f2b85 100644 --- a/lifecycle-operator/controllers/common/config/config.go +++ b/lifecycle-operator/controllers/common/config/config.go @@ -11,13 +11,10 @@ const defaultKeptnAppCreationRequestTimeout = 30 * time.Second type IConfig interface { SetCreationRequestTimeout(value time.Duration) GetCreationRequestTimeout() time.Duration - SetCloudEventsEndpoint(endpoint string) - GetCloudEventsEndpoint() string } type ControllerConfig struct { keptnAppCreationRequestTimeout time.Duration - cloudEventsEndpoint string } var instance *ControllerConfig @@ -37,11 +34,3 @@ func (o *ControllerConfig) SetCreationRequestTimeout(value time.Duration) { func (o *ControllerConfig) GetCreationRequestTimeout() time.Duration { return o.keptnAppCreationRequestTimeout } - -func (o *ControllerConfig) SetCloudEventsEndpoint(endpoint string) { - o.cloudEventsEndpoint = endpoint -} - -func (o *ControllerConfig) GetCloudEventsEndpoint() string { - return o.cloudEventsEndpoint -} diff --git a/lifecycle-operator/controllers/common/evaluationhandler.go b/lifecycle-operator/controllers/common/evaluationhandler.go index 8a889b133f..4f993e406e 100644 --- a/lifecycle-operator/controllers/common/evaluationhandler.go +++ b/lifecycle-operator/controllers/common/evaluationhandler.go @@ -8,6 +8,7 @@ import ( "github.com/go-logr/logr" klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/lifecycle/interfaces" "go.opentelemetry.io/otel/codes" @@ -21,11 +22,11 @@ import ( type EvaluationHandler struct { client.Client - EventSender EventSender + EventSender IEvent Log logr.Logger Tracer trace.Tracer Scheme *runtime.Scheme - SpanHandler ISpanHandler + SpanHandler telemetry.ISpanHandler } type CreateEvaluationAttributes struct { @@ -55,7 +56,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co evaluationExists := false if oldstatus != evaluationStatus.Status { - r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Normal", reconcileObject, apicommon.PhaseStateStatusChanged, fmt.Sprintf("evaluation status changed from %s to %s", oldstatus, evaluationStatus.Status), piWrapper.GetVersion()) + r.EventSender.SendEvent(apicommon.PhaseReconcileEvaluation, "Normal", reconcileObject, apicommon.PhaseStateStatusChanged, fmt.Sprintf("evaluation status changed from %s to %s", oldstatus, evaluationStatus.Status), piWrapper.GetVersion()) } // Check if evaluation has already succeeded or failed @@ -126,7 +127,7 @@ 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") - r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateFailed, "could not create KeptnEvaluation", piWrapper.GetVersion()) + r.EventSender.SendEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateFailed, "could not create KeptnEvaluation", piWrapper.GetVersion()) return "", err } @@ -142,7 +143,7 @@ func (r EvaluationHandler) emitEvaluationFailureEvents(evaluation *klcv1alpha3.K k8sEventMessage = fmt.Sprintf("%s\n%s", k8sEventMessage, msg) } } - r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateFailed, k8sEventMessage, piWrapper.GetVersion()) + r.EventSender.SendEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateFailed, k8sEventMessage, piWrapper.GetVersion()) } func (r EvaluationHandler) setupEvaluations(evaluationCreateAttributes CreateEvaluationAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha3.ItemStatus) { diff --git a/lifecycle-operator/controllers/common/eventsender.go b/lifecycle-operator/controllers/common/eventsender.go index cce0c7ae2c..2b7edb1b1a 100644 --- a/lifecycle-operator/controllers/common/eventsender.go +++ b/lifecycle-operator/controllers/common/eventsender.go @@ -2,25 +2,40 @@ package common import ( "fmt" - apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" ) -type EventSender struct { +//go:generate moq -pkg fake -skip-ensure -out ./fake/event_mock.go . IEvent:MockEvent +type IEvent interface { + SendEvent(phase apicommon.KeptnPhaseType, eventType string, reconcileObject client.Object, status string, message string, version string) +} + +// ===== Main ===== + +func NewEventSender(recorder record.EventRecorder) IEvent { + return newK8sSender(recorder) +} + +// ===== Cloud Event Sender ===== +// TODO: implement Cloud Event logic + +// ===== K8s Event Sender ===== + +type k8sEvent struct { recorder record.EventRecorder } -func NewEventSender(recorder record.EventRecorder) EventSender { - return EventSender{ +func newK8sSender(recorder record.EventRecorder) IEvent { + return &k8sEvent{ 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) +// SendEvent creates k8s Event and adds it to Eventqueue +func (s *k8sEvent) SendEvent(phase apicommon.KeptnPhaseType, eventType string, reconcileObject client.Object, status string, message string, version string) { + msg := setEventMessage(phase, reconcileObject, message, version) annotations := setAnnotations(reconcileObject, phase) - s.recorder.AnnotatedEventf(reconcileObject, annotations, eventType, fmt.Sprintf("%s%s", phase.ShortName, shortReason), msg) + s.recorder.AnnotatedEventf(reconcileObject, annotations, eventType, fmt.Sprintf("%s%s", phase.ShortName, status), msg) } diff --git a/lifecycle-operator/controllers/common/eventsender_test.go b/lifecycle-operator/controllers/common/eventsender_test.go index 715178b2df..c1b5241fc0 100644 --- a/lifecycle-operator/controllers/common/eventsender_test.go +++ b/lifecycle-operator/controllers/common/eventsender_test.go @@ -13,9 +13,9 @@ import ( func TestEventSender_SendK8sEvent(t *testing.T) { fakeRecorder := record.NewFakeRecorder(100) - eventSender := NewEventSender(fakeRecorder) + eventSender := newK8sSender(fakeRecorder) - eventSender.SendK8sEvent(common.PhaseAppDeployment, "pre-event", &v1alpha3.KeptnAppVersion{ + eventSender.SendEvent(common.PhaseAppDeployment, "pre-event", &v1alpha3.KeptnAppVersion{ ObjectMeta: v1.ObjectMeta{ Name: "app", Namespace: "ns", diff --git a/lifecycle-operator/controllers/common/phasehandler.go b/lifecycle-operator/controllers/common/phasehandler.go index 80b9556351..4327897ee3 100644 --- a/lifecycle-operator/controllers/common/phasehandler.go +++ b/lifecycle-operator/controllers/common/phasehandler.go @@ -6,6 +6,7 @@ import ( "github.com/go-logr/logr" apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/lifecycle/interfaces" "go.opentelemetry.io/otel/codes" @@ -16,9 +17,9 @@ import ( type PhaseHandler struct { client.Client - EventSender EventSender + EventSender IEvent Log logr.Logger - SpanHandler ISpanHandler + SpanHandler telemetry.ISpanHandler } type PhaseResult struct { @@ -39,7 +40,7 @@ func (r PhaseHandler) HandlePhase(ctx context.Context, ctxTrace context.Context, return &PhaseResult{Continue: false, Result: ctrl.Result{}}, nil } if oldPhase != phase.ShortName { - r.EventSender.SendK8sEvent(phase, "Normal", reconcileObject, apicommon.PhaseStateStarted, "has started", piWrapper.GetVersion()) + r.EventSender.SendEvent(phase, "Normal", reconcileObject, apicommon.PhaseStateStarted, "has started", piWrapper.GetVersion()) piWrapper.SetCurrentPhase(phase.ShortName) } @@ -51,7 +52,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") - r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateReconcileError, "could not get reconciled", piWrapper.GetVersion()) + r.EventSender.SendEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateReconcileError, "could not get reconciled", piWrapper.GetVersion()) span.SetStatus(codes.Error, err.Error()) return &PhaseResult{Continue: false, Result: requeueResult}, err } @@ -88,7 +89,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()) } - r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateFailed, "has failed", piWrapper.GetVersion()) + r.EventSender.SendEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateFailed, "has failed", piWrapper.GetVersion()) piWrapper.DeprecateRemainingPhases(phase) return &PhaseResult{Continue: false, Result: ctrl.Result{}}, nil } @@ -100,7 +101,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()) } - r.EventSender.SendK8sEvent(phase, "Normal", reconcileObject, apicommon.PhaseStateFinished, "has finished", piWrapper.GetVersion()) + r.EventSender.SendEvent(phase, "Normal", reconcileObject, apicommon.PhaseStateFinished, "has finished", piWrapper.GetVersion()) return &PhaseResult{Continue: true, Result: ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}}, nil } diff --git a/lifecycle-operator/controllers/common/phasehandler_test.go b/lifecycle-operator/controllers/common/phasehandler_test.go index 4b5db6ee1d..09a73d0ba6 100644 --- a/lifecycle-operator/controllers/common/phasehandler_test.go +++ b/lifecycle-operator/controllers/common/phasehandler_test.go @@ -8,6 +8,7 @@ import ( "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/trace" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -33,7 +34,7 @@ func TestPhaseHandler(t *testing.T) { { name: "deprecated", handler: PhaseHandler{ - SpanHandler: &SpanHandler{}, + SpanHandler: &telemetry.SpanHandler{}, }, object: &v1alpha3.KeptnAppVersion{ Status: v1alpha3.KeptnAppVersionStatus{ @@ -51,7 +52,7 @@ func TestPhaseHandler(t *testing.T) { { name: "reconcilePhase error", handler: PhaseHandler{ - SpanHandler: &SpanHandler{}, + SpanHandler: &telemetry.SpanHandler{}, Log: ctrl.Log.WithName("controller"), EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), @@ -78,7 +79,7 @@ func TestPhaseHandler(t *testing.T) { { name: "reconcilePhase pending state", handler: PhaseHandler{ - SpanHandler: &SpanHandler{}, + SpanHandler: &telemetry.SpanHandler{}, Log: ctrl.Log.WithName("controller"), EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), @@ -105,7 +106,7 @@ func TestPhaseHandler(t *testing.T) { { name: "reconcilePhase progressing state", handler: PhaseHandler{ - SpanHandler: &SpanHandler{}, + SpanHandler: &telemetry.SpanHandler{}, Log: ctrl.Log.WithName("controller"), EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), @@ -132,7 +133,7 @@ func TestPhaseHandler(t *testing.T) { { name: "reconcilePhase succeeded state", handler: PhaseHandler{ - SpanHandler: &SpanHandler{}, + SpanHandler: &telemetry.SpanHandler{}, Log: ctrl.Log.WithName("controller"), EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), @@ -159,7 +160,7 @@ func TestPhaseHandler(t *testing.T) { { name: "reconcilePhase failed state", handler: PhaseHandler{ - SpanHandler: &SpanHandler{}, + SpanHandler: &telemetry.SpanHandler{}, Log: ctrl.Log.WithName("controller"), EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), @@ -187,7 +188,7 @@ func TestPhaseHandler(t *testing.T) { { name: "reconcilePhase unknown state", handler: PhaseHandler{ - SpanHandler: &SpanHandler{}, + SpanHandler: &telemetry.SpanHandler{}, Log: ctrl.Log.WithName("controller"), EventSender: NewEventSender(record.NewFakeRecorder(100)), Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), diff --git a/lifecycle-operator/controllers/common/taskhandler.go b/lifecycle-operator/controllers/common/taskhandler.go index 8713b12da0..54c010ef58 100644 --- a/lifecycle-operator/controllers/common/taskhandler.go +++ b/lifecycle-operator/controllers/common/taskhandler.go @@ -8,6 +8,7 @@ import ( "github.com/go-logr/logr" klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/lifecycle/interfaces" "go.opentelemetry.io/otel/codes" @@ -21,11 +22,11 @@ import ( type TaskHandler struct { client.Client - EventSender EventSender + EventSender IEvent Log logr.Logger Tracer trace.Tracer Scheme *runtime.Scheme - SpanHandler ISpanHandler + SpanHandler telemetry.ISpanHandler } type CreateTaskAttributes struct { @@ -57,7 +58,7 @@ func (r TaskHandler) ReconcileTasks(ctx context.Context, phaseCtx context.Contex taskExists := false if oldstatus != taskStatus.Status { - r.EventSender.SendK8sEvent(phase, "Normal", reconcileObject, apicommon.PhaseStateStatusChanged, fmt.Sprintf("task status changed from %s to %s", oldstatus, taskStatus.Status), piWrapper.GetVersion()) + r.EventSender.SendEvent(phase, "Normal", reconcileObject, apicommon.PhaseStateStatusChanged, fmt.Sprintf("task status changed from %s to %s", oldstatus, taskStatus.Status), piWrapper.GetVersion()) } // Check if task has already succeeded or failed @@ -130,7 +131,7 @@ 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") - r.EventSender.SendK8sEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateFailed, "could not create KeptnTask", piWrapper.GetVersion()) + r.EventSender.SendEvent(phase, "Warning", reconcileObject, apicommon.PhaseStateFailed, "could not create KeptnTask", piWrapper.GetVersion()) return "", err } diff --git a/lifecycle-operator/controllers/common/metrics.go b/lifecycle-operator/controllers/common/telemetry/metrics.go similarity index 99% rename from lifecycle-operator/controllers/common/metrics.go rename to lifecycle-operator/controllers/common/telemetry/metrics.go index 7da45afd30..d96358beec 100644 --- a/lifecycle-operator/controllers/common/metrics.go +++ b/lifecycle-operator/controllers/common/telemetry/metrics.go @@ -1,4 +1,4 @@ -package common +package telemetry import ( "context" diff --git a/lifecycle-operator/controllers/common/metrics_test.go b/lifecycle-operator/controllers/common/telemetry/metrics_test.go similarity index 99% rename from lifecycle-operator/controllers/common/metrics_test.go rename to lifecycle-operator/controllers/common/telemetry/metrics_test.go index 45a77d703e..3f6ec0bc3e 100644 --- a/lifecycle-operator/controllers/common/metrics_test.go +++ b/lifecycle-operator/controllers/common/telemetry/metrics_test.go @@ -1,4 +1,4 @@ -package common +package telemetry import ( "context" diff --git a/lifecycle-operator/controllers/common/otel_utils.go b/lifecycle-operator/controllers/common/telemetry/otel_utils.go similarity index 99% rename from lifecycle-operator/controllers/common/otel_utils.go rename to lifecycle-operator/controllers/common/telemetry/otel_utils.go index 695dd39bfd..abaf933477 100644 --- a/lifecycle-operator/controllers/common/otel_utils.go +++ b/lifecycle-operator/controllers/common/telemetry/otel_utils.go @@ -1,4 +1,4 @@ -package common +package telemetry import ( "context" diff --git a/lifecycle-operator/controllers/common/otel_utils_test.go b/lifecycle-operator/controllers/common/telemetry/otel_utils_test.go similarity index 99% rename from lifecycle-operator/controllers/common/otel_utils_test.go rename to lifecycle-operator/controllers/common/telemetry/otel_utils_test.go index 769f5fce85..a8d9aa6875 100644 --- a/lifecycle-operator/controllers/common/otel_utils_test.go +++ b/lifecycle-operator/controllers/common/telemetry/otel_utils_test.go @@ -1,4 +1,4 @@ -package common +package telemetry import ( "net" diff --git a/lifecycle-operator/controllers/common/spanhandler.go b/lifecycle-operator/controllers/common/telemetry/spanhandler.go similarity index 99% rename from lifecycle-operator/controllers/common/spanhandler.go rename to lifecycle-operator/controllers/common/telemetry/spanhandler.go index 7560b463db..ae6342e7e6 100644 --- a/lifecycle-operator/controllers/common/spanhandler.go +++ b/lifecycle-operator/controllers/common/telemetry/spanhandler.go @@ -1,4 +1,4 @@ -package common +package telemetry import ( "context" diff --git a/lifecycle-operator/controllers/common/spanhandler_test.go b/lifecycle-operator/controllers/common/telemetry/spanhandler_test.go similarity index 99% rename from lifecycle-operator/controllers/common/spanhandler_test.go rename to lifecycle-operator/controllers/common/telemetry/spanhandler_test.go index 1ba377c446..e9d491a732 100644 --- a/lifecycle-operator/controllers/common/spanhandler_test.go +++ b/lifecycle-operator/controllers/common/telemetry/spanhandler_test.go @@ -1,4 +1,4 @@ -package common +package telemetry import ( "context" diff --git a/lifecycle-operator/controllers/common/tracer.go b/lifecycle-operator/controllers/common/telemetry/tracer.go similarity index 94% rename from lifecycle-operator/controllers/common/tracer.go rename to lifecycle-operator/controllers/common/telemetry/tracer.go index 4fda3a5a96..6c86418830 100644 --- a/lifecycle-operator/controllers/common/tracer.go +++ b/lifecycle-operator/controllers/common/telemetry/tracer.go @@ -1,4 +1,4 @@ -package common +package telemetry import "go.opentelemetry.io/otel/trace" diff --git a/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go b/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go index ea52347ed0..6b7227a62a 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnapp/controller.go @@ -19,6 +19,7 @@ package keptnapp import ( "context" "fmt" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "github.com/go-logr/logr" klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" @@ -47,9 +48,9 @@ const traceComponentName = "keptn/lifecycle-operator/app" type KeptnAppReconciler struct { client.Client Scheme *runtime.Scheme - EventSender controllercommon.EventSender + EventSender controllercommon.IEvent Log logr.Logger - TracerFactory controllercommon.TracerFactory + TracerFactory telemetry.TracerFactory } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnapps,verbs=get;list;watch;create;update;patch;delete @@ -105,7 +106,7 @@ 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()) - r.EventSender.SendK8sEvent(common.PhaseCreateAppVersion, "Warning", appVersion, common.PhaseStateFailed, "Could not create KeptnAppVersion", appVersion.Spec.Version) + r.EventSender.SendEvent(common.PhaseCreateAppVersion, "Warning", appVersion, common.PhaseStateFailed, "Could not create KeptnAppVersion", appVersion.Spec.Version) return ctrl.Result{}, err } @@ -171,7 +172,7 @@ 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()) - r.EventSender.SendK8sEvent(common.PhaseDeprecateAppVersion, "Warning", app, common.PhaseStateFailed, fmt.Sprintf("could not deprecate outdated revisions of KeptnAppVersion: %s", app.GetAppVersionName()), app.Spec.Version) + r.EventSender.SendEvent(common.PhaseDeprecateAppVersion, "Warning", app, common.PhaseStateFailed, fmt.Sprintf("could not deprecate outdated revisions of KeptnAppVersion: %s", app.GetAppVersionName()), app.Spec.Version) return err } } @@ -200,6 +201,6 @@ func (r *KeptnAppReconciler) deprecateAppVersions(ctx context.Context, app *klcv return lastResultErr } -func (r *KeptnAppReconciler) getTracer() controllercommon.ITracer { +func (r *KeptnAppReconciler) getTracer() telemetry.ITracer { return r.TracerFactory.GetTracer(traceComponentName) } diff --git a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go index 7b081b128e..19e8446246 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnappversion/controller.go @@ -19,6 +19,7 @@ package keptnappversion import ( "context" "fmt" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "time" "github.com/go-logr/logr" @@ -47,10 +48,10 @@ type KeptnAppVersionReconciler struct { Scheme *runtime.Scheme client.Client Log logr.Logger - EventSender controllercommon.EventSender - TracerFactory controllercommon.TracerFactory + EventSender controllercommon.IEvent + TracerFactory telemetry.TracerFactory Meters apicommon.KeptnMeters - SpanHandler controllercommon.ISpanHandler + SpanHandler telemetry.ISpanHandler } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnappversions,verbs=get;list;watch;create;update;patch;delete @@ -181,7 +182,7 @@ func (r *KeptnAppVersionReconciler) finishKeptnAppVersionReconcile(ctx context.C return ctrl.Result{Requeue: true}, err } - r.EventSender.SendK8sEvent(apicommon.PhaseAppCompleted, "Normal", appVersion, apicommon.PhaseStateFinished, "has finished", appVersion.GetVersion()) + r.EventSender.SendEvent(apicommon.PhaseAppCompleted, "Normal", appVersion, apicommon.PhaseStateFinished, "has finished", appVersion.GetVersion()) attrs := appVersion.GetMetricsAttributes() diff --git a/lifecycle-operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go b/lifecycle-operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go index f206ed6a4d..09297e6f7f 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go +++ b/lifecycle-operator/controllers/lifecycle/keptnappversion/reconcile_workloadsstate.go @@ -41,7 +41,7 @@ func (r *KeptnAppVersionReconciler) reconcileWorkloads(ctx context.Context, appV } if !found { - r.EventSender.SendK8sEvent(phase, "Warning", appVersion, apicommon.PhaseStateNotFound, fmt.Sprintf("could not find KeptnWorkloadInstance for KeptnWorkload: %s ", w.Name), appVersion.GetVersion()) + r.EventSender.SendEvent(phase, "Warning", appVersion, apicommon.PhaseStateNotFound, fmt.Sprintf("could not find KeptnWorkloadInstance for KeptnWorkload: %s ", w.Name), appVersion.GetVersion()) } newStatus = append(newStatus, klcv1alpha3.WorkloadStatus{ diff --git a/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go b/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go index 6f8874a43b..9b51f8b76d 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnevaluation/controller.go @@ -19,6 +19,7 @@ package keptnevaluation import ( "context" "fmt" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "time" "github.com/go-logr/logr" @@ -46,10 +47,10 @@ const traceComponentName = "keptn/lifecycle-operator/evaluation" type KeptnEvaluationReconciler struct { client.Client Scheme *runtime.Scheme - EventSender controllercommon.EventSender + EventSender controllercommon.IEvent Log logr.Logger Meters apicommon.KeptnMeters - TracerFactory controllercommon.TracerFactory + TracerFactory telemetry.TracerFactory Namespace string } @@ -138,7 +139,7 @@ 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 { - r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateReconcileError, "could not update status", "") + r.EventSender.SendEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateReconcileError, "could not update status", "") span.SetStatus(codes.Error, err.Error()) return err } @@ -148,7 +149,7 @@ func (r *KeptnEvaluationReconciler) handleEvaluationIncomplete(ctx context.Conte } func (r *KeptnEvaluationReconciler) handleEvaluationExceededRetries(ctx context.Context, evaluation *klcv1alpha3.KeptnEvaluation, span trace.Span) { - r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateReconcileTimeout, "retryCount exceeded", "") + r.EventSender.SendEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateReconcileTimeout, "retryCount exceeded", "") err := controllererrors.ErrRetryCountExceeded span.SetStatus(codes.Error, err.Error()) evaluation.Status.OverallStatus = apicommon.StateFailed @@ -237,7 +238,7 @@ func (r *KeptnEvaluationReconciler) updateFinishedEvaluationMetrics(ctx context. err := r.Client.Status().Update(ctx, evaluation) if err != nil { span.SetStatus(codes.Error, err.Error()) - r.EventSender.SendK8sEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateReconcileError, "could not update status", "") + r.EventSender.SendEvent(apicommon.PhaseReconcileEvaluation, "Warning", evaluation, apicommon.PhaseStateReconcileError, "could not update status", "") return err } @@ -261,6 +262,6 @@ func (r *KeptnEvaluationReconciler) SetupWithManager(mgr ctrl.Manager) error { Complete(r) } -func (r *KeptnEvaluationReconciler) getTracer() controllercommon.ITracer { +func (r *KeptnEvaluationReconciler) getTracer() telemetry.ITracer { return r.TracerFactory.GetTracer(traceComponentName) } diff --git a/lifecycle-operator/controllers/lifecycle/keptntask/controller.go b/lifecycle-operator/controllers/lifecycle/keptntask/controller.go index 640c4f76d7..7768a764da 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntask/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptntask/controller.go @@ -18,6 +18,7 @@ package keptntask import ( "context" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "time" "github.com/go-logr/logr" @@ -43,10 +44,10 @@ const traceComponentName = "keptn/lifecycle-operator/task" type KeptnTaskReconciler struct { client.Client Scheme *runtime.Scheme - EventSender controllercommon.EventSender + EventSender controllercommon.IEvent Log logr.Logger Meters apicommon.KeptnMeters - TracerFactory controllercommon.TracerFactory + TracerFactory telemetry.TracerFactory } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptntasks,verbs=get;list;watch;create;update;patch;delete @@ -137,6 +138,6 @@ func (r *KeptnTaskReconciler) SetupWithManager(mgr ctrl.Manager) error { Complete(r) } -func (r *KeptnTaskReconciler) getTracer() controllercommon.ITracer { +func (r *KeptnTaskReconciler) getTracer() telemetry.ITracer { return r.TracerFactory.GetTracer(traceComponentName) } diff --git a/lifecycle-operator/controllers/lifecycle/keptntask/job_runner_builder.go b/lifecycle-operator/controllers/lifecycle/keptntask/job_runner_builder.go index fd741f91d7..ef2371014f 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntask/job_runner_builder.go +++ b/lifecycle-operator/controllers/lifecycle/keptntask/job_runner_builder.go @@ -20,7 +20,7 @@ type JobRunnerBuilder interface { // BuilderOptions contains everything needed to build the current job type BuilderOptions struct { client.Client - eventSender controllercommon.EventSender + eventSender controllercommon.IEvent req ctrl.Request Log logr.Logger task *klcv1alpha3.KeptnTask diff --git a/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go b/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go index 70c1d23e10..22290dfba7 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go +++ b/lifecycle-operator/controllers/lifecycle/keptntask/job_utils.go @@ -21,7 +21,7 @@ func (r *KeptnTaskReconciler) createJob(ctx context.Context, req ctrl.Request, t definition, err := controllercommon.GetTaskDefinition(r.Client, r.Log, ctx, task.Spec.TaskDefinition, req.Namespace) if err != nil { r.Log.Error(err, fmt.Sprintf("could not find KeptnTaskDefinition: %s ", task.Spec.TaskDefinition)) - r.EventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", task, apicommon.PhaseStateNotFound, fmt.Sprintf("could not find KeptnTaskDefinition: %s ", task.Spec.TaskDefinition), "") + r.EventSender.SendEvent(apicommon.PhaseCreateTask, "Warning", task, apicommon.PhaseStateNotFound, fmt.Sprintf("could not find KeptnTaskDefinition: %s ", task.Spec.TaskDefinition), "") return err } @@ -47,7 +47,7 @@ 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") - r.EventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", task, apicommon.PhaseStateFailed, fmt.Sprintf("could not create Job: %s ", task.Name), "") + r.EventSender.SendEvent(apicommon.PhaseCreateTask, "Warning", task, apicommon.PhaseStateFailed, fmt.Sprintf("could not create Job: %s ", task.Name), "") return job.Name, err } diff --git a/lifecycle-operator/controllers/lifecycle/keptntask/runtime_builder.go b/lifecycle-operator/controllers/lifecycle/keptntask/runtime_builder.go index 89505dea61..9b2e7d4fdb 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntask/runtime_builder.go +++ b/lifecycle-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 { - fb.options.eventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, apicommon.PhaseStateFailed, fmt.Sprintf("could not merge KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") + fb.options.eventSender.SendEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, apicommon.PhaseStateFailed, 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 { - fb.options.eventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, apicommon.PhaseStateNotFound, fmt.Sprintf("could not find KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") + fb.options.eventSender.SendEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, apicommon.PhaseStateNotFound, 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 { - fb.options.eventSender.SendK8sEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, apicommon.PhaseStateFailed, fmt.Sprintf("could not merge KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") + fb.options.eventSender.SendEvent(apicommon.PhaseCreateTask, "Warning", fb.options.task, apicommon.PhaseStateFailed, fmt.Sprintf("could not merge KeptnTaskDefinition: %s ", fb.options.task.Spec.TaskDefinition), "") return err } diff --git a/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go b/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go index 5e8aa6fd58..0dcb3daf01 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/controller.go @@ -36,7 +36,7 @@ type KeptnTaskDefinitionReconciler struct { client.Client Scheme *runtime.Scheme Log logr.Logger - EventSender controllercommon.EventSender + EventSender controllercommon.IEvent } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptntaskdefinitions,verbs=get;list;watch;create;update;patch;delete diff --git a/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go b/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go index 1579c54401..4f1b9b9c85 100644 --- a/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go +++ b/lifecycle-operator/controllers/lifecycle/keptntaskdefinition/reconcile_function.go @@ -32,14 +32,14 @@ func (r *KeptnTaskDefinitionReconciler) reconcileConfigMap(ctx context.Context, err := r.Client.Create(ctx, functionCm) if err != nil { r.Log.Error(err, "could not create ConfigMap") - r.EventSender.SendK8sEvent(apicommon.PhaseReconcileTask, "Warning", functionCm, apicommon.PhaseStateFailed, "could not create configmap", "") + r.EventSender.SendEvent(apicommon.PhaseReconcileTask, "Warning", functionCm, apicommon.PhaseStateFailed, "could not create configmap", "") return } } else if !reflect.DeepEqual(cm, functionCm) && functionCm != nil { // cm and inline func exists but differ err := r.Client.Update(ctx, functionCm) if err != nil { r.Log.Error(err, "could not update ConfigMap") - r.EventSender.SendK8sEvent(apicommon.PhaseReconcileTask, "Warning", functionCm, apicommon.PhaseStateFailed, "could not update configmap", "") + r.EventSender.SendEvent(apicommon.PhaseReconcileTask, "Warning", functionCm, apicommon.PhaseStateFailed, "could not update configmap", "") return } } diff --git a/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go b/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go index d5439c9687..80619df6dc 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnworkload/controller.go @@ -19,6 +19,7 @@ package keptnworkload import ( "context" "fmt" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "github.com/go-logr/logr" klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3" @@ -46,9 +47,9 @@ const traceComponentName = "keptn/lifecycle-operator/workload" type KeptnWorkloadReconciler struct { client.Client Scheme *runtime.Scheme - EventSender controllercommon.EventSender + EventSender controllercommon.IEvent Log logr.Logger - TracerFactory controllercommon.TracerFactory + TracerFactory telemetry.TracerFactory } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloads,verbs=get;list;watch;create;update;patch;delete @@ -104,7 +105,7 @@ func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Reques if err != nil { r.Log.Error(err, "could not create WorkloadInstance") span.SetStatus(codes.Error, err.Error()) - r.EventSender.SendK8sEvent(apicommon.PhaseCreateWorklodInstance, "Warning", workloadInstance, apicommon.PhaseStateFailed, "could not create KeptnWorkloadInstance ", workloadInstance.Spec.Version) + r.EventSender.SendEvent(apicommon.PhaseCreateWorklodInstance, "Warning", workloadInstance, apicommon.PhaseStateFailed, "could not create KeptnWorkloadInstance ", workloadInstance.Spec.Version) return ctrl.Result{}, err } workload.Status.CurrentVersion = workload.Spec.Version @@ -161,6 +162,6 @@ func (r *KeptnWorkloadReconciler) createWorkloadInstance(ctx context.Context, wo return &workloadInstance, err } -func (r *KeptnWorkloadReconciler) getTracer() controllercommon.ITracer { +func (r *KeptnWorkloadReconciler) getTracer() telemetry.ITracer { return r.TracerFactory.GetTracer(traceComponentName) } diff --git a/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go b/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go index 255d4a9802..ff92590572 100644 --- a/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go +++ b/lifecycle-operator/controllers/lifecycle/keptnworkloadinstance/controller.go @@ -19,6 +19,7 @@ package keptnworkloadinstance import ( "context" "fmt" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "time" "github.com/go-logr/logr" @@ -46,11 +47,11 @@ const traceComponentName = "keptn/lifecycle-operator/workloadinstance" type KeptnWorkloadInstanceReconciler struct { client.Client Scheme *runtime.Scheme - EventSender controllercommon.EventSender + EventSender controllercommon.IEvent Log logr.Logger Meters apicommon.KeptnMeters - SpanHandler *controllercommon.SpanHandler - TracerFactory controllercommon.TracerFactory + SpanHandler *telemetry.SpanHandler + TracerFactory telemetry.TracerFactory } // +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloadinstances,verbs=get;list;watch;create;update;patch;delete @@ -190,7 +191,7 @@ func (r *KeptnWorkloadInstanceReconciler) finishKeptnWorkloadInstanceReconcile(c return ctrl.Result{Requeue: true}, err } - r.EventSender.SendK8sEvent(apicommon.PhaseWorkloadCompleted, "Normal", workloadInstance, apicommon.PhaseStateFinished, "has finished", workloadInstance.GetVersion()) + r.EventSender.SendEvent(apicommon.PhaseWorkloadCompleted, "Normal", workloadInstance, apicommon.PhaseStateFinished, "has finished", workloadInstance.GetVersion()) attrs := workloadInstance.GetMetricsAttributes() @@ -224,7 +225,7 @@ func (r *KeptnWorkloadInstanceReconciler) SetupWithManager(mgr ctrl.Manager) err func (r *KeptnWorkloadInstanceReconciler) sendUnfinishedPreEvaluationEvents(appPreEvalStatus apicommon.KeptnState, phase apicommon.KeptnPhaseType, workloadInstance *klcv1alpha3.KeptnWorkloadInstance) { if appPreEvalStatus.IsFailed() { - r.EventSender.SendK8sEvent(phase, "Warning", workloadInstance, apicommon.PhaseStateFailed, "has failed since app has failed", workloadInstance.GetVersion()) + r.EventSender.SendEvent(phase, "Warning", workloadInstance, apicommon.PhaseStateFailed, "has failed since app has failed", workloadInstance.GetVersion()) } } @@ -260,11 +261,11 @@ func (r *KeptnWorkloadInstanceReconciler) checkPreEvaluationStatusOfApp(ctx cont found, appVersion, err := r.getAppVersionForWorkloadInstance(ctx, workloadInstance) if err != nil { span.SetStatus(codes.Error, err.Error()) - r.EventSender.SendK8sEvent(phase, "Warning", workloadInstance, "GetAppVersionFailed", "has failed since app could not be retrieved", workloadInstance.GetVersion()) + r.EventSender.SendEvent(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") - r.EventSender.SendK8sEvent(phase, "Warning", workloadInstance, "AppVersionNotFound", "has failed since app could not be found", workloadInstance.GetVersion()) + r.EventSender.SendEvent(phase, "Warning", workloadInstance, "AppVersionNotFound", "has failed since app could not be found", workloadInstance.GetVersion()) return true, fmt.Errorf(controllererrors.ErrCannotFetchAppVersionForWorkloadInstanceMsg) } @@ -314,7 +315,7 @@ func (r *KeptnWorkloadInstanceReconciler) getAppVersionForWorkloadInstance(ctx c return true, latestVersion, nil } -func (r *KeptnWorkloadInstanceReconciler) getTracer() controllercommon.ITracer { +func (r *KeptnWorkloadInstanceReconciler) getTracer() telemetry.ITracer { return r.TracerFactory.GetTracer(traceComponentName) } diff --git a/lifecycle-operator/controllers/options/keptnconfig_controller.go b/lifecycle-operator/controllers/options/keptnconfig_controller.go index 55ec7d459b..fac1677e06 100644 --- a/lifecycle-operator/controllers/options/keptnconfig_controller.go +++ b/lifecycle-operator/controllers/options/keptnconfig_controller.go @@ -20,11 +20,11 @@ import ( "context" "fmt" "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "time" "github.com/go-logr/logr" optionsv1alpha1 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/options/v1alpha1" - controllercommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common" controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -78,7 +78,7 @@ func (r *KeptnConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) func (r *KeptnConfigReconciler) reconcileOtelCollectorUrl(config *optionsv1alpha1.KeptnConfig) (ctrl.Result, error) { r.Log.Info(fmt.Sprintf("reconciling Keptn Config: %s", config.Name)) - otelConfig := controllercommon.GetOtelInstance() + otelConfig := telemetry.GetOtelInstance() if err := otelConfig.InitOtelCollector(config.Spec.OTelCollectorUrl); err != nil { r.Log.Error(err, "unable to initialize OTel tracer options") diff --git a/lifecycle-operator/main.go b/lifecycle-operator/main.go index 14c382fb3d..214bf03d11 100644 --- a/lifecycle-operator/main.go +++ b/lifecycle-operator/main.go @@ -19,6 +19,7 @@ package main import ( "flag" "fmt" + "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry" "log" "net/http" "os" @@ -115,7 +116,7 @@ func main() { } provider := metric.NewMeterProvider(metric.WithReader(exporter)) meter := provider.Meter("keptn/task") - keptnMeters := controllercommon.SetUpKeptnTaskMeters(meter) + keptnMeters := telemetry.SetUpKeptnTaskMeters(meter) // Start the prometheus HTTP server and pass the exporter Collector to it go serveMetrics() @@ -164,12 +165,12 @@ func main() { } // Enabling OTel - err = controllercommon.GetOtelInstance().InitOtelCollector("") + err = telemetry.GetOtelInstance().InitOtelCollector("") if err != nil { setupLog.Error(err, "unable to initialize OTel tracer options") } - spanHandler := &controllercommon.SpanHandler{} + spanHandler := &telemetry.SpanHandler{} taskLogger := ctrl.Log.WithName("KeptnTask Controller") taskReconciler := &keptntask.KeptnTaskReconciler{ @@ -178,7 +179,7 @@ func main() { Log: taskLogger.V(env.KeptnTaskControllerLogLevel), EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptntask-controller")), Meters: keptnMeters, - TracerFactory: controllercommon.GetOtelInstance(), + TracerFactory: telemetry.GetOtelInstance(), } if err = (taskReconciler).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeptnTask") @@ -203,7 +204,7 @@ func main() { Scheme: mgr.GetScheme(), Log: appLogger.V(env.KeptnAppControllerLogLevel), EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnapp-controller")), - TracerFactory: controllercommon.GetOtelInstance(), + TracerFactory: telemetry.GetOtelInstance(), } if err = (appReconciler).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeptnApp") @@ -227,7 +228,7 @@ func main() { Scheme: mgr.GetScheme(), Log: workloadLogger.V(env.KeptnWorkloadControllerLogLevel), EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnworkload-controller")), - TracerFactory: controllercommon.GetOtelInstance(), + TracerFactory: telemetry.GetOtelInstance(), } if err = (workloadReconciler).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeptnWorkload") @@ -241,7 +242,7 @@ func main() { Log: workloadInstanceLogger.V(env.KeptnWorkloadInstanceControllerLogLevel), EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnworkloadinstance-controller")), Meters: keptnMeters, - TracerFactory: controllercommon.GetOtelInstance(), + TracerFactory: telemetry.GetOtelInstance(), SpanHandler: spanHandler, } if err = (workloadInstanceReconciler).SetupWithManager(mgr); err != nil { @@ -255,7 +256,7 @@ func main() { Scheme: mgr.GetScheme(), Log: appVersionLogger.V(env.KeptnAppVersionControllerLogLevel), EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnappversion-controller")), - TracerFactory: controllercommon.GetOtelInstance(), + TracerFactory: telemetry.GetOtelInstance(), Meters: keptnMeters, SpanHandler: spanHandler, } @@ -270,7 +271,7 @@ func main() { Scheme: mgr.GetScheme(), Log: evaluationLogger.V(env.KeptnEvaluationControllerLogLevel), EventSender: controllercommon.NewEventSender(mgr.GetEventRecorderFor("keptnevaluation-controller")), - TracerFactory: controllercommon.GetOtelInstance(), + TracerFactory: telemetry.GetOtelInstance(), Meters: keptnMeters, Namespace: env.PodNamespace, } @@ -313,7 +314,7 @@ func main() { } // +kubebuilder:scaffold:builder - controllercommon.SetUpKeptnMeters(meter, mgr.GetClient()) + telemetry.SetUpKeptnMeters(meter, mgr.GetClient()) if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") diff --git a/lifecycle-operator/webhooks/pod_mutator/pod_mutating_webhook.go b/lifecycle-operator/webhooks/pod_mutator/pod_mutating_webhook.go index d930f74247..2f96390dfe 100644 --- a/lifecycle-operator/webhooks/pod_mutator/pod_mutating_webhook.go +++ b/lifecycle-operator/webhooks/pod_mutator/pod_mutating_webhook.go @@ -39,7 +39,7 @@ type PodMutatingWebhook struct { Client client.Client Tracer trace.Tracer Decoder *admission.Decoder - EventSender controllercommon.EventSender + EventSender controllercommon.IEvent Log logr.Logger } @@ -288,7 +288,7 @@ 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") - a.EventSender.SendK8sEvent(apicommon.PhaseCreateWorkload, "Warning", workload, apicommon.PhaseStateFailed, "could not create KeptnWorkload", workload.Spec.Version) + a.EventSender.SendEvent(apicommon.PhaseCreateWorkload, "Warning", workload, apicommon.PhaseStateFailed, "could not create KeptnWorkload", workload.Spec.Version) span.SetStatus(codes.Error, err.Error()) return err } @@ -312,7 +312,7 @@ 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") - a.EventSender.SendK8sEvent(apicommon.PhaseUpdateWorkload, "Warning", workload, apicommon.PhaseStateFailed, "could not update KeptnWorkload", workload.Spec.Version) + a.EventSender.SendEvent(apicommon.PhaseUpdateWorkload, "Warning", workload, apicommon.PhaseStateFailed, "could not update KeptnWorkload", workload.Spec.Version) span.SetStatus(codes.Error, err.Error()) return err } @@ -340,7 +340,7 @@ 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 AppCreationRequest") - a.EventSender.SendK8sEvent(apicommon.PhaseCreateAppCreationRequest, "Warning", appCreationRequest, apicommon.PhaseStateFailed, "could not create KeptnAppCreationRequest", appCreationRequest.Spec.AppName) + a.EventSender.SendEvent(apicommon.PhaseCreateAppCreationRequest, "Warning", appCreationRequest, apicommon.PhaseStateFailed, "could not create KeptnAppCreationRequest", appCreationRequest.Spec.AppName) span.SetStatus(codes.Error, err.Error()) return err }