Skip to content

Commit 38a03f2

Browse files
authored
Iterate modified and non-ready resources in order (#3533)
1 parent 4809cf0 commit 38a03f2

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

internal/cmd/agent/deployer/monitor/updatestatus.go

+59-27
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
package monitor
55

66
import (
7+
"cmp"
78
"context"
89
"errors"
910
"fmt"
11+
"iter"
12+
"maps"
13+
"slices"
1014
"sort"
1115
"strings"
1216

@@ -219,40 +223,60 @@ func (m *Monitor) updateFromResources(ctx context.Context, logger logr.Logger, b
219223
}
220224

221225
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))
228227
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 {
229237
if len(result) >= 10 {
230238
return result
231239
}
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")
237243
}
244+
}
238245

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+
})
250256
}
251257
}
252258

253259
return result
254260
}
255261

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+
256280
// modified returns a list of modified statuses based on the provided plan and previous release resources.
257281
// The function iterates through the plan's create, delete, and update actions and constructs a modified status
258282
// for each resource.
@@ -263,7 +287,11 @@ func modified(ctx context.Context, c client.Client, logger logr.Logger, plan des
263287
return sortKey(result[i]) < sortKey(result[j])
264288
})
265289
}()
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+
})
267295
for _, key := range keys {
268296
if len(result) >= 10 {
269297
return result
@@ -303,7 +331,11 @@ func modified(ctx context.Context, c client.Client, logger logr.Logger, plan des
303331
}
304332
}
305333

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+
})
307339
for _, key := range keys {
308340
if len(result) >= 10 {
309341
return result
@@ -326,10 +358,10 @@ func modified(ctx context.Context, c client.Client, logger logr.Logger, plan des
326358
}
327359
}
328360

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) {
331363
if len(result) >= 10 {
332-
break
364+
return result
333365
}
334366

335367
apiVersion, kind := gvk.ToAPIVersionAndKind()

0 commit comments

Comments
 (0)