Skip to content

Commit 8648739

Browse files
authored
[NPM] fix incorrect NsMap local cache management between nameSpaceController and PodController (#866)
* fix incorrect NsMap local cache management between nameSpaceController and podcontroller * Correct comment * PodController adds list in ipset and npm-namespace into NsMap local cache
1 parent da7f2c7 commit 8648739

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

npm/ipsm/ipsm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ func (ipsMgr *IpsetManager) CreateSet(setName string, spec []string) error {
256256
spec: spec,
257257
}
258258
log.Logf("Creating Set: %+v", entry)
259+
// (TODO): need to differentiate errCode handler
260+
// since errCode can be one in case of "set with the same name already exists" and "maximal number of sets reached, cannot create more."
261+
// It may have more situations with errCode==1.
259262
if errCode, err := ipsMgr.Run(entry); err != nil && errCode != 1 {
260263
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to create ipset.")
261264
return err

npm/nameSpaceController.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Namespace struct {
4141
}
4242

4343
// newNS constructs a new namespace object.
44+
// (TODO): need to change newNS function. It always returns "nil"
4445
func newNs(name string) (*Namespace, error) {
4546
ns := &Namespace{
4647
name: name,

npm/podController.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
435426
func (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

Comments
 (0)