Skip to content

Commit faaba62

Browse files
committed
refactor and clean up
1 parent bd922ae commit faaba62

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

cocoonset/reconciler.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cocoonset
22

33
import (
4+
"cmp"
45
"context"
56
"fmt"
67
"maps"
@@ -332,22 +333,35 @@ func (r *Reconciler) applySuspend(ctx context.Context, classified classifiedPods
332333
// CocoonHibernation status would still read Hibernated, and the user
333334
// would observe a phantom-running pod.
334335
//
335-
// The cheap path (no in-flight CRs in the namespace) only walks the
336-
// owned pods and skips ones whose hibernate annotation is already
337-
// absent — PatchHibernateState(false) is a no-op there.
336+
// Fast path: the steady-state non-suspended CocoonSet has no pod
337+
// carrying the hibernate annotation, so we skip the CocoonHibernation
338+
// List entirely. One pass over classified.allByName collects the
339+
// hibernated subset; if it's empty we return without touching the
340+
// API. Otherwise we sort just that subset (for deterministic patch
341+
// order under test) and consult the CR list to decide which ones to
342+
// leave alone.
338343
func (r *Reconciler) applyUnsuspend(ctx context.Context, namespace string, classified classifiedPods) error {
344+
var hibernated []*corev1.Pod
345+
for _, pod := range classified.allByName {
346+
if bool(meta.ReadHibernateState(pod)) {
347+
hibernated = append(hibernated, pod)
348+
}
349+
}
350+
if len(hibernated) == 0 {
351+
return nil
352+
}
353+
slices.SortFunc(hibernated, func(a, b *corev1.Pod) int {
354+
return cmp.Compare(a.Name, b.Name)
355+
})
356+
339357
hibernatedByCR, err := r.podsHibernatedByCR(ctx, namespace)
340358
if err != nil {
341359
return err
342360
}
343-
for _, name := range slices.Sorted(maps.Keys(classified.allByName)) {
361+
for _, pod := range hibernated {
344362
if ctxErr := ctx.Err(); ctxErr != nil {
345363
return ctxErr
346364
}
347-
pod := classified.allByName[name]
348-
if !bool(meta.ReadHibernateState(pod)) {
349-
continue
350-
}
351365
if _, ownedByCR := hibernatedByCR[pod.Name]; ownedByCR {
352366
continue
353367
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.25.0
55
require (
66
github.com/cocoonstack/cocoon-common v0.1.4-0.20260411114436-0cf2e26e5798
77
github.com/cocoonstack/epoch v0.1.8-0.20260411050236-353f65a833f9
8+
github.com/go-logr/logr v1.4.3
89
github.com/projecteru2/core v0.0.0-20241016125006-ff909eefe04c
910
k8s.io/api v0.35.3
1011
k8s.io/apimachinery v0.35.3
@@ -25,7 +26,6 @@ require (
2526
github.com/fsnotify/fsnotify v1.9.0 // indirect
2627
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
2728
github.com/getsentry/sentry-go v0.20.0 // indirect
28-
github.com/go-logr/logr v1.4.3 // indirect
2929
github.com/go-openapi/jsonpointer v0.21.0 // indirect
3030
github.com/go-openapi/jsonreference v0.20.2 // indirect
3131
github.com/go-openapi/swag v0.23.0 // indirect

0 commit comments

Comments
 (0)