This is an automated email from the ASF dual-hosted git repository.

zhangjintao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new bbaba6f  fix: filter useless pod update event (#894)
bbaba6f is described below

commit bbaba6f9c22196e5d3142ad7e1a2224474a1c553
Author: cmssczy <[email protected]>
AuthorDate: Thu Mar 3 14:44:04 2022 +0800

    fix: filter useless pod update event (#894)
---
 pkg/ingress/pod.go      | 26 +++++++++++++++-----------
 pkg/ingress/pod_test.go | 19 +++++++++++++------
 2 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/pkg/ingress/pod.go b/pkg/ingress/pod.go
index 51a5cea..fb78067 100644
--- a/pkg/ingress/pod.go
+++ b/pkg/ingress/pod.go
@@ -84,29 +84,33 @@ func (c *podController) onAdd(obj interface{}) {
        c.controller.MetricsCollector.IncrEvents("pod", "add")
 }
 
-func (c *podController) onUpdate(_, cur interface{}) {
-       pod := cur.(*corev1.Pod)
+func (c *podController) onUpdate(oldObj, newObj interface{}) {
+       prev := oldObj.(*corev1.Pod)
+       curr := newObj.(*corev1.Pod)
+       if prev.GetResourceVersion() >= curr.GetResourceVersion() {
+               return
+       }
 
-       if !c.controller.isWatchingNamespace(pod.Namespace + "/" + pod.Name) {
+       if !c.controller.isWatchingNamespace(curr.Namespace + "/" + curr.Name) {
                return
        }
        log.Debugw("pod update event arrived",
-               zap.Any("pod namespace", pod.Namespace),
-               zap.Any("pod name", pod.Name),
+               zap.Any("pod namespace", curr.Namespace),
+               zap.Any("pod name", curr.Name),
        )
-       if pod.DeletionTimestamp != nil {
-               if err := c.controller.podCache.Delete(pod); err != nil {
+       if curr.DeletionTimestamp != nil {
+               if err := c.controller.podCache.Delete(curr); err != nil {
                        log.Errorw("failed to delete pod from cache",
                                zap.Error(err),
-                               zap.Any("pod", pod),
+                               zap.Any("pod", curr),
                        )
                }
        }
-       if pod.Status.PodIP != "" {
-               if err := c.controller.podCache.Add(pod); err != nil {
+       if curr.Status.PodIP != "" {
+               if err := c.controller.podCache.Add(curr); err != nil {
                        log.Errorw("failed to add pod to cache",
                                zap.Error(err),
-                               zap.Any("pod", pod),
+                               zap.Any("pod", curr),
                        )
                }
        }
diff --git a/pkg/ingress/pod_test.go b/pkg/ingress/pod_test.go
index e12248d..b75886b 100644
--- a/pkg/ingress/pod_test.go
+++ b/pkg/ingress/pod_test.go
@@ -125,7 +125,7 @@ func TestPodOnUpdate(t *testing.T) {
                },
        }
 
-       pod := &corev1.Pod{
+       pod0 := &corev1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                        Namespace: "default",
                        Name:      "nginx",
@@ -138,17 +138,24 @@ func TestPodOnUpdate(t *testing.T) {
                        PodIP: "10.0.5.12",
                },
        }
-       assert.Nil(t, ctl.controller.podCache.Add(pod), "adding pod")
+       pod1 := pod0.DeepCopy()
+       pod1.SetResourceVersion("1")
 
-       ctl.onUpdate(nil, pod)
+       ctl.onUpdate(pod1, pod0)
        name, err := ctl.controller.podCache.GetNameByIP("10.0.5.12")
+       assert.Equal(t, "", name)
+       assert.Equal(t, types.ErrPodNotFound, err)
+
+       ctl.onUpdate(pod0, pod1)
+       name, err = ctl.controller.podCache.GetNameByIP("10.0.5.12")
        assert.Equal(t, "nginx", name)
        assert.Equal(t, nil, err)
 
        pod2 := &corev1.Pod{
                ObjectMeta: metav1.ObjectMeta{
-                       Namespace: "public",
-                       Name:      "abc",
+                       Namespace:       "public",
+                       Name:            "abc",
+                       ResourceVersion: "2",
                },
                Status: corev1.PodStatus{
                        Phase: corev1.PodRunning,
@@ -156,7 +163,7 @@ func TestPodOnUpdate(t *testing.T) {
                },
        }
        assert.Nil(t, ctl.controller.podCache.Add(pod2), "adding pod")
-       ctl.onUpdate(nil, pod2)
+       ctl.onUpdate(pod1, pod2)
        name, err = ctl.controller.podCache.GetNameByIP("10.0.5.13")
        assert.Equal(t, "abc", name)
        assert.Nil(t, err)

Reply via email to