From 2e031f5e2dc5cc40885ea2e6be4548a6df4bc2a8 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 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/scrapers/kubernetes/informers.go b/scrapers/kubernetes/informers.go index 8b30cd399..78d60ec8f 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 }