Skip to content

Commit d71d210

Browse files
eggfoobartremes
authored andcommitted
fix: order conditions by type to limit un-needed updates (openshift#667)
Signed-off-by: ehila <[email protected]> Signed-off-by: ehila <[email protected]>
1 parent 87a32bb commit d71d210

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
)
@@ -102,10 +104,15 @@ func (c *conditions) findCondition(condition configv1.ClusterStatusConditionType
102104
return nil
103105
}
104106

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

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)