77 "github.com/pkg/errors"
88 "go.uber.org/zap"
99 v1 "k8s.io/api/core/v1"
10+ "k8s.io/apimachinery/pkg/fields"
1011 ctrl "sigs.k8s.io/controller-runtime"
1112 "sigs.k8s.io/controller-runtime/pkg/client"
1213 "sigs.k8s.io/controller-runtime/pkg/event"
@@ -50,7 +51,7 @@ type limiter interface {
5051 Allow () bool
5152}
5253
53- // NotifierFunc returns a reconcile.Func that lists Pods to get the latest
54+ // NewNotifierFunc returns a reconcile.Func that lists Pods to get the latest
5455// state and notifies listeners of the resulting Pods.
5556// listOpts are passed to the client.List call to filter the Pod list.
5657// limiter is an optional rate limiter which may be used to limit the
@@ -80,6 +81,54 @@ func (p *watcher) NewNotifierFunc(listOpts *client.ListOptions, limiter limiter,
8081 }
8182}
8283
84+ // NewPodIPDemandNotifierFunc returns a reconcile.Func optimized for IP demand calculation
85+ // that uses server-side filtering to exclude terminal Pods (Succeeded/Failed) from the results.
86+ // This avoids client-side iteration over all pods by using multiple targeted queries.
87+ func (p * watcher ) NewPodIPDemandNotifierFunc (baseListOpts * client.ListOptions , limiter limiter , listeners ... func ([]v1.Pod )) reconcile.Func {
88+ p .z .Info ("adding pod IP demand notifier for listeners" , zap .Int ("listeners" , len (listeners )))
89+ return func (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
90+ if ! limiter .Allow () {
91+ // rate limit exceeded, requeue
92+ p .z .Info ("rate limit exceeded" )
93+ return ctrl.Result {Requeue : true }, nil
94+ }
95+
96+ // Create field selectors for active pod phases (exclude Succeeded and Failed)
97+ activePhases := []v1.PodPhase {v1 .PodRunning , v1 .PodPending , v1 .PodUnknown }
98+ var allActivePods []v1.Pod
99+
100+ for _ , phase := range activePhases {
101+ // Clone base list options and add phase filter
102+ listOpts := & client.ListOptions {}
103+ if baseListOpts .FieldSelector != nil {
104+ // Combine existing field selector with phase filter
105+ phaseSelector := fields .SelectorFromSet (fields.Set {"status.phase" : string (phase )})
106+ combinedSelector := fields .AndSelectors (baseListOpts .FieldSelector , phaseSelector )
107+ listOpts .FieldSelector = combinedSelector
108+ } else {
109+ listOpts .FieldSelector = fields .SelectorFromSet (fields.Set {"status.phase" : string (phase )})
110+ }
111+
112+ // Copy other options from base
113+ listOpts .LabelSelector = baseListOpts .LabelSelector
114+ listOpts .Namespace = baseListOpts .Namespace
115+ listOpts .Limit = baseListOpts .Limit
116+ listOpts .Continue = baseListOpts .Continue
117+
118+ podList := & v1.PodList {}
119+ if err := p .cli .List (ctx , podList , listOpts ); err != nil {
120+ return ctrl.Result {}, errors .Wrapf (err , "failed to list pods with phase %s" , phase )
121+ }
122+ allActivePods = append (allActivePods , podList .Items ... )
123+ }
124+
125+ for _ , l := range listeners {
126+ l (allActivePods )
127+ }
128+ return ctrl.Result {}, nil
129+ }
130+ }
131+
83132var hostNetworkIndexer = client .IndexerFunc (func (o client.Object ) []string {
84133 pod , ok := o .(* v1.Pod )
85134 if ! ok {
@@ -88,12 +137,23 @@ var hostNetworkIndexer = client.IndexerFunc(func(o client.Object) []string {
88137 return []string {strconv .FormatBool (pod .Spec .HostNetwork )}
89138})
90139
140+ var statusPhaseIndexer = client .IndexerFunc (func (o client.Object ) []string {
141+ pod , ok := o .(* v1.Pod )
142+ if ! ok {
143+ return nil
144+ }
145+ return []string {string (pod .Status .Phase )}
146+ })
147+
91148// SetupWithManager Sets up the reconciler with a new manager, filtering using NodeNetworkConfigFilter on nodeName.
92149func (p * watcher ) SetupWithManager (ctx context.Context , mgr ctrl.Manager ) error {
93150 p .cli = mgr .GetClient ()
94151 if err := mgr .GetFieldIndexer ().IndexField (ctx , & v1.Pod {}, "spec.hostNetwork" , hostNetworkIndexer ); err != nil {
95152 return errors .Wrap (err , "failed to set up hostNetwork indexer" )
96153 }
154+ if err := mgr .GetFieldIndexer ().IndexField (ctx , & v1.Pod {}, "status.phase" , statusPhaseIndexer ); err != nil {
155+ return errors .Wrap (err , "failed to set up status.phase indexer" )
156+ }
97157 if err := ctrl .NewControllerManagedBy (mgr ).
98158 For (& v1.Pod {}).
99159 WithEventFilter (predicate.Funcs { // we only want create/delete events
0 commit comments