From a256c0c8006d3c007d9779adcecc080509309c0b Mon Sep 17 00:00:00 2001 From: Lyndon-Li Date: Wed, 8 Jan 2025 15:00:32 +0800 Subject: [PATCH] add event burst Signed-off-by: Lyndon-Li --- pkg/util/kube/event.go | 2 ++ pkg/util/kube/event_test.go | 43 ++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/pkg/util/kube/event.go b/pkg/util/kube/event.go index de91d3533d..96502d5332 100644 --- a/pkg/util/kube/event.go +++ b/pkg/util/kube/event.go @@ -16,6 +16,7 @@ limitations under the License. package kube import ( + "math" "sync" "time" @@ -60,6 +61,7 @@ func NewEventRecorder(kubeClient kubernetes.Interface, scheme *runtime.Scheme, e } res.broadcaster = record.NewBroadcasterWithCorrelatorOptions(record.CorrelatorOptions{ + BurstSize: math.MaxInt32, MaxEvents: 1, MessageFunc: func(event *v1.Event) string { return event.Message diff --git a/pkg/util/kube/event_test.go b/pkg/util/kube/event_test.go index 080d955467..2f24b2515c 100644 --- a/pkg/util/kube/event_test.go +++ b/pkg/util/kube/event_test.go @@ -43,9 +43,12 @@ func TestEvent(t *testing.T) { } cases := []struct { - name string - events []testEvent - expected int + name string + events []testEvent + generateDiff int + generateSame int + generateEnding bool + expected int }{ { name: "update events, different message", @@ -116,6 +119,18 @@ func TestEvent(t *testing.T) { }, expected: -1, }, + { + name: "auto generate 200", + generateDiff: 200, + generateEnding: true, + expected: 201, + }, + { + name: "auto generate 200, update", + generateSame: 200, + generateEnding: true, + expected: 2, + }, } shutdownTimeout = time.Second * 5 @@ -143,6 +158,28 @@ func TestEvent(t *testing.T) { _, err = client.CoreV1().Pods("fake-ns").Create(context.Background(), pod, metav1.CreateOptions{}) require.NoError(t, err) + for i := 0; i < tc.generateDiff; i++ { + tc.events = append(tc.events, testEvent{ + reason: fmt.Sprintf("fake-reason-%v", i), + message: fmt.Sprintf("fake-message-%v", i), + }) + } + + for i := 0; i < tc.generateSame; i++ { + tc.events = append(tc.events, testEvent{ + reason: "fake-reason", + message: fmt.Sprintf("fake-message-%v", i), + }) + } + + if tc.generateEnding { + tc.events = append(tc.events, testEvent{ + reason: "fake-ending-reason", + message: "fake-ending-message", + ending: true, + }) + } + for _, e := range tc.events { if e.ending { recorder.EndingEvent(pod, e.warning, e.reason, e.message)