99
1010 "github.com/sirupsen/logrus"
1111 authorizationv1 "k8s.io/api/authorization/v1"
12+ core "k8s.io/api/core/v1"
1213 storagev1 "k8s.io/api/storage/v1"
1314 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415 "k8s.io/client-go/informers"
@@ -42,7 +43,7 @@ type Manager struct {
4243 factory informers.SharedInformerFactory
4344 cacheSyncTimeout time.Duration
4445
45- nodes NodeInformer
46+ nodes * nodeInformer
4647 pods * podInformer
4748 volumeAttachments * vaInformer
4849
@@ -63,10 +64,11 @@ func WithCacheSyncTimeout(timeout time.Duration) Option {
6364
6465func EnablePodInformer () Option {
6566 return func (m * Manager ) {
66- m .pods = & podInformer {
67- informer : m .factory .Core ().V1 ().Pods ().Informer (),
68- lister : m .factory .Core ().V1 ().Pods ().Lister (),
69- }
67+ m .pods = NewPodInformer (
68+ m .factory .Core ().V1 ().Pods ().Informer (),
69+ m .factory .Core ().V1 ().Pods ().Lister (),
70+ nil ,
71+ )
7072 }
7173}
7274
@@ -82,7 +84,26 @@ func EnableNodeInformer() Option {
8284// WithNodeIndexers sets custom indexers for the node informer.
8385func WithNodeIndexers (indexers cache.Indexers ) Option {
8486 return func (n * Manager ) {
85- n .nodes .SetIndexers (indexers )
87+ n .nodes .indexers = indexers
88+ }
89+ }
90+
91+ func WithDefaultPodNodeNameIndexer () Option {
92+ return WithPodIndexers (cache.Indexers {
93+ PodIndexerName : func (obj any ) ([]string , error ) {
94+ pod , ok := obj .(* core.Pod )
95+ if ! ok {
96+ return nil , nil
97+ }
98+ return []string {pod .Spec .NodeName }, nil
99+ },
100+ })
101+ }
102+
103+ // WithPodIndexers sets custom indexers for the pod informer.
104+ func WithPodIndexers (indexers cache.Indexers ) Option {
105+ return func (n * Manager ) {
106+ n .pods .indexers = indexers
86107 }
87108}
88109
@@ -168,7 +189,7 @@ func (m *Manager) Start(ctx context.Context) error {
168189 }
169190
170191 m .log .Info ("waiting for node informer cache to sync..." )
171- if ! cache .WaitForCacheSync (syncCtx .Done (), m .nodes .HasSynced ) {
192+ if ! cache .WaitForCacheSync (syncCtx .Done (), m .nodes .informer . HasSynced ) {
172193 cancel ()
173194 return fmt .Errorf ("failed to sync node informer cache within %v" , m .cacheSyncTimeout )
174195 }
@@ -178,7 +199,7 @@ func (m *Manager) Start(ctx context.Context) error {
178199
179200 if m .pods != nil {
180201 m .log .Info ("waiting for pod informer cache to sync..." )
181- if ! cache .WaitForCacheSync (syncCtx .Done (), m .pods .HasSynced ) {
202+ if ! cache .WaitForCacheSync (syncCtx .Done (), m .pods .informer . HasSynced ) {
182203 cancel ()
183204 return fmt .Errorf ("failed to sync pod informer cache within %v" , m .cacheSyncTimeout )
184205 }
@@ -247,7 +268,7 @@ func (m *Manager) GetNodeLister() listerv1.NodeLister {
247268 if m .nodes == nil {
248269 return nil
249270 }
250- return m .nodes .Lister ()
271+ return m .nodes .lister
251272}
252273
253274// GetNodeInformer returns the node informer for watching node events.
@@ -258,20 +279,26 @@ func (m *Manager) GetNodeInformer() NodeInformer {
258279 return m .nodes
259280}
260281
282+ // Nodes returns the node informer for node domain operations.
283+ // This is a convenience method that returns the same as GetNodeInformer().
284+ func (m * Manager ) Nodes () NodeInformer {
285+ return m .GetNodeInformer ()
286+ }
287+
261288// GetPodLister returns the pod lister for querying the pod cache.
262289func (m * Manager ) GetPodLister () listerv1.PodLister {
263290 if m .pods == nil {
264291 return nil
265292 }
266- return m .pods .Lister ()
293+ return m .pods .lister
267294}
268295
269- // GetPodInformer returns the pod informer for watching pod events .
270- func (m * Manager ) GetPodInformer () cache. SharedIndexInformer {
296+ // Pods returns the pod informer for pod domain operations .
297+ func (m * Manager ) Pods () PodInformer {
271298 if m .pods == nil {
272299 return nil
273300 }
274- return m .pods . Informer ()
301+ return m .pods
275302}
276303
277304// IsVAAvailable indicates whether the VolumeAttachment informer is available.
@@ -346,8 +373,8 @@ func (m *Manager) checkVAPermissions(ctx context.Context) error {
346373}
347374
348375func (m * Manager ) addIndexers () error {
349- if m .nodes != nil && m .nodes .Indexers () != nil {
350- if err := m .nodes .Informer () .AddIndexers (m .nodes .Indexers () ); err != nil {
376+ if m .nodes != nil && m .nodes .indexers != nil {
377+ if err := m .nodes .informer .AddIndexers (m .nodes .indexers ); err != nil {
351378 return fmt .Errorf ("adding node indexers: %w" , err )
352379 }
353380 }
@@ -374,14 +401,14 @@ func (m *Manager) reportCacheSize(ctx context.Context) {
374401 return
375402 case <- ticker .C :
376403 if m .nodes != nil {
377- nodes := m .nodes .Informer () .GetStore ().ListKeys ()
404+ nodes := m .nodes .informer .GetStore ().ListKeys ()
378405 size := len (nodes )
379406 m .log .WithField ("cache_size" , size ).Debug ("node informer cache size" )
380407 metrics .SetInformerCacheSize ("node" , size )
381408 }
382409
383410 if m .pods != nil {
384- pods := m .pods .Informer () .GetStore ().ListKeys ()
411+ pods := m .pods .informer .GetStore ().ListKeys ()
385412 size := len (pods )
386413 m .log .WithField ("cache_size" , size ).Debug ("pod informer cache size" )
387414 metrics .SetInformerCacheSize ("pod" , size )
0 commit comments