Skip to content

Commit

Permalink
fix: re-use the maxQueuedEvents (kyverno#10024) (kyverno#10031)
Browse files Browse the repository at this point in the history
* fix: re-use the maxQueuedEvents



* fix: use the apierrors.IsNotFound instead of checking a specfic error msg



---------

Signed-off-by: Mariam Fahmy <[email protected]>
Co-authored-by: Mariam Fahmy <[email protected]>
Co-authored-by: shuting <[email protected]>
  • Loading branch information
3 people authored Apr 11, 2024
1 parent f8c058c commit 6c71685
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/background-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func main() {
eventGenerator := event.NewEventGenerator(
setup.EventsClient,
logging.WithName("EventGenerator"),
maxQueuedEvents,
strings.Split(omitEvents, ",")...,
)
eventController := internal.NewController(
Expand Down
1 change: 1 addition & 0 deletions cmd/cleanup-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func main() {
eventGenerator := event.NewEventGenerator(
setup.EventsClient,
logging.WithName("EventGenerator"),
maxQueuedEvents,
)
eventController := internal.NewController(
event.ControllerName,
Expand Down
1 change: 1 addition & 0 deletions cmd/kyverno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func main() {
eventGenerator := event.NewEventGenerator(
setup.EventsClient,
logging.WithName("EventGenerator"),
maxQueuedEvents,
strings.Split(omitEvents, ",")...,
)
gcstore := store.New()
Expand Down
1 change: 1 addition & 0 deletions cmd/reports-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func main() {
eventGenerator := event.NewEventGenerator(
setup.EventsClient,
logging.WithName("EventGenerator"),
maxQueuedEvents,
strings.Split(omitEvents, ",")...,
)
eventController := internal.NewController(
Expand Down
11 changes: 9 additions & 2 deletions pkg/event/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.opentelemetry.io/otel/metric"
corev1 "k8s.io/api/core/v1"
eventsv1 "k8s.io/api/events/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -44,10 +45,11 @@ type controller struct {
clock clock.Clock
hostname string
droppedEventsCounter metric.Int64Counter
maxQueuedEvents int
}

// NewEventGenerator to generate a new event controller
func NewEventGenerator(eventsClient v1.EventsV1Interface, logger logr.Logger, omitEvents ...string) *controller {
func NewEventGenerator(eventsClient v1.EventsV1Interface, logger logr.Logger, maxQueuedEvents int, omitEvents ...string) *controller {
clock := clock.RealClock{}
hostname, _ := os.Hostname()
meter := otel.GetMeterProvider().Meter(metrics.MeterName)
Expand All @@ -66,13 +68,18 @@ func NewEventGenerator(eventsClient v1.EventsV1Interface, logger logr.Logger, om
clock: clock,
hostname: hostname,
droppedEventsCounter: droppedEventsCounter,
maxQueuedEvents: maxQueuedEvents,
}
}

// Add queues an event for generation
func (gen *controller) Add(infos ...Info) {
logger := gen.logger
logger.V(3).Info("generating events", "count", len(infos))
if gen.maxQueuedEvents == 0 || gen.queue.Len() > gen.maxQueuedEvents {
logger.V(2).Info("exceeds the event queue limit, dropping the event", "maxQueuedEvents", gen.maxQueuedEvents, "current size", gen.queue.Len())
return
}
for _, info := range infos {
// don't create event for resources with generateName as the name is not generated yet
if info.Regarding.Name == "" {
Expand Down Expand Up @@ -119,7 +126,7 @@ func (gen *controller) processNextWorkItem(ctx context.Context) bool {
return true
}
_, err := gen.eventsClient.Events(event.Namespace).Create(ctx, event, metav1.CreateOptions{})
if err != nil {
if err != nil && !apierrors.IsNotFound(err) {
if gen.queue.NumRequeues(key) < workQueueRetryLimit {
logger.Error(err, "failed to create event", "key", key)
gen.queue.AddRateLimited(key)
Expand Down
2 changes: 1 addition & 1 deletion pkg/event/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestEventGenerator(t *testing.T) {
logger := logr.Discard()

eventsClient := clientset.EventsV1()
eventGenerator := NewEventGenerator(eventsClient, logger)
eventGenerator := NewEventGenerator(eventsClient, logger, 1000)

go eventGenerator.Run(ctx, Workers)
time.Sleep(1 * time.Second)
Expand Down

0 comments on commit 6c71685

Please sign in to comment.