Skip to content

Commit 12adc54

Browse files
committed
fix: order conditions by type to limit un-needed updates
Signed-off-by: ehila <[email protected]>
1 parent e8107ac commit 12adc54

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pkg/controller/status/conditions.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package status
22

33
import (
4+
"sort"
5+
46
configv1 "github.com/openshift/api/config/v1"
57
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
68
)
@@ -101,10 +103,15 @@ func (c *conditions) findCondition(condition configv1.ClusterStatusConditionType
101103
return nil
102104
}
103105

106+
// entries returns a sorted list of status conditions from the mapped values.
107+
// The list is sorted by by type ClusterStatusConditionType to ensure consistent ordering for deep equal checks.
104108
func (c *conditions) entries() []configv1.ClusterOperatorStatusCondition {
105109
var res []configv1.ClusterOperatorStatusCondition
106110
for _, v := range c.entryMap {
107111
res = append(res, v)
108112
}
113+
sort.SliceStable(res, func(i, j int) bool {
114+
return string(res[i].Type) < string(res[j].Type)
115+
})
109116
return res
110117
}

pkg/controller/status/conditions_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,49 @@ func Test_conditions_entries(t *testing.T) {
5252
},
5353
},
5454
},
55+
{
56+
name: "Condition array is always sorted by type",
57+
fields: fields{entryMap: map[configv1.ClusterStatusConditionType]configv1.ClusterOperatorStatusCondition{
58+
configv1.OperatorProgressing: {
59+
Type: configv1.OperatorProgressing,
60+
Status: configv1.ConditionUnknown,
61+
LastTransitionTime: time,
62+
Reason: "",
63+
},
64+
configv1.OperatorAvailable: {
65+
Type: configv1.OperatorAvailable,
66+
Status: configv1.ConditionUnknown,
67+
LastTransitionTime: time,
68+
Reason: "",
69+
},
70+
configv1.OperatorDegraded: {
71+
Type: configv1.OperatorDegraded,
72+
Status: configv1.ConditionUnknown,
73+
LastTransitionTime: time,
74+
Reason: "",
75+
},
76+
}},
77+
want: []configv1.ClusterOperatorStatusCondition{
78+
{
79+
Type: configv1.OperatorAvailable,
80+
Status: configv1.ConditionUnknown,
81+
LastTransitionTime: time,
82+
Reason: "",
83+
},
84+
{
85+
Type: configv1.OperatorDegraded,
86+
Status: configv1.ConditionUnknown,
87+
LastTransitionTime: time,
88+
Reason: "",
89+
},
90+
{
91+
Type: configv1.OperatorProgressing,
92+
Status: configv1.ConditionUnknown,
93+
LastTransitionTime: time,
94+
Reason: "",
95+
},
96+
},
97+
},
5598
}
5699
for _, tt := range tests {
57100
t.Run(tt.name, func(t *testing.T) {
@@ -60,6 +103,9 @@ func Test_conditions_entries(t *testing.T) {
60103
}
61104
got := c.entries()
62105
assert.ElementsMatchf(t, got, tt.want, "entries() = %v, want %v", got, tt.want)
106+
for i, expected := range tt.want {
107+
assert.Equal(t, expected, got[i])
108+
}
63109
})
64110
}
65111
}

0 commit comments

Comments
 (0)