Skip to content

Commit 71176a3

Browse files
authored
Merge pull request #11429 from fabriziopandini/refine-v1beta2-aggregation-object-order
🌱 Refine v1beta2 object sort for aggregation
2 parents b12ad96 + 115f3cd commit 71176a3

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

util/conditions/v1beta2/merge_strategies.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ func aggregateMessages(conditions []ConditionWithOwnerInfo, n *int, dropEmpty bo
375375
messageObjMap := map[string]map[string][]string{}
376376
messagePriorityMap := map[string]MergePriority{}
377377
messageMustGoFirst := map[string]bool{}
378+
cpMachines := sets.Set[string]{}
378379
for _, condition := range conditions {
379380
if dropEmpty && condition.Message == "" {
380381
continue
@@ -387,6 +388,11 @@ func aggregateMessages(conditions []ConditionWithOwnerInfo, n *int, dropEmpty bo
387388
}
388389
messageObjMap[condition.OwnerResource.Kind][m] = append(messageObjMap[condition.OwnerResource.Kind][m], condition.OwnerResource.Name)
389390

391+
// Keep track of CP machines
392+
if condition.OwnerResource.IsControlPlaneMachine {
393+
cpMachines.Insert(condition.OwnerResource.Name)
394+
}
395+
390396
// Keep track of the priority of the message.
391397
// In case the same message exists with different priorities, the highest according to issue/unknown/info applies.
392398
currentPriority, ok := messagePriorityMap[m]
@@ -456,7 +462,9 @@ func aggregateMessages(conditions []ConditionWithOwnerInfo, n *int, dropEmpty bo
456462

457463
msg := ""
458464
allObjects := messageObjMapForKind[m]
459-
sort.Strings(allObjects)
465+
sort.Slice(allObjects, func(i, j int) bool {
466+
return sortObj(allObjects[i], allObjects[j], cpMachines)
467+
})
460468
switch {
461469
case len(allObjects) == 0:
462470
// This should never happen, entry in the map exists only when an object reports a message.
@@ -520,6 +528,16 @@ func sortMessage(i, j string, messageMustGoFirst map[string]bool, messagePriorit
520528
return strings.Join(messageObjMapForKind[i], ",") < strings.Join(messageObjMapForKind[j], ",")
521529
}
522530

531+
func sortObj(i, j string, cpMachines sets.Set[string]) bool {
532+
if cpMachines.Has(i) && !cpMachines.Has(j) {
533+
return true
534+
}
535+
if !cpMachines.Has(i) && cpMachines.Has(j) {
536+
return false
537+
}
538+
return i < j
539+
}
540+
523541
func indentIfMultiline(m string) string {
524542
msg := ""
525543
if strings.Contains(m, "\n") || strings.HasPrefix(m, "* ") {

util/conditions/v1beta2/merge_strategies_test.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
. "github.com/onsi/gomega"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/util/sets"
2526
)
2627

2728
func TestSortMessages(t *testing.T) {
@@ -70,6 +71,24 @@ func TestSortMessages(t *testing.T) {
7071
g.Expect(got).To(BeFalse())
7172
}
7273

74+
func TestSortObj(t *testing.T) {
75+
g := NewWithT(t)
76+
cpMachines := sets.Set[string]{}
77+
cpMachines.Insert("m3")
78+
79+
// Control plane goes before not control planes
80+
got := sortObj("m3", "m1", cpMachines)
81+
g.Expect(got).To(BeTrue())
82+
got = sortObj("m1", "m3", cpMachines)
83+
g.Expect(got).To(BeFalse())
84+
85+
// machines must be sorted alphabetically
86+
got = sortObj("m1", "m2", cpMachines)
87+
g.Expect(got).To(BeTrue())
88+
got = sortObj("m2", "m1", cpMachines)
89+
g.Expect(got).To(BeFalse())
90+
}
91+
7392
func TestAggregateMessages(t *testing.T) {
7493
t.Run("Groups by kind, return max 3 messages, aggregate objects, count others", func(t *testing.T) {
7594
g := NewWithT(t)
@@ -137,13 +156,13 @@ func TestAggregateMessages(t *testing.T) {
137156
"And 5 MachineDeployments with status unknown", // MachineDeployments obj01, obj02, obj05, obj08, obj09 (Message 1) << This doesn't show up because even if it applies to 5 machines because it has merge priority unknown
138157
}))
139158
})
140-
t.Run("Control plane machines always goes before unknown", func(t *testing.T) {
159+
t.Run("Control plane machines always goes before other machines", func(t *testing.T) {
141160
g := NewWithT(t)
142161

143162
conditions := []ConditionWithOwnerInfo{
144163
// NOTE: objects are intentionally not in order so we can validate they are sorted by name
145-
{OwnerResource: ConditionOwnerInfo{Kind: "Machine", Name: "obj02", IsControlPlaneMachine: true}, Condition: metav1.Condition{Type: "A", Message: "Message-1", Status: metav1.ConditionUnknown}},
146-
{OwnerResource: ConditionOwnerInfo{Kind: "Machine", Name: "obj01"}, Condition: metav1.Condition{Type: "A", Message: "* Message-2", Status: metav1.ConditionFalse}},
164+
{OwnerResource: ConditionOwnerInfo{Kind: "Machine", Name: "obj02", IsControlPlaneMachine: true}, Condition: metav1.Condition{Type: "A", Message: "* Message-1", Status: metav1.ConditionUnknown}},
165+
{OwnerResource: ConditionOwnerInfo{Kind: "Machine", Name: "obj01"}, Condition: metav1.Condition{Type: "A", Message: "* Message-1", Status: metav1.ConditionUnknown}},
147166
{OwnerResource: ConditionOwnerInfo{Kind: "Machine", Name: "obj04"}, Condition: metav1.Condition{Type: "A", Message: "* Message-2", Status: metav1.ConditionFalse}},
148167
{OwnerResource: ConditionOwnerInfo{Kind: "Machine", Name: "obj03"}, Condition: metav1.Condition{Type: "A", Message: "* Message-2", Status: metav1.ConditionFalse}},
149168
{OwnerResource: ConditionOwnerInfo{Kind: "Machine", Name: "obj06"}, Condition: metav1.Condition{Type: "A", Message: "* Message-3A\n* Message-3B", Status: metav1.ConditionFalse}},
@@ -155,8 +174,9 @@ func TestAggregateMessages(t *testing.T) {
155174

156175
g.Expect(n).To(Equal(0))
157176
g.Expect(messages).To(Equal([]string{
158-
"* Machine obj02: Message-1", // control plane always go first, no matter if priority or number of objects
159-
"* Machines obj01, obj03, obj04, ... (1 more):\n" +
177+
"* Machines obj02, obj01:\n" + // control plane machines always go first, no matter if priority or number of objects (note, cp machine also go first in the machine list)
178+
" * Message-1",
179+
"* Machines obj03, obj04, obj05:\n" +
160180
" * Message-2",
161181
"* Machine obj06:\n" +
162182
" * Message-3A\n" +

0 commit comments

Comments
 (0)