@@ -242,6 +242,19 @@ func (c *Controller) enqueueAddPod(obj any) {
242242 }
243243}
244244
245+ func (c * Controller ) getNsLabels (nsName , podName string ) map [string ]string {
246+ podNs , err := c .namespacesLister .Get (nsName )
247+ if err != nil {
248+ if k8serrors .IsNotFound (err ) {
249+ klog .V (3 ).Infof ("namespace %s not found for pod %s, use empty ns labels" , nsName , podName )
250+ } else {
251+ klog .Errorf ("failed to get namespace %s: %v, use empty ns labels" , nsName , err )
252+ }
253+ return nil
254+ }
255+ return podNs .Labels
256+ }
257+
245258func (c * Controller ) enqueueDeletePod (obj any ) {
246259 var p * v1.Pod
247260 switch t := obj .(type ) {
@@ -274,9 +287,9 @@ func (c *Controller) enqueueDeletePod(obj any) {
274287 }
275288
276289 if c .config .EnableANP {
277- podNs , _ := c .namespacesLister . Get (p .Namespace )
278- c .updateAnpsByLabelsMatch (podNs . Labels , p .Labels )
279- c .updateCnpsByLabelsMatch (podNs . Labels , p .Labels )
290+ nsLabels := c .getNsLabels (p .Namespace , p . Name )
291+ c .updateAnpsByLabelsMatch (nsLabels , p .Labels )
292+ c .updateCnpsByLabelsMatch (nsLabels , p .Labels )
280293 }
281294
282295 key := cache .MetaObjectToName (p ).String ()
@@ -351,18 +364,18 @@ func (c *Controller) enqueueUpdatePod(oldObj, newObj any) {
351364 }
352365
353366 if c .config .EnableANP {
354- podNs , _ := c .namespacesLister . Get (newPod .Namespace )
367+ nsLabels := c .getNsLabels (newPod .Namespace , newPod . Name )
355368 if ! maps .Equal (oldPod .Labels , newPod .Labels ) {
356- c .updateAnpsByLabelsMatch (podNs . Labels , newPod .Labels )
357- c .updateCnpsByLabelsMatch (podNs . Labels , newPod .Labels )
369+ c .updateAnpsByLabelsMatch (nsLabels , newPod .Labels )
370+ c .updateCnpsByLabelsMatch (nsLabels , newPod .Labels )
358371 }
359372
360373 for _ , podNet := range podNets {
361374 oldAllocated := oldPod .Annotations [fmt .Sprintf (util .AllocatedAnnotationTemplate , podNet .ProviderName )]
362375 newAllocated := newPod .Annotations [fmt .Sprintf (util .AllocatedAnnotationTemplate , podNet .ProviderName )]
363376 if oldAllocated != newAllocated {
364- c .updateAnpsByLabelsMatch (podNs . Labels , newPod .Labels )
365- c .updateCnpsByLabelsMatch (podNs . Labels , newPod .Labels )
377+ c .updateAnpsByLabelsMatch (nsLabels , newPod .Labels )
378+ c .updateCnpsByLabelsMatch (nsLabels , newPod .Labels )
366379 break
367380 }
368381 }
0 commit comments