From 932e0712393a2bbf0aac72ef50d7c3eaf80dedf4 Mon Sep 17 00:00:00 2001 From: Luca Sepe Date: Tue, 30 Jul 2024 16:06:29 +0200 Subject: [PATCH] feat: better check on composition id (#16) --- README.md | 7 +++++-- internal/router/labels.go | 7 +++++-- internal/router/router.go | 19 ++++++++++++------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e8a0d9b..ec9fa24 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # EventRouter -This service serves as an active watcher of event resource in the kubernetes system, which takes those events and pushes them to a user specified _HTTP hook_ (called _Registration_). +This service serves as an active watcher of event resource in the kubernetes system, which: -this service will POST to the specified _endpoint_ a JSON containing the event info. +- consider only events related to _involvedObjects_ with a composition identifier +- takes those events and pushes them to a user specified _HTTP hook_ (called _Registration_) + +The service will POST to the specified _endpoint_ a JSON containing the event info. Here a sample event info: diff --git a/internal/router/labels.go b/internal/router/labels.go index 850bb19..402af1d 100644 --- a/internal/router/labels.go +++ b/internal/router/labels.go @@ -58,13 +58,16 @@ func createPatchData(labels map[string]string) ([]byte, error) { return json.Marshal(patch) } -func wasPatchedByKrateo(obj *corev1.Event) bool { +func hasCompositionId(obj *corev1.Event) bool { labels := obj.GetLabels() if len(labels) == 0 { return false } - _, ok := labels[keyPatchedBy] + val, ok := labels[keyCompositionID] + if len(val) == 0 { + return false + } return ok } diff --git a/internal/router/router.go b/internal/router/router.go index 7e16f79..c047a15 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -99,7 +99,18 @@ func (er *EventRouter) OnDelete(obj interface{}) { } func (er *EventRouter) onEvent(event *corev1.Event) { - if wasPatchedByKrateo(event) { + klog.V(4).InfoS("Received event", + "msg", event.Message, + "namespace", event.Namespace, + "reason", event.Reason, + "involvedObject", event.InvolvedObject.Name) + + if hasCompositionId(event) { + klog.V(4).InfoS("CompositionID already present", + "msg", event.Message, + "namespace", event.Namespace, + "reason", event.Reason, + "involvedObject", event.InvolvedObject.Name) return } @@ -112,11 +123,5 @@ func (er *EventRouter) onEvent(event *corev1.Event) { // return // } - klog.V(4).InfoS("Received event", - "msg", event.Message, - "namespace", event.Namespace, - "reason", event.Reason, - "involvedObject", event.InvolvedObject.Name) - er.handler.Handle(*event.DeepCopy()) }