4
4
package monitor
5
5
6
6
import (
7
+ "cmp"
7
8
"context"
8
9
"errors"
9
10
"fmt"
11
+ "iter"
12
+ "maps"
13
+ "slices"
10
14
"sort"
11
15
"strings"
12
16
@@ -219,40 +223,60 @@ func (m *Monitor) updateFromResources(ctx context.Context, logger logr.Logger, b
219
223
}
220
224
221
225
func nonReady (logger logr.Logger , plan desiredset.Plan , ignoreOptions fleet.IgnoreOptions ) (result []fleet.NonReadyStatus ) {
222
- defer func () {
223
- sort .Slice (result , func (i , j int ) bool {
224
- return result [i ].UID < result [j ].UID
225
- })
226
- }()
227
-
226
+ objects := make ([]* unstructured.Unstructured , 0 , len (plan .Objects ))
228
227
for _ , obj := range plan .Objects {
228
+ if u , ok := obj .(* unstructured.Unstructured ); ok {
229
+ objects = append (objects , u )
230
+ }
231
+ }
232
+ sort .Slice (objects , func (i , j int ) bool {
233
+ return objects [i ].GetUID () < objects [j ].GetUID ()
234
+ })
235
+
236
+ for _ , u := range objects {
229
237
if len (result ) >= 10 {
230
238
return result
231
239
}
232
- if u , ok := obj .(* unstructured.Unstructured ); ok {
233
- if ignoreOptions .Conditions != nil {
234
- if err := excludeIgnoredConditions (u , ignoreOptions ); err != nil {
235
- logger .Error (err , "failed to ignore conditions" )
236
- }
240
+ if ignoreOptions .Conditions != nil {
241
+ if err := excludeIgnoredConditions (u , ignoreOptions ); err != nil {
242
+ logger .Error (err , "failed to ignore conditions" )
237
243
}
244
+ }
238
245
239
- summary := summary .Summarize (u )
240
- if ! summary .IsReady () {
241
- result = append (result , fleet.NonReadyStatus {
242
- UID : u .GetUID (),
243
- Kind : u .GetKind (),
244
- APIVersion : u .GetAPIVersion (),
245
- Namespace : u .GetNamespace (),
246
- Name : u .GetName (),
247
- Summary : summary ,
248
- })
249
- }
246
+ summary := summary .Summarize (u )
247
+ if ! summary .IsReady () {
248
+ result = append (result , fleet.NonReadyStatus {
249
+ UID : u .GetUID (),
250
+ Kind : u .GetKind (),
251
+ APIVersion : u .GetAPIVersion (),
252
+ Namespace : u .GetNamespace (),
253
+ Name : u .GetName (),
254
+ Summary : summary ,
255
+ })
250
256
}
251
257
}
252
258
253
259
return result
254
260
}
255
261
262
+ type stringer interface {
263
+ comparable
264
+ fmt.Stringer
265
+ }
266
+
267
+ // inOrder returns a key-value iterator that will run through the keys always in the same order
268
+ func inOrder [K stringer , V any ](m map [K ]V ) iter.Seq2 [K , V ] {
269
+ return func (yield func (K , V ) bool ) {
270
+ for _ , gvk := range slices .SortedFunc (maps .Keys (m ), func (a K , b K ) int {
271
+ return cmp .Compare (a .String (), b .String ())
272
+ }) {
273
+ if ! yield (gvk , m [gvk ]) {
274
+ return
275
+ }
276
+ }
277
+ }
278
+ }
279
+
256
280
// modified returns a list of modified statuses based on the provided plan and previous release resources.
257
281
// The function iterates through the plan's create, delete, and update actions and constructs a modified status
258
282
// for each resource.
@@ -263,7 +287,11 @@ func modified(ctx context.Context, c client.Client, logger logr.Logger, plan des
263
287
return sortKey (result [i ]) < sortKey (result [j ])
264
288
})
265
289
}()
266
- for gvk , keys := range plan .Create {
290
+ for gvk , keys := range inOrder (plan .Create ) {
291
+ // Also sort object keys
292
+ slices .SortFunc (keys , func (a , b objectset.ObjectKey ) int {
293
+ return cmp .Compare (a .String (), b .String ())
294
+ })
267
295
for _ , key := range keys {
268
296
if len (result ) >= 10 {
269
297
return result
@@ -303,7 +331,11 @@ func modified(ctx context.Context, c client.Client, logger logr.Logger, plan des
303
331
}
304
332
}
305
333
306
- for gvk , keys := range plan .Delete {
334
+ for gvk , keys := range inOrder (plan .Delete ) {
335
+ // Also sort object keys
336
+ slices .SortFunc (keys , func (a , b objectset.ObjectKey ) int {
337
+ return cmp .Compare (a .String (), b .String ())
338
+ })
307
339
for _ , key := range keys {
308
340
if len (result ) >= 10 {
309
341
return result
@@ -326,10 +358,10 @@ func modified(ctx context.Context, c client.Client, logger logr.Logger, plan des
326
358
}
327
359
}
328
360
329
- for gvk , patches := range plan .Update {
330
- for key , patch := range patches {
361
+ for gvk , patches := range inOrder ( plan .Update ) {
362
+ for key , patch := range inOrder ( patches ) {
331
363
if len (result ) >= 10 {
332
- break
364
+ return result
333
365
}
334
366
335
367
apiVersion , kind := gvk .ToAPIVersionAndKind ()
0 commit comments