1
0
Fork 0
mirror of https://github.com/kyverno/policy-reporter.git synced 2024-12-14 11:57:32 +00:00
policy-reporter/pkg/kubernetes/debouncer.go
Frank Jogeleit f3b980bfd3
Implement MetadataClient (#257)
* Implement MetadataClient

Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de>
2023-02-08 15:53:59 +01:00

69 lines
1.4 KiB
Go

package kubernetes
import (
"sync"
"time"
"github.com/kyverno/policy-reporter/pkg/report"
)
type Debouncer interface {
Add(e report.LifecycleEvent)
}
type debouncer struct {
waitDuration time.Duration
events map[string]report.LifecycleEvent
publisher report.EventPublisher
mutx *sync.Mutex
}
func (d *debouncer) Add(event report.LifecycleEvent) {
_, ok := d.events[event.PolicyReport.GetID()]
if event.Type != report.Updated && ok {
d.mutx.Lock()
delete(d.events, event.PolicyReport.GetID())
d.mutx.Unlock()
}
if event.Type != report.Updated {
d.publisher.Publish(event)
return
}
if len(event.PolicyReport.GetResults()) == 0 && !ok {
d.mutx.Lock()
d.events[event.PolicyReport.GetID()] = event
d.mutx.Unlock()
go func() {
time.Sleep(d.waitDuration)
d.mutx.Lock()
if event, ok := d.events[event.PolicyReport.GetID()]; ok {
d.publisher.Publish(event)
delete(d.events, event.PolicyReport.GetID())
}
d.mutx.Unlock()
}()
return
}
if len(event.PolicyReport.GetResults()) > 0 && ok {
d.mutx.Lock()
delete(d.events, event.PolicyReport.GetID())
d.mutx.Unlock()
}
d.publisher.Publish(event)
}
func NewDebouncer(waitDuration time.Duration, publisher report.EventPublisher) Debouncer {
return &debouncer{
waitDuration: waitDuration,
events: make(map[string]report.LifecycleEvent),
mutx: new(sync.Mutex),
publisher: publisher,
}
}