From 515f409a308799364a69a9c7d5c1fc56fe394e3b Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Thu, 5 Dec 2024 13:37:03 +0545 Subject: [PATCH] compare owner ref --- scrapers/kubernetes/informers.go | 19 ++++++++++++++-- scrapers/kubernetes/informers_test.go | 31 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/scrapers/kubernetes/informers.go b/scrapers/kubernetes/informers.go index 8b30cd39..78d60ec8 100644 --- a/scrapers/kubernetes/informers.go +++ b/scrapers/kubernetes/informers.go @@ -16,6 +16,7 @@ import ( "github.com/flanksource/is-healthy/pkg/health" "github.com/google/uuid" "github.com/samber/lo" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/informers" @@ -363,6 +364,10 @@ func pqComparator(a, b any) int { return opResult } + if opResult := pqCompareOwnerRef(qa.Obj.GetOwnerReferences(), qb.Obj.GetOwnerReferences()); opResult != 0 { + return opResult + } + if opResult := pqCompareKind(qa.Obj.GetKind(), qb.Obj.GetKind()); opResult != 0 { return opResult } @@ -383,6 +388,14 @@ func pqCompareOperation(a, b QueueItemOperation) int { return a.Priority() - b.Priority() } +func pqCompareOwnerRef(a, b []metav1.OwnerReference) int { + if len(a) == len(b) { + return 0 + } + + return len(b) - len(a) +} + func pqCompareKind(a, b string) int { // smaller means earlier in the queue priority := map[string]int{ @@ -406,8 +419,10 @@ func pqCompareKind(a, b string) int { "Event": 5, } - pa := lo.CoalesceOrEmpty(priority[a], 3) - pb := lo.CoalesceOrEmpty(priority[b], 3) + const unknownKindPriority = 3 // set medium priority for unknown kinds + + pa := lo.CoalesceOrEmpty(priority[a], unknownKindPriority) + pb := lo.CoalesceOrEmpty(priority[b], unknownKindPriority) return pa - pb } diff --git a/scrapers/kubernetes/informers_test.go b/scrapers/kubernetes/informers_test.go index 015cb712..ebcecfab 100644 --- a/scrapers/kubernetes/informers_test.go +++ b/scrapers/kubernetes/informers_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/emirpasic/gods/queues/priorityqueue" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) @@ -114,6 +115,18 @@ func TestPqComparator(t *testing.T) { }, expected: []string{"b", "a"}, }, + { + name: "identical objects of unknown kind with owner reference", + a: QueueItem{ + Operation: QueueItemOperationAdd, + Obj: getUnstructuredWithOwnerRef("Custom", "a", now, metav1.OwnerReference{Name: "http-canary", Kind: "Canary"}), + }, + b: QueueItem{ + Operation: QueueItemOperationAdd, + Obj: getUnstructured("Custom", "b", now), + }, + expected: []string{"a", "b"}, + }, } for _, tt := range tests { @@ -169,3 +182,21 @@ func getUnstructured(kind, name string, creationTimestamp time.Time) *unstructur }, } } + +func getUnstructuredWithOwnerRef(kind, name string, creationTimestamp time.Time, ownerRef metav1.OwnerReference) *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]any{ + "kind": kind, + "metadata": map[string]any{ + "name": name, + "creationTimestamp": creationTimestamp.Format(time.RFC3339), + "ownerReferences": []any{ + map[string]any{ + "name": ownerRef.Name, + "kind": ownerRef.Kind, + }, + }, + }, + }, + } +}