Skip to content

Commit

Permalink
Add concurrent access protection to the event recorder
Browse files Browse the repository at this point in the history
Signed-off-by: Benamar Mekhissi <[email protected]>
(cherry picked from commit a3498a4)
  • Loading branch information
Benamar Mekhissi authored and BenamarMk committed Sep 14, 2023
1 parent 1c0adc9 commit 9adf856
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions controllers/util/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package util

import (
"fmt"
"sync"
"time"

"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -100,6 +101,9 @@ type EventReporter struct {

// lastReportedEventTime will be the time of lastReportedEvent
lastReportedEventTime map[string]time.Time

// Mutex for protecting concurrent access to the recorder map
mutex sync.Mutex
}

// NewEventReporter returns EventReporter object
Expand All @@ -123,6 +127,9 @@ func ReportIfNotPresent(recorder *EventReporter, instance runtime.Object,

eventKey := getEventKey(eventType, eventReason, msg)

recorder.mutex.Lock()
defer recorder.mutex.Unlock()

if recorder.lastReportedEvent[nameSpacedName] != eventKey ||
recorder.lastReportedEventTime[nameSpacedName].Add(time.Second*LastEventValidDuration).Before(time.Now()) {
recorder.lastReportedEvent[nameSpacedName] = eventKey
Expand Down

0 comments on commit 9adf856

Please sign in to comment.