diff --git a/cmd/main.go b/cmd/main.go index 0b85d6d..ec02941 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -222,7 +222,7 @@ func main() { if err = (&controller.CachedImageReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), - PacingEngine: pacing.NewEngine(mgr.GetClient(), podNamespace), + PacingEngine: pacing.NewEngine(mgr.GetAPIReader(), podNamespace), Recorder: mgr.GetEventRecorder("cachedimage-controller"), PodNamespace: podNamespace, }).SetupWithManager(mgr); err != nil { diff --git a/docs/go.mod b/docs/go.mod index cc0eced..a8b9b26 100644 --- a/docs/go.mod +++ b/docs/go.mod @@ -1,5 +1,3 @@ module github.com/corewire/drop/docs go 1.26.0 - -require github.com/imfing/hextra v0.12.3 // indirect diff --git a/docs/go.sum b/docs/go.sum index afa8680..e69de29 100644 --- a/docs/go.sum +++ b/docs/go.sum @@ -1,2 +0,0 @@ -github.com/imfing/hextra v0.12.3 h1:DZHY2rUWYteyzjlHi9r4n7Bb5e2Q+6LXe4C1Dqn0ZjM= -github.com/imfing/hextra v0.12.3/go.mod h1:vi+yhpq8YPp/aghvJlNKVnJKcPJ/VyAEcfC1BSV9ARo= diff --git a/internal/pacing/engine.go b/internal/pacing/engine.go index 219cc93..598e777 100644 --- a/internal/pacing/engine.go +++ b/internal/pacing/engine.go @@ -21,13 +21,17 @@ type Decision struct { // Engine evaluates pacing constraints before creating new drop Pods. type Engine struct { - Client client.Client + // Reader is used to list active pull Pods. It must bypass the informer cache + // (e.g. mgr.GetAPIReader()) so that pods created in the current reconcile + // cycle are visible to subsequent reconciles before the cache is updated. + Reader client.Reader PodNamespace string } -// NewEngine creates a new pacing engine. -func NewEngine(c client.Client, podNamespace string) *Engine { - return &Engine{Client: c, PodNamespace: podNamespace} +// NewEngine creates a new pacing engine. reader should be a direct API-server +// reader (mgr.GetAPIReader()) to avoid stale cache reads when counting active pods. +func NewEngine(reader client.Reader, podNamespace string) *Engine { + return &Engine{Reader: reader, PodNamespace: podNamespace} } const ( @@ -74,7 +78,7 @@ func (e *Engine) PullSlots(ctx context.Context, policy *v1alpha1.PullPolicy, cac client.InNamespace(ns), client.MatchingLabels{podbuilder.LabelManagedBy: podbuilder.LabelManagedByValue}, } - if err := e.Client.List(ctx, podList, listOpts...); err != nil { + if err := e.Reader.List(ctx, podList, listOpts...); err != nil { return Decision{}, err }