diff --git a/pkg/ingress/pod.go b/pkg/ingress/pod.go index be961a8fda..7272865c77 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.namespaceWatching(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 10a2fe06ec..69907ac6c5 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)