From e05ef45e7db5213130fea82c493f871841ba7760 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Mon, 24 Jun 2024 13:02:43 +0545 Subject: [PATCH] fix: ignore events whose involved object's UID is not a UUID (#662) * fix: ignore events whose involved object's UID is not a UUID * chore: try to do a lookup by namespace, kind & name when invovled object UID is not a valid UUID * chore: namespace lookups by cluster name --- db/config.go | 3 ++- scrapers/kubernetes/kubernetes.go | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/db/config.go b/db/config.go index 74fb304d..233cd0a1 100644 --- a/db/config.go +++ b/db/config.go @@ -62,10 +62,11 @@ func FindConfigIDsByRelationshipSelector(ctx context.Context, selector v1.Relati } // FindConfigIDsByNamespaceNameClass returns the uuid of config items which matches the given type, name & namespace -func FindConfigIDsByNamespaceNameClass(ctx context.Context, namespace, name, configClass string) ([]uuid.UUID, error) { +func FindConfigIDsByNamespaceNameClass(ctx context.Context, cluster, namespace, name, configClass string) ([]uuid.UUID, error) { rs := types.ResourceSelector{ Name: name, Namespace: namespace, + TagSelector: fmt.Sprintf("cluster=%s", cluster), FieldSelector: fmt.Sprintf("config_class=%s", configClass), } diff --git a/scrapers/kubernetes/kubernetes.go b/scrapers/kubernetes/kubernetes.go index 1a928423..415a4eac 100644 --- a/scrapers/kubernetes/kubernetes.go +++ b/scrapers/kubernetes/kubernetes.go @@ -13,8 +13,10 @@ import ( "github.com/flanksource/commons/collections" "github.com/flanksource/duty/context" "github.com/flanksource/duty/models" + "github.com/google/uuid" "github.com/samber/lo" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -300,6 +302,18 @@ func ExtractResults(ctx api.ScrapeContext, config v1.Kubernetes, objs []*unstruc continue } + if _, err := uuid.Parse(string(event.InvolvedObject.UID)); err != nil { + ids, err := db.FindConfigIDsByNamespaceNameClass(ctx.DutyContext(), config.ClusterName, event.InvolvedObject.Namespace, event.InvolvedObject.Name, event.InvolvedObject.Kind) + if err != nil { + return results.Errorf(err, "failed to get config IDs for object %s/%s/%s", event.InvolvedObject.Namespace, event.InvolvedObject.Name, event.InvolvedObject.Kind) + } else if len(ids) == 0 { + ctx.Logger.V(3).Infof("skipping event (reason=%s, message=%s) because the involved object ID is not a valid UUID: %s", event.Reason, event.Message, event.InvolvedObject.UID) + continue + } + + event.InvolvedObject.UID = types.UID(ids[0].String()) + } + change := getChangeFromEvent(event, config.Event.SeverityKeywords) if change != nil { changeTypExclusion, changeSeverityExclusion, err := getObjectChangeExclusionAnnotations(ctx, string(event.InvolvedObject.UID), objChangeExclusionByType, objChangeExclusionBySeverity) @@ -448,7 +462,7 @@ func ExtractResults(ctx api.ScrapeContext, config v1.Kubernetes, objs []*unstruc if selector, err := f.Eval(obj.GetLabels(), env); err != nil { return results.Errorf(err, "failed to evaluate selector: %v for config relationship", f) } else if selector != nil { - linkedConfigItemIDs, err := db.FindConfigIDsByNamespaceNameClass(ctx.DutyContext(), selector.Namespace, selector.Name, selector.Kind) + linkedConfigItemIDs, err := db.FindConfigIDsByNamespaceNameClass(ctx.DutyContext(), config.ClusterName, selector.Namespace, selector.Name, selector.Kind) if err != nil { return results.Errorf(err, "failed to get linked config items by kubernetes selector(%v)", selector) }