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 515f409
Show file tree
Hide file tree
Showing 2 changed files with 48 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
}
31 changes: 31 additions & 0 deletions scrapers/kubernetes/informers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
},
},
},
},
}
}

0 comments on commit 515f409

Please sign in to comment.