Skip to content

Commit

Permalink
fix: filter useless pod update event (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmssczy authored and tao12345666333 committed Apr 22, 2022
1 parent cbcae44 commit 8329b7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
26 changes: 15 additions & 11 deletions pkg/ingress/pod.go
Expand Up @@ -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),
)
}
}
Expand Down
19 changes: 13 additions & 6 deletions pkg/ingress/pod_test.go
Expand Up @@ -125,7 +125,7 @@ func TestPodOnUpdate(t *testing.T) {
},
}

pod := &corev1.Pod{
pod0 := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "nginx",
Expand All @@ -138,25 +138,32 @@ 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,
PodIP: "10.0.5.13",
},
}
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)
Expand Down

0 comments on commit 8329b7c

Please sign in to comment.