@@ -384,18 +384,9 @@ func (c *podController) syncAddedPod(podObj *corev1.Pod) error {
384384 ipsMgr := c .npMgr .NsMap [util .KubeAllNamespacesFlag ].IpsMgr
385385 klog .Infof ("POD CREATING: [%s%s/%s/%s%+v%s]" , string (podObj .GetUID ()), podNs , podObj .Name , podObj .Spec .NodeName , podObj .Labels , podObj .Status .PodIP )
386386
387- // Add pod namespace if it doesn't exist
388387 var err error
389- if _ , exists := c .npMgr .NsMap [podNs ]; ! exists {
390- // (TODO): need to change newNS function. It always returns "nil"
391- c .npMgr .NsMap [podNs ], _ = newNs (podNs )
392- klog .Infof ("Creating set: %v, hashedSet: %v" , podNs , util .GetHashedName (podNs ))
393- if err = ipsMgr .CreateSet (podNs , append ([]string {util .IpsetNetHashFlag })); err != nil {
394- return fmt .Errorf ("[syncAddedPod] Error: creating ipset %s with err: %v" , podNs , err )
395- }
396- }
397-
398388 npmPodObj := newNpmPod (podObj )
389+
399390 // Add the pod to its namespace's ipset.
400391 klog .Infof ("Adding pod %s to ipset %s" , npmPodObj .PodIP , podNs )
401392 if err = ipsMgr .AddToSet (podNs , npmPodObj .PodIP , util .IpsetNetHashFlag , podKey ); err != nil {
@@ -433,24 +424,28 @@ func (c *podController) syncAddedPod(podObj *corev1.Pod) error {
433424
434425// syncAddAndUpdatePod handles updating pod ip in its label's ipset.
435426func (c * podController ) syncAddAndUpdatePod (newPodObj * corev1.Pod ) error {
436- podKey , _ := cache .MetaNamespaceKeyFunc (newPodObj )
437-
438- klog .Infof ("[syncAddAndUpdatePod] updating Pod with key %s" , podKey )
439- newPodObjNs := util .GetNSNameWithPrefix (newPodObj .ObjectMeta .Namespace )
427+ var err error
440428 ipsMgr := c .npMgr .NsMap [util .KubeAllNamespacesFlag ].IpsMgr
441429
442- // Add pod namespace if it doesn't exist
443- var err error
430+ // Create ipset related to namespace which this pod belong to if it does not exist.
431+ newPodObjNs := util . GetNSNameWithPrefix ( newPodObj . Namespace )
444432 if _ , exists := c .npMgr .NsMap [newPodObjNs ]; ! exists {
445- // (TODO): need to change newNS function. It always returns "nil"
446- c .npMgr .NsMap [newPodObjNs ], _ = newNs (newPodObjNs )
447- klog .Infof ("Creating set: %v, hashedSet: %v" , newPodObjNs , util .GetHashedName (newPodObjNs ))
448433 if err = ipsMgr .CreateSet (newPodObjNs , []string {util .IpsetNetHashFlag }); err != nil {
449- return fmt .Errorf ("[syncAddAndUpdatePod] Error: creating ipset %s with err: %v" , newPodObjNs , err )
434+ return fmt .Errorf ("[syncAddAndUpdatePod] Error: failed to create ipset for namespace %s with err: %v" , newPodObjNs , err )
435+ }
436+
437+ if err = ipsMgr .AddToList (util .KubeAllNamespacesFlag , newPodObjNs ); err != nil {
438+ return fmt .Errorf ("[syncAddAndUpdatePod] Error: failed to add %s to all-namespace ipset list with err: %v" , newPodObjNs , err )
450439 }
440+
441+ // Add namespace object into NsMap cache only when two ipset operations are successful.
442+ npmNs , _ := newNs (newPodObjNs )
443+ c .npMgr .NsMap [newPodObjNs ] = npmNs
451444 }
452445
446+ podKey , _ := cache .MetaNamespaceKeyFunc (newPodObj )
453447 cachedNpmPodObj , exists := c .npMgr .PodMap [podKey ]
448+ klog .Infof ("[syncAddAndUpdatePod] updating Pod with key %s" , podKey )
454449 // No cached npmPod exists. start adding the pod in a cache
455450 if ! exists {
456451 if err = c .syncAddedPod (newPodObj ); err != nil {
@@ -469,7 +464,7 @@ func (c *podController) syncAddAndUpdatePod(newPodObj *corev1.Pod) error {
469464 // then, re-add new pod obj.
470465 if cachedNpmPodObj .PodIP != newPodObj .Status .PodIP {
471466 metrics .SendErrorLogAndMetric (util .PodID , "[syncAddAndUpdatePod] Info: Unexpected state. Pod (Namespace:%s, Name:%s, newUid:%s) , has cachedPodIp:%s which is different from PodIp:%s" ,
472- newPodObjNs , newPodObj .Name , string (newPodObj .UID ), cachedNpmPodObj .PodIP , newPodObj .Status .PodIP )
467+ newPodObj . Namespace , newPodObj .Name , string (newPodObj .UID ), cachedNpmPodObj .PodIP , newPodObj .Status .PodIP )
473468
474469 klog .Infof ("Deleting cached Pod with key:%s first due to IP Mistmatch" , podKey )
475470 if err = c .cleanUpDeletedPod (podKey ); err != nil {
0 commit comments