Skip to content

Commit

Permalink
compare owner ref
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Dec 5, 2024
1 parent c3727eb commit 2e031f5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions scrapers/kubernetes/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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{
Expand All @@ -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
}

0 comments on commit 2e031f5

Please sign in to comment.