@@ -5,6 +5,9 @@ package podownercache
55
66import (
77 "context"
8+ "strings"
9+
10+ "github.com/spidernet-io/spiderpool/pkg/constant"
811 "github.com/spidernet-io/spiderpool/pkg/lock"
912 "github.com/spidernet-io/spiderpool/pkg/logutils"
1013 "go.uber.org/zap"
@@ -24,6 +27,8 @@ type PodOwnerCache struct {
2427 cacheLock lock.RWMutex
2528 pods map [types.NamespacedName ]Pod
2629 ipToPod map [string ]types.NamespacedName
30+ // Cache for final owner references to reduce API calls, using pod NamespacedName as key
31+ ownerCache map [types.NamespacedName ]* OwnerInfo
2732}
2833
2934type Pod struct {
@@ -50,11 +55,12 @@ func New(ctx context.Context, podInformer cache.SharedIndexInformer, apiReader c
5055 logger .Info ("create PodOwnerCache informer" )
5156
5257 res := & PodOwnerCache {
53- ctx : ctx ,
54- apiReader : apiReader ,
55- cacheLock : lock.RWMutex {},
56- pods : make (map [types.NamespacedName ]Pod ),
57- ipToPod : make (map [string ]types.NamespacedName ),
58+ ctx : ctx ,
59+ apiReader : apiReader ,
60+ cacheLock : lock.RWMutex {},
61+ pods : make (map [types.NamespacedName ]Pod ),
62+ ipToPod : make (map [string ]types.NamespacedName ),
63+ ownerCache : make (map [types.NamespacedName ]* OwnerInfo ),
5864 }
5965
6066 _ , err := podInformer .AddEventHandler (cache.ResourceEventHandlerFuncs {
@@ -70,11 +76,20 @@ func New(ctx context.Context, podInformer cache.SharedIndexInformer, apiReader c
7076 return res , nil
7177}
7278
73- func (s * PodOwnerCache ) onPodAdd (obj interface {} ) {
79+ func (s * PodOwnerCache ) onPodAdd (obj any ) {
7480 if pod , ok := obj .(* corev1.Pod ); ok {
7581 if pod .Spec .HostNetwork {
7682 return
7783 }
84+
85+ if pod .Annotations [constant .MultusNetworkStatus ] == "" {
86+ return
87+ }
88+
89+ if ! strings .Contains (pod .Annotations [constant .MultusNetworkStatus ], "rdma-device" ) {
90+ return
91+ }
92+
7893 if len (pod .Status .PodIPs ) > 0 {
7994 ips := make ([]string , 0 , len (pod .Status .PodIPs ))
8095 for _ , p := range pod .Status .PodIPs {
@@ -123,6 +138,23 @@ func (s *PodOwnerCache) onPodDel(obj interface{}) {
123138func (s * PodOwnerCache ) getFinalOwner (obj metav1.Object ) (* OwnerInfo , error ) {
124139 var finalOwner * OwnerInfo
125140
141+ // Create pod NamespacedName as the cache key
142+ podKey := types.NamespacedName {
143+ Namespace : obj .GetNamespace (),
144+ Name : obj .GetName (),
145+ }
146+
147+ // Check if we already have a cached final owner for this pod
148+ s .cacheLock .RLock ()
149+ cachedOwner , exists := s .ownerCache [podKey ]
150+ s .cacheLock .RUnlock ()
151+
152+ if exists {
153+ // If we found a cached result, return it immediately
154+ logger .Sugar ().Debugf ("Using cached owner for pod %s/%s" , obj .GetNamespace (), obj .GetName ())
155+ return cachedOwner , nil
156+ }
157+
126158 for {
127159 ownerRefs := obj .GetOwnerReferences ()
128160 if len (ownerRefs ) == 0 {
@@ -131,6 +163,8 @@ func (s *PodOwnerCache) getFinalOwner(obj metav1.Object) (*OwnerInfo, error) {
131163
132164 // Assuming the first owner reference
133165 ownerRef := ownerRefs [0 ]
166+
167+ // If not in cache, create the owner info
134168 finalOwner = & OwnerInfo {
135169 APIVersion : ownerRef .APIVersion ,
136170 Kind : ownerRef .Kind ,
@@ -148,12 +182,12 @@ func (s *PodOwnerCache) getFinalOwner(obj metav1.Object) (*OwnerInfo, error) {
148182 Name : ownerRef .Name ,
149183 }, ownerObj )
150184 if err != nil {
151- if errors .IsForbidden (err ) {
152- logger .Sugar ().Debugf ("forbidden to get owner of pod %s/%s" , obj .GetNamespace (), obj .GetName ())
153- return nil , nil
154- }
155- if errors . IsNotFound ( err ) {
156- logger . Sugar (). Debugf ( "owner not found for pod %s/%s" , obj . GetNamespace (), obj . GetName () )
185+ if errors .IsForbidden (err ) || errors . IsNotFound ( err ) {
186+ logger .Sugar ().Debugf ("%v for pod %s/%s" , err , obj .GetNamespace (), obj .GetName ())
187+ // Cache the negative result
188+ s . cacheLock . Lock ()
189+ s . ownerCache [ podKey ] = nil
190+ s . cacheLock . Unlock ( )
157191 return nil , nil
158192 }
159193 return nil , err
@@ -163,6 +197,19 @@ func (s *PodOwnerCache) getFinalOwner(obj metav1.Object) (*OwnerInfo, error) {
163197 obj = ownerObj
164198 }
165199
200+ // Cache the final owner (or nil if no owner found)
201+ s .cacheLock .Lock ()
202+ s .ownerCache [podKey ] = finalOwner
203+ s .cacheLock .Unlock ()
204+
205+ if finalOwner != nil {
206+ logger .Sugar ().Debugf ("Cached final owner %s/%s of kind %s for pod %s/%s" ,
207+ finalOwner .Namespace , finalOwner .Name , finalOwner .Kind ,
208+ podKey .Namespace , podKey .Name )
209+ } else {
210+ logger .Sugar ().Debugf ("No owner found for pod %s/%s" , podKey .Namespace , podKey .Name )
211+ }
212+
166213 return finalOwner , nil
167214}
168215
0 commit comments