Skip to content

Commit

Permalink
fix: ignore events whose involved object's UID is not a UUID (#662)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adityathebe authored Jun 24, 2024
1 parent 886c724 commit e05ef45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
16 changes: 15 additions & 1 deletion scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit e05ef45

Please sign in to comment.